Skip to content

Commit 3aa8091

Browse files
committed
unique valueReferences
1 parent 1316deb commit 3aa8091

6 files changed

Lines changed: 93 additions & 73 deletions

File tree

export/include/fmu4cpp/fmu_base.hpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#include <filesystem>
1818

19-
#define FMU4CPP_INSTANTIATE(MODELCLASS) \
20-
std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, \
21-
const std::filesystem::path &fmuResourceLocation) { \
22-
return std::make_unique<MODELCLASS>(instanceName, fmuResourceLocation); \
19+
#define FMU4CPP_INSTANTIATE(MODELCLASS) \
20+
std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const std::string &instanceName, \
21+
const std::filesystem::path &fmuResourceLocation) { \
22+
return std::make_unique<MODELCLASS>(instanceName, fmuResourceLocation); \
2323
}
2424

2525
namespace fmu4cpp {
@@ -111,76 +111,86 @@ namespace fmu4cpp {
111111
void get_integer(const unsigned int vr[], size_t nvr, int value[]) const {
112112
for (unsigned i = 0; i < nvr; i++) {
113113
const auto ref = vr[i];
114-
value[i] = integers_[ref].get();
114+
const auto idx = vrToIntegerIndices_.at(ref);
115+
value[i] = integers_[idx].get();
115116
}
116117
}
117118

118119
void get_real(const unsigned int vr[], size_t nvr, double value[]) const {
119120
for (unsigned i = 0; i < nvr; i++) {
120121
const auto ref = vr[i];
121-
value[i] = reals_[ref].get();
122+
const auto idx = vrToRealIndices_.at(ref);
123+
value[i] = reals_[idx].get();
122124
}
123125
}
124126

125127
//fmi2
126128
void get_boolean(const unsigned int vr[], size_t nvr, int value[]) const {
127129
for (unsigned i = 0; i < nvr; i++) {
128130
const auto ref = vr[i];
129-
value[i] = static_cast<int>(booleans_[ref].get());
131+
const auto idx = vrToBooleanIndices_.at(ref);
132+
value[i] = static_cast<int>(booleans_[idx].get());
130133
}
131134
}
132135

133136
//fmi3
134137
void get_boolean(const unsigned int vr[], size_t nvr, bool value[]) const {
135138
for (unsigned i = 0; i < nvr; i++) {
136139
const auto ref = vr[i];
137-
value[i] = booleans_[ref].get();
140+
const auto idx = vrToBooleanIndices_.at(ref);
141+
value[i] = booleans_[idx].get();
138142
}
139143
}
140144

141145
void get_string(const unsigned int vr[], size_t nvr, const char *value[]) {
142146
stringBuffer_.clear();
143147
for (unsigned i = 0; i < nvr; i++) {
144148
const auto ref = vr[i];
145-
stringBuffer_.push_back(strings_[ref].get());
149+
const auto idx = vrToStringIndices_.at(ref);
150+
stringBuffer_.push_back(strings_[idx].get());
146151
value[i] = stringBuffer_.back().c_str();
147152
}
148153
}
149154

150155
void set_integer(const unsigned int vr[], size_t nvr, const int value[]) {
151156
for (unsigned i = 0; i < nvr; i++) {
152157
const auto ref = vr[i];
153-
integers_[ref].set(value[i]);
158+
const auto idx = vrToIntegerIndices_.at(ref);
159+
integers_[idx].set(value[i]);
154160
}
155161
}
156162

157163
void set_real(const unsigned int vr[], size_t nvr, const double value[]) {
158164
for (unsigned i = 0; i < nvr; i++) {
159165
const auto ref = vr[i];
160-
reals_[ref].set(value[i]);
166+
const auto idx = vrToRealIndices_.at(ref);
167+
reals_[idx].set(value[i]);
161168
}
162169
}
163170

164171
//fmi2
165172
void set_boolean(const unsigned int vr[], size_t nvr, const int value[]) {
166173
for (unsigned i = 0; i < nvr; i++) {
167174
const auto ref = vr[i];
168-
booleans_[ref].set(static_cast<bool>(value[i]));
175+
const auto idx = vrToBooleanIndices_.at(ref);
176+
booleans_[idx].set(static_cast<bool>(value[i]));
169177
}
170178
}
171179

172180
//fmi3
173181
void set_boolean(const unsigned int vr[], size_t nvr, const bool value[]) {
174182
for (unsigned i = 0; i < nvr; i++) {
175183
const auto ref = vr[i];
176-
booleans_[ref].set(value[i]);
184+
const auto idx = vrToBooleanIndices_.at(ref);
185+
booleans_[idx].set(value[i]);
177186
}
178187
}
179188

180189
void set_string(const unsigned int vr[], size_t nvr, const char *const value[]) {
181190
for (unsigned i = 0; i < nvr; i++) {
182191
const auto ref = vr[i];
183-
strings_[ref].set(value[i]);
192+
const auto idx = vrToStringIndices_.at(ref);
193+
strings_[idx].set(value[i]);
184194
}
185195
}
186196

@@ -260,17 +270,23 @@ namespace fmu4cpp {
260270
std::optional<double> tolerance_;
261271

262272
logger *logger_ = nullptr;
263-
size_t numVariables_{1};
273+
size_t numVariables_{0};
264274

265275
std::string instanceName_;
266276
std::filesystem::path resourceLocation_;
267277

268278
std::vector<IntVariable> integers_;
279+
std::unordered_map<unsigned int, size_t> vrToIntegerIndices_;
280+
269281
std::vector<RealVariable> reals_;
282+
std::unordered_map<unsigned int, size_t> vrToRealIndices_;
283+
270284
std::vector<BoolVariable> booleans_;
271-
std::vector<StringVariable> strings_;
285+
std::unordered_map<unsigned int, size_t> vrToBooleanIndices_;
272286

287+
std::vector<StringVariable> strings_;
273288
std::vector<std::string> stringBuffer_;
289+
std::unordered_map<unsigned int, size_t> vrToStringIndices_;
274290
};
275291

276292
model_info get_model_info();

export/src/fmu4cpp/fmu_base.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,51 +67,55 @@ namespace fmu4cpp {
6767
}
6868

6969
IntVariable fmu_base::integer(const std::string &name, int *ptr) {
70-
return {name, static_cast<unsigned int>(integers_.size()), numVariables_++, ptr};
70+
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, ptr};
7171
}
7272

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

7777
RealVariable fmu_base::real(const std::string &name, double *ptr) {
78-
return {name, static_cast<unsigned int>(reals_.size()), numVariables_++, ptr};
78+
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, ptr};
7979
}
8080

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

