diff options
author | Hop311 <Hop3114@gmail.com> | 2024-01-09 11:58:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-09 11:58:25 +0100 |
commit | 1d0dc5660040d03fd30168150f951ba98eaaa900 (patch) | |
tree | b0b404a1826d455767b16d3e3ae1d6bf7516e06d /src/openvic-simulation/dataloader | |
parent | 79b8b73304753fedab822e6aa859fa15673f52cc (diff) | |
parent | 83802dfead4938e6f98b4b9961b286e06ab78b18 (diff) |
Merge pull request #123 from OpenVicProject/map_callback
Added map_callback and expect_item_dictionary_reserve_length
Diffstat (limited to 'src/openvic-simulation/dataloader')
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.hpp | 1 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.cpp | 15 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.hpp | 47 |
3 files changed, 50 insertions, 13 deletions
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 <openvic-dataloader/csv/Parser.hpp> #include <openvic-dataloader/v2script/Parser.hpp> -#include <unordered_map> //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<std::vector<std::string>&&> callback) { +node_callback_t NodeTools::name_list_callback(callback_t<name_list_t&&> callback) { return [callback](ast::NodeCPtr node) -> bool { - std::vector<std::string> list; + name_list_t list; bool ret = expect_list_reserve_length( list, expect_identifier_or_string(vector_callback<std::string_view>(list)) )(node); @@ -411,6 +411,17 @@ node_callback_t NodeTools::name_list_callback(callback_t<std::vector<std::string }; } +std::ostream& OpenVic::operator<<(std::ostream& stream, name_list_t const& name_list) { + stream << '['; + if (!name_list.empty()) { + stream << name_list.front(); + std::for_each(name_list.begin() + 1, name_list.end(), [&stream](std::string const& name) -> void { + stream << ", " << name; + }); + } + return stream << ']'; +} + callback_t<std::string_view> NodeTools::assign_variable_callback_string(std::string& var) { return assign_variable_callback_cast<std::string_view>(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<std::string>; using case_insensitive_string_set_t = case_insensitive_ordered_set<std::string>; + using name_list_t = std::vector<std::string>; + std::ostream& operator<<(std::ostream& stream, name_list_t const& name_list); + namespace NodeTools { template<typename Fn, typename Return = void, typename... Args> @@ -234,21 +238,22 @@ namespace OpenVic { t.reserve(size_t {}); }; template<Reservable T> + LengthCallback auto reserve_length_callback(T& t) { + return [&t](size_t size) -> size_t { + t.reserve(size); + return size; + }; + } + template<Reservable T> 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<Reservable T> 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<std::vector<std::string>&&> callback); + node_callback_t name_list_callback(callback_t<name_list_t&&> callback); template<typename T, class Hash, class KeyEqual> Callback<std::string_view> auto expect_mapped_string( @@ -351,10 +356,32 @@ namespace OpenVic { }; } - template<typename T, typename...SetArgs> + template<typename T, typename U, typename...SetArgs> + Callback<T> auto set_callback(tsl::ordered_set<U, SetArgs...>& set) { + return [&set](T val) -> bool { + if (!set.emplace(std::move(val)).second) { + Logger::warning("Duplicate set entry: \"", val, "\""); + } + return true; + }; + } + + template<std::derived_from<HasIdentifier> T, typename...SetArgs> Callback<T const&> auto set_callback_pointer(tsl::ordered_set<T const*, SetArgs...>& set) { return [&set](T const& val) -> bool { - set.insert(&val); + if (!set.emplace(&val).second) { + Logger::warning("Duplicate set entry: \"", &val, "\""); + } + return true; + }; + } + + template<std::derived_from<HasIdentifier> Key, typename Value, typename... MapArgs> + Callback<Value> auto map_callback(tsl::ordered_map<Key const*, Value, MapArgs...>& 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; }; } |