From dd65fa7dd431264caa08d083cb3a94922a4084d5 Mon Sep 17 00:00:00 2001 From: hop311 Date: Fri, 6 Sep 2024 21:43:11 +0100 Subject: Add define caching to avoid post-load lookups --- src/openvic-simulation/misc/Define.hpp | 55 ++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 13 deletions(-) (limited to 'src/openvic-simulation/misc/Define.hpp') diff --git a/src/openvic-simulation/misc/Define.hpp b/src/openvic-simulation/misc/Define.hpp index 3f7b3dc..7d8fbf7 100644 --- a/src/openvic-simulation/misc/Define.hpp +++ b/src/openvic-simulation/misc/Define.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" @@ -11,35 +9,66 @@ namespace OpenVic { struct Define : HasIdentifier { friend struct DefineManager; - enum class Type : unsigned char { Date, Country, Economy, Military, Diplomacy, Pops, Ai, Graphics }; + enum class Type : unsigned char { Unknown, Date, Country, Economy, Military, Diplomacy, Pops, Ai, Graphics }; + + static std::string_view type_to_string(Type type); + // This only accepts type names found in defines.lua, so it will never return Type::Date + static Type string_to_type(std::string_view str); private: std::string PROPERTY(value); const Type PROPERTY(type); - Define(std::string_view new_identifier, std::string&& new_value, Type new_type); + Define(std::string_view new_identifier, std::string_view new_value, Type new_type); public: Define(Define&&) = default; - fixed_point_t get_value_as_fp() const; - int64_t get_value_as_int() const; - uint64_t get_value_as_uint() const; + Date get_value_as_date(bool* successful = nullptr) const; + fixed_point_t get_value_as_fp(bool* successful = nullptr) const; + int64_t get_value_as_int(bool* successful = nullptr) const; + uint64_t get_value_as_uint(bool* successful = nullptr) const; }; + std::ostream& operator<<(std::ostream& os, Define::Type type); + struct DefineManager { private: IdentifierRegistry IDENTIFIER_REGISTRY(define); - std::optional start_date; - std::optional end_date; + // Date + Date PROPERTY(start_date); // start_date + Date PROPERTY(end_date); // end_date + + // Country + + // Economy + + // Military + + // Diplomacy + + // Pops + + // Ai + + // Graphics + + template + bool load_define(T& value, Define::Type type, std::string_view name) const; + + template + bool _load_define_timespan(Timespan& value, Define::Type type, std::string_view name) const; + + bool load_define_days(Timespan& value, Define::Type type, std::string_view name) const; + bool load_define_months(Timespan& value, Define::Type type, std::string_view name) const; + bool load_define_years(Timespan& value, Define::Type type, std::string_view name) const; public: - bool add_define(std::string_view name, std::string&& value, Define::Type type); - bool add_date_define(std::string_view name, Date date); + DefineManager(); + + bool add_define(std::string_view name, std::string_view value, Define::Type type); - Date get_start_date() const; - Date get_end_date() const; bool in_game_period(Date date) const; bool load_defines_file(ast::NodeCPtr root); -- cgit v1.2.3-56-ga3b1