Skip to content

Commit f2cadea

Browse files
committed
Refactor life_rating_t to be type-safe
1 parent b5d865f commit f2cadea

9 files changed

Lines changed: 36 additions & 17 deletions

File tree

src/openvic-simulation/defines/CountryDefines.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ node_callback_t CountryDefines::expect_defines() {
4040
"COLONIAL_RANK", ONE_EXACTLY, expect_uint(assign_variable_callback(secondary_power_rank)),
4141
"COLONY_TO_STATE_PRESTIGE_GAIN", ONE_EXACTLY,
4242
expect_fixed_point(assign_variable_callback(colony_to_state_prestige_gain)),
43-
"COLONIAL_LIFERATING", ONE_EXACTLY, expect_uint(assign_variable_callback(colonial_liferating)),
43+
"COLONIAL_LIFERATING", ONE_EXACTLY, expect_strong_typedef<life_rating_t>(assign_variable_callback(colonial_liferating)),
4444
"BASE_GREATPOWER_DAILY_INFLUENCE", ONE_EXACTLY,
4545
expect_fixed_point(assign_variable_callback(base_greatpower_daily_influence)),
4646
"AI_SUPPORT_REFORM", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(ai_support_reform)),

src/openvic-simulation/defines/CountryDefines.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "openvic-simulation/dataloader/NodeTools.hpp"
44
#include "openvic-simulation/types/Date.hpp"
55
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
6-
#include "openvic-simulation/types/ProvinceLifeRating.hpp"
6+
#include "openvic-simulation/map/LifeRating.hpp"
77
#include "openvic-simulation/utility/Getters.hpp"
88

99
namespace OpenVic {
@@ -40,7 +40,7 @@ namespace OpenVic {
4040
Timespan PROPERTY(campaign_duration);
4141
size_t PROPERTY(secondary_power_rank, 0);
4242
fixed_point_t PROPERTY(colony_to_state_prestige_gain);
43-
life_rating_t PROPERTY(colonial_liferating, 0);
43+
life_rating_t PROPERTY(colonial_liferating, life_rating_t { 0 });
4444
fixed_point_t PROPERTY(base_greatpower_daily_influence);
4545
fixed_point_t PROPERTY(ai_support_reform);
4646
fixed_point_t PROPERTY(base_monthly_diplopoints);

src/openvic-simulation/defines/PopsDefines.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ node_callback_t PopsDefines::expect_defines() {
2323
"LUXURY_THRESHOLD", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(luxury_threshold)),
2424
"BASE_GOODS_DEMAND", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(base_goods_demand)),
2525
"BASE_POPGROWTH", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(base_popgrowth)),
26-
"MIN_LIFE_RATING_FOR_GROWTH", ONE_EXACTLY, expect_uint(assign_variable_callback(min_life_rating_for_growth)),
26+
"MIN_LIFE_RATING_FOR_GROWTH", ONE_EXACTLY, expect_strong_typedef<life_rating_t>(assign_variable_callback(min_life_rating_for_growth)),
2727
"LIFE_RATING_GROWTH_BONUS", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(life_rating_growth_bonus)),
2828
"LIFE_NEED_STARVATION_LIMIT", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(life_need_starvation_limit)),
2929
"MIL_LACK_EVERYDAY_NEED", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(mil_lack_everyday_need)),

src/openvic-simulation/defines/PopsDefines.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "openvic-simulation/dataloader/NodeTools.hpp"
44
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
55
#include "openvic-simulation/population/PopSum.hpp"
6-
#include "openvic-simulation/types/ProvinceLifeRating.hpp"
6+
#include "openvic-simulation/map/LifeRating.hpp"
77
#include "openvic-simulation/utility/Getters.hpp"
88

