From 2f28a75735a62d400cb7d2e2019a2dcfe843f696 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Tue, 30 Dec 2025 14:33:52 -0500 Subject: [PATCH] Refactor technology_unlock_level_t to be type-safe --- .../country/CountryInstance.cpp | 50 +++++++++---------- .../country/CountryInstance.hpp | 6 +-- .../history/CountryHistory.cpp | 2 +- .../history/CountryHistory.hpp | 2 +- .../research/TechnologyUnlockLevel.hpp | 29 +++++++++++ .../types/TechnologyUnlockLevel.hpp | 7 --- 6 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 src/openvic-simulation/research/TechnologyUnlockLevel.hpp delete mode 100644 src/openvic-simulation/types/TechnologyUnlockLevel.hpp diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index 27f3434e9..23cbb7a19 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -784,8 +784,8 @@ bool CountryInstance::modify_unit_type_unlock(UnitTypeBranched const& un if (unlock_level + unlock_level_change < 0) { spdlog::error_s( "Attempted to change unlock level for unit type {} in country {} to invalid value: current level = {}, change = {}, invalid new value = {}", - unit_type, *this, static_cast(unlock_level), - static_cast(unlock_level_change), static_cast(unlock_level + unlock_level_change) + unit_type, *this, unlock_level, + unlock_level_change, unlock_level + unlock_level_change ); return false; } @@ -844,7 +844,7 @@ bool CountryInstance::modify_unit_type_unlock(UnitType const& unit_type, technol } bool CountryInstance::unlock_unit_type(UnitType const& unit_type) { - return modify_unit_type_unlock(unit_type, 1); + return modify_unit_type_unlock(unit_type, technology_unlock_level_t { 1 }); } bool CountryInstance::is_unit_type_unlocked(UnitType const& unit_type) const { @@ -873,8 +873,8 @@ bool CountryInstance::modify_building_type_unlock( if (unlock_level + unlock_level_change < 0) { spdlog::error_s( "Attempted to change unlock level for building type {} in country {} to invalid value: current level = {}, change = {}, invalid new value = {}", - building_type, *this, static_cast(unlock_level), static_cast(unlock_level_change), - static_cast(unlock_level + unlock_level_change) + building_type, *this, unlock_level, unlock_level_change, + unlock_level + unlock_level_change ); return false; } @@ -889,7 +889,7 @@ bool CountryInstance::modify_building_type_unlock( } bool CountryInstance::unlock_building_type(BuildingType const& building_type) { - return modify_building_type_unlock(building_type, 1); + return modify_building_type_unlock(building_type, technology_unlock_level_t { 1 }); } bool CountryInstance::is_building_type_unlocked(BuildingType const& building_type) const { @@ -903,8 +903,8 @@ bool CountryInstance::modify_crime_unlock(Crime const& crime, technology_unlock_ if (unlock_level + unlock_level_change < 0) { spdlog::error_s( "Attempted to change unlock level for crime {} in country {} to invalid value: current level = {}, change = {}, invalid new value = {}", - crime, *this, static_cast(unlock_level), - static_cast(unlock_level_change), static_cast(unlock_level + unlock_level_change) + crime, *this, unlock_level, + unlock_level_change, unlock_level + unlock_level_change ); return false; } @@ -915,7 +915,7 @@ bool CountryInstance::modify_crime_unlock(Crime const& crime, technology_unlock_ } bool CountryInstance::unlock_crime(Crime const& crime) { - return modify_crime_unlock(crime, 1); + return modify_crime_unlock(crime, technology_unlock_level_t { 1 }); } bool CountryInstance::is_crime_unlocked(Crime const& crime) const { @@ -927,8 +927,8 @@ bool CountryInstance::modify_gas_attack_unlock(technology_unlock_level_t unlock_ if (gas_attack_unlock_level + unlock_level_change < 0) { spdlog::error_s( "Attempted to change unlock level for gas attack in country {} to invalid value: current level = {}, change = {}, invalid new value = {}", - *this, static_cast(gas_attack_unlock_level), - static_cast(unlock_level_change), static_cast(gas_attack_unlock_level + unlock_level_change) + *this, gas_attack_unlock_level, + unlock_level_change, gas_attack_unlock_level + unlock_level_change ); return false; } @@ -939,7 +939,7 @@ bool CountryInstance::modify_gas_attack_unlock(technology_unlock_level_t unlock_ } bool CountryInstance::unlock_gas_attack() { - return modify_gas_attack_unlock(1); + return modify_gas_attack_unlock(technology_unlock_level_t { 1 }); } bool CountryInstance::is_gas_attack_unlocked() const { @@ -951,8 +951,8 @@ bool CountryInstance::modify_gas_defence_unlock(technology_unlock_level_t unlock if (gas_defence_unlock_level + unlock_level_change < 0) { spdlog::error_s( "Attempted to change unlock level for gas defence in country {} to invalid value: current level = {}, change = {}, invalid new value = {}", - *this, static_cast(gas_defence_unlock_level), - static_cast(unlock_level_change), static_cast(gas_defence_unlock_level + unlock_level_change) + *this, gas_defence_unlock_level, + unlock_level_change, gas_defence_unlock_level + unlock_level_change ); return false; } @@ -963,7 +963,7 @@ bool CountryInstance::modify_gas_defence_unlock(technology_unlock_level_t unlock } bool CountryInstance::unlock_gas_defence() { - return modify_gas_defence_unlock(1); + return modify_gas_defence_unlock(technology_unlock_level_t { 1 }); } bool CountryInstance::is_gas_defence_unlocked() const { @@ -988,8 +988,8 @@ bool CountryInstance::modify_unit_variant_unlock(unit_variant_t unit_variant, te if (unlock_level + unlock_level_change < 0) { spdlog::error_s( "Attempted to change unlock level for unit variant {} in country {} to invalid value: current level = {}, change = {}, invalid new value = {}", - static_cast(unit_variant), *this, static_cast(unlock_level), - static_cast(unlock_level_change), static_cast(unlock_level + unlock_level_change) + static_cast(unit_variant), *this, unlock_level, + unlock_level_change, unlock_level + unlock_level_change ); ret = false; } else { @@ -1004,7 +1004,7 @@ bool CountryInstance::modify_unit_variant_unlock(unit_variant_t unit_variant, te } bool CountryInstance::unlock_unit_variant(unit_variant_t unit_variant) { - return modify_unit_variant_unlock(unit_variant, 1); + return modify_unit_variant_unlock(unit_variant, technology_unlock_level_t { 1 }); } unit_variant_t CountryInstance::get_max_unlocked_unit_variant() const { @@ -1020,8 +1020,8 @@ bool CountryInstance::modify_technology_unlock( if (unlock_level + unlock_level_change < 0) { spdlog::error_s( "Attempted to change unlock level for technology {} in country {} to invalid value: current level = {}, change = {}, invalid new value = {}", - technology, *this, static_cast(unlock_level), - static_cast(unlock_level_change), static_cast(unlock_level + unlock_level_change) + technology, *this, unlock_level, + unlock_level_change, unlock_level + unlock_level_change ); return false; } @@ -1053,7 +1053,7 @@ bool CountryInstance::set_technology_unlock_level( } bool CountryInstance::unlock_technology(Technology const& technology) { - return modify_technology_unlock(technology, 1); + return modify_technology_unlock(technology, technology_unlock_level_t { 1 }); } bool CountryInstance::is_technology_unlocked(Technology const& technology) const { @@ -1069,8 +1069,8 @@ bool CountryInstance::modify_invention_unlock( if (unlock_level + unlock_level_change < 0) { spdlog::error_s( "Attempted to change unlock level for invention {} in country {} to invalid value: current level = {}, change = {}, invalid new value = {}", - invention, *this, static_cast(unlock_level), - static_cast(unlock_level_change), static_cast(unlock_level + unlock_level_change) + invention, *this, unlock_level, + unlock_level_change, unlock_level + unlock_level_change ); return false; } @@ -1116,7 +1116,7 @@ bool CountryInstance::set_invention_unlock_level( } bool CountryInstance::unlock_invention(Invention const& invention) { - return modify_invention_unlock(invention, 1); + return modify_invention_unlock(invention, technology_unlock_level_t { 1 }); } bool CountryInstance::is_invention_unlocked(Invention const& invention) const { @@ -1239,7 +1239,7 @@ bool CountryInstance::apply_history_to_country( ret &= set_technology_unlock_level(*technology, level); } for (auto const& [invention, activated] : entry.get_inventions()) { - ret &= set_invention_unlock_level(*invention, activated ? 1 : 0); + ret &= set_invention_unlock_level(*invention, technology_unlock_level_t { static_cast(activated ? 1 : 0) }); } apply_foreign_investments(entry.get_foreign_investment(), country_instance_manager); diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp index 65cf159a0..5f5e60950 100644 --- a/src/openvic-simulation/country/CountryInstance.hpp +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -18,7 +18,7 @@ #include "openvic-simulation/types/HasIndex.hpp" #include "openvic-simulation/types/IndexedFlatMap.hpp" #include "openvic-simulation/types/OrderedContainers.hpp" -#include "openvic-simulation/types/TechnologyUnlockLevel.hpp" +#include "openvic-simulation/research/TechnologyUnlockLevel.hpp" #include "openvic-simulation/types/TypedIndices.hpp" #include "openvic-simulation/types/UnitBranchType.hpp" #include "openvic-simulation/types/UnitVariant.hpp" @@ -389,8 +389,8 @@ namespace OpenVic { OV_IFLATMAP_PROPERTY(RegimentType, technology_unlock_level_t, regiment_type_unlock_levels); regiment_allowed_cultures_t PROPERTY(allowed_regiment_cultures, regiment_allowed_cultures_t::NO_CULTURES); OV_IFLATMAP_PROPERTY(ShipType, technology_unlock_level_t, ship_type_unlock_levels); - technology_unlock_level_t PROPERTY(gas_attack_unlock_level, 0); - technology_unlock_level_t PROPERTY(gas_defence_unlock_level, 0); + technology_unlock_level_t PROPERTY(gas_attack_unlock_level, technology_unlock_level_t { 0 }); + technology_unlock_level_t PROPERTY(gas_defence_unlock_level, technology_unlock_level_t { 0 }); memory::vector SPAN_PROPERTY(unit_variant_unlock_levels); public: diff --git a/src/openvic-simulation/history/CountryHistory.cpp b/src/openvic-simulation/history/CountryHistory.cpp index ca62311bf..93845197e 100644 --- a/src/openvic-simulation/history/CountryHistory.cpp +++ b/src/openvic-simulation/history/CountryHistory.cpp @@ -112,7 +112,7 @@ bool CountryHistoryMap::_load_history_entry( { Technology const* technology = technology_manager.get_technology_by_identifier(key); if (technology != nullptr) { - return expect_uint( + return expect_strong_typedef( [&entry, technology](decltype(entry.technologies)::mapped_type value) -> bool { if (value > 1) { spdlog::warn_s( diff --git a/src/openvic-simulation/history/CountryHistory.hpp b/src/openvic-simulation/history/CountryHistory.hpp index 8db1fbcc2..9e4129681 100644 --- a/src/openvic-simulation/history/CountryHistory.hpp +++ b/src/openvic-simulation/history/CountryHistory.hpp @@ -7,7 +7,7 @@ #include "openvic-simulation/types/IndexedFlatMap.hpp" #include "openvic-simulation/types/OrderedContainers.hpp" #include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" -#include "openvic-simulation/types/TechnologyUnlockLevel.hpp" +#include "openvic-simulation/research/TechnologyUnlockLevel.hpp" #include "openvic-simulation/utility/Containers.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/research/TechnologyUnlockLevel.hpp b/src/openvic-simulation/research/TechnologyUnlockLevel.hpp new file mode 100644 index 000000000..f87cbf8fd --- /dev/null +++ b/src/openvic-simulation/research/TechnologyUnlockLevel.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include +#include + +#include + +namespace OpenVic { + struct technology_unlock_level_t + : type_safe::strong_typedef, + type_safe::strong_typedef_op::equality_comparison, + type_safe::strong_typedef_op::relational_comparison, + type_safe::strong_typedef_op::integer_arithmetic, + type_safe::strong_typedef_op::mixed_addition, + type_safe::strong_typedef_op::mixed_subtraction, + type_safe::strong_typedef_op::mixed_relational_comparison, + type_safe::strong_typedef_op::mixed_equality_comparison { + using strong_typedef::strong_typedef; + }; +} + +template<> +struct fmt::formatter : fmt::formatter { + fmt::format_context::iterator format(OpenVic::technology_unlock_level_t const& value, fmt::format_context& ctx) const { + return fmt::formatter::format(type_safe::get(value), ctx); + } +}; diff --git a/src/openvic-simulation/types/TechnologyUnlockLevel.hpp b/src/openvic-simulation/types/TechnologyUnlockLevel.hpp deleted file mode 100644 index 001462c3c..000000000 --- a/src/openvic-simulation/types/TechnologyUnlockLevel.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include - -namespace OpenVic { - using technology_unlock_level_t = int8_t; -} \ No newline at end of file