Skip to content

Commit 268a694

Browse files
committed
Added reserve_more, expect_dictionary_key[s|_map]_reserve_length[_and_default]
1 parent d4e597d commit 268a694

Some content is hidden

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

41 files changed

+517
-346
lines changed

src/openvic-simulation/dataloader/Dataloader.cpp

Lines changed: 138 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ bool Dataloader::_load_interface_files(UIManager& ui_manager) const {
287287
static const std::vector<std::string_view> gui_files {
288288
"province_interface.gui", "topbar.gui"
289289
};
290+
291+
ui_manager.reserve_more_scenes(gui_files.size());
292+
290293
for (std::string_view const& gui_file : gui_files) {
291294
if (!ui_manager.load_gui_file(
292295
gui_file, parse_defines(lookup_file(append_string_views(interface_directory, gui_file))).get_file_node()
@@ -307,8 +310,10 @@ bool Dataloader::_load_pop_types(GameManager& game_manager) {
307310
IdeologyManager const& ideology_manager = game_manager.get_politics_manager().get_ideology_manager();
308311

309312
static constexpr std::string_view pop_type_directory = "poptypes";
313+
310314
const path_vector_t pop_type_files = lookup_files_in_dir(pop_type_directory, ".txt");
311-
pop_manager.reserve_pop_types(pop_type_files.size());
315+
316+
pop_manager.reserve_all_pop_types(pop_type_files.size());
312317

313318
bool ret = apply_to_files(
314319
pop_type_files,
@@ -318,8 +323,8 @@ bool Dataloader::_load_pop_types(GameManager& game_manager) {
318323
);
319324
}
320325
);
321-
pop_manager.lock_stratas();
322-
pop_manager.lock_pop_types();
326+
327+
pop_manager.lock_all_pop_types();
323328

324329
if (pop_manager.get_slave_sprite() <= 0) {
325330
Logger::error("No slave pop type sprite found!");
@@ -343,8 +348,12 @@ bool Dataloader::_load_units(GameManager& game_manager) const {
343348

344349
UnitManager& unit_manager = game_manager.get_military_manager().get_unit_manager();
345350

351+
const path_vector_t unit_files = lookup_files_in_dir(units_directory, ".txt");
352+
353+
unit_manager.reserve_more_units(unit_files.size());
354+
346355
bool ret = apply_to_files(
347-
lookup_files_in_dir(units_directory, ".txt"),
356+
unit_files,
348357
[&game_manager, &unit_manager](fs::path const& file) -> bool {
349358
return unit_manager.load_unit_file(
350359
game_manager.get_economy_manager().get_good_manager(), parse_defines(file).get_file_node()
@@ -481,112 +490,152 @@ bool Dataloader::_load_decisions(GameManager& game_manager) {
481490

482491
bool Dataloader::_load_history(GameManager& game_manager, bool unused_history_file_warnings) const {
483492

484-
/* Country History */
485-
static constexpr std::string_view country_history_directory = "history/countries";
486-
bool ret = apply_to_files(
487-
lookup_basic_indentifier_prefixed_files_in_dir(country_history_directory, ".txt"),
488-
[this, &game_manager, unused_history_file_warnings](fs::path const& file) -> bool {
489-
const std::string filename = file.stem().string();
490-
const std::string_view country_id = extract_basic_identifier_prefix(filename);
491-
492-
Country const* country = game_manager.get_country_manager().get_country_by_identifier(country_id);
493-
if (country == nullptr) {
494-
if (unused_history_file_warnings) {
495-
Logger::warning("Found history file for non-existent country: ", country_id);
493+
bool ret = true;
494+
495+
{
496+
/* Country History */
497+
CountryHistoryManager& country_history_manager = game_manager.get_history_manager().get_country_manager();
498+
499+
static constexpr std::string_view country_history_directory = "history/countries";
500+
const path_vector_t country_history_files =
501+
lookup_basic_indentifier_prefixed_files_in_dir(country_history_directory, ".txt");
502+
503+
country_history_manager.reserve_more_country_histories(country_history_files.size());
504+
505+
ret &= apply_to_files(
506+
country_history_files,
507+
[this, &game_manager, &country_history_manager, unused_history_file_warnings](fs::path const& file) -> bool {
508+
const std::string filename = file.stem().string();
509+
const std::string_view country_id = extract_basic_identifier_prefix(filename);
510+
511+
Country const* country = game_manager.get_country_manager().get_country_by_identifier(country_id);
512+
if (country == nullptr) {
513+
if (unused_history_file_warnings) {
514+
Logger::warning("Found history file for non-existent country: ", country_id);
515+
}
516+
return true;
496517
}
497-
return true;
518+
519+
return country_history_manager.load_country_history_file(
520+
game_manager, *this, *country, parse_defines(file).get_file_node()
521+
);
498522
}
523+
);
499524

500-
return game_manager.get_history_manager().get_country_manager().load_country_history_file(
501-
game_manager, *this, *country, parse_defines(file).get_file_node()
502-
);
503-
}
504-
);
505-
game_manager.get_history_manager().get_country_manager().lock_country_histories();
525+
country_history_manager.lock_country_histories();
526+
}
506527

507528
{
508529
DeploymentManager& deployment_manager = game_manager.get_military_manager().get_deployment_manager();
530+
509531
deployment_manager.lock_deployments();
532+
510533
if (deployment_manager.get_missing_oob_file_count() > 0) {
511534
Logger::warning(deployment_manager.get_missing_oob_file_count(), " missing OOB files!");
512535
}
513536
}
514537

515-
/* Province History */
516-
static constexpr std::string_view province_history_directory = "history/provinces";
517-
ret &= apply_to_files(
518-
lookup_basic_indentifier_prefixed_files_in_dir_recursive(province_history_directory, ".txt"),
519-
[this, &game_manager, unused_history_file_warnings](fs::path const& file) -> bool {
520-
const std::string filename = file.stem().string();
521-
const std::string_view province_id = extract_basic_identifier_prefix(filename);
522-
523-
Province const* province = game_manager.get_map().get_province_by_identifier(province_id);
524-
if (province == nullptr) {
525-
if (unused_history_file_warnings) {
526-
Logger::warning("Found history file for non-existent province: ", province_id);
538+
{
539+
/* Province History */
540+
ProvinceHistoryManager& province_history_manager = game_manager.get_history_manager().get_province_manager();
541+
Map const& map = game_manager.get_map();
542+
543+
static constexpr std::string_view province_history_directory = "history/provinces";
544+
const path_vector_t province_history_files =
545+
lookup_basic_indentifier_prefixed_files_in_dir_recursive(province_history_directory, ".txt");
546+
547+
province_history_manager.reserve_more_province_histories(province_history_files.size());
548+
549+
ret &= apply_to_files(
550+
province_history_files,
551+
[this, &game_manager, &province_history_manager, &map, unused_history_file_warnings](fs::path const& file) -> bool {
552+
const std::string filename = file.stem().string();
553+
const std::string_view province_id = extract_basic_identifier_prefix(filename);
554+
555+
Province const* province = map.get_province_by_identifier(province_id);
556+
if (province == nullptr) {
557+
if (unused_history_file_warnings) {
558+
Logger::warning("Found history file for non-existent province: ", province_id);
559+
}
560+
return true;
527561
}
528-
return true;
562+
563+
return province_history_manager.load_province_history_file(
564+
game_manager, *province, parse_defines(file).get_file_node()
565+
);
529566
}
567+
);
530568

531-
return game_manager.get_history_manager().get_province_manager().load_province_history_file(
532-
game_manager, *province, parse_defines(file).get_file_node()
533-
);
534-
}
535-
);
569+
/* Pop History */
570+
static constexpr std::string_view pop_history_directory = "history/pops/";
571+
572+
const string_set_t pop_history_dirs = lookup_dirs_in_dir(pop_history_directory);
573+
const Date last_bookmark_date = game_manager.get_history_manager().get_bookmark_manager().get_last_bookmark_date();
574+
575+
for (std::string const& dir : pop_history_dirs) {
576+
bool successful = false;
577+
const Date date = Date::from_string(dir, &successful);
578+
579+
if (successful && date <= last_bookmark_date) {
580+
bool non_integer_size = false;
581+
582+
ret &= apply_to_files(
583+
lookup_files_in_dir(StringUtils::append_string_views(pop_history_directory, dir), ".txt"),
584+
[this, &game_manager, &province_history_manager, date, &non_integer_size](fs::path const& file) -> bool {
585+
return province_history_manager.load_pop_history_file(
586+
game_manager, date, parse_defines(file).get_file_node(), &non_integer_size
587+
);
588+
}
589+
);
536590

537-
/* Pop History */
538-
static constexpr std::string_view pop_history_directory = "history/pops/";
539-
const string_set_t pop_history_dirs = lookup_dirs_in_dir(pop_history_directory);
540-
const Date last_bookmark_date = game_manager.get_history_manager().get_bookmark_manager().get_last_bookmark_date();
541-
for (std::string const& dir : pop_history_dirs) {
542-
bool successful = false;
543-
const Date date = Date::from_string(dir, &successful);
544-
if (successful && date <= last_bookmark_date) {
545-
bool non_integer_size = false;
546-
ret &= apply_to_files(
547-
lookup_files_in_dir(StringUtils::append_string_views(pop_history_directory, dir), ".txt"),
548-
[this, &game_manager, date, &non_integer_size](fs::path const& file) -> bool {
549-
return game_manager.get_history_manager().get_province_manager().load_pop_history_file(
550-
game_manager, date, parse_defines(file).get_file_node(), &non_integer_size
551-
);
591+
if (non_integer_size) {
592+
Logger::warning("Non-integer pop sizes in pop history files for ", date);
552593
}
553-
);
554-
if (non_integer_size) {
555-
Logger::warning("Non-integer pop sizes in pop history files for ", date);
556594
}
557595
}
596+
597+
province_history_manager.lock_province_histories(map, false);
558598
}
559599

560-
game_manager.get_history_manager().get_province_manager().lock_province_histories(game_manager.get_map(), false);
600+
{
601+
/* Diplomacy History */
602+
DiplomaticHistoryManager& diplomatic_history_manager = game_manager.get_history_manager().get_diplomacy_manager();
561603

562-
/* Diplomacy History */
563-
static constexpr std::string_view diplomacy_history_directory = "history/diplomacy";
564-
ret &= apply_to_files(
565-
lookup_files_in_dir(diplomacy_history_directory, ".txt"),
566-
[this, &game_manager](fs::path const& file) -> bool {
567-
return game_manager.get_history_manager().get_diplomacy_manager().load_diplomacy_history_file(
568-
game_manager.get_country_manager(), parse_defines(file).get_file_node()
569-
);
570-
}
571-
);
604+
static constexpr std::string_view diplomacy_history_directory = "history/diplomacy";
572605

573-
/* War History */
574-
static constexpr std::string_view war_history_directory = "history/wars";
575-
ret &= apply_to_files(
576-
lookup_files_in_dir(war_history_directory, ".txt"),
577-
[this, &game_manager](fs::path const& file) -> bool {
578-
return game_manager.get_history_manager().get_diplomacy_manager().load_war_history_file(
579-
game_manager, parse_defines(file).get_file_node()
580-
);
581-
}
582-
);
583-
game_manager.get_history_manager().get_diplomacy_manager().lock_diplomatic_history();
606+
ret &= apply_to_files(
607+
lookup_files_in_dir(diplomacy_history_directory, ".txt"),
608+
[this, &game_manager, &diplomatic_history_manager](fs::path const& file) -> bool {
609+
return diplomatic_history_manager.load_diplomacy_history_file(
610+
game_manager.get_country_manager(), parse_defines(file).get_file_node()
611+
);
612+
}
613+
);
614+
615+
/* War History */
616+
static constexpr std::string_view war_history_directory = "history/wars";
617+
const path_vector_t war_history_files = lookup_files_in_dir(war_history_directory, ".txt");
618+
619+
diplomatic_history_manager.reserve_more_wars(war_history_files.size());
620+
621+
ret &= apply_to_files(
622+
war_history_files,
623+
[this, &game_manager, &diplomatic_history_manager](fs::path const& file) -> bool {
624+
return diplomatic_history_manager.load_war_history_file(
625+
game_manager, parse_defines(file).get_file_node()
626+
);
627+
}
628+
);
629+
630+
diplomatic_history_manager.lock_diplomatic_history();
631+
}
584632

585633
return ret;
586634
}
587635

588636
bool Dataloader::_load_events(GameManager& game_manager) {
589637
static constexpr std::string_view events_directory = "events";
638+
590639
const bool ret = apply_to_files(
591640
lookup_files_in_dir(events_directory, ".txt"),
592641
[this, &game_manager](fs::path const& file) -> bool {
@@ -595,6 +644,7 @@ bool Dataloader::_load_events(GameManager& game_manager) {
595644
);
596645
}
597646
);
647+
598648
game_manager.get_event_manager().lock_events();
599649
return ret;
600650
}
@@ -959,9 +1009,13 @@ bool Dataloader::load_defines(GameManager& game_manager) {
9591009
return ret;
9601010
}
9611011

962-
#define PARSE_SCRIPTS(name, mgr) \
963-
if (!mgr.parse_scripts(game_manager)) { Logger::error("Failed to parse ", name, " scripts!"); ret = false; } \
964-
else Logger::info("Successfully parsed ", name, " scripts!");
1012+
#define PARSE_SCRIPTS(name, manager) \
1013+
if (!manager.parse_scripts(game_manager)) { \
1014+
Logger::error("Failed to parse ", name, " scripts!"); \
1015+
ret = false; \
1016+
} else { \
1017+
Logger::info("Successfully parsed ", name, " scripts!"); \
1018+
}
9651019

9661020
bool Dataloader::parse_scripts(GameManager& game_manager) const {
9671021
bool ret = true;

src/openvic-simulation/dataloader/NodeTools.hpp

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ namespace OpenVic {
3232
using name_list_t = std::vector<std::string>;
3333
std::ostream& operator<<(std::ostream& stream, name_list_t const& name_list);
3434

35+
template<typename T>
36+
concept Reservable = requires(T& t, size_t size) {
37+
{ t.size() } -> std::same_as<size_t>;
38+
t.reserve(size);
39+
};
40+
constexpr void reserve_more(Reservable auto& t, size_t size) {
41+
t.reserve(t.size() + size);
42+
}
43+
3544
namespace NodeTools {
3645

3746
template<typename Fn, typename Return = void, typename... Args>
@@ -232,25 +241,43 @@ namespace OpenVic {
232241
);
233242
}
234243

235-
template<typename T>
236-
concept Reservable = requires(T& t) {
237-
{ t.size() } -> std::same_as<size_t>;
238-
t.reserve(size_t {});
239-
};
240-
template<Reservable T>
241-
LengthCallback auto reserve_length_callback(T& t) {
242-
return [&t](size_t size) -> size_t {
243-
t.reserve(size);
244+
LengthCallback auto reserve_length_callback(Reservable auto& reservable) {
245+
return [&reservable](size_t size) -> size_t {
246+
reserve_more(reservable, size);
244247
return size;
245248
};
246249
}
247-
template<Reservable T>
248-
NodeCallback auto expect_list_reserve_length(T& t, NodeCallback auto callback) {
249-
return expect_list_and_length(reserve_length_callback(t), callback);
250+
NodeCallback auto expect_list_reserve_length(Reservable auto& reservable, NodeCallback auto callback) {
251+
return expect_list_and_length(reserve_length_callback(reservable), callback);
250252
}
251-
template<Reservable T>
252-
NodeCallback auto expect_dictionary_reserve_length(T& t, KeyValueCallback auto callback) {
253-
return expect_list_reserve_length(t, expect_assign(callback));
253+
NodeCallback auto expect_dictionary_reserve_length(Reservable auto& reservable, KeyValueCallback auto callback) {
254+
return expect_dictionary_and_length(reserve_length_callback(reservable), callback);
255+
}
256+
template<typename... Args>
257+
NodeCallback auto expect_dictionary_key_map_reserve_length_and_default(
258+
Reservable auto& reservable, key_map_t key_map, KeyValueCallback auto default_callback, Args... args
259+
) {
260+
return expect_dictionary_key_map_and_length_and_default(
261+
std::move(key_map), reserve_length_callback(reservable), default_callback, args...
262+
);
263+
}
264+
template<typename... Args>
265+
NodeCallback auto expect_dictionary_key_map_reserve_length(
266+
Reservable auto& reservable, key_map_t key_map, Args... args
267+
) {
268+
return expect_dictionary_key_map_and_length(std::move(key_map), reserve_length_callback(reservable), args...);
269+
}
270+
template<typename... Args>
271+
NodeCallback auto expect_dictionary_keys_reserve_length_and_default(
272+
Reservable auto& reservable, KeyValueCallback auto default_callback, Args... args
273+
) {
274+
return expect_dictionary_keys_and_length_and_default(
275+
reserve_length_callback(reservable), default_callback, args...
276+
);
277+
}
278+
template<typename... Args>
279+
NodeCallback auto expect_dictionary_keys_reserve_length(Reservable auto& reservable, Args... args) {
280+
return expect_dictionary_keys_and_length(reserve_length_callback(reservable), args...);
254281
}
255282

256283
node_callback_t name_list_callback(callback_t<name_list_t&&> callback);

src/openvic-simulation/economy/Good.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool GoodManager::load_goods_file(ast::NodeCPtr root) {
6464
}
6565
)(root);
6666
lock_good_categories();
67-
goods.reserve(goods.size() + total_expected_goods);
67+
reserve_more_goods(total_expected_goods);
6868
ret &= expect_good_category_dictionary([this](GoodCategory const& good_category, ast::NodeCPtr good_category_value) -> bool {
6969
return expect_dictionary([this, &good_category](std::string_view key, ast::NodeCPtr value) -> bool {
7070
colour_t colour = colour_t::null();

src/openvic-simulation/economy/ProductionType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ bool ProductionTypeManager::load_production_types_file(
204204
)(root);
205205

206206
/* Pass #3: actually load production types */
207-
production_types.reserve(production_types.size() + expected_types);
207+
reserve_more_production_types(expected_types);
208208
ret &= expect_dictionary(
209209
[this, &good_manager, &pop_manager, &template_target_map, &template_node_map](
210210
std::string_view key, ast::NodeCPtr node) -> bool {

0 commit comments

Comments
 (0)