From c93cdaf332f82a41272627e90361f40835d9b80a Mon Sep 17 00:00:00 2001 From: hop311 Date: Mon, 15 Apr 2024 00:27:41 +0100 Subject: Add std::optional assign pointer callback with overwrite control --- src/openvic-simulation/dataloader/NodeTools.hpp | 12 ++++++++++-- src/openvic-simulation/history/CountryHistory.cpp | 14 +++++++------- src/openvic-simulation/history/ProvinceHistory.cpp | 8 ++++---- 3 files changed, 21 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index c41c09e..b99fc48 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -443,9 +443,17 @@ namespace OpenVic { }; } + /* By default this will only allow an optional to be set once. Set allow_overwrite + * to true to allow multiple assignments, with the last taking precedence. */ template - Callback auto assign_variable_callback_pointer(std::optional& var) { - return [&var](T const& val) -> bool { + Callback auto assign_variable_callback_pointer_opt( + std::optional& var, bool allow_overwrite = false + ) { + return [&var, allow_overwrite](T const& val) -> bool { + if (!allow_overwrite && var.has_value()) { + Logger::error("Canoot assign pointer value to already-initialised optional!"); + return false; + } var = &val; return true; }; diff --git a/src/openvic-simulation/history/CountryHistory.cpp b/src/openvic-simulation/history/CountryHistory.cpp index 5ee5e38..1bd3dd0 100644 --- a/src/openvic-simulation/history/CountryHistory.cpp +++ b/src/openvic-simulation/history/CountryHistory.cpp @@ -68,25 +68,25 @@ bool CountryHistoryMap::_load_history_entry( ); }, "capital", ZERO_OR_ONE, - game_manager.get_map().expect_province_identifier(assign_variable_callback_pointer(entry.capital)), + game_manager.get_map().expect_province_identifier(assign_variable_callback_pointer_opt(entry.capital)), "primary_culture", ZERO_OR_ONE, - culture_manager.expect_culture_identifier(assign_variable_callback_pointer(entry.primary_culture)), + culture_manager.expect_culture_identifier(assign_variable_callback_pointer_opt(entry.primary_culture)), "culture", ZERO_OR_MORE, culture_manager.expect_culture_identifier( vector_callback_pointer(entry.accepted_cultures) ), "religion", ZERO_OR_ONE, game_manager.get_pop_manager().get_religion_manager().expect_religion_identifier( - assign_variable_callback_pointer(entry.religion) + assign_variable_callback_pointer_opt(entry.religion) ), "government", ZERO_OR_ONE, politics_manager.get_government_type_manager().expect_government_type_identifier( - assign_variable_callback_pointer(entry.government_type) + assign_variable_callback_pointer_opt(entry.government_type) ), "plurality", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(entry.plurality)), "nationalvalue", ZERO_OR_ONE, politics_manager.get_national_value_manager().expect_national_value_identifier( - assign_variable_callback_pointer(entry.national_value) + assign_variable_callback_pointer_opt(entry.national_value) ), "civilized", ZERO_OR_ONE, expect_bool(assign_variable_callback(entry.civilised)), "prestige", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(entry.prestige)), - "ruling_party", ZERO_OR_ONE, country.expect_party_identifier(assign_variable_callback_pointer(entry.ruling_party)), + "ruling_party", ZERO_OR_ONE, country.expect_party_identifier(assign_variable_callback_pointer_opt(entry.ruling_party)), "last_election", ZERO_OR_ONE, expect_date(assign_variable_callback(entry.last_election)), "upper_house", ZERO_OR_ONE, politics_manager.get_ideology_manager().expect_ideology_dictionary_reserve_length( entry.upper_house, @@ -105,7 +105,7 @@ bool CountryHistoryMap::_load_history_entry( } ), "schools", ZERO_OR_ONE, technology_manager.expect_technology_school_identifier( - assign_variable_callback_pointer(entry.tech_school) + assign_variable_callback_pointer_opt(entry.tech_school) ), "foreign_investment", ZERO_OR_ONE, country_manager.expect_country_decimal_map(move_variable_callback(entry.foreign_investment)), diff --git a/src/openvic-simulation/history/ProvinceHistory.cpp b/src/openvic-simulation/history/ProvinceHistory.cpp index b5b2210..c22567b 100644 --- a/src/openvic-simulation/history/ProvinceHistory.cpp +++ b/src/openvic-simulation/history/ProvinceHistory.cpp @@ -53,9 +53,9 @@ bool ProvinceHistoryMap::_load_history_entry( return _load_history_sub_entry_callback(game_manager, entry.get_date(), value, key, value); }, "owner", ZERO_OR_ONE, - country_manager.expect_country_identifier(assign_variable_callback_pointer(entry.owner)), + country_manager.expect_country_identifier(assign_variable_callback_pointer_opt(entry.owner, true)), "controller", ZERO_OR_ONE, - country_manager.expect_country_identifier(assign_variable_callback_pointer(entry.controller)), + country_manager.expect_country_identifier(assign_variable_callback_pointer_opt(entry.controller, true)), "add_core", ZERO_OR_MORE, country_manager.expect_country_identifier(vector_callback_pointer(entry.add_cores)), "remove_core", ZERO_OR_MORE, country_manager.expect_country_identifier( vector_callback_pointer(entry.remove_cores) @@ -65,10 +65,10 @@ bool ProvinceHistoryMap::_load_history_entry( "colony", ZERO_OR_ONE, expect_identifier(expect_mapped_string(colony_status_map, assign_variable_callback(entry.colonial))), "is_slave", ZERO_OR_ONE, expect_bool(assign_variable_callback(entry.slave)), - "trade_goods", ZERO_OR_ONE, good_manager.expect_good_identifier(assign_variable_callback_pointer(entry.rgo)), + "trade_goods", ZERO_OR_ONE, good_manager.expect_good_identifier(assign_variable_callback_pointer_opt(entry.rgo)), "life_rating", ZERO_OR_ONE, expect_uint(assign_variable_callback(entry.life_rating)), "terrain", ZERO_OR_ONE, terrain_type_manager.expect_terrain_type_identifier( - assign_variable_callback_pointer(entry.terrain_type) + assign_variable_callback_pointer_opt(entry.terrain_type) ), "party_loyalty", ZERO_OR_MORE, [&ideology_manager, &entry](ast::NodeCPtr node) -> bool { Ideology const* ideology = nullptr; -- cgit v1.2.3-56-ga3b1