Added option to add custom types ScriptValue.

This commit is contained in:
Patrick 2024-07-31 22:18:51 +02:00
parent cdcf99237b
commit 2942149cb5

View File

@ -10,6 +10,7 @@
#include <vector>
#include "../container/optional.hpp"
#include "../util/traits.hpp"
namespace mijin
{
@ -17,9 +18,14 @@ namespace mijin
// public types
//
template<typename TConcrete, typename... TAdditional>
class ScriptValue;
template<typename TScriptValue>
struct ArrayValue
{
std::vector<class ScriptValue> values;
std::vector<TScriptValue> 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<typename TConcrete, typename... TAdditional>
using script_value_base_t = std::variant<
UndefinedValue,
bool,
script_int_t,
script_float_t,
std::string,
ArrayValue
ArrayValue<TConcrete>,
TAdditional...
>;
template<typename TConcrete, typename... TAdditional>
class ScriptValue
{
public:
using base_t = script_value_base_t<TConcrete, TAdditional...>;
using concrete_t = TConcrete;
using array_t = ArrayValue<TConcrete>;
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<script_int_t>
{
using type_t = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<type_t, bool> || std::is_same_v<type_t, script_int_t> || std::is_same_v<type_t, script_float_t>)
if constexpr (std::is_arithmetic_v<type_t>)
{
return static_cast<script_int_t>(value);
}
@ -110,7 +124,7 @@ public:
return visit([&](auto&& value) -> Optional<script_float_t>
{
using type_t = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<type_t, bool> || std::is_same_v<type_t, script_int_t> || std::is_same_v<type_t, script_float_t>)
if constexpr (std::is_arithmetic_v<type_t>)
{
return static_cast<script_float_t>(value);
}
@ -185,9 +199,17 @@ public:
{
return toString();
}
else if constexpr (std::is_same_v<type_t, ScriptValue>)
else if constexpr (std::is_same_v<type_t, TConcrete>)
{
return *this;
return *static_cast<copy_cv_t<std::remove_reference_t<decltype(*this)>, TConcrete>*>(this);
}
else if constexpr (is_any_type_v<T, TAdditional...>)
{
if (std::holds_alternative<T>(base_))
{
return std::get<T>(base_);
}
return NULL_OPTIONAL;
}
else
{
@ -199,8 +221,9 @@ public:
//
// public functions
//
template<typename TConcrete, typename... TAdditional>
template<typename TValue>
ScriptValue::ScriptValue(TValue&& value) : base_(std::forward<TValue>(value))
ScriptValue<TConcrete, TAdditional...>::ScriptValue(TValue&& value) : base_(std::forward<TValue>(value))
{
}
} // namespace mijin