aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/dataloader
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/dataloader')
-rw-r--r--src/openvic-simulation/dataloader/NodeTools.hpp18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp
index 79fb0ea..51e3e82 100644
--- a/src/openvic-simulation/dataloader/NodeTools.hpp
+++ b/src/openvic-simulation/dataloader/NodeTools.hpp
@@ -443,6 +443,22 @@ namespace OpenVic {
return assign_variable_callback_cast<T, T>(var);
}
+ /* By default this will only allow an optional to be set once. Set allow_overwrite
+ * to true to allow multiple assignments, with the last taking precedence. */
+ template<typename T>
+ Callback<T> auto assign_variable_callback_opt(
+ std::optional<T>& var, bool allow_overwrite = false
+ ) {
+ return [&var, allow_overwrite](T const& val) -> bool {
+ if (!allow_overwrite && var.has_value()) {
+ Logger::error("Cannot assign value to already-initialised optional!");
+ return false;
+ }
+ var = val;
+ return true;
+ };
+ }
+
callback_t<std::string_view> assign_variable_callback_string(std::string& var);
template<typename T>
@@ -487,7 +503,7 @@ namespace OpenVic {
) {
return [&var, allow_overwrite](T const& val) -> bool {
if (!allow_overwrite && var.has_value()) {
- Logger::error("Canoot assign pointer value to already-initialised optional!");
+ Logger::error("Cannot assign pointer value to already-initialised optional!");
return false;
}
var = &val;