Skip to content

Commit 13ce4da

Browse files
committed
add missing get_binary
1 parent 80bf1b1 commit 13ce4da

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

export/include/fmu4cpp/fmu_base.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ namespace fmu4cpp {
131131
void get_boolean(const unsigned int vr[], size_t nvr, bool value[]) const;
132132

133133
void get_string(const unsigned int vr[], size_t nvr, const char *value[]);
134+
void get_binary(const unsigned int vr[], size_t nvr, size_t valueSizes[], const uint8_t *values[]);
135+
134136
void set_integer(const unsigned int vr[], size_t nvr, const int value[]);
135137
void set_real(const unsigned int vr[], size_t nvr, const double value[]);
136138

@@ -224,11 +226,11 @@ namespace fmu4cpp {
224226
std::unordered_map<unsigned int, size_t> vrToBooleanIndices_;
225227

226228
std::vector<StringVariable> strings_;
227-
std::vector<std::string> stringBuffer_;
229+
std::string stringBuffer_;
228230
std::unordered_map<unsigned int, size_t> vrToStringIndices_;
229231

230232
std::vector<BinaryVariable> binary_;
231-
std::vector<std::string> binaryBuffer_;
233+
std::vector<uint8_t> binaryBuffer_;
232234
std::unordered_map<unsigned int, size_t> vrToBinaryIndices_;
233235

234236
std::function<void *(void *)> get_state_ptr_{nullptr};

export/src/fmu4cpp/fmi3/fmi3.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,26 @@ fmi3Status fmi3SetString(
517517
}
518518

519519
fmi3Status fmi3GetBinary(fmi3Instance c,
520-
const fmi3ValueReference valueReferences[],
520+
const fmi3ValueReference vr[],
521521
size_t nvr,
522522
size_t valueSizes[],
523523
fmi3Binary values[],
524524
size_t nValues) {
525525

526526
const auto component = static_cast<Fmi3Component *>(c);
527527

528-
component->logger->log(fmiError, "Unsupported function fmi3GetBinary");
529-
return fmi3Error;
528+
try {
529+
component->slave->get_binary(vr, nvr, valueSizes, values);
530+
return fmi3OK;
531+
} catch (const fmu4cpp::fatal_error &ex) {
532+
component->logger->log(fmiFatal, ex.what());
533+
component->state = Fmi3Component::State::Invalid;
534+
return fmi3Fatal;
535+
} catch (const std::exception &ex) {
536+
component->logger->log(fmiError, ex.what());
537+
component->state = Fmi3Component::State::Terminated;
538+
return fmi3Error;
539+
}
530540
}
531541

532542
fmi3Status fmi3SetBinary(fmi3Instance c,

export/src/fmu4cpp/fmu_base.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,43 +106,60 @@ void fmu_base::get_integer(const unsigned int vr[], size_t nvr, int value[]) con
106106
value[i] = integers_[idx].get();
107107
}
108108
}
109+
109110
void fmu_base::get_real(const unsigned int vr[], size_t nvr, double value[]) const {
110111
for (unsigned i = 0; i < nvr; i++) {
111112
const auto ref = vr[i];
112113
const auto idx = vrToRealIndices_.at(ref);
113114
value[i] = reals_[idx].get();
114115
}
115116
}
117+
116118
void fmu_base::get_boolean(const unsigned int vr[], size_t nvr, int value[]) const {
117119
for (unsigned i = 0; i < nvr; i++) {
118120
const auto ref = vr[i];
119121
const auto idx = vrToBooleanIndices_.at(ref);
120122
value[i] = static_cast<int>(booleans_[idx].get());
121123
}
122124
}
125+
123126
void fmu_base::get_boolean(const unsigned int vr[], size_t nvr, bool value[]) const {
124127
for (unsigned i = 0; i < nvr; i++) {
125128
const auto ref = vr[i];
126129
const auto idx = vrToBooleanIndices_.at(ref);
127130
value[i] = booleans_[idx].get();
128131
}
129132
}
133+
130134
void fmu_base::get_string(const unsigned int vr[], size_t nvr, const char *value[]) {
131-
stringBuffer_.clear();
132135
for (unsigned i = 0; i < nvr; i++) {
133136
const auto ref = vr[i];
134137
const auto idx = vrToStringIndices_.at(ref);
135-
stringBuffer_.push_back(strings_[idx].get());
136-
value[i] = stringBuffer_.back().c_str();
138+
stringBuffer_ = strings_[idx].get();
139+
value[i] = stringBuffer_.c_str();
140+
}
141+
}
142+
143+
void fmu_base::get_binary(const unsigned int vr[], size_t nvr, size_t valueSizes[], const uint8_t *values[]) {
144+
145+
for (auto i = 0; i < nvr; i++) {
146+
const auto ref = vr[i];
147+
const auto idx = vrToBinaryIndices_.at(ref);
148+
const auto &data = binary_[idx].get();
149+
valueSizes[i] = data.size();
150+
binaryBuffer_.assign(data.begin(), data.end());
151+
values[i] = binaryBuffer_.data();
137152
}
138153
}
154+
139155
void fmu_base::set_integer(const unsigned int vr[], size_t nvr, const int value[]) {
140156
for (unsigned i = 0; i < nvr; i++) {
141157
const auto ref = vr[i];
142158
const auto idx = vrToIntegerIndices_.at(ref);
143159
integers_[idx].set(value[i]);
144160
}
145161
}
162+
146163
void fmu_base::set_real(const unsigned int vr[], size_t nvr, const double value[]) {
147164
for (unsigned i = 0; i < nvr; i++) {
148165
const auto ref = vr[i];
@@ -164,17 +181,16 @@ void fmu_base::set_boolean(const unsigned int vr[], size_t nvr, const bool value
164181
booleans_[idx].set(value[i]);
165182
}
166183
}
184+
167185
void fmu_base::set_string(const unsigned int vr[], size_t nvr, const char *const value[]) {
168186
for (unsigned i = 0; i < nvr; i++) {
169187
const auto ref = vr[i];
170188
const auto idx = vrToStringIndices_.at(ref);
171189
strings_[idx].set(value[i]);
172190
}
173191
}
192+
174193
void fmu_base::set_binary(const unsigned int vr[], size_t nvr, const size_t valueSizes[], const uint8_t *const value[]) {
175-
#ifdef FMI2
176-
static_assert("set_binary not available for FMI2");
177-
#endif
178194

179195
for (unsigned i = 0; i < nvr; i++) {
180196
const auto ref = vr[i];

0 commit comments

Comments
 (0)