99
namespace OpenVic {
@@ -24,7 +24,7 @@ namespace OpenVic {
2424
fixed_point_t PROPERTY(luxury_threshold);
2525
fixed_point_t PROPERTY(base_goods_demand);
2626
fixed_point_t PROPERTY(base_popgrowth);
27-
life_rating_t PROPERTY(min_life_rating_for_growth, 0);
27+
life_rating_t PROPERTY(min_life_rating_for_growth, life_rating_t { 0 });
2828
fixed_point_t PROPERTY(life_rating_growth_bonus);
2929
fixed_point_t PROPERTY(life_need_starvation_limit);
3030
fixed_point_t PROPERTY(mil_lack_everyday_need);

src/openvic-simulation/history/ProvinceHistory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ bool ProvinceHistoryMap::_load_history_entry(
135135
allow_empty_true, //could be explicitly setting trade_goods to null
136136
do_warn //could be typo in good identifier
137137
),
138-
"life_rating", ZERO_OR_ONE, expect_uint<life_rating_t>(assign_variable_callback(entry.life_rating)),
138+
"life_rating", ZERO_OR_ONE, expect_strong_typedef<life_rating_t>(assign_variable_callback(entry.life_rating)),
139139
"terrain", ZERO_OR_ONE, terrain_type_manager.expect_terrain_type_identifier(
140140
assign_variable_callback_pointer_opt(entry.terrain_type)
141141
),

src/openvic-simulation/history/ProvinceHistory.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "openvic-simulation/types/Date.hpp"
1111
#include "openvic-simulation/types/OrderedContainers.hpp"
1212
#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp"
13-
#include "openvic-simulation/types/ProvinceLifeRating.hpp"
13+
#include "openvic-simulation/map/LifeRating.hpp"
1414
#include "openvic-simulation/utility/Containers.hpp"
1515
#include "openvic-simulation/utility/Getters.hpp"
1616

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
#include <fmt/base.h>
6+
#include <fmt/format.h>
7+
8+
#include <type_safe/strong_typedef.hpp>
9+
10+
namespace OpenVic {
11+
struct life_rating_t : type_safe::strong_typedef<life_rating_t, std::int8_t>,
12+
type_safe::strong_typedef_op::equality_comparison<life_rating_t>,
13+
type_safe::strong_typedef_op::relational_comparison<life_rating_t>,
14+
type_safe::strong_typedef_op::integer_arithmetic<life_rating_t>,
15+
type_safe::strong_typedef_op::mixed_addition<life_rating_t, std::uint8_t>,
16+
type_safe::strong_typedef_op::mixed_subtraction<life_rating_t, std::uint8_t> {
17+
using strong_typedef::strong_typedef;
18+
};
19+
}
20+
21+
template<>
22+
struct fmt::formatter<OpenVic::life_rating_t> : fmt::formatter<std::int8_t> {
23+
fmt::format_context::iterator format(OpenVic::life_rating_t const& value, fmt::format_context& ctx) const {
24+
return fmt::formatter<std::int8_t>::format(type_safe::get(value), ctx);
25+
}
26+
};

src/openvic-simulation/map/ProvinceInstance.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "openvic-simulation/types/HasIdentifier.hpp"
1414
#include "openvic-simulation/types/HasIndex.hpp"
1515
#include "openvic-simulation/types/OrderedContainers.hpp"
16-
#include "openvic-simulation/types/ProvinceLifeRating.hpp"
16+
#include "openvic-simulation/map/LifeRating.hpp"
1717
#include "openvic-simulation/types/TypedIndices.hpp"
1818
#include "openvic-simulation/types/UnitBranchType.hpp"
1919
#include "openvic-simulation/utility/Containers.hpp"
@@ -72,7 +72,7 @@ namespace OpenVic {
7272
GameRulesManager const& game_rules_manager;
7373

7474
TerrainType const* PROPERTY(terrain_type);
75-
life_rating_t PROPERTY(life_rating, 0);
75+
life_rating_t PROPERTY(life_rating, life_rating_t { 0 });
7676
colony_status_t PROPERTY(colony_status, colony_status_t::STATE);
7777
State* PROPERTY_PTR(state, nullptr);
7878

src/openvic-simulation/types/ProvinceLifeRating.hpp

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)