diff options
author | Hop311 <Hop3114@gmail.com> | 2024-09-08 19:43:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-08 19:43:16 +0200 |
commit | 79dbf71dd23623059ea9eabd87956f93604be1ed (patch) | |
tree | 9e35442225f6505d9f457e2abc63ffc39c16ba88 /src/openvic-simulation/misc/Define.hpp | |
parent | e97eef1fdd7525e4f33c9864f207a3e3d711c3c5 (diff) | |
parent | dd65fa7dd431264caa08d083cb3a94922a4084d5 (diff) |
Merge pull request #194 from OpenVicProject/defines-caching
Add define caching to avoid post-load lookups
Diffstat (limited to 'src/openvic-simulation/misc/Define.hpp')
-rw-r--r-- | src/openvic-simulation/misc/Define.hpp | 55 |
1 files changed, 42 insertions, 13 deletions
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 <optional> - #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<Define> IDENTIFIER_REGISTRY(define); - std::optional<Date> start_date; - std::optional<Date> end_date; + // Date + Date PROPERTY(start_date); // start_date + Date PROPERTY(end_date); // end_date + + // Country + + // Economy + + // Military + + // Diplomacy + + // Pops + + // Ai + + // Graphics + + template<typename T> + bool load_define(T& value, Define::Type type, std::string_view name) const; + + template<Timespan (*Func)(Timespan::day_t)> + 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); |