diff options
author | hop311 <hop3114@gmail.com> | 2024-04-15 01:27:41 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2024-04-15 01:27:41 +0200 |
commit | c93cdaf332f82a41272627e90361f40835d9b80a (patch) | |
tree | d34511304f1d22306a616e0b1f5f4a7bd47cfaa6 /src/openvic-simulation/dataloader/NodeTools.hpp | |
parent | 109d31f147512c8d51f35f9773cd3c6bb1b8b994 (diff) |
Add std::optional assign pointer callback with overwrite controlpointer-opt
Diffstat (limited to 'src/openvic-simulation/dataloader/NodeTools.hpp')
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.hpp | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index c41c09e..b99fc48 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -443,9 +443,17 @@ namespace OpenVic { }; } + /* 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 const&> auto assign_variable_callback_pointer(std::optional<T const*>& var) { - return [&var](T const& val) -> bool { + Callback<T const&> auto assign_variable_callback_pointer_opt( + std::optional<T const*>& var, bool allow_overwrite = false + ) { + return [&var, allow_overwrite](T const& val) -> bool { + if (!allow_overwrite && var.has_value()) { + Logger::error("Canoot assign pointer value to already-initialised optional!"); + return false; + } var = &val; return true; }; |