diff --git a/src/api/libcellml/generator.h b/src/api/libcellml/generator.h index d9fb7debb..7e9b7915d 100644 --- a/src/api/libcellml/generator.h +++ b/src/api/libcellml/generator.h @@ -16,7 +16,7 @@ limitations under the License. #pragma once -#include "libcellml/generatorvariabletracker.h" +#include "libcellml/generatorprofile.h" #include "libcellml/logger.h" namespace libcellml { @@ -62,6 +62,8 @@ class LIBCELLML_EXPORT Generator: public Logger std::string interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile, const GeneratorVariableTrackerPtr &generatorVariableTracker); /** + * @overload + * * @brief Get the interface code for the @ref AnalyserModel. * * Return the interface code for the @ref AnalyserModel, using the @ref GeneratorProfile. @@ -75,6 +77,23 @@ class LIBCELLML_EXPORT Generator: public Logger std::string interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile); /** + * @overload + * + * @brief Get the interface code for the @ref AnalyserModel. + * + * Return the interface code for the @ref AnalyserModel, using the @ref GeneratorProfile. + * + * @param analyserModel The @ref AnalyserModel for which we want to generate some interface code. + * @param generatorProfile The @ref GeneratorProfile to use to generate the interface code. + * + * @return The interface code as a @c std::string. + */ + + std::string interfaceCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile); + + /** + * @overload + * * @brief Get the interface code for the @ref AnalyserModel. * * Return the interface code for the @ref AnalyserModel, using the @ref GeneratorVariableTracker. @@ -88,6 +107,8 @@ class LIBCELLML_EXPORT Generator: public Logger std::string interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker); /** + * @overload + * * @brief Get the interface code for the @ref AnalyserModel. * * Return the interface code for the @ref AnalyserModel, using the @ref GeneratorProfile. @@ -113,6 +134,8 @@ class LIBCELLML_EXPORT Generator: public Logger std::string implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile, const GeneratorVariableTrackerPtr &generatorVariableTracker); /** + * @overload + * * @brief Get the implementation code for the @ref AnalyserModel. * * Return the implementation code for the @ref AnalyserModel, using the @ref GeneratorProfile. @@ -126,6 +149,23 @@ class LIBCELLML_EXPORT Generator: public Logger std::string implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile); /** + * @overload + * + * @brief Get the implementation code for the @ref AnalyserModel. + * + * Return the implementation code for the @ref AnalyserModel, using the @ref GeneratorProfile::Profile. + * + * @param analyserModel The @ref AnalyserModel for which we want to generate some implementation code. + * @param profile The @ref GeneratorProfile::Profile type to use to generate the implementation code. + * + * @return The implementation code as a @c std::string. + */ + + std::string implementationCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile); + + /** + * @overload + * * @brief Get the implementation code for the @ref AnalyserModel. * * Return the implementation code for the @ref AnalyserModel, using the @ref GeneratorVariableTracker. @@ -139,6 +179,8 @@ class LIBCELLML_EXPORT Generator: public Logger std::string implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker); /** + * @overload + * * @brief Get the implementation code for the @ref AnalyserModel. * * Return the implementation code for the @ref AnalyserModel, using the @ref GeneratorProfile. @@ -150,6 +192,8 @@ class LIBCELLML_EXPORT Generator: public Logger std::string implementationCode(const AnalyserModelPtr &analyserModel); /** + * @overload + * * @brief Get the equation code for the given @ref AnalyserEquationAst. * * Return the equation code for the given @ref AnalyserEquationAst using @ref GeneratorProfile. @@ -164,6 +208,8 @@ class LIBCELLML_EXPORT Generator: public Logger const GeneratorProfilePtr &generatorProfile); /** + * @overload + * * @brief Get the equation code for the given @ref AnalyserEquationAst using a default @ref GeneratorProfile. * * Return the equation code for the given @ref AnalyserEquationAst using a default @ref GeneratorProfile. diff --git a/src/bindings/interface/generator.i b/src/bindings/interface/generator.i index 68cc1624b..17916c03a 100644 --- a/src/bindings/interface/generator.i +++ b/src/bindings/interface/generator.i @@ -19,6 +19,9 @@ %feature("docstring") libcellml::Generator::interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile) "Returns the interface code for the analyser model using the generator profile."; +%feature("docstring") libcellml::Generator::interfaceCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile) +"Returns the interface code for the analyser model using the generator profile enumeration type."; + %feature("docstring") libcellml::Generator::interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker) "Returns the interface code for the analyser model using the generator variable tracker."; @@ -31,6 +34,9 @@ %feature("docstring") libcellml::Generator::implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorProfilePtr &generatorProfile) "Returns the implementation code for the analyser model using the generator profile."; +%feature("docstring") libcellml::Generator::implementationCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile) +"Returns the implementation code for the analyser model using the generator profile enumeration type."; + %feature("docstring") libcellml::Generator::implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker) "Returns the implementation code for the analyser model using the generator variable tracker."; @@ -45,6 +51,7 @@ %{ #include "libcellml/generator.h" +#include "libcellml/generatorvariabletracker.h" %} %pythoncode %{ diff --git a/src/bindings/javascript/generator.cpp b/src/bindings/javascript/generator.cpp index 678e0bb83..e23838a46 100644 --- a/src/bindings/javascript/generator.cpp +++ b/src/bindings/javascript/generator.cpp @@ -29,10 +29,12 @@ EMSCRIPTEN_BINDINGS(libcellml_generator) .smart_ptr_constructor("Generator", &libcellml::Generator::create) .function("interfaceCode", select_overload(&libcellml::Generator::interfaceCode)) .function("interfaceCodeByProfile", select_overload(&libcellml::Generator::interfaceCode)) + .function("interfaceCodeByProfileEnumeration", select_overload(&libcellml::Generator::interfaceCode)) .function("interfaceCodeByVariableTracker", select_overload(&libcellml::Generator::interfaceCode)) .function("interfaceCodeByProfileAndVariableTracker", select_overload(&libcellml::Generator::interfaceCode)) .function("implementationCode", select_overload(&libcellml::Generator::implementationCode)) .function("implementationCodeByProfile", select_overload(&libcellml::Generator::implementationCode)) + .function("implementationCodeByProfileEnumeration", select_overload(&libcellml::Generator::implementationCode)) .function("implementationCodeByVariableTracker", select_overload(&libcellml::Generator::implementationCode)) .function("implementationCodeByProfileAndVariableTracker", select_overload(&libcellml::Generator::implementationCode)) .class_function("equationCode", select_overload(&libcellml::Generator::equationCode)) diff --git a/src/generator.cpp b/src/generator.cpp index 8b184649e..3cdd5f701 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -2293,6 +2293,11 @@ std::string Generator::interfaceCode(const AnalyserModelPtr &analyserModel, cons return interfaceCode(analyserModel, generatorProfile, nullptr); } +std::string Generator::interfaceCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile) +{ + return interfaceCode(analyserModel, GeneratorProfile::create(profile), nullptr); +} + std::string Generator::interfaceCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker) { @@ -2420,6 +2425,11 @@ std::string Generator::implementationCode(const AnalyserModelPtr &analyserModel, return implementationCode(analyserModel, generatorProfile, nullptr); } +std::string Generator::implementationCode(const AnalyserModelPtr &analyserModel, GeneratorProfile::Profile profile) +{ + return implementationCode(analyserModel, GeneratorProfile::create(profile), nullptr); +} + std::string Generator::implementationCode(const AnalyserModelPtr &analyserModel, const GeneratorVariableTrackerPtr &generatorVariableTracker) { return implementationCode(analyserModel, pFunc()->mDefaultProfile, generatorVariableTracker); diff --git a/tests/bindings/javascript/generator.test.js b/tests/bindings/javascript/generator.test.js index ee61d1d37..2c328e6ff 100644 --- a/tests/bindings/javascript/generator.test.js +++ b/tests/bindings/javascript/generator.test.js @@ -51,6 +51,9 @@ describe("Generator tests", () => { const interface_lines_profile = g.interfaceCodeByProfile(am, gp).split('\n') expect(interface_lines_profile.length).toBe(38) + const interface_lines_profile_enumeration = g.interfaceCodeByProfileEnumeration(am, libcellml.GeneratorProfile.Profile.C).split('\n') + expect(interface_lines_profile_enumeration.length).toBe(38) + const gvt = new libcellml.GeneratorVariableTracker() const interface_lines_variable_tracker = g.interfaceCodeByVariableTracker(am, gvt).split('\n') @@ -65,6 +68,9 @@ describe("Generator tests", () => { const implementation_lines_profile = g.implementationCodeByProfile(am, gp).split('\n') expect(implementation_lines_profile.length).toBe(97) + const implementation_lines_profile_enumeration = g.implementationCodeByProfileEnumeration(am, libcellml.GeneratorProfile.Profile.C).split('\n') + expect(implementation_lines_profile_enumeration.length).toBe(97) + const implementation_lines_variable_tracker = g.implementationCodeByVariableTracker(am, gvt).split('\n') expect(implementation_lines_variable_tracker.length).toBe(97) diff --git a/tests/bindings/python/test_generator.py b/tests/bindings/python/test_generator.py index 65435a9fd..e0aca348d 100644 --- a/tests/bindings/python/test_generator.py +++ b/tests/bindings/python/test_generator.py @@ -41,6 +41,9 @@ def test_algebraic_eqn_computed_var_on_rhs(self): self.assertEqual("", g.interfaceCode(am, profile)) self.assertEqual(file_contents("generator/algebraic_eqn_computed_var_on_rhs/model.py"), g.implementationCode(am, profile)) + self.assertEqual("", g.interfaceCode(am, GeneratorProfile.Profile.PYTHON)) + self.assertEqual(file_contents("generator/algebraic_eqn_computed_var_on_rhs/model.py"), g.implementationCode(am, GeneratorProfile.Profile.PYTHON)) + variable_tracker = GeneratorVariableTracker() self.assertEqual("", g.interfaceCode(am, profile, variable_tracker)) diff --git a/tests/generator/generator.cpp b/tests/generator/generator.cpp index 00282fe33..411080c52 100644 --- a/tests/generator/generator.cpp +++ b/tests/generator/generator.cpp @@ -1732,3 +1732,25 @@ TEST(Generator, modelWithComplexUnitsOutOfScope) EXPECT_EQ_FILE_CONTENTS("generator/cellml_slc_example/model.py", generator->implementationCode(analyserModel, profile)); } + +TEST(Generator, generateCodeUsingProfileEnum) +{ + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(fileContents("generator/algebraic_eqn_computed_var_on_rhs/model.cellml")); + + EXPECT_EQ(size_t(0), parser->issueCount()); + + auto analyser = libcellml::Analyser::create(); + + analyser->analyseModel(model); + + EXPECT_EQ(size_t(0), analyser->errorCount()); + + auto analyserModel = analyser->analyserModel(); + auto generator = libcellml::Generator::create(); + + EXPECT_EQ_FILE_CONTENTS("generator/algebraic_eqn_computed_var_on_rhs/model.h", generator->interfaceCode(analyserModel, libcellml::GeneratorProfile::Profile::C)); + EXPECT_EQ_FILE_CONTENTS("generator/algebraic_eqn_computed_var_on_rhs/model.c", generator->implementationCode(analyserModel, libcellml::GeneratorProfile::Profile::C)); + + EXPECT_EQ_FILE_CONTENTS("generator/algebraic_eqn_computed_var_on_rhs/model.py", generator->implementationCode(analyserModel, libcellml::GeneratorProfile::Profile::PYTHON)); +}