Skip to content

Commit

Permalink
use ptr rather than lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Dec 3, 2024
1 parent 422dc07 commit 8746355
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 97 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,25 @@ public:

register_variable(
real(
"height", [this] { return height; })
"height", &height)
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::CONTINUOUS));

register_variable(
real(
"velocity", [this] { return velocity; })
"velocity", &velocity)
.setCausality(causality_t::LOCAL)
.setVariability(variability_t::CONTINUOUS));

register_variable(
real(
"gravity", [this] { return gravity; },
[this](const auto &input) { gravity = input; })
"gravity", &gravity)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

register_variable(
real(
"bounceFactor",
[this] { return bounceFactor; },
[this](const auto &input) { bounceFactor = input; })
"bounceFactor", &bounceFactor)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

Expand Down
11 changes: 4 additions & 7 deletions export/examples/BouncingBall/bouncing_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,26 @@ class BouncingBall : public fmu_base {

register_variable(
real(
"height", [this] { return height_; })
"height", &height_)
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::CONTINUOUS)
.setInitial(initial_t::EXACT));

register_variable(
real(
"velocity", [this] { return velocity_; })
"velocity", &velocity_)
.setCausality(causality_t::LOCAL)
.setVariability(variability_t::CONTINUOUS));

register_variable(
real(
"gravity", [this] { return gravity_; },
[this](const auto &input) { gravity_ = input; })
"gravity", &gravity_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

register_variable(
real(
"bounceFactor",
[this] { return bounceFactor_; },
[this](const auto &input) { bounceFactor_ = input; })
"bounceFactor", &bounceFactor_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

Expand Down
8 changes: 5 additions & 3 deletions export/examples/Resource/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class Resource : public fmu_base {

std::ifstream ifs(resources / "file.txt");

std::string line;
std::getline(ifs, line);
std::getline(ifs, content_);

register_variable(
string(
"content", [line] { return line; })
"content", &content_)
.setVariability(variability_t::CONSTANT)
.setCausality(causality_t::OUTPUT));

Expand All @@ -37,6 +36,9 @@ class Resource : public fmu_base {
// do nothing
}

private:
std::string content_;

};

model_info fmu4cpp::get_model_info() {
Expand Down
18 changes: 5 additions & 13 deletions export/examples/SimplePendulum/simple_pendulum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,23 @@ class SimplePendulum : public fmu_base {
SimplePendulum(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

register_variable(real("angle",
[this] { return angle_; })
register_variable(real("angle", &angle_)
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::CONTINUOUS));

register_variable(real("angularVelocity",
[this] { return angularVelocity_; })
register_variable(real("angularVelocity", &angularVelocity_)
.setCausality(causality_t::LOCAL)
.setVariability(variability_t::CONTINUOUS));
register_variable(real(
"gravity",
[this] { return gravity_; },
[this](double input) { gravity_ = input; })
"gravity", &gravity_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));
register_variable(real(
"length",
[this] { return length_; },
[this](double input) { length_ = input; })
"length", &length_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));
register_variable(real(
"damping",
[this] { return damping_; },
[this](double input) { damping_ = input; })
"damping", &damping_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

Expand Down
16 changes: 4 additions & 12 deletions export/include/fmu4cpp/fmu_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,13 @@ namespace fmu4cpp {
virtual ~fmu_base() = default;

protected:
IntVariable integer(const std::string &name,
const std::function<int()> &getter,
const std::optional<std::function<void(int)>> &setter = std::nullopt);
IntVariable integer(const std::string &name, int *ptr);

RealVariable real(const std::string &name,
const std::function<double()> &getter,
const std::optional<std::function<void(double)>> &setter = std::nullopt);
RealVariable real(const std::string &name, double *ptr);

BoolVariable boolean(const std::string &name,
const std::function<bool()> &getter,
const std::optional<std::function<void(bool)>> &setter = std::nullopt);
BoolVariable boolean(const std::string &name, bool *ptr);

StringVariable string(const std::string &name,
const std::function<std::string()> &getter,
const std::optional<std::function<void(std::string)>> &setter = std::nullopt);
StringVariable string(const std::string &name, std::string *ptr);

void register_variable(IntVariable v);
void register_variable(RealVariable v);
Expand Down
42 changes: 17 additions & 25 deletions export/include/fmu4cpp/fmu_variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#ifndef FMU4CPP_FMU_VARIABLE_HPP
#define FMU4CPP_FMU_VARIABLE_HPP

#include <functional>
#include <optional>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -95,17 +94,19 @@ namespace fmu4cpp {
public:
Variable(
const std::string &name,
unsigned int vr, size_t index,
std::function<T()> getter,
std::optional<std::function<void(T)>> setter)
: VariableBase(name, vr, index), getter_(std::move(getter)), setter_(std::move(setter)) {}
unsigned int vr, size_t index, T *ptr)
: VariableBase(name, vr, index), ptr_(ptr) {}

[[nodiscard]] T get() const {
return getter_();
return *ptr_;
}

void set(T value) {
if (setter_) setter_->operator()(value);
if (causality_ == causality_t::LOCAL || causality_ == causality_t::OUTPUT) {
throw std::logic_error("Cannot set value for variable with causality: " + to_string(causality_));
}

*ptr_ = value;
}

V &setCausality(causality_t causality) {
Expand All @@ -131,19 +132,16 @@ namespace fmu4cpp {
}

private:
std::function<T()> getter_;
std::optional<std::function<void(T)>> setter_;
T *ptr_;
};

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

public:
IntVariable(
const std::string &name,
unsigned int vr, size_t index,
const std::function<int()> &getter,
const std::optional<std::function<void(int)>> &setter)
: Variable(name, vr, index, getter, setter) {}
unsigned int vr, size_t index, int *ptr)
: Variable(name, vr, index, ptr) {}

[[nodiscard]] std::optional<int> getMin() const {
return min_;
Expand Down Expand Up @@ -173,10 +171,8 @@ namespace fmu4cpp {
public:
RealVariable(
const std::string &name,
unsigned int vr, size_t index,
const std::function<double()> &getter,
const std::optional<std::function<void(double)>> &setter)
: Variable(name, vr, index, getter, setter) {
unsigned int vr, size_t index, double *ptr)
: Variable(name, vr, index, ptr) {

variability_ = variability_t::CONTINUOUS;
}
Expand Down Expand Up @@ -209,21 +205,17 @@ namespace fmu4cpp {
public:
BoolVariable(
const std::string &name,
unsigned int vr, size_t index,
const std::function<bool()> &getter,
const std::optional<std::function<void(bool)>> &setter)
: Variable(name, vr, index, getter, setter) {}
unsigned int vr, size_t index, bool *ptr)
: Variable(name, vr, index, ptr) {}
};

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

public:
StringVariable(
const std::string &name,
unsigned int vr, size_t index,
const std::function<std::string()> &getter,
const std::optional<std::function<void(std::string)>> &setter)
: Variable(name, vr, index, getter, setter) {}
unsigned int vr, size_t index, std::string *ptr)
: Variable(name, vr, index, ptr) {}
};

bool requires_start(const VariableBase &v);
Expand Down
16 changes: 8 additions & 8 deletions export/src/fmu4cpp/fmu_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,20 @@ namespace fmu4cpp {
return ss.str();
}

IntVariable fmu_base::integer(const std::string &name, const std::function<int()> &getter, const std::optional<std::function<void(int)>> &setter) {
return {name, static_cast<unsigned int>(integers_.size()), numVariables_++, getter, setter};
IntVariable fmu_base::integer(const std::string &name, int* ptr) {
return {name, static_cast<unsigned int>(integers_.size()), numVariables_++, ptr};
}

RealVariable fmu_base::real(const std::string &name, const std::function<double()> &getter, const std::optional<std::function<void(double)>> &setter) {
return {name, static_cast<unsigned int>(reals_.size()), numVariables_++, getter, setter};
RealVariable fmu_base::real(const std::string &name, double* ptr) {
return {name, static_cast<unsigned int>(reals_.size()), numVariables_++, ptr};
}

BoolVariable fmu_base::boolean(const std::string &name, const std::function<bool()> &getter, const std::optional<std::function<void(bool)>> &setter) {
return {name, static_cast<unsigned int>(booleans_.size()), numVariables_++, getter, setter};
BoolVariable fmu_base::boolean(const std::string &name, bool* ptr) {
return {name, static_cast<unsigned int>(booleans_.size()), numVariables_++, ptr};
}

StringVariable fmu_base::string(const std::string &name, const std::function<std::string()> &getter, const std::optional<std::function<void(std::string)>> &setter) {
return {name, static_cast<unsigned int>(strings_.size()), numVariables_++, getter, setter};
StringVariable fmu_base::string(const std::string &name, std::string* ptr) {
return {name, static_cast<unsigned int>(strings_.size()), numVariables_++, ptr};
}

void fmu_base::register_variable(IntVariable v) {
Expand Down
1 change: 1 addition & 0 deletions export/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ function(make_test modelIdentifier sources)
endfunction()

make_test(basic_test basic_test.cpp)
make_test(identity_test identity_test.cpp)
make_test(test_resource test_resource.cpp)
12 changes: 8 additions & 4 deletions export/tests/basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Model : public fmu4cpp::fmu_base {
Model(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

register_variable(real("myReal", [this] { return real_; })
register_variable(real("myReal", &real_)
.setCausality(fmu4cpp::causality_t::OUTPUT));
register_variable(integer("myInteger", [this] { return integer_; })
register_variable(integer("myInteger", &integer_)
.setCausality(fmu4cpp::causality_t::OUTPUT));
register_variable(boolean("myBoolean", [this] { return boolean_; })
register_variable(boolean("myBoolean", &boolean_)
.setCausality(fmu4cpp::causality_t::OUTPUT));
register_variable(string("myString", [this] { return str_; })
register_variable(string("myString", &str_)
.setCausality(fmu4cpp::causality_t::OUTPUT));

Model::reset();
Expand Down Expand Up @@ -74,6 +74,10 @@ TEST_CASE("basic_test") {
instance->enter_initialisation_mode();
instance->exit_initialisation_mode();

unsigned int vr = boolean->value_reference();
int testFail = false;
REQUIRE_THROWS(instance->set_boolean(&vr, 1, &testFail));

int i = 0;
while (t < 10) {
instance->do_step(t, dt);
Expand Down
Loading

0 comments on commit 8746355

Please sign in to comment.