From 31e98b32fa06cf1dcf2527b1d258c3e83fa74522 Mon Sep 17 00:00:00 2001 From: "joaquin.f.fernandez" Date: Tue, 27 Aug 2024 19:15:00 -0300 Subject: [PATCH] Update model instance and generator code. --- src/mmoc/generator/generator.cpp | 30 +++++++++++++-------------- src/mmoc/generator/generator.h | 10 ++++----- src/mmoc/generator/model_instance.h | 32 +++++++++++++++-------------- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/mmoc/generator/generator.cpp b/src/mmoc/generator/generator.cpp index 8408c7f5..48ef8460 100644 --- a/src/mmoc/generator/generator.cpp +++ b/src/mmoc/generator/generator.cpp @@ -36,13 +36,13 @@ using namespace IR; using namespace Util; namespace Generator { -Generator::Generator(const StoredDefinition& std, CompileFlags& flags) +Generator::Generator(const StoredDefinition& std, const CompileFlags& flags) : _std(std), _flags(flags), _model_instance(nullptr), _writer(nullptr), _includes(), _fheader() { if (_flags.output()) { - _writer = WriterPtr(new MemoryWriter()); + _writer = WriterPtr(std::make_shared()); } else { - _writer = WriterPtr(new FileWriter()); + _writer = WriterPtr(std::make_shared()); } } @@ -56,15 +56,15 @@ int Generator::generate() } _writer->setFile(base_name + ".c"); switch (model.annotations().solver()) { - case DOPRI: - case DASSL: - case CVODE_BDF: - case IDA: - case CVODE_AM: - _model_instance = ModelInstancePtr(new ClassicModelInstance(model, _flags, _writer)); + case Solver::DOPRI: + case Solver::DASSL: + case Solver::CVODE_BDF: + case Solver::IDA: + case Solver::CVODE_AM: + _model_instance = ModelInstancePtr(std::make_shared(model, _flags, _writer)); break; default: - _model_instance = ModelInstancePtr(new QSSModelInstance(model, _flags, _writer)); + _model_instance = ModelInstancePtr(std::make_shared(model, _flags, _writer)); } _model_instance->generate(); _writer->clearFile(); @@ -77,7 +77,7 @@ int Generator::generate() files.run(); files.plot(); files.settings(model.annotations()); - if (model.annotations().solver() == LIQSS_BDF && !model.annotations().BDFPartition()->empty()) { + if (model.annotations().solver() == Solver::LIQSS_BDF && !model.annotations().BDFPartition()->empty()) { files.bdfPartition(); } if (model.externalFunctions()) { @@ -103,7 +103,7 @@ int Generator::generate() return Error::instance().errors(); } -void Generator::generateIncludes(string name) +void Generator::generateIncludes(const string& name) { stringstream buffer; buffer << "#include " << endl; @@ -112,15 +112,13 @@ void Generator::generateIncludes(string name) _writer->write(buffer, WRITER::Function_Header); } -void Generator::generateModel() {} - -void Generator::calledFunctionHeader(string file_name) +void Generator::calledFunctionHeader(const string& file_name) { string indent = _writer->indent(1); string file = file_name; file.append(".h"); _writer->setFile(file); - for (list::iterator it = _fheader.begin(); it != _fheader.end(); it++) { + for (auto it = _fheader.begin(); it != _fheader.end(); it++) { _writer->print(*it); } _writer->clearFile(); diff --git a/src/mmoc/generator/generator.h b/src/mmoc/generator/generator.h index c4d9713c..249d55d2 100644 --- a/src/mmoc/generator/generator.h +++ b/src/mmoc/generator/generator.h @@ -17,7 +17,7 @@ ******************************************************************************/ -#pragma once +#pragma once #include #include @@ -37,14 +37,13 @@ namespace Generator { class Generator { public: - Generator(const IR::StoredDefinition& std, Util::CompileFlags& flags); + Generator(const IR::StoredDefinition& std, const Util::CompileFlags& flags); ~Generator() = default; int generate(); private: - void generateIncludes(string name); - void generateModel(); - void calledFunctionHeader(string file_name); + void generateIncludes(const string& name); + void calledFunctionHeader(const string& file_name); IR::StoredDefinition _std; IR::Function _function; IR::Package _package; @@ -57,4 +56,3 @@ class Generator { }; } // namespace Generator } // namespace MicroModelica - diff --git a/src/mmoc/generator/model_instance.h b/src/mmoc/generator/model_instance.h index 5a81efb1..c2b5b8be 100644 --- a/src/mmoc/generator/model_instance.h +++ b/src/mmoc/generator/model_instance.h @@ -35,7 +35,7 @@ namespace Generator { namespace MODEL_INSTANCE { -typedef enum { +enum class Component { Model_Settings, Model, Deps, @@ -47,9 +47,10 @@ typedef enum { BdfModel, CLC_Init, QSS_Init -} Component; +}; + +enum class NodeType { SD, SZ, HD, HZ, DD }; -typedef enum { SD, SZ, HD, HZ, DD } NodeType; } // namespace MODEL_INSTANCE class ModelInstance { @@ -118,14 +119,14 @@ class QSSModelInstance : public ModelInstance { public: QSSModelInstance(); QSSModelInstance(IR::Model &model, Util::CompileFlags &flags, WriterPtr writer); - ~QSSModelInstance() = default; - void initializeDataStructures(); - Graph computationalGraph(); - void generate(); - void header(); + ~QSSModelInstance() override = default; + void initializeDataStructures() override; + Graph computationalGraph() override; + void generate() override; + void header() override; protected: - void definition(); + void definition() override; void dependencies(); void bdfDefinition(); @@ -142,13 +143,13 @@ class QSSModelInstance : public ModelInstance { class ClassicModelInstance : public ModelInstance { public: ClassicModelInstance(IR::Model &model, Util::CompileFlags &flags, WriterPtr writer); - ~ClassicModelInstance() = default; - void initializeDataStructures(); - void generate(); - void header(); + ~ClassicModelInstance() override = default; + void initializeDataStructures() override; + void generate() override; + void header() override; protected: - void definition(); + void definition() override; private: void allocateSolver(); @@ -159,6 +160,7 @@ class ClassicModelInstance : public ModelInstance { WriterPtr _writer; }; -typedef std::shared_ptr ModelInstancePtr; +using ModelInstancePtr = std::shared_ptr; + } // namespace Generator } // namespace MicroModelica