Skip to content

Commit 6f8408c

Browse files
committed
switch binary to std::vector<uint_8>
1 parent 50430cf commit 6f8408c

4 files changed

Lines changed: 48 additions & 24 deletions

File tree

export/include/fmu4cpp/fmu_base.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ namespace fmu4cpp {
213213
const auto idx = vrToBinaryIndices_.at(ref);
214214
const uint8_t *ptr = value[i];
215215
const size_t len = valueSizes[i];
216-
binary_[idx].set(std::string(reinterpret_cast<const char*>(ptr), len));
216+
binary_[idx].set(std::vector(ptr, ptr + len));
217217
}
218218
}
219219

@@ -267,10 +267,10 @@ namespace fmu4cpp {
267267
const std::function<std::string()> &getter,
268268
const std::optional<std::function<void(std::string)>> &setter = std::nullopt);
269269

270-
BinaryVariable binary(const std::string &name, std::string *ptr, const std::function<void()> &onChange = {});
270+
BinaryVariable binary(const std::string &name, BinaryType *ptr, const std::function<void()> &onChange = {});
271271
BinaryVariable binary(const std::string &name,
272-
const std::function<std::string()> &getter,
273-
const std::optional<std::function<void(std::string)>> &setter = std::nullopt);
272+
const std::function<std::vector<uint8_t>()> &getter,
273+
const std::optional<std::function<void(std::vector<uint8_t>)>> &setter = std::nullopt);
274274

275275
void register_variable(IntVariable v);
276276
void register_variable(RealVariable v);

export/include/fmu4cpp/fmu_variable.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
#include <string>
1111
#include <utility>
1212
#include <vector>
13+
#include <cstdint>
1314

