aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/history
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2024-01-24 01:47:16 +0100
committer GitHub <noreply@github.com>2024-01-24 01:47:16 +0100
commit6bcb1e70a15755ceb0aaa13a080f85f8edb50911 (patch)
treeb30e9b5774130552fe97e27deaf0370d83920c43 /src/openvic-simulation/history
parentd4e597da089a81f719a9c33b46111d1c2c590124 (diff)
parent268a6948c0400905dfc335427395519689f067f5 (diff)
Merge pull request #137 from OpenVicProject/reserve_more
Added reserve_more, expect_dictionary_key[s|_map]_reserve_length[_and_default]
Diffstat (limited to 'src/openvic-simulation/history')
-rw-r--r--src/openvic-simulation/history/Bookmark.cpp43
-rw-r--r--src/openvic-simulation/history/CountryHistory.cpp8
-rw-r--r--src/openvic-simulation/history/CountryHistory.hpp4
-rw-r--r--src/openvic-simulation/history/DiplomaticHistory.cpp12
-rw-r--r--src/openvic-simulation/history/DiplomaticHistory.hpp3
-rw-r--r--src/openvic-simulation/history/HistoryManager.hpp3
-rw-r--r--src/openvic-simulation/history/HistoryMap.hpp1
-rw-r--r--src/openvic-simulation/history/ProvinceHistory.cpp8
-rw-r--r--src/openvic-simulation/history/ProvinceHistory.hpp6
9 files changed, 54 insertions, 34 deletions
diff --git a/src/openvic-simulation/history/Bookmark.cpp b/src/openvic-simulation/history/Bookmark.cpp
index b758867..ee7b494 100644
--- a/src/openvic-simulation/history/Bookmark.cpp
+++ b/src/openvic-simulation/history/Bookmark.cpp
@@ -23,27 +23,30 @@ bool BookmarkManager::add_bookmark(
}
bool BookmarkManager::load_bookmark_file(ast::NodeCPtr root) {
- const bool ret = expect_dictionary([this](std::string_view key, ast::NodeCPtr value) -> bool {
- if (key != "bookmark") {
- Logger::error("Invalid bookmark declaration ", key);
- return false;
+ const bool ret = expect_dictionary_reserve_length(
+ bookmarks,
+ [this](std::string_view key, ast::NodeCPtr value) -> bool {
+ if (key != "bookmark") {
+ Logger::error("Invalid bookmark declaration ", key);
+ return false;
+ }
+
+ std::string_view name, description;
+ Date date;
+ uint32_t initial_camera_x, initial_camera_y;
+
+ bool ret = expect_dictionary_keys(
+ "name", ONE_EXACTLY, expect_string(assign_variable_callback(name)),
+ "desc", ONE_EXACTLY, expect_string(assign_variable_callback(description)),
+ "date", ONE_EXACTLY, expect_date(assign_variable_callback(date)),
+ "cameraX", ONE_EXACTLY, expect_uint(assign_variable_callback(initial_camera_x)),
+ "cameraY", ONE_EXACTLY, expect_uint(assign_variable_callback(initial_camera_y))
+ )(value);
+
+ ret &= add_bookmark(name, description, date, initial_camera_x, initial_camera_y);
+ return ret;
}
-
- std::string_view name, description;
- Date date;
- uint32_t initial_camera_x, initial_camera_y;
-
- bool ret = expect_dictionary_keys(
- "name", ONE_EXACTLY, expect_string(assign_variable_callback(name)),
- "desc", ONE_EXACTLY, expect_string(assign_variable_callback(description)),
- "date", ONE_EXACTLY, expect_date(assign_variable_callback(date)),
- "cameraX", ONE_EXACTLY, expect_uint(assign_variable_callback(initial_camera_x)),
- "cameraY", ONE_EXACTLY, expect_uint(assign_variable_callback(initial_camera_y))
- )(value);
-
- ret &= add_bookmark(name, description, date, initial_camera_x, initial_camera_y);
- return ret;
- })(root);
+ )(root);
lock_bookmarks();
return ret;
diff --git a/src/openvic-simulation/history/CountryHistory.cpp b/src/openvic-simulation/history/CountryHistory.cpp
index 3b4dfd8..5ee5e38 100644
--- a/src/openvic-simulation/history/CountryHistory.cpp
+++ b/src/openvic-simulation/history/CountryHistory.cpp
@@ -180,6 +180,14 @@ bool CountryHistoryMap::_load_history_entry(
)(root);
}
+void CountryHistoryManager::reserve_more_country_histories(size_t size) {
+ if (locked) {
+ Logger::error("Failed to reserve space for ", size, " countries in CountryHistoryManager - already locked!");
+ } else {
+ reserve_more(country_histories, size);
+ }
+}
+
void CountryHistoryManager::lock_country_histories() {
Logger::info("Locked country history registry after registering ", country_histories.size(), " items");
locked = true;
diff --git a/src/openvic-simulation/history/CountryHistory.hpp b/src/openvic-simulation/history/CountryHistory.hpp
index 6d4392b..1c1b7f1 100644
--- a/src/openvic-simulation/history/CountryHistory.hpp
+++ b/src/openvic-simulation/history/CountryHistory.hpp
@@ -1,10 +1,8 @@
#pragma once
-#include <map>
#include <optional>
#include "openvic-simulation/country/Country.hpp"
-#include "openvic-simulation/history/Bookmark.hpp"
#include "openvic-simulation/history/HistoryMap.hpp"
#include "openvic-simulation/map/Province.hpp"
#include "openvic-simulation/military/Deployment.hpp"
@@ -17,7 +15,6 @@
#include "openvic-simulation/pop/Religion.hpp"
#include "openvic-simulation/research/Invention.hpp"
#include "openvic-simulation/research/Technology.hpp"
-#include "openvic-simulation/types/Colour.hpp"
#include "openvic-simulation/types/Date.hpp"
#include "openvic-simulation/types/OrderedContainers.hpp"
@@ -88,6 +85,7 @@ namespace OpenVic {
public:
CountryHistoryManager() = default;
+ void reserve_more_country_histories(size_t size);
void lock_country_histories();
bool is_locked() const;
diff --git a/src/openvic-simulation/history/DiplomaticHistory.cpp b/src/openvic-simulation/history/DiplomaticHistory.cpp
index 883a212..55d6d6b 100644
--- a/src/openvic-simulation/history/DiplomaticHistory.cpp
+++ b/src/openvic-simulation/history/DiplomaticHistory.cpp
@@ -47,6 +47,14 @@ SubjectHistory::SubjectHistory(
const Period new_period
) : overlord { new_overlord }, subject { new_subject }, type { new_type }, period {new_period} {}
+void DiplomaticHistoryManager::reserve_more_wars(size_t size) {
+ if (locked) {
+ Logger::error("Failed to reserve space for ", size, " wars in DiplomaticHistoryManager - already locked!");
+ } else {
+ reserve_more(wars, size);
+ }
+}
+
void DiplomaticHistoryManager::lock_diplomatic_history() {
Logger::info("Locked diplomacy history registry after registering ", alliances.size() + subjects.size() + wars.size(), " items");
locked = true;
@@ -184,7 +192,7 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(CountryManager const&
}
bool DiplomaticHistoryManager::load_war_history_file(GameManager const& game_manager, ast::NodeCPtr root) {
- std::string name = "";
+ std::string_view name {};
std::vector<WarHistory::war_participant_t> attackers {};
std::vector<WarHistory::war_participant_t> defenders {};
std::vector<WarHistory::added_wargoal_t> wargoals {};
@@ -270,7 +278,7 @@ bool DiplomaticHistoryManager::load_war_history_file(GameManager const& game_man
)(node);
return ret;
},
- "name", ZERO_OR_ONE, expect_string(assign_variable_callback_string(name))
+ "name", ZERO_OR_ONE, expect_string(assign_variable_callback(name))
)(root);
wars.push_back({ name, std::move(attackers), std::move(defenders), std::move(wargoals) });
diff --git a/src/openvic-simulation/history/DiplomaticHistory.hpp b/src/openvic-simulation/history/DiplomaticHistory.hpp
index 07302ac..35ba6fb 100644
--- a/src/openvic-simulation/history/DiplomaticHistory.hpp
+++ b/src/openvic-simulation/history/DiplomaticHistory.hpp
@@ -1,8 +1,6 @@
#pragma once
-#include <bitset>
#include <vector>
-#include <map>
#include <optional>
#include "openvic-simulation/country/Country.hpp"
@@ -100,6 +98,7 @@ namespace OpenVic {
public:
DiplomaticHistoryManager() {}
+ void reserve_more_wars(size_t size);
void lock_diplomatic_history();
bool is_locked() const;
diff --git a/src/openvic-simulation/history/HistoryManager.hpp b/src/openvic-simulation/history/HistoryManager.hpp
index 35aebbf..1e540cc 100644
--- a/src/openvic-simulation/history/HistoryManager.hpp
+++ b/src/openvic-simulation/history/HistoryManager.hpp
@@ -2,9 +2,8 @@
#include "openvic-simulation/history/Bookmark.hpp"
#include "openvic-simulation/history/CountryHistory.hpp"
-#include "openvic-simulation/history/ProvinceHistory.hpp"
#include "openvic-simulation/history/DiplomaticHistory.hpp"
-#include "openvic-simulation/types/IdentifierRegistry.hpp"
+#include "openvic-simulation/history/ProvinceHistory.hpp"
namespace OpenVic {
struct HistoryManager {
diff --git a/src/openvic-simulation/history/HistoryMap.hpp b/src/openvic-simulation/history/HistoryMap.hpp
index 64975bc..15f838d 100644
--- a/src/openvic-simulation/history/HistoryMap.hpp
+++ b/src/openvic-simulation/history/HistoryMap.hpp
@@ -1,6 +1,5 @@
#pragma once
-#include <map>
#include <memory>
#include "openvic-simulation/dataloader/NodeTools.hpp"
diff --git a/src/openvic-simulation/history/ProvinceHistory.cpp b/src/openvic-simulation/history/ProvinceHistory.cpp
index fd174a3..b5b2210 100644
--- a/src/openvic-simulation/history/ProvinceHistory.cpp
+++ b/src/openvic-simulation/history/ProvinceHistory.cpp
@@ -112,6 +112,14 @@ bool ProvinceHistoryMap::_load_history_entry(
)(root);
}
+void ProvinceHistoryManager::reserve_more_province_histories(size_t size) {
+ if (locked) {
+ Logger::error("Failed to reserve space for ", size, " provinces in ProvinceHistoryManager - already locked!");
+ } else {
+ reserve_more(province_histories, size);
+ }
+}
+
void ProvinceHistoryManager::lock_province_histories(Map const& map, bool detailed_errors) {
std::vector<bool> province_checklist(map.get_province_count());
for (decltype(province_histories)::value_type const& entry : province_histories) {
diff --git a/src/openvic-simulation/history/ProvinceHistory.hpp b/src/openvic-simulation/history/ProvinceHistory.hpp
index 0bda35d..7611907 100644
--- a/src/openvic-simulation/history/ProvinceHistory.hpp
+++ b/src/openvic-simulation/history/ProvinceHistory.hpp
@@ -1,13 +1,10 @@
#pragma once
-#include <bitset>
-#include <map>
#include <vector>
#include "openvic-simulation/country/Country.hpp"
#include "openvic-simulation/economy/BuildingType.hpp"
#include "openvic-simulation/economy/Good.hpp"
-#include "openvic-simulation/history/Bookmark.hpp"
#include "openvic-simulation/history/HistoryMap.hpp"
#include "openvic-simulation/map/Province.hpp"
#include "openvic-simulation/map/TerrainType.hpp"
@@ -63,7 +60,7 @@ namespace OpenVic {
struct ProvinceHistoryManager {
private:
- ordered_map<Province const*, ProvinceHistoryMap> PROPERTY(province_histories);
+ ordered_map<Province const*, ProvinceHistoryMap> province_histories;
bool locked = false;
ProvinceHistoryMap* _get_or_make_province_history(Province const& province);
@@ -71,6 +68,7 @@ namespace OpenVic {
public:
ProvinceHistoryManager() = default;
+ void reserve_more_province_histories(size_t size);
void lock_province_histories(Map const& map, bool detailed_errors);
bool is_locked() const;