Skip to content

Commit 6bc573c

Browse files
committed
Refactor politcs and leftovers to use reference_wrapper
1 parent d6b9312 commit 6bc573c

38 files changed

Lines changed: 262 additions & 207 deletions

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/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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

src/openvic-simulation/country/CountryInstanceManager.cpp

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void CountryInstanceManager::update_rankings(const Date today) {
5252

5353
for (CountryInstance& country : get_country_instances()) {
5454
if (country.exists()) {
55-
total_ranking.push_back(&country);
55+
total_ranking.emplace_back(country);
5656
}
5757
}
5858

@@ -62,37 +62,37 @@ void CountryInstanceManager::update_rankings(const Date today) {
6262

6363
std::stable_sort(
6464
total_ranking.begin(), total_ranking.end(),
65-
[](CountryInstance* a, CountryInstance* b) -> bool {
66-
const bool a_civilised = a->is_civilised();
67-
const bool b_civilised = b->is_civilised();
68-
return a_civilised != b_civilised ? a_civilised : a->total_score.get_untracked() > b->total_score.get_untracked();
65+
[](CountryInstance& a, CountryInstance& b) -> bool {
66+
const bool a_civilised = a.is_civilised();
67+
const bool b_civilised = b.is_civilised();
68+
return a_civilised != b_civilised ? a_civilised : a.total_score.get_untracked() > b.total_score.get_untracked();
6969
}
7070
);
7171
std::stable_sort(
7272
prestige_ranking.begin(), prestige_ranking.end(),
73-
[](CountryInstance const* a, CountryInstance const* b) -> bool {
74-
return a->get_prestige_untracked() > b->get_prestige_untracked();
73+
[](CountryInstance const& a, CountryInstance const& b) -> bool {
74+
return a.get_prestige_untracked() > b.get_prestige_untracked();
7575
}
7676
);
7777
std::stable_sort(
7878
industrial_power_ranking.begin(), industrial_power_ranking.end(),
79-
[](CountryInstance const* a, CountryInstance const* b) -> bool {
80-
return a->get_industrial_power_untracked() > b->get_industrial_power_untracked();
79+
[](CountryInstance const& a, CountryInstance const& b) -> bool {
80+
return a.get_industrial_power_untracked() > b.get_industrial_power_untracked();
8181
}
8282
);
8383
std::stable_sort(
8484
military_power_ranking.begin(), military_power_ranking.end(),
85-
[](CountryInstance* a, CountryInstance* b) -> bool {
86-
return a->military_power.get_untracked() > b->military_power.get_untracked();
85+
[](CountryInstance& a, CountryInstance& b) -> bool {
86+
return a.military_power.get_untracked() > b.military_power.get_untracked();
8787
}
8888
);
8989

9090
for (size_t index = 0; index < total_ranking.size(); ++index) {
9191
const size_t rank = index + 1;
92-
total_ranking[index]->total_rank = rank;
93-
prestige_ranking[index]->prestige_rank = rank;
94-
industrial_power_ranking[index]->industrial_rank = rank;
95-
military_power_ranking[index]->military_rank = rank;
92+
total_ranking[index].get().total_rank = rank;
93+
prestige_ranking[index].get().prestige_rank = rank;
94+
industrial_power_ranking[index].get().industrial_rank = rank;
95+
military_power_ranking[index].get().military_rank = rank;
9696
}
9797

9898
const size_t max_great_power_rank = country_defines.get_great_power_rank();
@@ -103,12 +103,12 @@ void CountryInstanceManager::update_rankings(const Date today) {
103103
// Demote great powers who have been below the max great power rank for longer than the demotion grace period and
104104
// remove them from the list. We don't just demote them all and clear the list as when rebuilding we'd need to look
105105
// ahead for countries below the max great power rank but still within the demotion grace period.
106-
std::erase_if(great_powers, [max_great_power_rank, today](CountryInstance* great_power) -> bool {
107-
if (OV_likely(great_power->get_country_status() == COUNTRY_STATUS_GREAT_POWER)) {
106+
std::erase_if(great_powers, [max_great_power_rank, today](CountryInstance& great_power) -> bool {
107+
if (OV_likely(great_power.get_country_status() == COUNTRY_STATUS_GREAT_POWER)) {
108108
if (OV_unlikely(
109-
great_power->get_total_rank() > max_great_power_rank && great_power->get_lose_great_power_date() < today
109+
great_power.get_total_rank() > max_great_power_rank && great_power.get_lose_great_power_date() < today
110110
)) {
111-
great_power->country_status = COUNTRY_STATUS_CIVILISED;
111+
great_power.country_status = COUNTRY_STATUS_CIVILISED;
112112
return true;
113113
} else {
114114
return false;
@@ -119,9 +119,9 @@ void CountryInstanceManager::update_rankings(const Date today) {
119119

120120
// Demote all secondary powers and clear the list. We will rebuilt the whole list from scratch, so there's no need to
121121
// keep countries which are still above the max secondary power rank (they might become great powers instead anyway).
122-
for (CountryInstance* secondary_power : secondary_powers) {
123-
if (secondary_power->country_status == COUNTRY_STATUS_SECONDARY_POWER) {
124-
secondary_power->country_status = COUNTRY_STATUS_CIVILISED;
122+
for (CountryInstance& secondary_power : secondary_powers) {
123+
if (secondary_power.country_status == COUNTRY_STATUS_SECONDARY_POWER) {
124+
secondary_power.country_status = COUNTRY_STATUS_CIVILISED;
125125
}
126126
}
127127
secondary_powers.clear();
@@ -133,41 +133,41 @@ void CountryInstanceManager::update_rankings(const Date today) {
133133
const size_t max_power_index = std::clamp(max_secondary_power_rank, max_great_power_rank, total_ranking.size());
134134

135135
for (size_t index = 0; index < max_power_index; index++) {
136-
CountryInstance* country = total_ranking[index];
136+
CountryInstance& country = total_ranking[index];
137137

138-
if (!country->is_civilised()) {
138+
if (!country.is_civilised()) {
139139
// All further countries are civilised and so ineligible for great or secondary power status.
140140
break;
141141
}
142142

143-
if (country->is_great_power()) {
143+
if (country.is_great_power()) {
144144
// The country already has great power status and is in the great powers list.
145145
continue;
146146
}
147147

148-
if (great_powers.size() < max_great_power_rank && country->get_total_rank() <= max_great_power_rank) {
148+
if (great_powers.size() < max_great_power_rank && country.get_total_rank() <= max_great_power_rank) {
149149
// The country is eligible for great power status and there are still slots available,
150150
// so it is promoted and added to the list.
151-
country->country_status = COUNTRY_STATUS_GREAT_POWER;
151+
country.country_status = COUNTRY_STATUS_GREAT_POWER;
152152
great_powers.push_back(country);
153-
} else if (country->get_total_rank() <= max_secondary_power_rank) {
153+
} else if (country.get_total_rank() <= max_secondary_power_rank) {
154154
// The country is eligible for secondary power status and so is promoted and added to the list.
155-
country->country_status = COUNTRY_STATUS_SECONDARY_POWER;
156-
secondary_powers.push_back(country);
155+
country.country_status = COUNTRY_STATUS_SECONDARY_POWER;
156+
secondary_powers.emplace_back(country);
157157
}
158158
}
159159

160160
// Sort the great powers list by total rank, as pre-existing great powers may have changed rank order and new great
161161
// powers will have been added to the end of the list regardless of rank.
162-
std::stable_sort(great_powers.begin(), great_powers.end(), [](CountryInstance const* a, CountryInstance const* b) -> bool {
163-
return a->get_total_rank() < b->get_total_rank();
162+
std::stable_sort(great_powers.begin(), great_powers.end(), [](CountryInstance const& a, CountryInstance const& b) -> bool {
163+
return a.get_total_rank() < b.get_total_rank();
164164
});
165165

166166
// Update the lose great power date for all great powers which are above the max great power rank.
167167
const Date new_lose_great_power_date = today + lose_great_power_grace_days;
168-
for (CountryInstance* great_power : great_powers) {
169-
if (great_power->get_total_rank() <= max_great_power_rank) {
170-
great_power->lose_great_power_date = new_lose_great_power_date;
168+
for (CountryInstance& great_power : great_powers) {
169+
if (great_power.get_total_rank() <= max_great_power_rank) {
170+
great_power.lose_great_power_date = new_lose_great_power_date;
171171
}
172172
}
173173
}

src/openvic-simulation/country/CountryInstanceManager.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <functional>
34
#include <string_view>
45

56
#include "openvic-simulation/country/CountryInstance.hpp"
@@ -32,13 +33,13 @@ namespace OpenVic {
3233

3334
OV_IFLATMAP_PROPERTY(CountryDefinition, CountryInstance, country_instance_by_definition);
3435

35-
memory::vector<CountryInstance*> SPAN_PROPERTY(great_powers);
36-
memory::vector<CountryInstance*> SPAN_PROPERTY(secondary_powers);
36+
memory::vector<std::reference_wrapper<CountryInstance>> SPAN_PROPERTY(great_powers);
37+
memory::vector<std::reference_wrapper<CountryInstance>> SPAN_PROPERTY(secondary_powers);
3738

38-
memory::vector<CountryInstance*> SPAN_PROPERTY(total_ranking);
39-
memory::vector<CountryInstance*> SPAN_PROPERTY(prestige_ranking);
40-
memory::vector<CountryInstance*> SPAN_PROPERTY(industrial_power_ranking);
41-
memory::vector<CountryInstance*> SPAN_PROPERTY(military_power_ranking);
39+
memory::vector<std::reference_wrapper<CountryInstance>> SPAN_PROPERTY(total_ranking);
40+
memory::vector<std::reference_wrapper<CountryInstance>> SPAN_PROPERTY(prestige_ranking);
41+
memory::vector<std::reference_wrapper<CountryInstance>> SPAN_PROPERTY(industrial_power_ranking);
42+
memory::vector<std::reference_wrapper<CountryInstance>> SPAN_PROPERTY(military_power_ranking);
4243

4344
void update_rankings(const Date today);
4445

src/openvic-simulation/economy/BuildingType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ bool BuildingTypeManager::add_building_type(
135135
if (ret) {
136136
building_type_types.emplace(building_type_args.type);
137137
if (province_building_index.has_value()) {
138-
province_building_types.emplace_back(&building_types.back());
138+
province_building_types.emplace_back(building_types.back());
139139
}
140140
}
141141

src/openvic-simulation/economy/BuildingType.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <functional>
34
#include <optional>
45

56
#include "openvic-simulation/modifier/Modifier.hpp"
@@ -112,7 +113,7 @@ namespace OpenVic {
112113
private:
113114
IdentifierRegistry<BuildingType> IDENTIFIER_REGISTRY(building_type);
114115
string_set_t PROPERTY(building_type_types);
115-
memory::vector<BuildingType const*> SPAN_PROPERTY(province_building_types);
116+
memory::vector<std::reference_wrapper<const BuildingType>> SPAN_PROPERTY(province_building_types);
116117
BuildingType const* PROPERTY(infrastructure_building_type);
117118
BuildingType const* PROPERTY(port_building_type);
118119

src/openvic-simulation/economy/GoodDefinition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ bool GoodDefinitionManager::add_good_definition(
6666
identifier, colour, GoodDefinition::index_t { get_good_definition_count() }, category, base_price, is_available_from_start,
6767
is_tradeable, is_money, has_overseas_penalty
6868
)) {
69-
category.good_definitions.push_back(&get_back_good_definition());
69+
category.good_definitions.emplace_back(get_back_good_definition());
7070
return true;
7171
} else {
7272
return false;

src/openvic-simulation/economy/GoodDefinition.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <functional>
4+
35
#include "openvic-simulation/types/HasIdentifier.hpp"
46
#include "openvic-simulation/types/HasIndex.hpp"
57
#include "openvic-simulation/types/IdentifierRegistry.hpp"
@@ -14,7 +16,7 @@ namespace OpenVic {
1416
friend struct GoodDefinitionManager;
1517

1618
private:
17-
memory::vector<GoodDefinition const*> SPAN_PROPERTY(good_definitions);
19+
memory::vector<std::reference_wrapper<const GoodDefinition>> SPAN_PROPERTY(good_definitions);
1820
public:
1921
GoodCategory(std::string_view new_identifier);
2022
GoodCategory(GoodCategory&&) = default;

src/openvic-simulation/history/CountryHistory.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@ bool CountryHistoryMap::_load_history_entry(
9696
std::string_view key,
9797
ast::NodeCPtr value
9898
) -> bool {
99-
ReformGroup const* reform_group = issue_manager.get_reform_group_by_identifier(key);
100-
if (reform_group != nullptr) {
101-
return issue_manager.expect_reform_identifier([&entry, reform_group](Reform const& reform) -> bool {
102-
if (&reform.get_reform_group() != reform_group) {
99+
ReformGroup const* reform_group_ptr = issue_manager.get_reform_group_by_identifier(key);
100+
if (reform_group_ptr != nullptr) {
101+
ReformGroup const& reform_group = *reform_group_ptr;
102+
return issue_manager.expect_reform_identifier([&entry, &reform_group](Reform const& reform) -> bool {
103+
if (reform.group != reform_group) {
103104
spdlog::warn_s(
104105
"Listing {} as belonging to the reform group {} when it actually belongs to {} in history of {}",
105-
reform, *reform_group, reform.get_reform_group(), entry.country
106+
reform, reform_group, reform.group, entry.country
106107
);
107108
}
108109
return set_callback_pointer(entry.reforms)(reform);

0 commit comments

Comments
 (0)