1415
namespace fmu4cpp {
1516

17+
using BinaryType = std::vector<uint8_t>;
18+
1619
enum class causality_t {
1720
PARAMETER,
1821
CALCULATED_PARAMETER,
@@ -279,19 +282,19 @@ namespace fmu4cpp {
279282
: Variable(name, vr, index, getter, setter) {}
280283
};
281284

282-
class BinaryVariable : public Variable<std::string, BinaryVariable> {
285+
class BinaryVariable : public Variable<BinaryType, BinaryVariable> {
283286

284287
public:
285288
BinaryVariable(
286289
const std::string &name,
287-
unsigned int vr, size_t index, std::string *ptr, const std::function<void()> &onChange)
290+
unsigned int vr, size_t index, BinaryType *ptr, const std::function<void()> &onChange)
288291
: Variable(name, vr, index, ptr, onChange) {}
289292

290293
BinaryVariable(
291294
const std::string &name,
292295
unsigned int vr, size_t index,
293-
const std::function<std::string()> &getter,
294-
const std::optional<std::function<void(std::string)>> &setter)
296+
const std::function<BinaryType()> &getter,
297+
const std::optional<std::function<void(BinaryType)>> &setter)
295298
: Variable(name, vr, index, getter, setter) {}
296299
};
297300

export/src/fmu4cpp/fmu_base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ namespace fmu4cpp {
6868
return {name, vr, numVariables_, getter, setter};
6969
}
7070

71-
BinaryVariable fmu_base::binary(const std::string &name, std::string *ptr, const std::function<void()> &onChange) {
71+
BinaryVariable fmu_base::binary(const std::string &name, BinaryType *ptr, const std::function<void()> &onChange) {
7272
const auto vr = static_cast<unsigned int>(numVariables_++);
7373
return {name, vr, numVariables_, ptr, onChange};
7474
}
7575

76-
BinaryVariable fmu_base::binary(const std::string &name, const std::function<std::string()> &getter, const std::optional<std::function<void(std::string)>> &setter) {
76+
BinaryVariable fmu_base::binary(const std::string &name, const std::function<BinaryType()> &getter, const std::optional<std::function<void(BinaryType)>> &setter) {
7777
const auto vr = static_cast<unsigned int>(numVariables_++);
7878
return {name, vr, numVariables_, getter, setter};
7979
}

export/tests/fmi3/binary_test.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,36 @@ class Model : public fmu4cpp::fmu_base {
1818
FMU4CPP_CTOR(Model) {
1919

2020
register_variable(binary("binaryIn", &binaryIn_).setCausality(fmu4cpp::causality_t::INPUT));
21-
register_variable(real("numberOut", &numberOut_).setCausality(fmu4cpp::causality_t::OUTPUT));
21+
register_variable(real("realOut", &realOut_).setCausality(fmu4cpp::causality_t::OUTPUT));
22+
register_variable(integer("integerOut", &integerOut_).setCausality(fmu4cpp::causality_t::OUTPUT));
23+
register_variable(string("stringOut", &stringOut_).setCausality(fmu4cpp::causality_t::OUTPUT));
2224

2325
Model::reset();
2426
}
2527

2628
bool do_step(double dt) override {
2729

28-
std::cout << binaryIn_ << std::endl;
2930
nlohmann::json j = nlohmann::json::from_msgpack(binaryIn_);
30-
numberOut_ = j["number"];
31+
realOut_ = j["real"];
32+
integerOut_ = j["integer"];
33+
stringOut_ = j["string"];
3134

3235
return true;
3336
}
3437

3538
void reset() override {
36-
binaryIn_ = "";
37-
numberOut_ = 0;
39+
binaryIn_ = {};
40+
realOut_ = 0;
41+
integerOut_ = 0;
42+
stringOut_ = "empty";
3843
}
3944

4045
private:
41-
std::string binaryIn_;
42-
double numberOut_;
46+
std::vector<uint8_t> binaryIn_;
47+
48+
double realOut_;
49+
int integerOut_;
50+
std::string stringOut_;
4351
};
4452

4553
fmu4cpp::model_info fmu4cpp::get_model_info() {
@@ -70,10 +78,14 @@ TEST_CASE("test_binary") {
7078
REQUIRE(fmi3EnterInitializationMode(c, false, 0, 0, false, 0) == fmi3OK);
7179
REQUIRE(fmi3ExitInitializationMode(c) == fmi3OK);
7280

73-
double numberIn = 3.9;
81+
double realIn = 3.9;
82+
int integerIn = 42;
83+
std::string stringIn = "hello";
7484

7585
nlohmann::json j;
76-
j["number"] = numberIn;
86+
j["real"] = realIn;
87+
j["integer"] = integerIn;
88+
j["string"] = stringIn;
7789

7890
const auto data = nlohmann::json::to_msgpack(j);
7991
const uint8_t *binaryPtr = data.data();
@@ -95,15 +107,24 @@ TEST_CASE("test_binary") {
95107
bool terminateSimulation;
96108
bool earlyReturn;
97109
double lastSucessfulTime;
98-
99110
REQUIRE(fmi3DoStep(c, 0, 0.1, true, &eventhandlingNeeded, &terminateSimulation, &earlyReturn, &lastSucessfulTime) == fmi3OK);
100111

101112

102-
fmi3ValueReference outVr = 2;
103-
double numberOut;
104-
fmi3GetFloat64(c, &outVr, 1, &numberOut, 1);
113+
double realOut;
114+
fmi3ValueReference realVr = 2;
115+
CHECK(fmi3GetFloat64(c, &realVr, 1, &realOut, 1) == fmi3OK);
116+
117+
int integerOut;
118+
fmi3ValueReference integerVr = 3;
119+
CHECK(fmi3GetInt32(c, &integerVr, 1, &integerOut, 1) == fmi3OK);
120+
121+
fmi3String stringOut;
122+
fmi3ValueReference stringVr = 4;
123+
CHECK(fmi3GetString(c, &stringVr, 1, &stringOut, 1) == fmi3OK);
105124

106-
CHECK(numberOut == Catch::Approx(3.9));
125+
CHECK(realOut == Catch::Approx(realIn));
126+
CHECK(integerOut == integerIn);
127+
CHECK(stringOut == stringIn);
107128

108129
REQUIRE(fmi3Terminate(c) == fmi3OK);
109130

0 commit comments

Comments
 (0)