aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/dataloader/NodeTools.hpp
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2024-04-15 21:02:06 +0200
committer GitHub <noreply@github.com>2024-04-15 21:02:06 +0200
commitd5d401efb4979011ce07f3dc3f3f5a0a283902fb (patch)
tree5ce0e4d838058305b10bd2b342b16d612b730a40 /src/openvic-simulation/dataloader/NodeTools.hpp
parenta5695934109559c57eae807a581cdc7f754764c1 (diff)
parentc93cdaf332f82a41272627e90361f40835d9b80a (diff)
Merge pull request #157 from OpenVicProject/pointer-opt
Add std::optional assign pointer callback with overwrite control
Diffstat (limited to 'src/openvic-simulation/dataloader/NodeTools.hpp')
-rw-r--r--src/openvic-simulation/dataloader/NodeTools.hpp12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp
index 0df057a..0bb4d5b 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;
};