aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/misc/Define.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/misc/Define.cpp')
-rw-r--r--src/openvic-simulation/misc/Define.cpp77
1 files changed, 36 insertions, 41 deletions
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;
}