diff --git a/export/include/fmu4cpp/fmu_base.hpp b/export/include/fmu4cpp/fmu_base.hpp index 74b04da..438fcb3 100644 --- a/export/include/fmu4cpp/fmu_base.hpp +++ b/export/include/fmu4cpp/fmu_base.hpp @@ -35,28 +35,28 @@ namespace fmu4cpp { return resourceLocation_; } - std::optional get_int_variable(const std::string &name) { + [[nodiscard]] std::optional get_int_variable(const std::string &name) const { for (const auto &v: integers_) { if (v.name() == name) return v; } return std::nullopt; } - std::optional get_real_variable(const std::string &name) { + [[nodiscard]] std::optional get_real_variable(const std::string &name) const { for (const auto &v: reals_) { if (v.name() == name) return v; } return std::nullopt; } - std::optional get_bool_variable(const std::string &name) { + [[nodiscard]] std::optional get_bool_variable(const std::string &name) const { for (const auto &v: booleans_) { if (v.name() == name) return v; } return std::nullopt; } - std::optional get_string_variable(const std::string &name) { + [[nodiscard]] std::optional get_string_variable(const std::string &name) const { for (const auto &v: strings_) { if (v.name() == name) return v; } @@ -77,21 +77,21 @@ namespace fmu4cpp { void get_integer(const unsigned int vr[], size_t nvr, int value[]) const { for (unsigned i = 0; i < nvr; i++) { - unsigned int ref = vr[i]; + const auto ref = vr[i]; value[i] = integers_[ref].get(); } } void get_real(const unsigned int vr[], size_t nvr, double value[]) const { for (unsigned i = 0; i < nvr; i++) { - unsigned int ref = vr[i]; + const auto ref = vr[i]; value[i] = reals_[ref].get(); } } void get_boolean(const unsigned int vr[], size_t nvr, int value[]) const { for (unsigned i = 0; i < nvr; i++) { - unsigned int ref = vr[i]; + const auto ref = vr[i]; value[i] = static_cast(booleans_[ref].get()); } } @@ -99,7 +99,7 @@ namespace fmu4cpp { void get_string(const unsigned int vr[], size_t nvr, const char *value[]) { stringBuffer_.clear(); for (unsigned i = 0; i < nvr; i++) { - unsigned int ref = vr[i]; + const auto ref = vr[i]; stringBuffer_.push_back(strings_[ref].get()); value[i] = stringBuffer_.back().c_str(); } @@ -107,28 +107,28 @@ namespace fmu4cpp { void set_integer(const unsigned int vr[], size_t nvr, const int value[]) { for (unsigned i = 0; i < nvr; i++) { - unsigned int ref = vr[i]; + const auto ref = vr[i]; integers_[ref].set(value[i]); } } void set_real(const unsigned int vr[], size_t nvr, const double value[]) { for (unsigned i = 0; i < nvr; i++) { - unsigned int ref = vr[i]; + const auto ref = vr[i]; reals_[ref].set(value[i]); } } void set_boolean(const unsigned int vr[], size_t nvr, const int value[]) { for (unsigned i = 0; i < nvr; i++) { - unsigned int ref = vr[i]; + const auto ref = vr[i]; booleans_[ref].set(static_cast(value[i])); } } void set_string(const unsigned int vr[], size_t nvr, const char *const value[]) { for (unsigned i = 0; i < nvr; i++) { - unsigned int ref = vr[i]; + const auto ref = vr[i]; strings_[ref].set(value[i]); } } diff --git a/export/include/fmu4cpp/fmu_except.hpp b/export/include/fmu4cpp/fmu_except.hpp index a9f23fa..f359028 100644 --- a/export/include/fmu4cpp/fmu_except.hpp +++ b/export/include/fmu4cpp/fmu_except.hpp @@ -7,7 +7,7 @@ namespace fmu4cpp { - class fatal_error : public std::runtime_error { + class fatal_error final : public std::runtime_error { public: explicit fatal_error(const std::string &msg) : std::runtime_error(msg) {} }; diff --git a/export/include/fmu4cpp/fmu_variable.hpp b/export/include/fmu4cpp/fmu_variable.hpp index 786fc21..b5b1f79 100644 --- a/export/include/fmu4cpp/fmu_variable.hpp +++ b/export/include/fmu4cpp/fmu_variable.hpp @@ -38,10 +38,6 @@ namespace fmu4cpp { std::string to_string(const initial_t &i); class VariableBase { - private: - std::string name_; - unsigned int vr_; - size_t index_; protected: causality_t causality_ = causality_t::LOCAL; @@ -86,13 +82,15 @@ namespace fmu4cpp { } virtual ~VariableBase() = default; + + private: + std::string name_; + unsigned int vr_; + size_t index_; }; template class Variable : public VariableBase { - private: - std::function getter_; - std::optional> setter_; public: Variable( @@ -131,12 +129,13 @@ namespace fmu4cpp { } return *static_cast(this); } - }; - class IntVariable : public Variable { private: - std::optional min_; - std::optional max_; + std::function getter_; + std::optional> setter_; + }; + + class IntVariable final : public Variable { public: IntVariable( @@ -163,12 +162,13 @@ namespace fmu4cpp { max_ = max; return *this; } - }; - class RealVariable : public Variable { private: - std::optional min_; - std::optional max_; + std::optional min_; + std::optional max_; + }; + + class RealVariable final : public Variable { public: RealVariable( @@ -198,9 +198,13 @@ namespace fmu4cpp { max_ = max; return *this; } + + private: + std::optional min_; + std::optional max_; }; - class BoolVariable : public Variable { + class BoolVariable final : public Variable { public: BoolVariable( @@ -211,7 +215,7 @@ namespace fmu4cpp { : Variable(name, vr, index, getter, setter) {} }; - class StringVariable : public Variable { + class StringVariable final : public Variable { public: StringVariable( diff --git a/export/include/fmu4cpp/lib_info.hpp b/export/include/fmu4cpp/lib_info.hpp index 04897ce..8450844 100644 --- a/export/include/fmu4cpp/lib_info.hpp +++ b/export/include/fmu4cpp/lib_info.hpp @@ -7,7 +7,7 @@ namespace fmu4cpp { /// Software version - struct version { + struct version final { int major = 0; int minor = 0; int patch = 0; diff --git a/export/include/fmu4cpp/logger.hpp b/export/include/fmu4cpp/logger.hpp index 4b8decc..28801a0 100644 --- a/export/include/fmu4cpp/logger.hpp +++ b/export/include/fmu4cpp/logger.hpp @@ -6,7 +6,7 @@ namespace fmu4cpp { - class logger { + class logger final { public: logger(const fmi2CallbackFunctions &f, std::string instanceName) diff --git a/export/include/fmu4cpp/model_info.hpp b/export/include/fmu4cpp/model_info.hpp index d2bc82c..94ef7a1 100644 --- a/export/include/fmu4cpp/model_info.hpp +++ b/export/include/fmu4cpp/model_info.hpp @@ -6,7 +6,7 @@ namespace fmu4cpp { - struct model_info { + struct model_info final { std::string modelName; std::string author; std::string description; diff --git a/export/src/fmu4cpp/fmi2.cpp b/export/src/fmu4cpp/fmi2.cpp index fb0de16..8ec5092 100644 --- a/export/src/fmu4cpp/fmi2.cpp +++ b/export/src/fmu4cpp/fmi2.cpp @@ -88,9 +88,9 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, return nullptr; } - const auto c = new Component(std::move(slave), *functions); + auto c = std::make_unique(std::move(slave), *functions); c->logger.setDebugLogging(loggingOn); - return c; + return c.release(); } fmi2Status fmi2SetupExperiment(fmi2Component c, @@ -109,9 +109,11 @@ fmi2Status fmi2SetupExperiment(fmi2Component c, try { component->slave->setup_experiment(startTime, stop, tol); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -122,9 +124,11 @@ fmi2Status fmi2EnterInitializationMode(fmi2Component c) { try { component->slave->enter_initialisation_mode(); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -134,9 +138,11 @@ fmi2Status fmi2ExitInitializationMode(fmi2Component c) { try { component->slave->exit_initialisation_mode(); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -146,9 +152,11 @@ fmi2Status fmi2Terminate(fmi2Component c) { try { component->slave->terminate(); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -167,9 +175,11 @@ fmi2Status fmi2DoStep( } return fmi2Discard; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -194,9 +204,11 @@ fmi2Status fmi2GetInteger( try { component->slave->get_integer(vr, nvr, value); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -211,9 +223,11 @@ fmi2Status fmi2GetReal( try { component->slave->get_real(vr, nvr, value); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -228,9 +242,11 @@ fmi2Status fmi2GetBoolean( try { component->slave->get_boolean(vr, nvr, value); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -245,9 +261,11 @@ fmi2Status fmi2GetString( try { component->slave->get_string(vr, nvr, value); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -262,9 +280,11 @@ fmi2Status fmi2SetInteger( try { component->slave->set_integer(vr, nvr, value); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -279,9 +299,11 @@ fmi2Status fmi2SetReal( try { component->slave->set_real(vr, nvr, value); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -296,9 +318,11 @@ fmi2Status fmi2SetBoolean( try { component->slave->set_boolean(vr, nvr, value); return fmi2OK; - } catch (const fmu4cpp::fatal_error &) { + } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; - } catch (const std::exception &) { + } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -314,8 +338,10 @@ fmi2Status fmi2SetString( component->slave->set_string(vr, nvr, value); return fmi2OK; } catch (const fmu4cpp::fatal_error &ex) { + component->logger.log(fmi2Fatal, ex.what()); return fmi2Fatal; } catch (const std::exception &ex) { + component->logger.log(fmi2Error, ex.what()); return fmi2Error; } } @@ -367,8 +393,8 @@ fmi2Status fmi2GetStringStatus( fmi2Status fmi2SetDebugLogging(fmi2Component c, fmi2Boolean loggingOn, - size_t nCategories, - const fmi2String categories[]) { + size_t /*nCategories*/, + const fmi2String /*categories*/[]) { const auto component = static_cast(c); component->logger.setDebugLogging(loggingOn); diff --git a/export/src/fmu4cpp/fmu_base.cpp b/export/src/fmu4cpp/fmu_base.cpp index 248b606..186c13c 100644 --- a/export/src/fmu4cpp/fmu_base.cpp +++ b/export/src/fmu4cpp/fmu_base.cpp @@ -96,7 +96,7 @@ namespace fmu4cpp { ss << "\t\n"; auto allVars = collect(integers_, reals_, booleans_, strings_); - std::sort(allVars.begin(), allVars.end(), [](const VariableBase *v1, const VariableBase *v2) { + std::ranges::sort(allVars, [](const VariableBase *v1, const VariableBase *v2) { return v1->index() < v2->index(); });