std::variant
, I tought that I could make an std::unordered_map<fmi2ValueReference, std::variant>
with all the possibile variable types, but I'm not entirely sure this is the correct approach (I still have to learn a lot about C++ though I've been studying it for 1 year now)
fmi2ValueReference
could represent each type in the variant (eg. valueref of 1 is valid for both int/real/boolean/string)
void FMU::set_inputs(const std::unordered_map<fmi2ValueReference, std::any> &inputs) {
std::unique_lock<std::mutex> lock(input_mutex);
for (const auto &i : inputs) {
if (i.second.type() == typeid(fmi2String)) {
if (!fmu_instance->write_string(i.first, std::any_cast<fmi2String>(i.second))) {
std::cerr << "Error! step() returned with status: " << to_string(fmu_instance->last_status()) << std::endl;
break;
}
} else if (i.second.type() == typeid(fmi2Boolean)) {
if (!fmu_instance->write_boolean(i.first, std::any_cast<fmi2Boolean>(i.second))) {
std::cerr << "Error! step() returned with status: " << to_string(fmu_instance->last_status()) << std::endl;
break;
}
} else if (i.second.type() == typeid(fmi2Integer)) {
if (!fmu_instance->write_integer(i.first, std::any_cast<fmi2Integer>(i.second))) {
std::cerr << "Error! step() returned with status: " << to_string(fmu_instance->last_status()) << std::endl;
break;
}
} else if (i.second.type() == typeid(fmi2Real)) {
if (!fmu_instance->write_real(i.first, std::any_cast<fmi2Real>(i.second))) {
std::cerr << "Error! step() returned with status: " << to_string(fmu_instance->last_status()) << std::endl;
break;
}
}
}
}
std::any
which seems to fit
write_real
/write_boolean
and so on for each one of the ports