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.cpp | 136 +++++++++++++++++------- 1 file changed, 95 insertions(+), 41 deletions(-) (limited to 'src/openvic-simulation/dataloader/NodeTools.cpp') diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index 5ef09d1..391ffb6 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -19,20 +19,28 @@ static node_callback_t _expect_type(callback_t callback) { }; } -template -requires(std::derived_from) -static callback_t _abstract_string_node_callback(callback_t callback) { - return [callback](T const& node) -> bool { - return callback(node._name); +template T> +static callback_t _abstract_string_node_callback(callback_t callback, bool allow_empty) { + return [callback, allow_empty](T const& node) -> bool { + if (allow_empty) { + return callback(node._name); + } else { + if (!node._name.empty()) { + return callback(node._name); + } else { + Logger::error("Invalid string value - empty!"); + return false; + } + } }; } node_callback_t NodeTools::expect_identifier(callback_t callback) { - return _expect_type(_abstract_string_node_callback(callback)); + return _expect_type(_abstract_string_node_callback(callback, false)); } -node_callback_t NodeTools::expect_string(callback_t callback) { - return _expect_type(_abstract_string_node_callback(callback)); +node_callback_t NodeTools::expect_string(callback_t callback, bool allow_empty) { + return _expect_type(_abstract_string_node_callback(callback, allow_empty)); } node_callback_t NodeTools::expect_identifier_or_string(callback_t callback) { @@ -43,7 +51,7 @@ node_callback_t NodeTools::expect_identifier_or_string(callback_tcast_to(); } if (cast_node != nullptr) { - return _abstract_string_node_callback(callback)(*cast_node); + return _abstract_string_node_callback(callback, false)(*cast_node); } Logger::error("Invalid node type ", node->get_type(), " when expecting ", ast::IdentifierNode::get_type_static(), " or ", ast::StringNode::get_type_static()); } else { @@ -54,20 +62,20 @@ node_callback_t NodeTools::expect_identifier_or_string(callback_t callback) { - return expect_identifier( - [callback](std::string_view identifier) -> bool { - if (identifier == "yes") { - return callback(true); - } else if (identifier == "no") { - return callback(false); - } - Logger::error("Invalid bool identifier text: ", identifier); - return false; - } - ); + static const string_map_t bool_map = { + { "yes", true }, { "no", false } + }; + return expect_identifier(expect_mapped_string(bool_map, callback)); +} + +node_callback_t NodeTools::expect_int_bool(callback_t callback) { + static const string_map_t bool_map = { + { "1", true }, { "0", false } + }; + return expect_identifier(expect_mapped_string(bool_map, callback)); } -node_callback_t NodeTools::expect_int(callback_t callback) { +node_callback_t NodeTools::expect_int64(callback_t callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { bool successful = false; @@ -81,7 +89,7 @@ node_callback_t NodeTools::expect_int(callback_t callback) { ); } -node_callback_t NodeTools::expect_uint(callback_t callback) { +node_callback_t NodeTools::expect_uint64(callback_t callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { bool successful = false; @@ -134,10 +142,6 @@ node_callback_t NodeTools::expect_colour(callback_t callback) { }; } -node_callback_t NodeTools::expect_timespan(callback_t callback) { - return expect_int(callback); -} - node_callback_t NodeTools::expect_date(callback_t callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { @@ -152,6 +156,24 @@ node_callback_t NodeTools::expect_date(callback_t callback) { ); } +node_callback_t NodeTools::expect_years(callback_t callback) { + return expect_uint([callback](Timespan::day_t val) -> bool { + return callback(Timespan::fromYears(val)); + }); +} + +node_callback_t NodeTools::expect_months(callback_t callback) { + return expect_uint([callback](Timespan::day_t val) -> bool { + return callback(Timespan::fromMonths(val)); + }); +} + +node_callback_t NodeTools::expect_days(callback_t callback) { + return expect_uint([callback](Timespan::day_t val) -> bool { + return callback(Timespan::fromDays(val)); + }); +} + template)> node_callback_t _expect_vec2(callback_t> callback) { return [callback](ast::NodeCPtr node) -> bool { @@ -166,7 +188,7 @@ node_callback_t _expect_vec2(callback_t> callback) { } node_callback_t NodeTools::expect_ivec2(callback_t callback) { - return _expect_vec2(callback); + return _expect_vec2(callback); } node_callback_t NodeTools::expect_fvec2(callback_t callback) { @@ -238,17 +260,24 @@ node_callback_t NodeTools::expect_length(callback_t callback) { }; } -node_callback_t NodeTools::expect_key(std::string_view key, node_callback_t callback) { +node_callback_t NodeTools::expect_key(std::string_view key, node_callback_t callback, bool* key_found) { return _expect_type( - [key, callback](ast::AbstractListNode const& list_node) -> bool { + [key, callback, key_found](ast::AbstractListNode const& list_node) -> bool { std::vector const& list = list_node._statements; for (ast::NodeUPtr const& sub_node : list_node._statements) { ast::AssignNode const* assign_node = sub_node->cast_to(); if (assign_node != nullptr && assign_node->_name == key) { + if (key_found != nullptr) { + *key_found = true; + } return callback(&*assign_node->_initializer); } } - Logger::error("Failed to find expected key: ", key); + if (key_found != nullptr) { + *key_found = false; + } else { + Logger::error("Failed to find expected key: ", key); + } return false; } ); @@ -262,21 +291,30 @@ node_callback_t NodeTools::expect_dictionary(key_value_callback_t callback) { return expect_dictionary_and_length(default_length_callback, callback); } -void NodeTools::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 NodeTools::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) { if (key_map.find(key) == key_map.end()) { key_map.emplace(key, dictionary_entry_t { expected_count, callback }); - } else { - Logger::error("Duplicate expected dictionary key: ", key); + return true; + } + Logger::error("Duplicate expected dictionary key: ", key); + return false; +} + +bool NodeTools::remove_key_map_entry(key_map_t& key_map, std::string_view key) { + const key_map_t::const_iterator it = key_map.find(key); + if (it != key_map.end()) { + key_map.erase(it); + return true; } + Logger::error("Failed to find dictionary key to remove: ", key); + return false; } -key_value_callback_t NodeTools::dictionary_keys_callback(key_map_t& key_map, bool allow_other_keys) { - return [&key_map, allow_other_keys](std::string_view key, ast::NodeCPtr value) -> bool { +key_value_callback_t NodeTools::dictionary_keys_callback(key_map_t& key_map, key_value_callback_t default_callback) { + return [&key_map, default_callback](std::string_view key, ast::NodeCPtr value) -> bool { const key_map_t::iterator it = key_map.find(key); if (it == key_map.end()) { - if (allow_other_keys) return true; - Logger::error("Invalid dictionary key: ", key); - return false; + return default_callback(key, value); } dictionary_entry_t& entry = it->second; if (++entry.count > 1 && !entry.can_repeat()) { @@ -300,16 +338,28 @@ bool NodeTools::check_key_map_counts(key_map_t& key_map) { return ret; } -node_callback_t NodeTools::_expect_dictionary_keys_and_length(length_callback_t length_callback, bool allow_other_keys, key_map_t&& key_map) { - return [length_callback, allow_other_keys, key_map = std::move(key_map)](ast::NodeCPtr node) mutable -> bool { +node_callback_t NodeTools::expect_dictionary_key_map_and_length_and_default(key_map_t key_map, length_callback_t length_callback, key_value_callback_t default_callback) { + return [length_callback, default_callback, key_map = std::move(key_map)](ast::NodeCPtr node) mutable -> bool { bool ret = expect_dictionary_and_length( - length_callback, dictionary_keys_callback(key_map, allow_other_keys) + length_callback, dictionary_keys_callback(key_map, default_callback) )(node); ret &= check_key_map_counts(key_map); return ret; }; } +node_callback_t NodeTools::expect_dictionary_key_map_and_length(key_map_t key_map, length_callback_t length_callback) { + return expect_dictionary_key_map_and_length_and_default(std::move(key_map), length_callback, key_value_invalid_callback); +} + +node_callback_t NodeTools::expect_dictionary_key_map_and_default(key_map_t key_map, key_value_callback_t default_callback) { + return expect_dictionary_key_map_and_length_and_default(std::move(key_map), default_length_callback, default_callback); +} + +node_callback_t NodeTools::expect_dictionary_key_map(key_map_t key_map) { + return expect_dictionary_key_map_and_length_and_default(std::move(key_map), default_length_callback, key_value_invalid_callback); +} + node_callback_t NodeTools::name_list_callback(std::vector& list) { return expect_list_reserve_length( list, @@ -325,3 +375,7 @@ node_callback_t NodeTools::name_list_callback(std::vector& list) { ) ); } + +callback_t NodeTools::assign_variable_callback_string(std::string& var) { + return assign_variable_callback_cast(var); +} -- cgit v1.2.3-56-ga3b1