diff options
author | Spartan322 <Megacake1234@gmail.com> | 2023-10-20 01:56:06 +0200 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2023-10-20 10:48:38 +0200 |
commit | 3d728f054e3c214f840b7e63539aea0c4c5246b5 (patch) | |
tree | c25a4e654d597ca2ca699ffb2b1bde6b6eeba053 /src/openvic-simulation/misc/Define.cpp | |
parent | 910d6cd73d7b1857ff481e1af060e780ec27d800 (diff) |
Add Defines Loading
Caches start_date and end_date in DefineManager
Add static `get_property` and HASID_PROPERTY macro to HasIdentifier
Diffstat (limited to 'src/openvic-simulation/misc/Define.cpp')
-rw-r--r-- | src/openvic-simulation/misc/Define.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/openvic-simulation/misc/Define.cpp b/src/openvic-simulation/misc/Define.cpp new file mode 100644 index 0000000..509b0a5 --- /dev/null +++ b/src/openvic-simulation/misc/Define.cpp @@ -0,0 +1,121 @@ + +#include "Define.hpp" + +#include <cassert> +#include <cstdlib> +#include <memory> + +#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> + +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +Define::Define( + std::string_view new_identifier, + std::string&& new_value, + Type new_type +) : HasIdentifier { new_identifier }, + value { std::move(new_value) }, + type { new_type } { +} + +fixed_point_t Define::get_value_as_fp() const { + return fixed_point_t::parse(value); +} + +int64_t Define::get_value_as_int() const { + return std::strtoll(value.data(), nullptr, 10); +} + +uint64_t Define::get_value_as_uint() const { + return std::strtoull(value.data(), nullptr, 10); +} + +DefineManager::DefineManager() : defines { "defines" } { +} + +bool DefineManager::add_define(std::string_view name, std::string&& value, Define::Type type) { + return defines.add_item({ name, std::move(value), type }, duplicate_warning_callback); +} + +const Date& DefineManager::get_start_date() const { + return *start_date; +} + +const Date& DefineManager::get_end_date() const { + return *end_date; +} + +bool DefineManager::add_date_define(std::string_view name, Date date) { + if (name != "start_date" && name != "end_date") return false; + + bool ret = defines.add_item({ name, date.to_string(), Define::Type::None }); + + if (name == "start_date") + start_date.reset(new Date(date)); + else if (name == "end_date") + end_date.reset(new Date(date)); + + return ret; +} + +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; + } + + ret &= add_define(inner_key, std::move(str_val), type); + return ret; + })(value); + } else if (key == "start_date" || key == "end_date") { + using namespace std::placeholders; + + return expect_identifier_or_string(expect_date_str([this, &key](Date date) -> bool { + return add_date_define(key, date); + }))(value); + } else { + return false; + } + }) + )(root); + lock_defines(); + return ret; +}
\ No newline at end of file |