Skip to content

Commit 04c8a63

Browse files
authored
Merge pull request #711 from OpenVicProject/refactor_politics_reference_wrapper
Refactor politics and leftovers to use reference_wrapper
2 parents d6b9312 + 416e36c commit 04c8a63

44 files changed

Lines changed: 401 additions & 294 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/headless/main.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,32 +260,31 @@ static bool run_headless(fs::path const& root, memory::vector<memory::string>& m
260260
SPDLOG_INFO("===== Ranking system test... =====");
261261
if (game_manager.get_instance_manager()) {
262262
const auto print_ranking_list = [ //
263-
](std::string_view title, OpenVic::forwardable_span<CountryInstance* const> countries) -> void {
263+
](std::string_view title, OpenVic::forwardable_span<const std::reference_wrapper<CountryInstance>> countries) -> void {
264264
memory::string countries_str;
265-
for (CountryInstance* country : countries) {
265+
for (CountryInstance& country : countries) {
266266
countries_str += fmt::format(
267267
"\n\t{} - Total #{} ({}), Prestige #{} ({}), Industry #{} ({}), Military #{} ({})", //
268-
*country, //
269-
country->get_total_rank(), country->total_score.get_untracked().to_string(1), //
270-
country->get_prestige_rank(), country->get_prestige_untracked().to_string(1),
271-
country->get_industrial_rank(), country->get_industrial_power_untracked().to_string(1),
272-
country->get_military_rank(), country->military_power.get_untracked().to_string(1)
268+
country, //
269+
country.get_total_rank(), country.total_score.get_untracked().to_string(1), //
270+
country.get_prestige_rank(), country.get_prestige_untracked().to_string(1),
271+
country.get_industrial_rank(), country.get_industrial_power_untracked().to_string(1),
272+
country.get_military_rank(), country.military_power.get_untracked().to_string(1)
273273
);
274274
}
275275
SPDLOG_INFO("{}:{}", title, countries_str);
276276
};
277277

278-
CountryInstanceManager const& country_instance_manager =
279-
game_manager.get_instance_manager()->get_country_instance_manager();
278+
CountryInstanceManager const& country_instance_manager = game_manager.get_instance_manager()->get_country_instance_manager();
280279

281-
OpenVic::forwardable_span<CountryInstance* const> great_powers = country_instance_manager.get_great_powers();
280+
OpenVic::forwardable_span<const std::reference_wrapper<CountryInstance>> great_powers = country_instance_manager.get_great_powers();
282281
print_ranking_list("Great Powers", great_powers);
283282
print_ranking_list("Secondary Powers", country_instance_manager.get_secondary_powers());
284283
print_ranking_list("All countries", country_instance_manager.get_total_ranking());
285284

286285
SPDLOG_INFO("===== RGO test... =====");
287286
for (size_t i = 0; i < std::min<size_t>(3, great_powers.size()); ++i) {
288-
CountryInstance const& great_power = *great_powers[i];
287+
CountryInstance const& great_power = great_powers[i];
289288
ProvinceInstance const* const capital_province = great_power.get_capital();
290289
if (capital_province == nullptr) {
291290
spdlog::warn_s("{} has no capital ProvinceInstance set.", great_power);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <cstddef>
55
#include <memory>
66
#include <string_view>
7-
#include <type_traits>
87

98
#include <type_safe/strong_typedef.hpp>
109

@@ -235,4 +234,4 @@ namespace OpenVic {
235234
concept strict_regular_invocable_r = std::regular_invocable<F, Args...> && requires(F f, Args&&... args) {
236235
{ f(static_cast<Args>(args)...) } -> std::same_as<Return>;
237236
};
238-
}
237+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include <functional>
4+
5+
#include <concepts/type_traits.hpp>
6+
7+
// bug fix for std::hash & std::equal_to to work with std::reference_wrapper
8+
namespace std {
9+
template <typename T>
10+
struct hash<std::reference_wrapper<T>> {
11+
using BaseType = std::remove_cv_t<T>;
12+
13+
[[nodiscard]] size_t operator()(T const& val) const noexcept {
14+
return std::hash<BaseType>{}(val);
15+
}
16+
};
17+
}
18+
19+
// bug fix for concepts/type_traits.hpp to work with std::reference_wrapper
20+
namespace concepts {
21+
namespace detail {
22+
template<typename T>
23+
constexpr bool _is_ref_wrapper = false;
24+
25+
template<typename T>
26+
constexpr bool _is_ref_wrapper<std::reference_wrapper<T>> = true;
27+
28+
template<typename R, typename T, typename RQual, typename TQual>
29+
concept _ref_wrap_common_reference_with = _is_ref_wrapper<R>
30+
&& requires { typename std::common_reference_t<typename R::type&, TQual>; }
31+
&& std::convertible_to<RQual, std::common_reference_t<typename R::type&, TQual>>;
32+
}
33+
34+
template<typename R, typename T, template<typename> class RQual, template<typename> class TQual>
35+
requires detail::_ref_wrap_common_reference_with<R, T, RQual<R>, TQual<T>>
36+
&& (!detail::_ref_wrap_common_reference_with<T, R, TQual<T>, RQual<R>>)
37+
struct basic_common_reference<R, T, RQual, TQual>
38+
{
39+
using type = std::common_reference_t<typename R::type&, TQual<T>>;
40+
};
41+
42+
template<typename T, typename R, template<typename> class TQual, template<typename> class RQual>
43+
requires detail::_ref_wrap_common_reference_with<R, T, RQual<R>, TQual<T>>
44+
&& (!detail::_ref_wrap_common_reference_with<T, R, TQual<T>, RQual<R>>)
45+
struct basic_common_reference<T, R, TQual, RQual>
46+
{
47+
using type = std::common_reference_t<typename R::type&, TQual<T>>;
48+
};
49+
}

src/openvic-simulation/country/CountryDefinition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ node_callback_t CountryDefinitionManager::load_country_party(
159159

160160
return politics_manager.get_issue_manager().expect_party_policy_identifier(
161161
[&party_policy_group, &policy](PartyPolicy const& party_policy) -> bool {
162-
if (&party_policy.issue_group == &party_policy_group) {
162+
if (&party_policy.group == &party_policy_group) {
163163
policy = &party_policy;
164164
return true;
165165
}
@@ -168,7 +168,7 @@ node_callback_t CountryDefinitionManager::load_country_party(
168168
spdlog::warn_s(
169169
"Invalid party policy \"{}\", group is \"{}\" when \"{}\" was expected.",
170170
party_policy,
171-
party_policy.issue_group,
171+
party_policy.group,
172172
party_policy_group
173173
);
174174
return true;

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -61,51 +61,51 @@ static constexpr size_t DAYS_OF_BALANCE_HISTORY = 30;
6161
static constexpr colour_t ERROR_COLOUR = colour_t::from_integer(0xFF0000);
6262

6363
CountryInstance::CountryInstance(
64-
CountryDefinition const* new_country_definition,
65-
SharedCountryValues* new_shared_country_values,
66-
CountryInstanceDeps const* country_instance_deps
64+
CountryDefinition const& new_country_definition,
65+
SharedCountryValues& new_shared_country_values,
66+
CountryInstanceDeps const& country_instance_deps
6767
) : FlagStrings { "country" },
68-
HasIndex { new_country_definition->index },
68+
HasIndex { new_country_definition.index },
6969
PopsAggregate {
70-
country_instance_deps->stratas,
71-
country_instance_deps->pop_types,
72-
country_instance_deps->ideologies
70+
country_instance_deps.stratas,
71+
country_instance_deps.pop_types,
72+
country_instance_deps.ideologies
7373
},
7474
/* Main attributes */
75-
country_definition { *new_country_definition },
76-
shared_country_values { *new_shared_country_values },
77-
78-
country_relations_manager { country_instance_deps->country_relations_manager },
79-
game_rules_manager { country_instance_deps->game_rules_manager },
80-
good_instance_manager { country_instance_deps->good_instance_manager },
81-
market_instance { country_instance_deps->market_instance },
82-
modifier_effect_cache { country_instance_deps->modifier_effect_cache },
83-
unit_type_manager { country_instance_deps->unit_type_manager },
75+
country_definition { new_country_definition },
76+
shared_country_values { new_shared_country_values },
77+
78+
country_relations_manager { country_instance_deps.country_relations_manager },
79+
game_rules_manager { country_instance_deps.game_rules_manager },
80+
good_instance_manager { country_instance_deps.good_instance_manager },
81+
market_instance { country_instance_deps.market_instance },
82+
modifier_effect_cache { country_instance_deps.modifier_effect_cache },
83+
unit_type_manager { country_instance_deps.unit_type_manager },
8484

85-
fallback_date_for_never_completing_research { country_instance_deps->fallback_date_for_never_completing_research },
86-
country_defines { country_instance_deps->country_defines },
87-
diplomacy_defines { country_instance_deps->diplomacy_defines },
88-
economy_defines { country_instance_deps->economy_defines },
89-
military_defines { country_instance_deps->military_defines },
85+
fallback_date_for_never_completing_research { country_instance_deps.fallback_date_for_never_completing_research },
86+
country_defines { country_instance_deps.country_defines },
87+
diplomacy_defines { country_instance_deps.diplomacy_defines },
88+
economy_defines { country_instance_deps.economy_defines },
89+
military_defines { country_instance_deps.military_defines },
9090

9191
colour { ERROR_COLOUR },
9292

9393
/* Production */
94-
building_type_unlock_levels { country_instance_deps->building_types },
94+
building_type_unlock_levels { country_instance_deps.building_types },
9595

9696
/* Budget */
9797
balance_history{DAYS_OF_BALANCE_HISTORY},
98-
taxable_income_by_pop_type { country_instance_deps->pop_types },
98+
taxable_income_by_pop_type { country_instance_deps.pop_types },
9999
effective_tax_rate_by_strata {
100-
country_instance_deps->stratas,
100+
country_instance_deps.stratas,
101101
[this](Strata const& strata)->auto {
102102
return [this,&strata](DependencyTracker& tracker)->fixed_point_t {
103103
return tax_efficiency.get(tracker) * tax_rate_slider_value_by_strata.at(strata).get_value(tracker);
104104
};
105105
}
106106
},
107107
administration_salary_base_by_pop_type{
108-
country_instance_deps->pop_types,
108+
country_instance_deps.pop_types,
109109
[this](PopType const& pop_type)->auto {
110110
return [this,&pop_type](DependencyTracker& tracker)->fixed_point_t {
111111
return corruption_cost_multiplier.get(tracker)
@@ -115,7 +115,7 @@ CountryInstance::CountryInstance(
115115
}
116116
},
117117
education_salary_base_by_pop_type{
118-
country_instance_deps->pop_types,
118+
country_instance_deps.pop_types,
119119
[this](PopType const& pop_type)->auto {
120120
return [this,&pop_type](DependencyTracker& tracker)->fixed_point_t {
121121
return corruption_cost_multiplier.get(tracker)
@@ -125,7 +125,7 @@ CountryInstance::CountryInstance(
125125
}
126126
},
127127
military_salary_base_by_pop_type{
128-
country_instance_deps->pop_types,
128+
country_instance_deps.pop_types,
129129
[this](PopType const& pop_type)->auto {
130130
return [this,&pop_type](DependencyTracker& tracker)->fixed_point_t {
131131
return corruption_cost_multiplier.get(tracker)
@@ -135,7 +135,7 @@ CountryInstance::CountryInstance(
135135
}
136136
},
137137
social_income_variant_base_by_pop_type{
138-
country_instance_deps->pop_types,
138+
country_instance_deps.pop_types,
139139
[this](PopType const& pop_type)->auto {
140140
return [this,&pop_type](DependencyTracker& tracker)->fixed_point_t {
141141
return corruption_cost_multiplier.get(tracker)
@@ -144,26 +144,26 @@ CountryInstance::CountryInstance(
144144
};
145145
}
146146
},
147-
tax_rate_slider_value_by_strata { country_instance_deps->stratas },
147+
tax_rate_slider_value_by_strata { country_instance_deps.stratas },
148148

149149
/* Technology */
150-
technology_unlock_levels { country_instance_deps->technologies },
151-
invention_unlock_levels { country_instance_deps->inventions },
150+
technology_unlock_levels { country_instance_deps.technologies },
151+
invention_unlock_levels { country_instance_deps.inventions },
152152

153153
/* Politics */
154-
upper_house_proportion_by_ideology { country_instance_deps->ideologies },
155-
reforms { country_instance_deps->reforms },
156-
flag_overrides_by_government_type { country_instance_deps->government_types },
157-
crime_unlock_levels { country_instance_deps->crimes },
154+
upper_house_proportion_by_ideology { country_instance_deps.ideologies },
155+
reforms { country_instance_deps.reforms },
156+
flag_overrides_by_government_type { country_instance_deps.government_types },
157+
crime_unlock_levels { country_instance_deps.crimes },
158158

159159
/* Trade */
160-
goods_data { country_instance_deps->good_instances },
160+
goods_data { country_instance_deps.good_instances },
161161

162162
/* Diplomacy */
163163

164164
/* Military */
165-
regiment_type_unlock_levels { country_instance_deps->regiment_types },
166-
ship_type_unlock_levels { country_instance_deps->ship_types },
165+
regiment_type_unlock_levels { country_instance_deps.regiment_types },
166+
ship_type_unlock_levels { country_instance_deps.ship_types },
167167

168168
/* DerivedState */
169169
flag_government_type { [this](DependencyTracker& tracker)->GovernmentType const* {
@@ -247,7 +247,7 @@ CountryInstance::CountryInstance(
247247
}
248248

249249
// army, navy and construction spending have minimums defined in EconomyDefines and always have an unmodified max (1.0).
250-
EconomyDefines const& economy_defines = country_instance_deps->economy_defines;
250+
EconomyDefines const& economy_defines = country_instance_deps.economy_defines;
251251
army_spending_slider_value.set_bounds(economy_defines.get_minimum_army_spending_slider_value(), 1);
252252
army_spending_slider_value.set_value(1);
253253

@@ -274,7 +274,7 @@ CountryInstance::CountryInstance(
274274
tariff_rate_slider_value.set_bounds(0, 0);
275275
tariff_rate_slider_value.set_value(0);
276276

277-
update_parties_for_votes(new_country_definition);
277+
update_parties_for_votes(&new_country_definition);
278278

279279
for (BuildingType const& building_type : building_type_unlock_levels.get_keys()) {
280280
if (building_type.is_enabled_by_default) {
@@ -393,17 +393,17 @@ void CountryInstance::set_alliance_with(CountryInstance& country, bool alliance)
393393
}
394394

395395
bool CountryInstance::is_at_war_with(CountryInstance const& country) const {
396-
return war_enemies.contains(&country);
396+
return war_enemies.contains(country);
397397
}
398398

399399
void CountryInstance::set_at_war_with(CountryInstance& country, bool at_war) {
400400
country_relations_manager.set_at_war_with(this, &country, at_war);
401401
if (at_war) {
402-
war_enemies.insert(&country);
403-
country.war_enemies.insert(this);
402+
war_enemies.emplace(country);
403+
country.war_enemies.emplace(*this);
404404
} else {
405-
war_enemies.unordered_erase(&country);
406-
country.war_enemies.unordered_erase(this);
405+
war_enemies.unordered_erase(country);
406+
country.war_enemies.unordered_erase(*this);
407407
}
408408
}
409409

@@ -639,7 +639,7 @@ bool CountryInstance::set_ruling_party(CountryParty const& new_ruling_party) {
639639
}
640640

641641
bool CountryInstance::add_reform(Reform const& new_reform) {
642-
ReformGroup const& reform_group = new_reform.get_reform_group();
642+
ReformGroup const& reform_group = new_reform.group;
643643
Reform const*& reform = reforms.at(reform_group);
644644

645645
if (reform != &new_reform) {
@@ -1211,7 +1211,7 @@ bool CountryInstance::can_research_tech(Technology const& technology, const Date
12111211

12121212
const Technology::area_index_t index_in_area = technology.index_in_area;
12131213

1214-
return index_in_area == 0 || is_technology_unlocked(*technology.area.get_technologies()[index_in_area - 1]);
1214+
return index_in_area == 0 || is_technology_unlocked(technology.area.get_technologies()[index_in_area - 1]);
12151215
}
12161216

12171217
void CountryInstance::start_research(Technology const& technology, const Date today) {
@@ -1233,7 +1233,7 @@ void CountryInstance::apply_foreign_investments(
12331233
fixed_point_map_t<CountryDefinition const*> const& investments, CountryInstanceManager const& country_instance_manager
12341234
) {
12351235
for (auto const& [country, money_invested] : investments) {
1236-
foreign_investments[&country_instance_manager.get_country_instance_by_definition(*country)] = money_invested;
1236+
foreign_investments[country_instance_manager.get_country_instance_by_definition(*country)] = money_invested;
12371237
}
12381238
}
12391239

@@ -1320,16 +1320,17 @@ void CountryInstance::_update_production() {
13201320
industrial_power_from_states.clear();
13211321
industrial_power_from_investments.clear();
13221322

1323-
for (State const* state : states) {
1324-
const fixed_point_t state_industrial_power = state->get_industrial_power();
1323+
for (State const* state_ptr : states) {
1324+
State const& state = *state_ptr;
1325+
const fixed_point_t state_industrial_power = state.get_industrial_power();
13251326
if (state_industrial_power != 0) {
13261327
industrial_power += state_industrial_power;
13271328
industrial_power_from_states.emplace_back(state, state_industrial_power);
13281329
}
13291330
}
13301331

13311332
for (auto const& [country, money_invested] : foreign_investments) {
1332-
if (country->exists()) {
1333+
if (country.get().exists()) {
13331334
const fixed_point_t investment_industrial_power = fixed_point_t::mul_div(
13341335
money_invested,
13351336
country_defines.get_country_investment_industrial_score_factor(),

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ namespace OpenVic {
154154

155155
/* Production */
156156
OV_STATE_PROPERTY(fixed_point_t, industrial_power);
157-
memory::vector<std::pair<State const*, fixed_point_t>> SPAN_PROPERTY(industrial_power_from_states);
158-
memory::vector<std::pair<CountryInstance const*, fixed_point_t>> SPAN_PROPERTY(industrial_power_from_investments);
157+
memory::vector<std::pair<std::reference_wrapper<const State>, fixed_point_t>> SPAN_PROPERTY(industrial_power_from_states);
158+
memory::vector<std::pair<std::reference_wrapper<const CountryInstance>, fixed_point_t>> SPAN_PROPERTY(industrial_power_from_investments);
159159
size_t PROPERTY(industrial_rank, 0);
160-
fixed_point_map_t<CountryInstance const*> PROPERTY(foreign_investments);
160+
fixed_point_map_t<std::reference_wrapper<const CountryInstance>> PROPERTY(foreign_investments);
161161
OV_IFLATMAP_PROPERTY(BuildingType, building_level_t, building_type_unlock_levels);
162162
// TODO - total amount of each good produced
163163

@@ -345,7 +345,7 @@ namespace OpenVic {
345345
// The last time this country lost a war, i.e. accepted a peace offer sent from their offer tab or the enemy's demand
346346
// tab, even white peace. Used for the "has_recently_lost_war" condition (true if the date is less than 5 years ago).
347347
Date PROPERTY(last_war_loss_date);
348-
vector_ordered_set<CountryInstance const*> PROPERTY(war_enemies);
348+
vector_ordered_set<std::reference_wrapper<const CountryInstance>> PROPERTY(war_enemies);
349349
OV_STATE_PROPERTY(CountryInstance const*, sphere_owner, nullptr);
350350
// TODO - colonial power, current wars
351351

@@ -397,11 +397,10 @@ namespace OpenVic {
397397
memory::vector<technology_unlock_level_t> SPAN_PROPERTY(unit_variant_unlock_levels);
398398

399399
public:
400-
//pointers instead of references to allow construction via std::tuple
401400
CountryInstance(
402-
CountryDefinition const* new_country_definition,
403-
SharedCountryValues* new_shared_country_values,
404-
CountryInstanceDeps const* country_instance_deps
401+
CountryDefinition const& new_country_definition,
402+
SharedCountryValues& new_shared_country_values,
403+
CountryInstanceDeps const& country_instance_deps
405404
);
406405
CountryInstance(CountryInstance const&) = delete;
407406
CountryInstance& operator=(CountryInstance const&) = delete;

0 commit comments

Comments
 (0)