From e50c67eb1aaa54f5fb31425f81616bea4e6b880a Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 12 Oct 2023 20:19:00 +0100 Subject: Lots of accumulated changes --- src/openvic-simulation/dataloader/NodeTools.hpp | 151 +++++++++++++++--------- 1 file changed, 94 insertions(+), 57 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 5ba9d63..44ac271 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -11,6 +11,11 @@ namespace OpenVic { namespace ast = ovdl::v2script::ast; + /* Template for map from strings to Ts, in which string_views can be + * searched for without needing to be copied into a string, */ + template + using string_map_t = std::map>; + namespace NodeTools { template @@ -21,17 +26,55 @@ namespace OpenVic { using key_value_callback_t = callback_t; constexpr bool key_value_success_callback(std::string_view, ast::NodeCPtr) { return true; } + inline bool key_value_invalid_callback(std::string_view key, ast::NodeCPtr) { + Logger::error("Invalid dictionary key: ", key); + return false; + } node_callback_t expect_identifier(callback_t callback); - node_callback_t expect_string(callback_t callback); + node_callback_t expect_string(callback_t callback, bool allow_empty = true); node_callback_t expect_identifier_or_string(callback_t callback); + node_callback_t expect_bool(callback_t callback); - node_callback_t expect_int(callback_t callback); - node_callback_t expect_uint(callback_t callback); + node_callback_t expect_int_bool(callback_t callback); + + node_callback_t expect_int64(callback_t callback); + node_callback_t expect_uint64(callback_t callback); + + template + node_callback_t expect_int(callback_t callback) { + return expect_int64([callback](int64_t val) -> bool { + if (static_cast(std::numeric_limits::lowest()) <= val && + val <= static_cast(std::numeric_limits::max())) { + return callback(val); + } + Logger::error("Invalid int: ", val, " (valid range: [", + static_cast(std::numeric_limits::lowest()), ", ", + static_cast(std::numeric_limits::max()), "])"); + return false; + }); + } + + template + node_callback_t expect_uint(callback_t callback) { + return expect_uint64([callback](uint64_t val) -> bool { + if (val <= static_cast(std::numeric_limits::max())) { + return callback(val); + } + Logger::error("Invalid uint: ", val, " (valid range: [0, ", + static_cast(std::numeric_limits::max()), "])"); + return false; + }); + } + node_callback_t expect_fixed_point(callback_t callback); node_callback_t expect_colour(callback_t callback); - node_callback_t expect_timespan(callback_t callback); + node_callback_t expect_date(callback_t callback); + node_callback_t expect_years(callback_t callback); + node_callback_t expect_months(callback_t callback); + node_callback_t expect_days(callback_t callback); + node_callback_t expect_ivec2(callback_t callback); node_callback_t expect_fvec2(callback_t callback); node_callback_t expect_assign(key_value_callback_t callback); @@ -44,7 +87,7 @@ namespace OpenVic { node_callback_t expect_list(node_callback_t callback); node_callback_t expect_length(callback_t callback); - node_callback_t expect_key(std::string_view key, node_callback_t callback); + node_callback_t expect_key(std::string_view key, node_callback_t callback, bool* key_found = nullptr); node_callback_t expect_dictionary_and_length(length_callback_t length_callback, key_value_callback_t callback); node_callback_t expect_dictionary(key_value_callback_t callback); @@ -73,41 +116,45 @@ namespace OpenVic { } }; using enum dictionary_entry_t::expected_count_t; - using key_map_t = std::map>; + using key_map_t = string_map_t; - void add_key_map_entry(key_map_t& key_map, std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback); - key_value_callback_t dictionary_keys_callback(key_map_t& key_map, bool allow_other_keys); + bool add_key_map_entry(key_map_t& key_map, std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback); + bool remove_key_map_entry(key_map_t& key_map, std::string_view key); + key_value_callback_t dictionary_keys_callback(key_map_t& key_map, key_value_callback_t default_callback); bool check_key_map_counts(key_map_t& key_map); - constexpr struct allow_other_keys_t {} ALLOW_OTHER_KEYS; - - node_callback_t _expect_dictionary_keys_and_length(length_callback_t length_callback, bool allow_other_keys, key_map_t&& key_map); + node_callback_t expect_dictionary_key_map_and_length_and_default(key_map_t key_map, length_callback_t length_callback, key_value_callback_t default_callback); + node_callback_t expect_dictionary_key_map_and_length(key_map_t key_map, length_callback_t length_callback); + node_callback_t expect_dictionary_key_map_and_default(key_map_t key_map, key_value_callback_t default_callback); + node_callback_t expect_dictionary_key_map(key_map_t key_map); template - node_callback_t _expect_dictionary_keys_and_length(length_callback_t length_callback, - bool allow_other_keys, key_map_t&& key_map, + node_callback_t expect_dictionary_key_map_and_length_and_default(key_map_t key_map, length_callback_t length_callback, key_value_callback_t default_callback, std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback, Args... args) { + // TODO - pass return value back up (part of big key_map_t rewrite?) add_key_map_entry(key_map, key, expected_count, callback); - return _expect_dictionary_keys_and_length(length_callback, allow_other_keys, std::move(key_map), args...); + return expect_dictionary_key_map_and_length_and_default(std::move(key_map), length_callback, default_callback, args...); } template - node_callback_t expect_dictionary_keys_and_length(length_callback_t length_callback, - std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback, - Args... args) { - return _expect_dictionary_keys_and_length(length_callback, false, {}, key, expected_count, callback, args...); + node_callback_t expect_dictionary_keys_and_length_and_default(length_callback_t length_callback, key_value_callback_t default_callback, Args... args) { + return expect_dictionary_key_map_and_length_and_default({}, length_callback, default_callback, args...); } template - node_callback_t expect_dictionary_keys_and_length(length_callback_t length_callback, - allow_other_keys_t, Args... args) { - return _expect_dictionary_keys_and_length(length_callback, true, {}, args...); + node_callback_t expect_dictionary_keys_and_length(length_callback_t length_callback, Args... args) { + return expect_dictionary_key_map_and_length_and_default({}, length_callback, key_value_invalid_callback, args...); + } + + template + node_callback_t expect_dictionary_keys_and_default(key_value_callback_t default_callback, Args... args) { + return expect_dictionary_key_map_and_length_and_default({}, default_length_callback, default_callback, args...); } template node_callback_t expect_dictionary_keys(Args... args) { - return expect_dictionary_keys_and_length(default_length_callback, args...); + return expect_dictionary_key_map_and_length_and_default({}, default_length_callback, key_value_invalid_callback, args...); } template @@ -133,13 +180,32 @@ namespace OpenVic { node_callback_t name_list_callback(std::vector& list); template - callback_t assign_variable_callback(T& var) { + callback_t expect_mapped_string(string_map_t const& map, callback_t callback) { + return [&map, callback](std::string_view string) -> bool { + const typename string_map_t::const_iterator it = map.find(string); + if (it != map.end()) { + return callback(it->second); + } + Logger::error("String not found in map: ", string); + return false; + }; + } + + template + callback_t assign_variable_callback_cast(U& var) { return [&var](T val) -> bool { var = val; return true; }; } + template + callback_t assign_variable_callback(T& var) { + return assign_variable_callback_cast(var); + } + + callback_t assign_variable_callback_string(std::string& var); + template callback_t move_variable_callback(T& var) { return [&var](T&& val) -> bool { @@ -161,41 +227,12 @@ namespace OpenVic { template requires requires(T& t) { - t--; - } - node_callback_t decrement_callback(T& var, node_callback_t callback) { - return [&var, callback](ast::NodeCPtr node) -> bool { - var--; - return callback(node); - }; - } - - template - requires(std::integral) - callback_t assign_variable_callback_uint(T& var) { - return [&var](uint64_t val) -> bool { - if (val <= static_cast(std::numeric_limits::max())) { - var = val; - return true; - } - Logger::error("Invalid uint: ", val, " (valid range: [0, ", - static_cast(std::numeric_limits::max()), "])"); - return false; - }; + t++; } - - template - requires(std::signed_integral) - callback_t assign_variable_callback_int(T& var) { - return [&var](int64_t val) -> bool { - if (static_cast(std::numeric_limits::lowest()) <= val && val <= static_cast(std::numeric_limits::max())) { - var = val; - return true; - } - Logger::error("Invalid int: ", val, " (valid range: [", - static_cast(std::numeric_limits::lowest()), ", ", - static_cast(std::numeric_limits::max()), "])"); - return false; + key_value_callback_t increment_callback(T& var) { + return [&var](std::string_view, ast::NodeCPtr) -> bool { + var++; + return true; }; } -- cgit v1.2.3-56-ga3b1