From d2e91829abc8dd46fa4685d1cab89ef6fe907471 Mon Sep 17 00:00:00 2001 From: hop311 Date: Fri, 20 Oct 2023 09:16:38 +0100 Subject: Separated node->string and string->value parsing --- src/openvic-simulation/country/Country.cpp | 6 +-- src/openvic-simulation/dataloader/NodeTools.cpp | 48 ++++++++++++---------- src/openvic-simulation/dataloader/NodeTools.hpp | 2 + src/openvic-simulation/economy/Building.cpp | 6 +-- src/openvic-simulation/economy/ProductionType.cpp | 2 +- src/openvic-simulation/map/TerrainType.cpp | 2 +- src/openvic-simulation/pop/Culture.cpp | 2 +- src/openvic-simulation/pop/Pop.cpp | 4 +- .../types/IdentifierRegistry.hpp | 20 +++++---- 9 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/openvic-simulation/country/Country.cpp b/src/openvic-simulation/country/Country.cpp index eadd036..bcc0488 100644 --- a/src/openvic-simulation/country/Country.cpp +++ b/src/openvic-simulation/country/Country.cpp @@ -131,7 +131,7 @@ bool CountryManager::load_country_data_file(GameManager& game_manager, std::stri bool ret = expect_dictionary_keys_and_default( [&game_manager, &alternative_colours, &name](std::string_view key, ast::NodeCPtr value) -> bool { const GovernmentType* colour_gov_type; - bool ret = game_manager.get_politics_manager().get_government_type_manager().expect_government_type_identifier(assign_variable_callback_pointer(colour_gov_type))(key); + bool ret = game_manager.get_politics_manager().get_government_type_manager().expect_government_type_str(assign_variable_callback_pointer(colour_gov_type))(key); if (!ret) return false; @@ -161,7 +161,7 @@ bool CountryManager::load_country_data_file(GameManager& game_manager, std::stri [&game_manager, &policies](std::string_view key, ast::NodeCPtr value) -> bool { const Issue* policy; bool ret = expect_identifier_or_string( - game_manager.get_politics_manager().get_issue_manager().expect_issue_identifier( + game_manager.get_politics_manager().get_issue_manager().expect_issue_str( assign_variable_callback_pointer(policy) ) )(value); @@ -176,7 +176,7 @@ bool CountryManager::load_country_data_file(GameManager& game_manager, std::stri "name", ONE_EXACTLY, expect_identifier_or_string(assign_variable_callback(party_name)), "start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start_date)), "end_date", ONE_EXACTLY, expect_date(assign_variable_callback(end_date)), - "ideology", ONE_EXACTLY, expect_identifier_or_string(game_manager.get_politics_manager().get_ideology_manager().expect_ideology_identifier(assign_variable_callback_pointer(ideology))) + "ideology", ONE_EXACTLY, expect_identifier_or_string(game_manager.get_politics_manager().get_ideology_manager().expect_ideology_str(assign_variable_callback_pointer(ideology))) )(value); country_parties.push_back({ party_name, start_date, end_date, *ideology, std::move(policies) }); diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index 85bc572..c99a2f0 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -103,18 +103,20 @@ node_callback_t NodeTools::expect_uint64(callback_t callback) { ); } -node_callback_t NodeTools::expect_fixed_point(callback_t callback) { - return expect_identifier( - [callback](std::string_view identifier) -> bool { - bool successful = false; - const fixed_point_t val = fixed_point_t::parse(identifier.data(), identifier.length(), &successful); - if (successful) { - return callback(val); - } - Logger::error("Invalid fixed point identifier text: ", identifier); - return false; +callback_t NodeTools::expect_fixed_point_str(callback_t callback) { + return [callback](std::string_view identifier) -> bool { + bool successful = false; + const fixed_point_t val = fixed_point_t::parse(identifier.data(), identifier.length(), &successful); + if (successful) { + return callback(val); } - ); + Logger::error("Invalid fixed point identifier text: ", identifier); + return false; + }; +} + +node_callback_t NodeTools::expect_fixed_point(callback_t callback) { + return expect_identifier(expect_fixed_point_str(callback)); } node_callback_t NodeTools::expect_colour(callback_t callback) { @@ -142,18 +144,20 @@ node_callback_t NodeTools::expect_colour(callback_t callback) { }; } -node_callback_t NodeTools::expect_date(callback_t callback) { - return expect_identifier( - [callback](std::string_view identifier) -> bool { - bool successful = false; - const Date date = Date::from_string(identifier, &successful); - if (successful) { - return callback(date); - } - Logger::error("Invalid date identifier text: ", identifier); - return false; +callback_t NodeTools::expect_date_str(callback_t callback) { + return [callback](std::string_view identifier) -> bool { + bool successful = false; + const Date date = Date::from_string(identifier, &successful); + if (successful) { + return callback(date); } - ); + Logger::error("Invalid date identifier text: ", identifier); + return false; + }; +} + +node_callback_t NodeTools::expect_date(callback_t callback) { + return expect_identifier(expect_date_str(callback)); } node_callback_t NodeTools::expect_years(callback_t callback) { diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 692e2cb..66b614a 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -97,9 +97,11 @@ namespace OpenVic { }); } + callback_t expect_fixed_point_str(callback_t callback); node_callback_t expect_fixed_point(callback_t callback); node_callback_t expect_colour(callback_t callback); + callback_t expect_date_str(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); diff --git a/src/openvic-simulation/economy/Building.cpp b/src/openvic-simulation/economy/Building.cpp index d132e8e..4a99249 100644 --- a/src/openvic-simulation/economy/Building.cpp +++ b/src/openvic-simulation/economy/Building.cpp @@ -227,7 +227,7 @@ bool BuildingManager::load_buildings_file(GoodManager const& good_manager, Produ ModifierValue modifier; bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(modifier), - "type", ONE_EXACTLY, expect_identifier(expect_building_type_identifier(assign_variable_callback_pointer(type))), + "type", ONE_EXACTLY, expect_building_type_identifier(assign_variable_callback_pointer(type)), "on_completion", ZERO_OR_ONE, expect_identifier(assign_variable_callback(on_completion)), "completion_size", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(completion_size)), "max_level", ONE_EXACTLY, expect_uint(assign_variable_callback(max_level)), @@ -237,8 +237,8 @@ bool BuildingManager::load_buildings_file(GoodManager const& good_manager, Produ "visibility", ONE_EXACTLY, expect_bool(assign_variable_callback(visibility)), "onmap", ONE_EXACTLY, expect_bool(assign_variable_callback(on_map)), "default_enabled", ZERO_OR_ONE, expect_bool(assign_variable_callback(default_enabled)), - "production_type", ZERO_OR_ONE, expect_identifier(production_type_manager.expect_production_type_identifier( - assign_variable_callback_pointer(production_type))), + "production_type", ZERO_OR_ONE, production_type_manager.expect_production_type_identifier( + assign_variable_callback_pointer(production_type)), "pop_build_factory", ZERO_OR_ONE, expect_bool(assign_variable_callback(pop_build_factory)), "strategic_factory", ZERO_OR_ONE, expect_bool(assign_variable_callback(strategic_factory)), "advanced_factory", ZERO_OR_ONE, expect_bool(assign_variable_callback(advanced_factory)), diff --git a/src/openvic-simulation/economy/ProductionType.cpp b/src/openvic-simulation/economy/ProductionType.cpp index 3a27cd6..f31ea59 100644 --- a/src/openvic-simulation/economy/ProductionType.cpp +++ b/src/openvic-simulation/economy/ProductionType.cpp @@ -178,7 +178,7 @@ bool ProductionTypeManager::add_production_type(PRODUCTION_TYPE_ARGS, GoodManage "type", ZERO_OR_ONE, expect_identifier(expect_mapped_string(type_map, assign_variable_callback(type))), \ "workforce", ZERO_OR_ONE, expect_uint(assign_variable_callback(workforce)), \ "input_goods", ZERO_OR_ONE, good_manager.expect_good_decimal_map(move_variable_callback(input_goods)), \ - "output_goods", ZERO_OR_ONE, expect_identifier(good_manager.expect_good_identifier(assign_variable_callback_pointer(output_goods))), \ + "output_goods", ZERO_OR_ONE, good_manager.expect_good_identifier(assign_variable_callback_pointer(output_goods)), \ "value", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(value)), \ "efficiency", ZERO_OR_ONE, good_manager.expect_good_decimal_map(move_variable_callback(efficiency)), \ "is_coastal", ZERO_OR_ONE, expect_bool(assign_variable_callback(coastal)), \ diff --git a/src/openvic-simulation/map/TerrainType.cpp b/src/openvic-simulation/map/TerrainType.cpp index 8624cdb..db910ce 100644 --- a/src/openvic-simulation/map/TerrainType.cpp +++ b/src/openvic-simulation/map/TerrainType.cpp @@ -115,7 +115,7 @@ bool TerrainTypeManager::_load_terrain_type_mapping(std::string_view mapping_key bool has_texture = true; bool ret = expect_dictionary_keys( - "type", ONE_EXACTLY, expect_identifier(expect_terrain_type_identifier(assign_variable_callback_pointer(type))), + "type", ONE_EXACTLY, expect_terrain_type_identifier(assign_variable_callback_pointer(type)), "color", ONE_EXACTLY, expect_list_reserve_length(terrain_indicies, expect_uint( [&terrain_indicies](TerrainTypeMapping::index_t val) -> bool { if (std::find(terrain_indicies.begin(), terrain_indicies.end(), val) == terrain_indicies.end()) { diff --git a/src/openvic-simulation/pop/Culture.cpp b/src/openvic-simulation/pop/Culture.cpp index 8a0ca37..85c96a0 100644 --- a/src/openvic-simulation/pop/Culture.cpp +++ b/src/openvic-simulation/pop/Culture.cpp @@ -119,7 +119,7 @@ bool CultureManager::_load_culture_group(size_t& total_expected_cultures, bool ret = expect_dictionary_keys_and_default( increment_callback(total_expected_cultures), "leader", ONE_EXACTLY, expect_identifier(assign_variable_callback(leader)), - "unit", ZERO_OR_ONE, expect_identifier(expect_graphical_culture_type_identifier(assign_variable_callback_pointer(unit_graphical_culture_type))), + "unit", ZERO_OR_ONE, expect_graphical_culture_type_identifier(assign_variable_callback_pointer(unit_graphical_culture_type)), "union", ZERO_OR_ONE, success_callback, "is_overseas", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_overseas)) )(culture_group_node); diff --git a/src/openvic-simulation/pop/Pop.cpp b/src/openvic-simulation/pop/Pop.cpp index 5b1ce02..5286393 100644 --- a/src/openvic-simulation/pop/Pop.cpp +++ b/src/openvic-simulation/pop/Pop.cpp @@ -221,8 +221,8 @@ bool PopManager::load_pop_into_province(Province& province, std::string_view pop Religion const* religion = nullptr; Pop::pop_size_t size = 0; bool ret = expect_dictionary_keys( - "culture", ONE_EXACTLY, expect_identifier(culture_manager.expect_culture_identifier(assign_variable_callback_pointer(culture))), - "religion", ONE_EXACTLY, expect_identifier(religion_manager.expect_religion_identifier(assign_variable_callback_pointer(religion))), + "culture", ONE_EXACTLY, culture_manager.expect_culture_identifier(assign_variable_callback_pointer(culture)), + "religion", ONE_EXACTLY, religion_manager.expect_religion_identifier(assign_variable_callback_pointer(religion)), "size", ONE_EXACTLY, expect_uint(assign_variable_callback(size)), "militancy", ZERO_OR_ONE, success_callback, "rebel_type", ZERO_OR_ONE, success_callback diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index f8ece3e..70f55ff 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -193,7 +193,7 @@ namespace OpenVic { value_type _const* get_item_by_index(size_t index) _const { \ return index < items.size() ? &items[index] : nullptr; \ } \ - NodeTools::callback_t expect_item_identifier(NodeTools::callback_t callback) _const { \ + NodeTools::callback_t expect_item_str(NodeTools::callback_t callback) _const { \ return [this, callback](std::string_view identifier) -> bool { \ value_type _const* item = get_item_by_identifier(identifier); \ if (item != nullptr) return callback(*item); \ @@ -201,14 +201,12 @@ namespace OpenVic { return false; \ }; \ } \ + NodeTools::node_callback_t expect_item_identifier(NodeTools::callback_t callback) _const { \ + return NodeTools::expect_identifier(expect_item_str(callback)); \ + } \ NodeTools::node_callback_t expect_item_dictionary(NodeTools::callback_t callback) _const { \ return NodeTools::expect_dictionary([this, callback](std::string_view key, ast::NodeCPtr value) -> bool { \ - value_type _const* item = get_item_by_identifier(key); \ - if (item != nullptr) { \ - return callback(*item, value); \ - } \ - Logger::error("Invalid ", name, " identifier: ", key); \ - return false; \ + return expect_item_str(std::bind(callback, std::placeholders::_1, value))(key); \ }); \ } @@ -309,7 +307,9 @@ GETTERS return plural.get_items(); } \ std::vector get_##singular##_identifiers() const { \ return plural.get_item_identifiers(); } \ - NodeTools::callback_t expect_##singular##_identifier(NodeTools::callback_t callback) const { \ + NodeTools::callback_t expect_##singular##_str(NodeTools::callback_t callback) const { \ + return plural.expect_item_str(callback); } \ + NodeTools::node_callback_t expect_##singular##_identifier(NodeTools::callback_t callback) const { \ return plural.expect_item_identifier(callback); } \ NodeTools::node_callback_t expect_##singular##_dictionary(NodeTools::callback_t callback) const { \ return plural.expect_item_dictionary(callback); } \ @@ -319,7 +319,9 @@ GETTERS #define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(singular, plural) \ decltype(plural)::value_type* get_##singular##_by_identifier(std::string_view identifier) { \ return plural.get_item_by_identifier(identifier); } \ - NodeTools::callback_t expect_##singular##_identifier(NodeTools::callback_t callback) { \ + NodeTools::callback_t expect_##singular##_str(NodeTools::callback_t callback) { \ + return plural.expect_item_str(callback); } \ + NodeTools::node_callback_t expect_##singular##_identifier(NodeTools::callback_t callback) { \ return plural.expect_item_identifier(callback); } \ NodeTools::node_callback_t expect_##singular##_dictionary(NodeTools::callback_t callback) { \ return plural.expect_item_dictionary(callback); } -- cgit v1.2.3-56-ga3b1