Skip to content

Commit 84debe8

Browse files
Spartan322wvpm
authored andcommitted
Add type-safe pop_sum_t for large value sums of pops
Refactor pop_size_t to be type-safe Move pop_size_t to population/PopSize.hpp Add equalable to Concepts.hpp Add expect_strong_typedef to NodeTools.hpp Add `&& equalable<ValueType, OtherValueType>` requirement to IndexedFlatMap's divide_assign_handle_zero Fix ovdl::detail vs. OpenVic::detail namespace ambiguity in Dataloader.cpp
1 parent c971d7c commit 84debe8

29 files changed

+318
-106
lines changed

src/openvic-simulation/core/template/Concepts.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,9 @@ namespace OpenVic {
226226
concept mul_add_assignable = requires(Lhs& lhs, const A a, const B b) {
227227
{ lhs += a * b } -> std::same_as<Lhs&>;
228228
};
229+
230+
template<typename Lhs, typename Rhs = Lhs>
231+
concept equalable = requires(Lhs const& lhs, Rhs const& rhs) {
232+
{ lhs == rhs } -> std::convertible_to<bool>;
233+
};
229234
}

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
#include "openvic-simulation/types/Date.hpp"
4444
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
4545
#include "openvic-simulation/types/IndexedFlatMap.hpp"
46-
#include "openvic-simulation/types/PopSize.hpp"
46+
#include "openvic-simulation/population/PopSize.hpp"
47+
#include "openvic-simulation/population/PopSum.hpp"
4748
#include "openvic-simulation/types/UnitBranchType.hpp"
4849
#include "openvic-simulation/utility/Containers.hpp"
4950
#include "openvic-simulation/utility/Logger.hpp"
@@ -1341,8 +1342,8 @@ void CountryInstance::_update_budget() {
13411342
OpenVic immediately updates both.
13421343
*/
13431344

1344-
pop_size_t total_non_colonial_population = 0;
1345-
pop_size_t administrators = 0;
1345+
pop_sum_t total_non_colonial_population = 0;
1346+
pop_sum_t administrators = 0;
13461347
for (State const* const state_ptr : states) {
13471348
if (state_ptr == nullptr) {
13481349
continue;
@@ -1353,7 +1354,7 @@ void CountryInstance::_update_budget() {
13531354
continue;
13541355
}
13551356

1356-
IndexedFlatMap<PopType, pop_size_t> const& state_population_by_type = state.get_population_by_type();
1357+
IndexedFlatMap<PopType, pop_sum_t> const& state_population_by_type = state.get_population_by_type();
13571358

13581359
for (auto const& [pop_type, size] : state_population_by_type) {
13591360
if (pop_type.is_administrator) {
@@ -1369,10 +1370,10 @@ void CountryInstance::_update_budget() {
13691370
} else {
13701371
administrator_percentage.set(fixed_point_t(administrators) / total_non_colonial_population);
13711372

1372-
const fixed_point_t desired_administrators = desired_administrator_percentage.get_untracked() * total_non_colonial_population;
1373+
const fixed_point_t desired_administrators = desired_administrator_percentage.get_untracked() * type_safe::get(total_non_colonial_population);
13731374
const fixed_point_t administrative_efficiency_from_administrators_unclamped = std::min(
13741375
fixed_point_t::mul_div(
1375-
administrators,
1376+
type_safe::get(administrators),
13761377
fixed_point_t::_1 + get_modifier_effect_value(*modifier_effect_cache.get_administrative_efficiency()),
13771378
desired_administrators
13781379
)
@@ -1398,7 +1399,7 @@ void CountryInstance::_update_budget() {
13981399
projected_education_spending_unscaled_by_slider_running_total += size * education_salary_base_by_pop_type.at(pop_type).get_untracked();
13991400
projected_military_spending_unscaled_by_slider_running_total += size * military_salary_base_by_pop_type.at(pop_type).get_untracked();
14001401
projected_pensions_spending_unscaled_by_slider_running_total += size * calculate_pensions_base(pop_type);
1401-
projected_unemployment_subsidies_spending_unscaled_by_slider_running_total += get_unemployed_pops_by_type(pop_type)
1402+
projected_unemployment_subsidies_spending_unscaled_by_slider_running_total += type_safe::get(get_unemployed_pops_by_type(pop_type))
14021403
* calculate_unemployment_subsidies_base(pop_type);
14031404
}
14041405

src/openvic-simulation/dataloader/Dataloader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ string_set_t Dataloader::lookup_dirs_in_dir(std::string_view path) const {
240240
return ret;
241241
}
242242

243-
template<std::derived_from<detail::BasicParser> Parser, bool (*parse_func)(Parser&)>
243+
template<std::derived_from<ovdl::detail::BasicParser> Parser, bool (*parse_func)(Parser&)>
244244
static Parser _run_ovdl_parser(fs::path const& path) {
245245
Parser parser;
246246
memory::string buffer;
247-
auto error_log_stream = detail::make_callback_stream<char>(
247+
auto error_log_stream = ovdl::detail::make_callback_stream<char>(
248248
[](void const* s, std::streamsize n, void* user_data) -> std::streamsize {
249249
if (s != nullptr && n > 0 && user_data != nullptr) {
250250
static_cast<memory::string*>(user_data)->append(static_cast<char const*>(s), n);

src/openvic-simulation/dataloader/NodeTools.hpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,49 @@ using namespace std::string_view_literals;
199199
return expect_uint(callback, base);
200200
}
201201

202+
template<derived_from_specialization_of<type_safe::strong_typedef> T, typename AsT = type_safe::underlying_type<T>>
203+
NodeCallback auto expect_strong_typedef(callback_t<T>& callback, int base = 10) {
204+
if constexpr (std::unsigned_integral<AsT>) {
205+
return expect_uint64(
206+
[callback](uint64_t val) mutable -> bool {
207+
if (val <= static_cast<uint64_t>(std::numeric_limits<AsT>::max())) {
208+
return callback(T(val));
209+
}
210+
spdlog::error_s(
211+
"Invalid uint: {} (valid range: [0, {}])", val,
212+
static_cast<uint64_t>(std::numeric_limits<AsT>::max())
213+
);
214+
return false;
215+
},
216+
base
217+
);
218+
} else {
219+
return expect_int64(
220+
[callback](int64_t val) mutable -> bool {
221+
if (val >= static_cast<int64_t>(std::numeric_limits<AsT>::min()) &&
222+
val <= static_cast<int64_t>(std::numeric_limits<AsT>::max())) {
223+
return callback(T(val));
224+
}
225+
spdlog::error_s(
226+
"Invalid int: {} (valid range: [{}, {}])", val,
227+
static_cast<int64_t>(std::numeric_limits<AsT>::min()),
228+
static_cast<int64_t>(std::numeric_limits<AsT>::max())
229+
);
230+
return false;
231+
},
232+
base
233+
);
234+
}
235+
}
236+
template<derived_from_specialization_of<type_safe::strong_typedef> T, typename AsT = type_safe::underlying_type<T>>
237+
NodeCallback auto expect_strong_typedef(callback_t<T>&& callback, int base = 10) {
238+
return expect_strong_typedef<T, AsT>(callback, base);
239+
}
240+
202241
template<derived_from_specialization_of<type_safe::strong_typedef> T>
203242
requires std::unsigned_integral<type_safe::underlying_type<T>>
204243
NodeCallback auto expect_index(callback_t<T>& callback, int base = 10) {
205-
using underlying_type = type_safe::underlying_type<T>;
206-
207-
return expect_uint64([callback](uint64_t val) mutable -> bool {
208-
if (val <= static_cast<uint64_t>(std::numeric_limits<underlying_type>::max())) {
209-
return callback(T(val));
210-
}
211-
spdlog::error_s(
212-
"Invalid uint: {} (valid range: [0, {}])",
213-
val, static_cast<uint64_t>(std::numeric_limits<underlying_type>::max())
214-
);
215-
return false;
216-
}, base);
244+
return expect_strong_typedef<T>(callback, base);
217245
}
218246
template<derived_from_specialization_of<type_safe::strong_typedef> T>
219247
requires std::unsigned_integral<type_safe::underlying_type<T>>

src/openvic-simulation/defines/MilitaryDefines.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "MilitaryDefines.hpp"
22

3-
#include <cstdint>
4-
53
#include "openvic-simulation/military/CombatWidth.hpp"
64

75
#include <type_safe/strong_typedef.hpp>
@@ -20,12 +18,9 @@ node_callback_t MilitaryDefines::expect_defines() {
2018
"DIG_IN_INCREASE_EACH_DAYS", ONE_EXACTLY, expect_days(assign_variable_callback(dig_in_increase_each_days)),
2119
"REINFORCE_SPEED", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(reinforce_speed)),
2220
"COMBAT_DIFFICULTY_IMPACT", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(combat_difficulty_impact)),
23-
"BASE_COMBAT_WIDTH", ONE_EXACTLY, expect_int<int8_t>([this](int8_t val)->bool{
24-
base_combat_width = combat_width_t(static_cast<type_safe::underlying_type<combat_width_t>>(val));
25-
return true;
26-
}),
27-
"POP_MIN_SIZE_FOR_REGIMENT", ONE_EXACTLY, expect_uint(assign_variable_callback(min_pop_size_for_regiment)),
28-
"POP_SIZE_PER_REGIMENT", ONE_EXACTLY, expect_uint(assign_variable_callback(pop_size_per_regiment)),
21+
"BASE_COMBAT_WIDTH", ONE_EXACTLY, expect_strong_typedef<combat_width_t>(assign_variable_callback(base_combat_width)),
22+
"POP_MIN_SIZE_FOR_REGIMENT", ONE_EXACTLY, expect_strong_typedef<pop_size_t>(assign_variable_callback(min_pop_size_for_regiment)),
23+
"POP_SIZE_PER_REGIMENT", ONE_EXACTLY, expect_strong_typedef<pop_size_t>(assign_variable_callback(pop_size_per_regiment)),
2924
"SOLDIER_TO_POP_DAMAGE", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(soldier_to_pop_damage)),
3025
"LAND_SPEED_MODIFIER", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(land_speed_modifier)),
3126
"NAVAL_SPEED_MODIFIER", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(naval_speed_modifier)),

src/openvic-simulation/defines/MilitaryDefines.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "openvic-simulation/military/CombatWidth.hpp"
55
#include "openvic-simulation/types/Date.hpp"
66
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
7-
#include "openvic-simulation/types/PopSize.hpp"
7+
#include "openvic-simulation/population/PopSize.hpp"
88
#include "openvic-simulation/utility/Getters.hpp"
99

1010
namespace OpenVic {

src/openvic-simulation/defines/PopsDefines.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ node_callback_t PopsDefines::expect_defines() {
7575
"MOVEMENT_SUPPORT_UH_FACTOR", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(movement_support_uh_factor)),
7676
"REBEL_OCCUPATION_STRENGTH_BONUS", ONE_EXACTLY,
7777
expect_fixed_point(assign_variable_callback(rebel_occupation_strength_bonus)),
78-
"LARGE_POPULATION_LIMIT", ONE_EXACTLY, expect_uint(assign_variable_callback(large_population_limit)),
78+
"LARGE_POPULATION_LIMIT", ONE_EXACTLY, expect_strong_typedef<pop_sum_t>(assign_variable_callback(large_population_limit)),
7979
"LARGE_POPULATION_INFLUENCE_PENALTY_CHUNK", ONE_EXACTLY,
80-
expect_uint(assign_variable_callback(large_population_influence_penalty_chunk))
80+
expect_strong_typedef<pop_sum_t>(assign_variable_callback(large_population_influence_penalty_chunk))
8181
);
8282
}

src/openvic-simulation/defines/PopsDefines.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "openvic-simulation/dataloader/NodeTools.hpp"
44
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
5-
#include "openvic-simulation/types/PopSize.hpp"
5+
#include "openvic-simulation/population/PopSum.hpp"
66
#include "openvic-simulation/types/ProvinceLifeRating.hpp"
77
#include "openvic-simulation/utility/Getters.hpp"
88

@@ -71,8 +71,8 @@ namespace OpenVic {
7171
fixed_point_t PROPERTY(nationalist_movement_mil_cap);
7272
fixed_point_t PROPERTY(movement_support_uh_factor);
7373
fixed_point_t PROPERTY(rebel_occupation_strength_bonus);
74-
pop_size_t PROPERTY(large_population_limit, 0);
75-
pop_size_t PROPERTY(large_population_influence_penalty_chunk, 0);
74+
pop_sum_t PROPERTY(large_population_limit, 0);
75+
pop_sum_t PROPERTY(large_population_influence_penalty_chunk, 0);
7676

7777
PopsDefines();
7878

src/openvic-simulation/economy/production/ArtisanalProducer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ void ArtisanalProducer::artisan_tick_handler::calculate_inputs(
7272
//throughput scalar, the minimum of stockpile / base_desired_quantity
7373
//inputs_bought_fraction uses base_desired_quantity as population size is cancelled in the production and input calculations.
7474
const pop_size_t pop_size = pop.get_size();
75-
fixed_point_t inputs_bought_numerator = pop_size,
76-
inputs_bought_denominator = production_type.base_workforce_size,
75+
fixed_point_t inputs_bought_numerator = type_safe::get(pop_size),
76+
inputs_bought_denominator = type_safe::get(production_type.base_workforce_size),
7777
inputs_bought_fraction_v = inputs_bought_numerator / inputs_bought_denominator;
7878

7979
distinct_goods_to_buy = 0;
@@ -479,7 +479,7 @@ std::optional<fixed_point_t> ArtisanalProducer::estimate_production_type_score(
479479
fixed_point_t ArtisanalProducer::calculate_production_type_score(
480480
const fixed_point_t revenue,
481481
const fixed_point_t costs,
482-
const pop_size_t workforce
482+
const pop_sum_t workforce
483483
) {
484484
if (costs >= revenue) {
485485
return 0;

src/openvic-simulation/economy/production/ArtisanalProducer.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
88
#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp"
99
#include "openvic-simulation/types/fixed_point/Fraction.hpp"
10-
#include "openvic-simulation/types/PopSize.hpp"
1110
#include "openvic-simulation/utility/Getters.hpp"
1211

1312
namespace OpenVic {
@@ -23,6 +22,8 @@ namespace OpenVic {
2322
struct ProductionType;
2423
struct ProvinceInstance;
2524
struct RandomU32;
25+
struct pop_size_t;
26+
struct pop_sum_t;
2627

2728
struct ArtisanalProducer {
2829
private:
@@ -48,7 +49,7 @@ namespace OpenVic {
4849
static fixed_point_t calculate_production_type_score(
4950
const fixed_point_t revenue,
5051
const fixed_point_t costs,
51-
const pop_size_t workforce
52+
const pop_sum_t workforce
5253
);
5354

5455
public:

0 commit comments

Comments
 (0)