From 83802dfead4938e6f98b4b9961b286e06ab78b18 Mon Sep 17 00:00:00 2001 From: hop311 Date: Mon, 8 Jan 2024 22:19:39 +0000 Subject: Added map_callback and expect_item_dictionary_reserve_length --- src/openvic-simulation/dataloader/NodeTools.hpp | 47 +++++++++++++++++++------ 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'src/openvic-simulation/dataloader/NodeTools.hpp') diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 00ba02d..b3fce16 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -11,6 +11,7 @@ #include "openvic-simulation/types/Colour.hpp" #include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/types/HasIdentifier.hpp" #include "openvic-simulation/types/OrderedContainers.hpp" #include "openvic-simulation/types/Vector.hpp" @@ -28,6 +29,9 @@ namespace OpenVic { using string_set_t = ordered_set; using case_insensitive_string_set_t = case_insensitive_ordered_set; + using name_list_t = std::vector; + std::ostream& operator<<(std::ostream& stream, name_list_t const& name_list); + namespace NodeTools { template @@ -234,21 +238,22 @@ namespace OpenVic { t.reserve(size_t {}); }; template + LengthCallback auto reserve_length_callback(T& t) { + return [&t](size_t size) -> size_t { + t.reserve(size); + return size; + }; + } + template NodeCallback auto expect_list_reserve_length(T& t, NodeCallback auto callback) { - return expect_list_and_length( - [&t](size_t size) -> size_t { - t.reserve(t.size() + size); - return size; - }, - callback - ); + return expect_list_and_length(reserve_length_callback(t), callback); } template NodeCallback auto expect_dictionary_reserve_length(T& t, KeyValueCallback auto callback) { return expect_list_reserve_length(t, expect_assign(callback)); } - node_callback_t name_list_callback(callback_t&&> callback); + node_callback_t name_list_callback(callback_t callback); template Callback auto expect_mapped_string( @@ -351,10 +356,32 @@ namespace OpenVic { }; } - template + template + Callback auto set_callback(tsl::ordered_set& set) { + return [&set](T val) -> bool { + if (!set.emplace(std::move(val)).second) { + Logger::warning("Duplicate set entry: \"", val, "\""); + } + return true; + }; + } + + template T, typename...SetArgs> Callback auto set_callback_pointer(tsl::ordered_set& set) { return [&set](T const& val) -> bool { - set.insert(&val); + if (!set.emplace(&val).second) { + Logger::warning("Duplicate set entry: \"", &val, "\""); + } + return true; + }; + } + + template Key, typename Value, typename... MapArgs> + Callback auto map_callback(tsl::ordered_map& map, Key const* key) { + return [&map, key](Value value) -> bool { + if (!map.emplace(key, std::move(value)).second) { + Logger::warning("Duplicate map entry with key: \"", key, "\""); + } return true; }; } -- cgit v1.2.3-56-ga3b1