aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/misc
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-01-22 21:02:58 +0100
committer hop311 <hop3114@gmail.com>2024-01-23 23:14:53 +0100
commit268a6948c0400905dfc335427395519689f067f5 (patch)
treeb30e9b5774130552fe97e27deaf0370d83920c43 /src/openvic-simulation/misc
parentd4e597da089a81f719a9c33b46111d1c2c590124 (diff)
Added reserve_more, expect_dictionary_key[s|_map]_reserve_length[_and_default]reserve_more
Diffstat (limited to 'src/openvic-simulation/misc')
-rw-r--r--src/openvic-simulation/misc/Decision.cpp3
-rw-r--r--src/openvic-simulation/misc/Define.cpp77
-rw-r--r--src/openvic-simulation/misc/Define.hpp3
-rw-r--r--src/openvic-simulation/misc/Event.cpp3
4 files changed, 41 insertions, 45 deletions
diff --git a/src/openvic-simulation/misc/Decision.cpp b/src/openvic-simulation/misc/Decision.cpp
index df78c80..e0bd7c0 100644
--- a/src/openvic-simulation/misc/Decision.cpp
+++ b/src/openvic-simulation/misc/Decision.cpp
@@ -52,7 +52,8 @@ bool DecisionManager::add_decision(
bool DecisionManager::load_decision_file(ast::NodeCPtr root) {
return expect_dictionary_keys(
- "political_decisions", ZERO_OR_ONE, expect_dictionary(
+ "political_decisions", ZERO_OR_ONE, expect_dictionary_reserve_length(
+ decisions,
[this](std::string_view identifier, ast::NodeCPtr node) -> bool {
bool alert = true, news = false;
std::string_view news_title, news_desc_long, news_desc_medium, news_desc_short, picture;
diff --git a/src/openvic-simulation/misc/Define.cpp b/src/openvic-simulation/misc/Define.cpp
index 45584f7..e4e6b52 100644
--- a/src/openvic-simulation/misc/Define.cpp
+++ b/src/openvic-simulation/misc/Define.cpp
@@ -30,6 +30,10 @@ uint64_t Define::get_value_as_uint() const {
}
bool DefineManager::add_define(std::string_view name, std::string&& value, Define::Type type) {
+ if (name.empty()) {
+ Logger::error("Invalid define identifier - empty!");
+ return false;
+ }
return defines.add_item({ name, std::move(value), type }, duplicate_warning_callback);
}
@@ -55,62 +59,53 @@ bool DefineManager::add_date_define(std::string_view name, Date date) {
} else if (name == "end_date") {
end_date = date;
} else {
+ Logger::error("Invalid date define identifier - \"", name, "\" (must be start_date or end_date)");
return false;
}
- return defines.add_item({ name, date.to_string(), Define::Type::None });
+ return defines.add_item({ name, date.to_string(), Define::Type::Date });
}
bool DefineManager::load_defines_file(ast::NodeCPtr root) {
bool ret = expect_dictionary_keys(
"defines", ONE_EXACTLY, expect_dictionary([this](std::string_view key, ast::NodeCPtr value) -> bool {
- if (key == "country" || key == "economy" || key == "military" || key == "diplomacy" ||
- key == "pops" || key == "ai" || key == "graphics") {
- return expect_dictionary([this, &key](std::string_view inner_key, ast::NodeCPtr value) -> bool {
- std::string str_val;
-
- bool ret = expect_identifier_or_string(assign_variable_callback_string(str_val))(value);
-
- Define::Type type;
- switch (key[0]) {
- using enum Define::Type;
- case 'c': // country
- type = Country;
- break;
- case 'e': // economy
- type = Economy;
- break;
- case 'm': // military
- type = Military;
- break;
- case 'd': // diplomacy
- type = Diplomacy;
- break;
- case 'p': // pops
- type = Pops;
- break;
- case 'a': // ai
- type = Ai;
- break;
- case 'g': // graphics
- type = Graphics;
- break;
- default:
- Logger::error("Unknown define type ", key, " found in defines!");
- return false;
+ using enum Define::Type;
+ static const string_map_t<Define::Type> type_map {
+ { "country", Country },
+ { "economy", Economy },
+ { "military", Military },
+ { "diplomacy", Diplomacy },
+ { "pops", Pops },
+ { "ai", Ai },
+ { "graphics", Graphics },
+ };
+
+ const string_map_t<Define::Type>::const_iterator type_it = type_map.find(key);
+
+ if (type_it != type_map.end()) {
+
+ return expect_dictionary_reserve_length(
+ defines,
+ [this, &key, type = type_it->second](std::string_view inner_key, ast::NodeCPtr value) -> bool {
+ std::string str_val;
+ bool ret = expect_identifier_or_string(assign_variable_callback_string(str_val))(value);
+ ret &= add_define(inner_key, std::move(str_val), type);
+ return ret;
}
+ )(value);
- ret &= add_define(inner_key, std::move(str_val), type);
- return ret;
- })(value);
} else if (key == "start_date" || key == "end_date") {
- return expect_identifier_or_string(expect_date_str([this, &key](Date date) -> bool {
- return add_date_define(key, date);
- }))(value);
+
+ return expect_identifier_or_string(expect_date_str(
+ std::bind_front(&DefineManager::add_date_define, this, key)
+ ))(value);
+
} else {
return false;
}
})
)(root);
+
lock_defines();
+
return ret;
}
diff --git a/src/openvic-simulation/misc/Define.hpp b/src/openvic-simulation/misc/Define.hpp
index c3c7888..64eb605 100644
--- a/src/openvic-simulation/misc/Define.hpp
+++ b/src/openvic-simulation/misc/Define.hpp
@@ -1,6 +1,5 @@
#pragma once
-#include <concepts>
#include <optional>
#include "openvic-simulation/types/IdentifierRegistry.hpp"
@@ -12,7 +11,7 @@ namespace OpenVic {
struct Define : HasIdentifier {
friend struct DefineManager;
- enum class Type : unsigned char { None, Country, Economy, Military, Diplomacy, Pops, Ai, Graphics };
+ enum class Type : unsigned char { Date, Country, Economy, Military, Diplomacy, Pops, Ai, Graphics };
private:
const std::string PROPERTY(value);
diff --git a/src/openvic-simulation/misc/Event.cpp b/src/openvic-simulation/misc/Event.cpp
index 34743e8..2617927 100644
--- a/src/openvic-simulation/misc/Event.cpp
+++ b/src/openvic-simulation/misc/Event.cpp
@@ -116,7 +116,8 @@ bool EventManager::add_on_action(std::string_view identifier, OnAction::weight_m
}
bool EventManager::load_event_file(IssueManager const& issue_manager, ast::NodeCPtr root) {
- return expect_dictionary(
+ return expect_dictionary_reserve_length(
+ events,
[this, &issue_manager](std::string_view key, ast::NodeCPtr value) -> bool {
Event::event_type_t type;
scope_t initial_scope;