8585
BoolVariable fmu_base::boolean(const std::string &name, bool *ptr) {
86-
return {name, static_cast<unsigned int>(booleans_.size()), numVariables_++, ptr};
86+
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, ptr};
8787
}
8888

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

9393
StringVariable fmu_base::string(const std::string &name, std::string *ptr) {
94-
return {name, static_cast<unsigned int>(strings_.size()), numVariables_++, ptr};
94+
return {name, static_cast<unsigned int>(numVariables_), ++numVariables_, ptr};
9595
}
9696

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

101101
void fmu_base::register_variable(IntVariable v) {
102102
integers_.emplace_back(std::move(v));
103+
vrToIntegerIndices_.emplace(v.value_reference(), integers_.size() - 1);
103104
}
104105

105106
void fmu_base::register_variable(RealVariable v) {
106107
reals_.emplace_back(std::move(v));
108+
vrToRealIndices_.emplace(v.value_reference(), reals_.size() - 1);
107109
}
108110

109111
void fmu_base::register_variable(BoolVariable v) {
110112
booleans_.emplace_back(std::move(v));
113+
vrToBooleanIndices_.emplace(v.value_reference(), booleans_.size() - 1);
111114
}
112115

113116
void fmu_base::register_variable(StringVariable v) {
114117
strings_.emplace_back(std::move(v));
118+
vrToStringIndices_.emplace(v.value_reference(), strings_.size() - 1);
115119
}
116120

117121
[[maybe_unused]] std::string fmu_base::guid() const {
@@ -346,7 +350,7 @@ namespace fmu4cpp {
346350

347351
auto type = [](const VariableBase *v) {
348352
if (auto i = dynamic_cast<const IntVariable *>(v)) {
349-
return "Int32";
353+
return "Int32";
350354
} else if (auto r = dynamic_cast<const RealVariable *>(v)) {
351355
return "Float64";
352356
} else if (auto s = dynamic_cast<const StringVariable *>(v)) {

export/tests/array_test.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
#include "catch2/matchers/catch_matchers_vector.hpp"
32
#include "fmi2/fmi2Functions.h"
43

5-
64
#include <catch2/catch_approx.hpp>
75
#include <catch2/catch_test_macros.hpp>
6+
#include <catch2/matchers/catch_matchers_vector.hpp>
7+
88
#include <cstdarg>
99
#include <iostream>
1010

@@ -44,10 +44,8 @@ fmu4cpp::model_info fmu4cpp::get_model_info() {
4444
return info;
4545
}
4646

47-
std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const std::string &instanceName,
48-
const std::filesystem::path &fmuResourceLocation) {
49-
return std::make_unique<Model>(instanceName, fmuResourceLocation);
50-
}
47+
FMU4CPP_INSTANTIATE(Model);
48+
5149

5250
void fmilogger(fmi2Component, fmi2String instanceName, fmi2Status status, fmi2String /*category*/, fmi2String message, ...) {
5351
va_list args;
@@ -63,6 +61,8 @@ TEST_CASE("test_array") {
6361
Model model("", "");
6462
const auto guid = model.guid();
6563

64+
std::cout << model.make_description_v2() << std::endl;
65+
6666
fmi2CallbackFunctions callbackFunctions;
6767
callbackFunctions.logger = &fmilogger;
6868

@@ -74,9 +74,9 @@ TEST_CASE("test_array") {
7474
REQUIRE(fmi2SetupExperiment(c, false, 0, 0, false, 0) == fmi2OK);
7575

7676
std::vector<double> values(4);
77-
for (int i = 1; i <= 4; i++) { // account for time
77+
for (int i = 1; i <= 4; i++) {// account for time
7878
fmi2ValueReference ref = i;
79-
fmi2GetReal(c, &ref, 1, &values[i-1]);
79+
fmi2GetReal(c, &ref, 1, &values[i - 1]);
8080
}
8181
REQUIRE(values == std::vector<double>{1, 2, 3, 4});
8282

@@ -88,7 +88,7 @@ TEST_CASE("test_array") {
8888

8989
for (int i = 1; i <= 4; i++) {
9090
fmi2ValueReference ref = i;
91-
fmi2GetReal(c, &ref, 1, &values[i-1]);
91+
fmi2GetReal(c, &ref, 1, &values[i - 1]);
9292
}
9393
REQUIRE(values == std::vector<double>{9, 9, 9, 9});
9494

export/tests/basic_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ fmu4cpp::model_info fmu4cpp::get_model_info() {
5050
return m;
5151
}
5252

53-
std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) {
54-
return std::make_unique<Model>(instanceName, fmuResourceLocation);
55-
}
53+
FMU4CPP_INSTANTIATE(Model);
54+
5655

5756
TEST_CASE("basic_test") {
5857

0 commit comments

Comments
 (0)