Skip to content

Commit 94fd9bf

Browse files
committed
Refactor pop to use reference_wrapper
1 parent 43944c9 commit 94fd9bf

11 files changed

Lines changed: 74 additions & 85 deletions

File tree

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2458,7 +2458,7 @@ void CountryInstance::report_output(ProductionType const& production_type, const
24582458
}
24592459

24602460
void CountryInstance::request_salaries_and_welfare_and_import_subsidies(Pop& pop) {
2461-
PopType const& pop_type = *pop.get_type();
2461+
PopType const& pop_type = pop.get_type();
24622462
const pop_size_t pop_size = pop.get_size();
24632463
SharedPopTypeValues const& pop_type_values = shared_country_values.get_shared_pop_type_values(pop_type);
24642464

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void ArtisanalProducer::artisan_tick(
294294
memory::vector<fixed_point_t>& reusable_map_1,
295295
fixed_point_map_t<GoodDefinition const*>& goods_to_sell
296296
) {
297-
CountryInstance* const country_to_report_economy_nullable = pop.get_location()->get_country_to_report_economy();
297+
CountryInstance* const country_to_report_economy_nullable = pop.get_location().get_country_to_report_economy();
298298
max_quantity_to_buy_per_good.clear();
299299
IndexedFlatMap<GoodDefinition, char>& wants_more_mask = reusable_goods_mask;
300300
memory::vector<fixed_point_t>& max_price_per_input = reusable_map_0;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Employee::Employee(Pop& new_pop, const pop_size_t new_size)
1111
{}
1212

1313
fixed_point_t Employee::update_minimum_wage(CountryInstance& country_to_report_economy) {
14-
const fixed_point_t minimum_wage_base = country_to_report_economy.calculate_minimum_wage_base(*pop.get_type());
14+
const fixed_point_t minimum_wage_base = country_to_report_economy.calculate_minimum_wage_base(pop.get_type());
1515
return minimum_wage_cached = minimum_wage_base * size / Pop::size_denominator;
1616
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void ResourceGatheringOperation::hire() {
203203

204204
std::span<const Job> jobs = production_type.get_jobs();
205205
for (Pop& pop : location.get_mutable_pops()){
206-
PopType const& pop_type = *pop.get_type();
206+
PopType const& pop_type = pop.get_type();
207207
for (Job const& job : jobs) {
208208
PopType const* const job_pop_type = job.pop_type;
209209
if (job_pop_type && *job_pop_type == pop_type) {
@@ -424,13 +424,8 @@ void ResourceGatheringOperation::pay_employees(memory::vector<fixed_point_t>& re
424424
Employee& employee = employees[i];
425425
Pop& employee_pop = employee.get_pop();
426426

427-
PopType const* employee_pop_type = employee_pop.get_type();
428-
if (employee_pop_type == nullptr) {
429-
spdlog::error_s("employee has nullptr pop_type.");
430-
return;
431-
}
432-
433-
if (employee_pop_type->is_slave) {
427+
PopType const& employee_pop_type = employee_pop.get_type();
428+
if (employee_pop_type.is_slave) {
434429
continue;
435430
}
436431

src/openvic-simulation/map/ProvinceInstance.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,20 @@ bool ProvinceInstance::expand_building(
181181
return buildings[index].expand(modifier_effect_cache, actor, *this);
182182
}
183183

184-
void ProvinceInstance::_add_pop(Pop&& pop) {
185-
pop.set_location(*this);
186-
pops.insert(std::move(pop));
187-
}
188-
189184
bool ProvinceInstance::add_pop_vec(
190185
std::span<const PopBase> pop_vec,
191186
PopDeps const& pop_deps
192187
) {
193188
if (!province_definition.is_water()) {
194189
reserve_more(pops, pop_vec.size());
195190
for (PopBase const& pop : pop_vec) {
196-
_add_pop(Pop {
191+
pops.emplace(
192+
*this,
197193
pop,
198194
get_supporter_equivalents_by_ideology().get_keys(),
199195
pop_deps,
200196
++last_pop_id
201-
});
197+
);
202198
}
203199
return true;
204200
} else {
@@ -231,7 +227,7 @@ void ProvinceInstance::_update_pops(MilitaryDefines const& military_defines) {
231227
: is_owner_core() ? fixed_point_t::_1 : military_defines.get_pop_size_per_regiment_non_core_multiplier();
232228

233229
for (Pop& pop : pops) {
234-
pops_cache_by_type.at(*pop.get_type()).push_back(&pop);
230+
pops_cache_by_type.at(pop.get_type()).push_back(&pop);
235231
pop.update_gamestate(military_defines, owner, pop_size_per_regiment_multiplier);
236232
add_pops_aggregate(pop);
237233
if (pop.get_culture_status() == Pop::culture_status_t::UNACCEPTED) {
@@ -319,11 +315,16 @@ bool ProvinceInstance::convert_rgo_worker_pops_to_equivalent(ProductionType cons
319315
std::span<const Job> jobs = production_type.get_jobs();
320316
for (Pop& pop : pops) {
321317
for (Job const& job : jobs) {
322-
PopType const* const job_pop_type = job.pop_type;
323-
PopType const* old_pop_type = pop.get_type();
318+
PopType const* const job_pop_type_ptr = job.pop_type;
319+
if (job_pop_type_ptr == nullptr) {
320+
continue;
321+
}
322+
323+
PopType const& job_pop_type = *job_pop_type_ptr;
324+
PopType const& old_pop_type = pop.get_type();
324325
if (job_pop_type != old_pop_type) {
325-
PopType const* const equivalent = old_pop_type->get_equivalent();
326-
if (job_pop_type == equivalent) {
326+
PopType const* const equivalent_ptr = old_pop_type.get_equivalent();
327+
if (equivalent_ptr != nullptr && job_pop_type == *equivalent_ptr) {
327328
is_valid_operation&=pop.convert_to_equivalent();
328329
}
329330
}

src/openvic-simulation/map/ProvinceInstance.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ namespace OpenVic {
117117

118118
private:
119119
pop_id_in_province_t last_pop_id{0};
120-
memory::colony<Pop> PROPERTY(pops); // TODO - replace with a more easily vectorisable container?
121-
void _add_pop(Pop&& pop);
120+
memory::colony<Pop> PROPERTY(pops); // TODO - replace with a more easily vectorisable container?
122121
void _update_pops(MilitaryDefines const& military_defines);
123122
bool convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type);
124123
void initialise_rgo();

src/openvic-simulation/military/UnitInstanceGroup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Pop* UnitInstanceManager::recruit_pop_in(ProvinceInstance& province, const bool
288288
}
289289
} else {
290290
for (auto& pop : province.get_mutable_pops()) {
291-
if (pop.get_type()->can_be_recruited && pop.try_recruit()) {
291+
if (pop.get_type().can_be_recruited && pop.try_recruit()) {
292292
/*
293293
Victoria 2 does not respect cultural restrictions when applying history.
294294
*/
@@ -306,7 +306,7 @@ Pop* UnitInstanceManager::recruit_pop_in(ProvinceInstance& province, const bool
306306
}
307307
} else {
308308
for (auto& pop : province.get_mutable_pops()) {
309-
if (pop.get_type()->can_be_recruited && pop.try_recruit_understrength()) {
309+
if (pop.get_type().can_be_recruited && pop.try_recruit_understrength()) {
310310
/*
311311
Victoria 2 does not respect cultural restrictions when applying history.
312312
*/

src/openvic-simulation/population/Pop.cpp

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
#include <optional>
88
#include <ranges>
99

10+
#include <fmt/std.h>
1011
#include <type_safe/strong_typedef.hpp>
1112

1213
#include "openvic-simulation/core/error/ErrorMacros.hpp"
13-
#include "openvic-simulation/core/FormatValidate.hpp"
1414
#include "openvic-simulation/core/Typedefs.hpp"
1515
#include "openvic-simulation/country/CountryParty.hpp"
1616
#include "openvic-simulation/country/CountryDefinition.hpp"
@@ -49,20 +49,21 @@ using namespace OpenVic;
4949
PopBase::PopBase(
5050
PopType const& new_type, Culture const& new_culture, Religion const& new_religion, pop_size_t new_size,
5151
fixed_point_t new_militancy, fixed_point_t new_consciousness, RebelType const* new_rebel_type
52-
) : type { &new_type }, culture { new_culture }, religion { new_religion }, size { new_size }, militancy { new_militancy },
52+
) : type { new_type }, culture { new_culture }, religion { new_religion }, size { new_size }, militancy { new_militancy },
5353
consciousness { new_consciousness }, rebel_type { new_rebel_type } {}
5454

5555
Pop::Pop(
56+
ProvinceInstance& new_location,
5657
PopBase const& pop_base,
5758
decltype(supporter_equivalents_by_ideology)::keys_span_type ideology_keys,
5859
PopDeps const& pop_deps,
5960
const pop_id_in_province_t new_id_in_province
60-
)
61-
: PopBase { pop_base },
61+
) : PopBase { pop_base },
62+
location { new_location },
6263
id_in_province { new_id_in_province },
6364
market_instance { pop_deps.market_instance },
6465
artisanal_producer_optional {
65-
type->is_artisan
66+
pop_base.get_type().is_artisan
6667
? std::optional<ArtisanalProducer> {
6768
pop_deps.artisanal_producer_deps
6869
}
@@ -73,7 +74,7 @@ Pop::Pop(
7374
}
7475

7576
fixed_point_t Pop::get_unemployment_fraction() const {
76-
if (!type->can_be_unemployed) {
77+
if (!get_type().can_be_unemployed) {
7778
return 0;
7879
}
7980
return fixed_point_t::from_fraction(get_unemployed(), size);
@@ -160,31 +161,21 @@ void Pop::setup_pop_test_values(IssueManager const& issue_manager) {
160161
}
161162

162163
bool Pop::convert_to_equivalent() {
163-
PopType const* const equivalent = get_type()->get_equivalent();
164+
PopType const& pop_type = type.get();
165+
PopType const* const equivalent = pop_type.get_equivalent();
164166
if (equivalent == nullptr) {
165-
spdlog::error_s("Tried to convert pop of type {} to equivalent, but there is no equivalent.", *get_type());
167+
spdlog::error_s("Tried to convert pop of type {} to equivalent, but there is no equivalent.", pop_type);
166168
return false;
167169
}
168170

169-
type = equivalent;
171+
type = *equivalent;
170172
reserve_needs_fulfilled_goods();
171173
return true;
172174
}
173175

174-
void Pop::set_location(ProvinceInstance& new_location) {
175-
if (location != &new_location) {
176-
location = &new_location;
177-
178-
update_location_based_attributes();
179-
}
180-
}
181-
182176
void Pop::update_location_based_attributes() {
183177
vote_equivalents_by_party.clear();
184-
if (location == nullptr) {
185-
return;
186-
}
187-
CountryInstance const* owner = location->get_owner();
178+
CountryInstance const* owner = get_location().get_owner();
188179
if (owner == nullptr) {
189180
return;
190181
}
@@ -266,24 +257,24 @@ void Pop::update_gamestate(
266257
memory::string Pop::get_pop_context_text() const {
267258
return memory::fmt::format(
268259
"location: {} type: {} culture: {} religion: {} size: {}",
269-
ovfmt::validate(location), *type, culture, religion, size
260+
location, type, culture, religion, size
270261
);
271262
}
272263

273264
void Pop::reserve_needs_fulfilled_goods() {
274-
PopType const& type_never_null = *type;
265+
PopType const& pop_type = type.get();
275266
#define RESERVE_NEEDS(need_category) \
276-
need_category##_needs_fulfilled_goods.reserve(type_never_null.get_##need_category##_needs().size());
267+
need_category##_needs_fulfilled_goods.reserve(pop_type.get_##need_category##_needs().size());
277268

278269
OV_DO_FOR_ALL_NEED_CATEGORIES(RESERVE_NEEDS)
279270
#undef RESERVE_NEEDS
280271
}
281272

282273
void Pop::fill_needs_fulfilled_goods_with_false() {
283-
PopType const& type_never_null = *type;
274+
PopType const& pop_type = type.get();
284275
#define FILL_WITH_FALSE(need_category) \
285276
need_category##_needs_fulfilled_goods.clear(); \
286-
for (auto [good, base_demand] : type_never_null.get_##need_category##_needs()) { \
277+
for (auto [good, base_demand] : pop_type.get_##need_category##_needs()) { \
287278
need_category##_needs_fulfilled_goods.emplace(good, false); \
288279
}
289280

@@ -292,13 +283,13 @@ void Pop::fill_needs_fulfilled_goods_with_false() {
292283
}
293284

294285
void Pop::pay_income_tax(fixed_point_t& income) {
295-
CountryInstance* const tax_collector_nullable = location->get_country_to_report_economy();
286+
CountryInstance* const tax_collector_nullable = get_location().get_country_to_report_economy();
296287
if (tax_collector_nullable == nullptr) {
297288
return;
298289
}
299-
const fixed_point_t effective_tax_rate = tax_collector_nullable->get_effective_tax_rate_by_strata(type->strata).get_untracked();
290+
const fixed_point_t effective_tax_rate = tax_collector_nullable->get_effective_tax_rate_by_strata(get_type().strata).get_untracked();
300291
const fixed_point_t tax = effective_tax_rate * income;
301-
tax_collector_nullable->report_pop_income_tax(*type, income, tax);
292+
tax_collector_nullable->report_pop_income_tax(type, income, tax);
302293
income -= tax;
303294
}
304295

@@ -532,8 +523,7 @@ void Pop::pop_tick_without_cleanup(
532523
#undef SET_TO_ZERO
533524
income = expenses = 0;
534525

535-
ProvinceInstance& location_never_null = *location;
536-
CountryInstance* const country_to_report_economy_nullable = location_never_null.get_country_to_report_economy();
526+
CountryInstance* const country_to_report_economy_nullable = get_location().get_country_to_report_economy();
537527

538528
if (country_to_report_economy_nullable != nullptr) {
539529
country_to_report_economy_nullable->request_salaries_and_welfare_and_import_subsidies(*this);
@@ -544,8 +534,8 @@ void Pop::pop_tick_without_cleanup(
544534
//import subsidies are based on yesterday
545535
yesterdays_import_value = 0;
546536

547-
PopType const& type_never_null = *type;
548-
PopStrataValuesFromProvince const& shared_strata_values = shared_values.get_effects_by_strata(type_never_null.strata);
537+
PopType const& pop_type = type;
538+
PopStrataValuesFromProvince const& shared_strata_values = shared_values.get_effects_by_strata(pop_type.strata);
549539
PopsDefines const& defines = shared_values.defines;
550540
const fixed_point_t base_needs_scalar = (
551541
fixed_point_t::_1 + 2 * consciousness / defines.get_pdef_base_con()
@@ -557,7 +547,7 @@ void Pop::pop_tick_without_cleanup(
557547
fixed_point_t need_category##_needs_price_inverse_sum = 0; \
558548
if (OV_likely(need_category##_needs_scalar > 0)) { \
559549
need_category##_needs_acquired_quantity = need_category##_needs_desired_quantity = 0; \
560-
for (auto [good_definition_ptr, quantity] : type_never_null.get_##need_category##_needs()) { \
550+
for (auto [good_definition_ptr, quantity] : pop_type.get_##need_category##_needs()) { \
561551
GoodDefinition const& good_definition = *good_definition_ptr; \
562552
if (!market_instance.get_is_available(good_definition)) { \
563553
continue; \
@@ -567,7 +557,7 @@ void Pop::pop_tick_without_cleanup(
567557
continue; \
568558
} \
569559
if (country_to_report_economy_nullable != nullptr) { \
570-
country_to_report_economy_nullable->report_pop_need_demand(*type, good_definition, max_quantity_to_buy); \
560+
country_to_report_economy_nullable->report_pop_need_demand(pop_type, good_definition, max_quantity_to_buy); \
571561
} \
572562
need_category##_needs_desired_quantity += max_quantity_to_buy; \
573563
auto goods_to_sell_iterator = goods_to_sell.find(good_definition_ptr); \
@@ -577,7 +567,7 @@ void Pop::pop_tick_without_cleanup(
577567
max_quantity_to_buy -= own_produce_consumed; \
578568
need_category##_needs_acquired_quantity += own_produce_consumed; \
579569
if (country_to_report_economy_nullable != nullptr) { \
580-
country_to_report_economy_nullable->report_pop_need_consumption(type_never_null, good_definition, own_produce_consumed); \
570+
country_to_report_economy_nullable->report_pop_need_consumption(pop_type, good_definition, own_produce_consumed); \
581571
} \
582572
} \
583573
if (OV_likely(max_quantity_to_buy > 0)) { \
@@ -664,8 +654,7 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) {
664654
}
665655

666656
Pop& pop = *static_cast<Pop*>(actor);
667-
ProvinceInstance& location_never_null = *pop.get_location();
668-
CountryInstance* const country_to_report_economy_nullable = location_never_null.get_country_to_report_economy();
657+
CountryInstance* const country_to_report_economy_nullable = pop.get_location().get_country_to_report_economy();
669658

670659
fixed_point_t money_spent = buy_result.money_spent_total;
671660
pop.yesterdays_import_value += buy_result.money_spent_on_imports;
@@ -696,7 +685,7 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) {
696685
}
697686
}
698687

699-
CountryInstance* get_country_to_report_economy_nullable = pop.location->get_country_to_report_economy();
688+
CountryInstance* get_country_to_report_economy_nullable = pop.get_location().get_country_to_report_economy();
700689
#define CONSUME_NEED(need_category) \
701690
if (quantity_left_to_consume <= 0) { \
702691
return; \
@@ -714,7 +703,7 @@ void Pop::after_buy(void* actor, BuyResult const& buy_result) {
714703
pop.need_category##_needs_acquired_quantity += consumed_quantity; \
715704
quantity_left_to_consume -= consumed_quantity; \
716705
if (get_country_to_report_economy_nullable != nullptr) { \
717-
get_country_to_report_economy_nullable->report_pop_need_consumption(*pop.type, good_definition, consumed_quantity); \
706+
get_country_to_report_economy_nullable->report_pop_need_consumption(pop.type, good_definition, consumed_quantity); \
718707
} \
719708
const fixed_point_t expense = fixed_point_t::mul_div( \
720709
money_spent, \

0 commit comments

Comments
 (0)