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/Dataloader.hpp | 1 - src/openvic-simulation/dataloader/NodeTools.cpp | 15 +++++++- src/openvic-simulation/dataloader/NodeTools.hpp | 47 +++++++++++++++++++----- 3 files changed, 50 insertions(+), 13 deletions(-) (limited to 'src/openvic-simulation/dataloader') diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index 069ccec..de72fcd 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -2,7 +2,6 @@ #include #include -#include //keep this here or mac builds will fail #include "openvic-simulation/dataloader/NodeTools.hpp" diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index 4612f77..957aa01 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -400,9 +400,9 @@ node_callback_t NodeTools::expect_dictionary_key_map(key_map_t key_map) { ); } -node_callback_t NodeTools::name_list_callback(callback_t&&> callback) { +node_callback_t NodeTools::name_list_callback(callback_t callback) { return [callback](ast::NodeCPtr node) -> bool { - std::vector list; + name_list_t list; bool ret = expect_list_reserve_length( list, expect_identifier_or_string(vector_callback(list)) )(node); @@ -411,6 +411,17 @@ node_callback_t NodeTools::name_list_callback(callback_t void { + stream << ", " << name; + }); + } + return stream << ']'; +} + callback_t NodeTools::assign_variable_callback_string(std::string& var) { return assign_variable_callback_cast(var); } 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