From 2942149cb5bafee4fdf94c14f4ce5735f8aa6657 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Wed, 31 Jul 2024 22:18:51 +0200 Subject: [PATCH] Added option to add custom types ScriptValue. --- source/mijin/types/script_value.hpp | 39 +++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/source/mijin/types/script_value.hpp b/source/mijin/types/script_value.hpp index 55bf671..869ca36 100644 --- a/source/mijin/types/script_value.hpp +++ b/source/mijin/types/script_value.hpp @@ -10,6 +10,7 @@ #include #include "../container/optional.hpp" +#include "../util/traits.hpp" namespace mijin { @@ -17,9 +18,14 @@ namespace mijin // public types // + +template +class ScriptValue; + +template struct ArrayValue { - std::vector values; + std::vector values; bool operator==(const ArrayValue& other) const; inline bool operator!=(const ArrayValue& other) const { @@ -37,19 +43,27 @@ inline constexpr UndefinedValue UNDEFINED_VALUE; using script_int_t = long long; using script_float_t = double; + +template using script_value_base_t = std::variant< UndefinedValue, bool, script_int_t, script_float_t, std::string, - ArrayValue + ArrayValue, + TAdditional... >; +template class ScriptValue { +public: + using base_t = script_value_base_t; + using concrete_t = TConcrete; + using array_t = ArrayValue; private: - script_value_base_t base_; + base_t base_; public: ScriptValue() noexcept = default; ScriptValue(const ScriptValue&) noexcept = default; @@ -83,7 +97,7 @@ public: return visit([&](auto&& value) -> Optional { using type_t = std::decay_t; - if constexpr (std::is_same_v || std::is_same_v || std::is_same_v) + if constexpr (std::is_arithmetic_v) { return static_cast(value); } @@ -110,7 +124,7 @@ public: return visit([&](auto&& value) -> Optional { using type_t = std::decay_t; - if constexpr (std::is_same_v || std::is_same_v || std::is_same_v) + if constexpr (std::is_arithmetic_v) { return static_cast(value); } @@ -185,9 +199,17 @@ public: { return toString(); } - else if constexpr (std::is_same_v) + else if constexpr (std::is_same_v) { - return *this; + return *static_cast, TConcrete>*>(this); + } + else if constexpr (is_any_type_v) + { + if (std::holds_alternative(base_)) + { + return std::get(base_); + } + return NULL_OPTIONAL; } else { @@ -199,8 +221,9 @@ public: // // public functions // +template template -ScriptValue::ScriptValue(TValue&& value) : base_(std::forward(value)) +ScriptValue::ScriptValue(TValue&& value) : base_(std::forward(value)) { } } // namespace mijin