|
16 | 16 |
|
17 | 17 | #include <filesystem> |
18 | 18 |
|
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); \ |
23 | 23 | } |
24 | 24 |
|
25 | 25 | namespace fmu4cpp { |
@@ -111,76 +111,86 @@ namespace fmu4cpp { |
111 | 111 | void get_integer(const unsigned int vr[], size_t nvr, int value[]) const { |
112 | 112 | for (unsigned i = 0; i < nvr; i++) { |
113 | 113 | const auto ref = vr[i]; |
114 | | - value[i] = integers_[ref].get(); |
| 114 | + const auto idx = vrToIntegerIndices_.at(ref); |
| 115 | + value[i] = integers_[idx].get(); |
115 | 116 | } |
116 | 117 | } |
117 | 118 |
|
118 | 119 | void get_real(const unsigned int vr[], size_t nvr, double value[]) const { |
119 | 120 | for (unsigned i = 0; i < nvr; i++) { |
120 | 121 | const auto ref = vr[i]; |
121 | | - value[i] = reals_[ref].get(); |
| 122 | + const auto idx = vrToRealIndices_.at(ref); |
| 123 | + value[i] = reals_[idx].get(); |
122 | 124 | } |
123 | 125 | } |
124 | 126 |
|
125 | 127 | //fmi2 |
126 | 128 | void get_boolean(const unsigned int vr[], size_t nvr, int value[]) const { |
127 | 129 | for (unsigned i = 0; i < nvr; i++) { |
128 | 130 | 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()); |
130 | 133 | } |
131 | 134 | } |
132 | 135 |
|
133 | 136 | //fmi3 |
134 | 137 | void get_boolean(const unsigned int vr[], size_t nvr, bool value[]) const { |
135 | 138 | for (unsigned i = 0; i < nvr; i++) { |
136 | 139 | const auto ref = vr[i]; |
137 | | - value[i] = booleans_[ref].get(); |
| 140 | + const auto idx = vrToBooleanIndices_.at(ref); |
| 141 | + value[i] = booleans_[idx].get(); |
138 | 142 | } |
139 | 143 | } |
140 | 144 |
|
141 | 145 | void get_string(const unsigned int vr[], size_t nvr, const char *value[]) { |
142 | 146 | stringBuffer_.clear(); |
143 | 147 | for (unsigned i = 0; i < nvr; i++) { |
144 | 148 | 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()); |
146 | 151 | value[i] = stringBuffer_.back().c_str(); |
147 | 152 | } |
148 | 153 | } |
149 | 154 |
|
150 | 155 | void set_integer(const unsigned int vr[], size_t nvr, const int value[]) { |
151 | 156 | for (unsigned i = 0; i < nvr; i++) { |
152 | 157 | const auto ref = vr[i]; |
153 | | - integers_[ref].set(value[i]); |
| 158 | + const auto idx = vrToIntegerIndices_.at(ref); |
| 159 | + integers_[idx].set(value[i]); |
154 | 160 | } |
155 | 161 | } |
156 | 162 |
|
157 | 163 | void set_real(const unsigned int vr[], size_t nvr, const double value[]) { |
158 | 164 | for (unsigned i = 0; i < nvr; i++) { |
159 | 165 | const auto ref = vr[i]; |
160 | | - reals_[ref].set(value[i]); |
| 166 | + const auto idx = vrToRealIndices_.at(ref); |
| 167 | + reals_[idx].set(value[i]); |
161 | 168 | } |
162 | 169 | } |
163 | 170 |
|
164 | 171 | //fmi2 |
165 | 172 | void set_boolean(const unsigned int vr[], size_t nvr, const int value[]) { |
166 | 173 | for (unsigned i = 0; i < nvr; i++) { |
167 | 174 | 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])); |
169 | 177 | } |
170 | 178 | } |
171 | 179 |
|
172 | 180 | //fmi3 |
173 | 181 | void set_boolean(const unsigned int vr[], size_t nvr, const bool value[]) { |
174 | 182 | for (unsigned i = 0; i < nvr; i++) { |
175 | 183 | const auto ref = vr[i]; |
176 | | - booleans_[ref].set(value[i]); |
| 184 | + const auto idx = vrToBooleanIndices_.at(ref); |
| 185 | + booleans_[idx].set(value[i]); |
177 | 186 | } |
178 | 187 | } |
179 | 188 |
|
180 | 189 | void set_string(const unsigned int vr[], size_t nvr, const char *const value[]) { |
181 | 190 | for (unsigned i = 0; i < nvr; i++) { |
182 | 191 | const auto ref = vr[i]; |
183 | | - strings_[ref].set(value[i]); |
| 192 | + const auto idx = vrToStringIndices_.at(ref); |
| 193 | + strings_[idx].set(value[i]); |
184 | 194 | } |
185 | 195 | } |
186 | 196 |
|
@@ -260,17 +270,23 @@ namespace fmu4cpp { |
260 | 270 | std::optional<double> tolerance_; |
261 | 271 |
|
262 | 272 | logger *logger_ = nullptr; |
263 | | - size_t numVariables_{1}; |
| 273 | + size_t numVariables_{0}; |
264 | 274 |
|
265 | 275 | std::string instanceName_; |
266 | 276 | std::filesystem::path resourceLocation_; |
267 | 277 |
|
268 | 278 | std::vector<IntVariable> integers_; |
| 279 | + std::unordered_map<unsigned int, size_t> vrToIntegerIndices_; |
| 280 | + |
269 | 281 | std::vector<RealVariable> reals_; |
| 282 | + std::unordered_map<unsigned int, size_t> vrToRealIndices_; |
| 283 | + |
270 | 284 | std::vector<BoolVariable> booleans_; |
271 | | - std::vector<StringVariable> strings_; |
| 285 | + std::unordered_map<unsigned int, size_t> vrToBooleanIndices_; |
272 | 286 |
|
| 287 | + std::vector<StringVariable> strings_; |
273 | 288 | std::vector<std::string> stringBuffer_; |
| 289 | + std::unordered_map<unsigned int, size_t> vrToStringIndices_; |
274 | 290 | }; |
275 | 291 |
|
276 | 292 | model_info get_model_info(); |
|
0 commit comments