Skip to content

Commit fda5ee6

Browse files
committed
Refactor region to use reference_wrapper
1 parent 84bb30c commit fda5ee6

11 files changed

Lines changed: 91 additions & 88 deletions

src/openvic-simulation/economy/BuildingType.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ bool BuildingType::can_be_built_in(
8989
if (is_limited_to_one_per_state) {
9090
State const* state = location.get_state();
9191
if (state != nullptr) {
92-
for (ProvinceInstance const* province_in_state : state->get_provinces()) {
93-
if (province_in_state == nullptr || *province_in_state == location) {
92+
for (ProvinceInstance const& province_in_state : state->get_provinces()) {
93+
if (province_in_state == location) {
9494
continue;
9595
}
9696

97-
const building_level_t other_building_level = province_in_state->get_buildings()[province_building_index.value()].get_level();
97+
const building_level_t other_building_level = province_in_state.get_buildings()[province_building_index.value()].get_level();
9898
if (other_building_level > building_level_t(0)) {
9999
return false;
100100
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ void ResourceGatheringOperation::pay_employees(memory::vector<fixed_point_t>& re
400400
upper_limit
401401
);
402402

403-
for (Pop* owner_pop_ptr : *owner_pops_cache_nullable) {
404-
Pop& owner_pop = *owner_pop_ptr;
403+
for (Pop& owner_pop : *owner_pops_cache_nullable) {
405404
const fixed_point_t income_for_this_pop = std::max(
406405
revenue_left * owner_share.mul_div<pop_sum_t>(owner_pop.get_size(), total_owner_count_in_state_cache),
407406
fixed_point_t::epsilon //revenue > 0 is already checked, so rounding up

src/openvic-simulation/economy/production/ResourceGatheringOperation.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/economy/production/Employee.hpp"
46
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
57
#include "openvic-simulation/types/IndexedFlatMap.hpp"
@@ -25,7 +27,7 @@ namespace OpenVic {
2527
ProvinceInstance* location_ptr = nullptr;
2628
pop_sum_t total_owner_count_in_state_cache = 0;
2729
pop_sum_t total_worker_count_in_province_cache = 0;
28-
memory::vector<Pop*> const* owner_pops_cache_nullable = nullptr;
30+
memory::vector<std::reference_wrapper<Pop>> const* owner_pops_cache_nullable = nullptr;
2931

3032
ProductionType const* PROPERTY_RW(production_type_nullable);
3133
fixed_point_t PROPERTY(revenue_yesterday);

src/openvic-simulation/map/MapDefinition.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,26 @@ bool MapDefinition::set_water_province(std::string_view identifier) {
371371
return false;
372372
}
373373

374-
ProvinceDefinition* province = get_province_definition_by_identifier(identifier);
374+
ProvinceDefinition* const province_ptr = get_province_definition_by_identifier(identifier);
375375

376-
if (province == nullptr) {
376+
if (province_ptr == nullptr) {
377377
spdlog::error_s("Unrecognised water province identifier: {}", identifier);
378378
return false;
379379
}
380-
if (province->is_water()) {
380+
ProvinceDefinition& province = *province_ptr;
381+
if (province.is_water()) {
381382
spdlog::warn_s("Province {} is already a water province!", identifier);
382383
return true;
383384
}
384385
if (!water_provinces.add_province(province)) {
385386
spdlog::error_s("Failed to add province {} to water province set!", identifier);
386387
return false;
387388
}
388-
province->water = true;
389-
path_map_sea.try_add_point(province->get_province_number(), path_map_land.get_point_position(province->get_province_number()));
389+
province.water = true;
390+
path_map_sea.try_add_point(
391+
province.get_province_number(),
392+
path_map_land.get_point_position(province.get_province_number())
393+
);
390394
return true;
391395
}
392396

@@ -417,7 +421,11 @@ size_t MapDefinition::get_water_province_count() const {
417421
return water_provinces.size();
418422
}
419423

420-
bool MapDefinition::add_region(std::string_view identifier, memory::vector<ProvinceDefinition const*>&& provinces, colour_t colour) {
424+
bool MapDefinition::add_region(
425+
std::string_view identifier,
426+
memory::vector<std::reference_wrapper<const ProvinceDefinition>>&& provinces,
427+
colour_t colour
428+
) {
421429
if (identifier.empty()) {
422430
spdlog::error_s("Invalid region identifier - empty!");
423431
return false;
@@ -428,8 +436,7 @@ bool MapDefinition::add_region(std::string_view identifier, memory::vector<Provi
428436
// mods like DoD + TGC use meta regions of water provinces
429437
bool is_meta = provinces.empty();
430438
size_t valid_provinces_count { provinces.size() };
431-
for (ProvinceDefinition const* const province_definition_ptr : provinces) {
432-
ProvinceDefinition const& province_definition = *province_definition_ptr;
439+
for (ProvinceDefinition const& province_definition : provinces) {
433440
if (OV_unlikely(province_definition.has_region())) {
434441
spdlog::warn_s(
435442
"Province {} is assigned to multiple regions, including {} and {}. First defined region wins.",
@@ -456,9 +463,9 @@ bool MapDefinition::add_region(std::string_view identifier, memory::vector<Provi
456463
ret &= region.add_provinces(provinces);
457464
} else {
458465
region.reserve(valid_provinces_count);
459-
for (ProvinceDefinition const* const province_definition_ptr : provinces) {
460-
if (!province_definition_ptr->has_region()) {
461-
region.add_province(province_definition_ptr);
466+
for (ProvinceDefinition const& province_definition : provinces) {
467+
if (!province_definition.has_region()) {
468+
region.add_province(province_definition);
462469
}
463470
}
464471
}
@@ -467,8 +474,8 @@ bool MapDefinition::add_region(std::string_view identifier, memory::vector<Provi
467474
if (OV_unlikely(is_meta)) {
468475
SPDLOG_INFO("Region {} is meta.", identifier);
469476
} else {
470-
for (ProvinceDefinition const* province_definition : region.get_provinces()) {
471-
remove_province_definition_const(province_definition)->region = &region;
477+
for (ProvinceDefinition const& province_definition : region.get_provinces()) {
478+
get_mutable_province_definition(province_definition).region = &region;
472479
}
473480
}
474481
return ret;
@@ -640,10 +647,10 @@ bool MapDefinition::load_region_file(ast::NodeCPtr root, std::span<const colour_
640647
const bool ret = expect_dictionary_reserve_length(
641648
regions,
642649
[this, &colours](std::string_view region_identifier, ast::NodeCPtr region_node) -> bool {
643-
memory::vector<ProvinceDefinition const*> provinces;
650+
memory::vector<std::reference_wrapper<const ProvinceDefinition>> provinces;
644651

645652
bool ret = expect_list_reserve_length(
646-
provinces, expect_province_definition_identifier(vector_callback_pointer(provinces))
653+
provinces, expect_province_definition_identifier(vector_emplace_callback(provinces))
647654
)(region_node);
648655

649656
ret &= add_region(region_identifier, std::move(provinces), colours[regions.size() % colours.size()]);
@@ -1124,10 +1131,10 @@ bool MapDefinition::load_climate_file(ModifierManager const& modifier_manager, a
11241131
ret &= expect_list_reserve_length(*cur_climate, expect_province_definition_identifier(
11251132
[cur_climate, &identifier](ProvinceDefinition& province) {
11261133
if (province.climate != cur_climate) {
1127-
cur_climate->add_province(&province);
1134+
cur_climate->add_province(province);
11281135
if (province.climate != nullptr) {
11291136
Climate* old_climate = const_cast<Climate*>(province.climate);
1130-
old_climate->remove_province(&province);
1137+
old_climate->remove_province(province);
11311138
spdlog::warn_s(
11321139
"Province with id {} found in multiple climates: {} and {}",
11331140
province, identifier, *old_climate
@@ -1168,13 +1175,13 @@ bool MapDefinition::load_continent_file(ModifierManager const& modifier_manager,
11681175
}
11691176

11701177
ModifierValue values;
1171-
memory::vector<ProvinceDefinition const*> prov_list;
1178+
memory::vector<std::reference_wrapper<const ProvinceDefinition>> prov_list;
11721179
bool ret = NodeTools::expect_dictionary_keys_and_default(
11731180
modifier_manager.expect_base_province_modifier(values),
11741181
"provinces", ONE_EXACTLY, expect_list_reserve_length(prov_list, expect_province_definition_identifier(
11751182
[&prov_list](ProvinceDefinition const& province) -> bool {
11761183
if (province.continent == nullptr) {
1177-
prov_list.emplace_back(&province);
1184+
prov_list.emplace_back(province);
11781185
} else {
11791186
spdlog::warn_s("Province {} found in multiple continents", province);
11801187
}
@@ -1194,8 +1201,8 @@ bool MapDefinition::load_continent_file(ModifierManager const& modifier_manager,
11941201
continent.add_provinces(prov_list);
11951202
continent.lock();
11961203

1197-
for (ProvinceDefinition const* prov : continent.get_provinces()) {
1198-
remove_province_definition_const(prov)->continent = &continent;
1204+
for (ProvinceDefinition const& prov : continent.get_provinces()) {
1205+
get_mutable_province_definition(prov).continent = &continent;
11991206
}
12001207

12011208
return ret;

src/openvic-simulation/map/MapDefinition.hpp

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

33
#include <cstdint>
44
#include <filesystem>
5+
#include <functional>
56
#include <string_view>
67

78
#include <openvic-dataloader/csv/LineObject.hpp>
@@ -114,20 +115,23 @@ namespace OpenVic {
114115
private:
115116
ProvinceDefinition* get_province_definition_at(ivec2_t pos);
116117

117-
/* This provides a safe way to remove the const qualifier of a ProvinceDefinition const*, via a non-const Map.
118-
* It uses a const_cast (the fastest/simplest solution), but this could also be done without it by looking up the
119-
* ProvinceDefinition* using the ProvinceDefinition const*'s index. Requiring a non-const Map ensures that this
120-
* function can only be used where the ProvinceDefinition* could already be accessed by other means, such as the
118+
/* This provides a safe way to remove the const qualifier of a ProvinceDefinition const&, via a non-const Map.
119+
* It looks up the ProvinceDefinition& using the ProvinceDefinition const&'s index. Requiring a non-const Map ensures that this
120+
* function can only be used where the ProvinceDefinition& could already be accessed by other means, such as the
121121
* index method, preventing misleading code, or in the worst case undefined behaviour. */
122-
constexpr ProvinceDefinition* remove_province_definition_const(ProvinceDefinition const* province) {
123-
return const_cast<ProvinceDefinition*>(province);
122+
constexpr ProvinceDefinition& get_mutable_province_definition(ProvinceDefinition const& province) {
123+
return *province_definitions.get_item_by_index(province.index);
124124
}
125125

126126
public:
127127
ProvinceDefinition const* get_province_definition_at(ivec2_t pos) const;
128128
bool set_max_provinces(ProvinceDefinition::index_t new_max_provinces);
129129

130-
bool add_region(std::string_view identifier, memory::vector<ProvinceDefinition const*>&& provinces, colour_t colour);
130+
bool add_region(
131+
std::string_view identifier,
132+
memory::vector<std::reference_wrapper<const ProvinceDefinition>>&& provinces,
133+
colour_t colour
134+
);
131135

132136
bool load_province_definitions(std::span<const ovdl::csv::LineObject> lines);
133137
/* Must be loaded after adjacencies so we know what provinces are coastal, and so can have a port */

src/openvic-simulation/map/ProvinceInstance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ModifierSum const& ProvinceInstance::get_owner_modifier_sum() const {
5353
return owner->get_modifier_sum();
5454
}
5555

56-
memory::vector<Pop*> const& ProvinceInstance::get_pops_cache_by_type(PopType const& key) const {
56+
memory::vector<std::reference_wrapper<Pop>> const& ProvinceInstance::get_pops_cache_by_type(PopType const& key) const {
5757
return pops_cache_by_type.at(key);
5858
}
5959

@@ -215,7 +215,7 @@ void ProvinceInstance::_update_pops(MilitaryDefines const& military_defines) {
215215

216216
has_unaccepted_pops = false;
217217

218-
for (memory::vector<Pop*>& pops_cache : pops_cache_by_type.get_values()) {
218+
for (memory::vector<std::reference_wrapper<Pop>>& pops_cache : pops_cache_by_type.get_values()) {
219219
pops_cache.clear();
220220
}
221221

@@ -227,7 +227,7 @@ void ProvinceInstance::_update_pops(MilitaryDefines const& military_defines) {
227227
: is_owner_core() ? fixed_point_t::_1 : military_defines.get_pop_size_per_regiment_non_core_multiplier();
228228

229229
for (Pop& pop : pops) {
230-
pops_cache_by_type.at(pop.get_type()).push_back(&pop);
230+
pops_cache_by_type.at(pop.get_type()).push_back(pop);
231231
pop.update_gamestate(military_defines, owner, pop_size_per_regiment_multiplier);
232232
add_pops_aggregate(pop);
233233
if (pop.get_culture_status() == Pop::culture_status_t::UNACCEPTED) {

src/openvic-simulation/map/ProvinceInstance.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 <plf_colony.h>
46

57
#include "openvic-simulation/core/portable/ForwardableSpan.hpp"
@@ -122,7 +124,7 @@ namespace OpenVic {
122124
bool convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type);
123125
void initialise_rgo();
124126

125-
OV_IFLATMAP_PROPERTY(PopType, memory::vector<Pop*>, pops_cache_by_type);
127+
OV_IFLATMAP_PROPERTY(PopType, memory::vector<std::reference_wrapper<Pop>>, pops_cache_by_type);
126128
public:
127129
ProvinceDefinition const& province_definition;
128130

src/openvic-simulation/map/Region.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@
55

66
using namespace OpenVic;
77

8-
bool ProvinceSet::add_province(ProvinceDefinition const* province) {
8+
bool ProvinceSet::add_province(ProvinceDefinition const& province) {
99
if (locked) {
1010
spdlog::error_s("Cannot add province to province set - locked!");
1111
return false;
1212
}
1313

14-
if (province == nullptr) {
15-
spdlog::error_s("Cannot add province to province set - null province!");
16-
return false;
17-
}
18-
1914
if (contains_province(province)) {
20-
spdlog::warn_s("Cannot add province {} to province set - already in the set!", *province);
15+
spdlog::warn_s("Cannot add province {} to province set - already in the set!", province);
2116
return false;
2217
}
2318

@@ -26,20 +21,15 @@ bool ProvinceSet::add_province(ProvinceDefinition const* province) {
2621
return true;
2722
}
2823

29-
bool ProvinceSet::remove_province(ProvinceDefinition const* province) {
24+
bool ProvinceSet::remove_province(ProvinceDefinition const& province) {
3025
if (locked) {
3126
spdlog::error_s("Cannot remove province from province set - locked!");
3227
return false;
3328
}
3429

35-
if (province == nullptr) {
36-
spdlog::error_s("Cannot remove province from province set - null province!");
37-
return false;
38-
}
39-
4030
const decltype(provinces)::const_iterator it = std::find(provinces.begin(), provinces.end(), province);
4131
if (it == provinces.end()) {
42-
spdlog::warn_s("Cannot remove province {} from province set - not in the set!", *province);
32+
spdlog::warn_s("Cannot remove province {} from province set - not in the set!", province);
4333
return false;
4434
}
4535

@@ -92,8 +82,8 @@ void ProvinceSet::reserve_more(size_t size) {
9282
OpenVic::reserve_more(*this, size);
9383
}
9484

95-
bool ProvinceSet::contains_province(ProvinceDefinition const* province) const {
96-
return province != nullptr && std::find(provinces.begin(), provinces.end(), province) != provinces.end();
85+
bool ProvinceSet::contains_province(ProvinceDefinition const& province) const {
86+
return std::find(provinces.begin(), provinces.end(), province) != provinces.end();
9787
}
9888

9989
ProvinceSetModifier::ProvinceSetModifier(std::string_view new_identifier, ModifierValue&& new_values, modifier_type_t new_type)

src/openvic-simulation/map/Region.hpp

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

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

@@ -12,29 +13,28 @@ namespace OpenVic {
1213

1314
struct ProvinceSet {
1415
private:
15-
memory::vector<ProvinceDefinition const*> SPAN_PROPERTY(provinces);
16+
memory::vector<std::reference_wrapper<const ProvinceDefinition>> SPAN_PROPERTY(provinces);
1617
bool locked = false;
1718

1819
public:
1920
/* Returns true if the province is successfully added, false if not (including if it's already in the set). */
20-
bool add_province(ProvinceDefinition const* province);
21+
bool add_province(ProvinceDefinition const& province);
2122

2223
template<std::ranges::sized_range Container>
23-
requires std::convertible_to<std::ranges::range_value_t<Container>, ProvinceDefinition const*>
24+
requires std::convertible_to<std::ranges::range_value_t<Container>, std::reference_wrapper<const ProvinceDefinition>>
2425
bool add_provinces(Container const& new_provinces) {
2526
reserve_more(new_provinces.size());
2627

2728
bool ret = true;
28-
29-
for (ProvinceDefinition const* province : new_provinces) {
29+
for (ProvinceDefinition const& province : new_provinces) {
3030
ret &= add_province(province);
3131
}
3232

3333
return ret;
3434
}
3535

3636
/* Returns true if the province is successfully removed, false if not (including if it's not in the set). */
37-
bool remove_province(ProvinceDefinition const* province);
37+
bool remove_province(ProvinceDefinition const& province);
3838
void lock(bool log = false);
3939
bool is_locked() const;
4040
void reset();
@@ -43,7 +43,7 @@ namespace OpenVic {
4343
size_t capacity() const;
4444
void reserve(size_t size);
4545
void reserve_more(size_t size);
46-
bool contains_province(ProvinceDefinition const* province) const;
46+
bool contains_province(ProvinceDefinition const& province) const;
4747
};
4848

4949
struct ProvinceSetModifier : Modifier, ProvinceSet {

0 commit comments

Comments
 (0)