From 5549753a6ae909b1a65f52a1cc67ef2b5efae55a Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sun, 17 Sep 2023 15:08:11 +0100 Subject: Futher scaffolding for province positions loading --- src/openvic-simulation/dataloader/NodeTools.cpp | 31 +++++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 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 63a97ad..7dd6e16 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -19,20 +19,20 @@ static node_callback_t _expect_type(callback_t callback) { }; } -template +template requires(std::derived_from) -static callback_t abstract_string_node_callback(callback_t callback) { +static callback_t _abstract_string_node_callback(callback_t callback) { return [callback](T const& node) -> bool { return callback(node._name); }; } 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)); } node_callback_t NodeTools::expect_string(callback_t callback) { - return _expect_type(abstract_string_node_callback(callback)); + return _expect_type(_abstract_string_node_callback(callback)); } node_callback_t NodeTools::expect_identifier_or_string(callback_t callback) { @@ -43,7 +43,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)(*cast_node); } Logger::error("Invalid node type ", node->get_type(), " when expecting ", ast::IdentifierNode::get_type_static(), " or ", ast::StringNode::get_type_static()); } else { @@ -148,6 +148,27 @@ node_callback_t NodeTools::expect_date(callback_t callback) { ); } +template)> +node_callback_t _expect_vec2(callback_t> callback) { + return [callback](ast::NodeCPtr node) -> bool { + vec2_t vec; + bool ret = expect_dictionary_keys( + "x", ONE_EXACTLY, expect_func(assign_variable_callback(vec.x)), + "y", ONE_EXACTLY, expect_func(assign_variable_callback(vec.y)) + )(node); + ret &= callback(vec); + return ret; + }; +} + +node_callback_t NodeTools::expect_ivec2(callback_t callback) { + return _expect_vec2(callback); +} + +node_callback_t NodeTools::expect_fvec2(callback_t callback) { + return _expect_vec2(callback); +} + node_callback_t NodeTools::expect_assign(key_value_callback_t callback) { return _expect_type( [callback](ast::AssignNode const& assign_node) -> bool { -- cgit v1.2.3-56-ga3b1