Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Dec 3, 2024
1 parent cdb8505 commit d81348e
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 62 deletions.
24 changes: 12 additions & 12 deletions export/include/fmu4cpp/fmu_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@ namespace fmu4cpp {
return resourceLocation_;
}

std::optional<IntVariable> get_int_variable(const std::string &name) {
[[nodiscard]] std::optional<IntVariable> get_int_variable(const std::string &name) const {
for (const auto &v: integers_) {
if (v.name() == name) return v;
}
return std::nullopt;
}

std::optional<RealVariable> get_real_variable(const std::string &name) {
[[nodiscard]] std::optional<RealVariable> get_real_variable(const std::string &name) const {
for (const auto &v: reals_) {
if (v.name() == name) return v;
}
return std::nullopt;
}

std::optional<BoolVariable> get_bool_variable(const std::string &name) {
[[nodiscard]] std::optional<BoolVariable> get_bool_variable(const std::string &name) const {
for (const auto &v: booleans_) {
if (v.name() == name) return v;
}
return std::nullopt;
}

std::optional<StringVariable> get_string_variable(const std::string &name) {
[[nodiscard]] std::optional<StringVariable> get_string_variable(const std::string &name) const {
for (const auto &v: strings_) {
if (v.name() == name) return v;
}
Expand All @@ -77,58 +77,58 @@ 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<int>(booleans_[ref].get());
}
}

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();
}
}

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<bool>(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]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion export/include/fmu4cpp/fmu_except.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
};
Expand Down
38 changes: 21 additions & 17 deletions export/include/fmu4cpp/fmu_variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,13 +82,15 @@ namespace fmu4cpp {
}

virtual ~VariableBase() = default;

private:
std::string name_;
unsigned int vr_;
size_t index_;
};

template<class T, class V>
class Variable : public VariableBase {
private:
std::function<T()> getter_;
std::optional<std::function<void(T)>> setter_;

public:
Variable(
Expand Down Expand Up @@ -131,12 +129,13 @@ namespace fmu4cpp {
}
return *static_cast<V *>(this);
}
};

class IntVariable : public Variable<int, IntVariable> {
private:
std::optional<int> min_;
std::optional<int> max_;
std::function<T()> getter_;
std::optional<std::function<void(T)>> setter_;
};

class IntVariable final : public Variable<int, IntVariable> {

public:
IntVariable(
Expand All @@ -163,12 +162,13 @@ namespace fmu4cpp {
max_ = max;
return *this;
}
};

class RealVariable : public Variable<double, RealVariable> {
private:
std::optional<double> min_;
std::optional<double> max_;
std::optional<int> min_;
std::optional<int> max_;
};

class RealVariable final : public Variable<double, RealVariable> {

public:
RealVariable(
Expand Down Expand Up @@ -198,9 +198,13 @@ namespace fmu4cpp {
max_ = max;
return *this;
}

private:
std::optional<double> min_;
std::optional<double> max_;
};

class BoolVariable : public Variable<bool, BoolVariable> {
class BoolVariable final : public Variable<bool, BoolVariable> {

public:
BoolVariable(
Expand All @@ -211,7 +215,7 @@ namespace fmu4cpp {
: Variable(name, vr, index, getter, setter) {}
};

class StringVariable : public Variable<std::string, StringVariable> {
class StringVariable final : public Variable<std::string, StringVariable> {

public:
StringVariable(
Expand Down
2 changes: 1 addition & 1 deletion export/include/fmu4cpp/lib_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace fmu4cpp {

/// Software version
struct version {
struct version final {
int major = 0;
int minor = 0;
int patch = 0;
Expand Down
2 changes: 1 addition & 1 deletion export/include/fmu4cpp/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace fmu4cpp {

class logger {
class logger final {

public:
logger(const fmi2CallbackFunctions &f, std::string instanceName)
Expand Down
2 changes: 1 addition & 1 deletion export/include/fmu4cpp/model_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace fmu4cpp {

struct model_info {
struct model_info final {
std::string modelName;
std::string author;
std::string description;
Expand Down
Loading

0 comments on commit d81348e

Please sign in to comment.