diff --git a/include/rfl/Generic.hpp b/include/rfl/Generic.hpp index e7463de5..1faeda8e 100644 --- a/include/rfl/Generic.hpp +++ b/include/rfl/Generic.hpp @@ -19,10 +19,10 @@ class Generic { using Array = std::vector; using Object = rfl::Object; - using VariantType = std::variant; using ReflectionType = std::optional< - std::variant>; + std::variant>; Generic(); @@ -115,14 +115,25 @@ class Generic { /// Casts the underlying value to an integer or returns an rfl::Error, if the /// underlying value is not an integer. Result to_int() const noexcept { - if (const int* ptr = std::get_if(&value_)) { - return *ptr; + if (const int64_t* ptr = std::get_if(&value_)) { + return static_cast(*ptr); } else { return error( "rfl::Generic: Could not cast the underlying value to an integer."); } } + /// Casts the underlying value to an int64 or returns an rfl::Error, if the + /// underlying value is not an integer. + Result to_int64() const noexcept { + if (const int64_t* ptr = std::get_if(&value_)) { + return *ptr; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to an int64."); + } + } + /// Casts the underlying value to an rfl::Generic::Object or returns an /// rfl::Error, if the underlying value is not an rfl::Generic::Object. Result to_object() const noexcept { @@ -191,6 +202,10 @@ inline Result to_double(const Generic& _g) noexcept { /// underlying value is not an integer. inline Result to_int(const Generic& _g) noexcept { return _g.to_int(); } +/// Casts the underlying value to an int64 or returns an rfl::Error, if the +/// underlying value is not an integer. +inline Result to_int64(const Generic& _g) noexcept { return _g.to_int64(); } + /// Casts the underlying value to an rfl::Generic::Object or returns an /// rfl::Error, if the underlying value is not an rfl::Generic::Object. inline Result to_object(const Generic& _g) noexcept { diff --git a/include/rfl/generic/Reader.hpp b/include/rfl/generic/Reader.hpp index c31a4aaa..8d679448 100644 --- a/include/rfl/generic/Reader.hpp +++ b/include/rfl/generic/Reader.hpp @@ -53,8 +53,13 @@ struct Reader { return _var.to_double().transform( [](const auto& _v) { return static_cast(_v); }); } else if constexpr (std::is_integral>()) { - return _var.to_int().transform( - [](const auto& _v) { return static_cast(_v); }); + if constexpr (sizeof(T) > sizeof(int)) { + return _var.to_int64().transform( + [](const auto& _v) { return static_cast(_v); }); + } else { + return _var.to_int().transform( + [](const auto& _v) { return static_cast(_v); }); + } } else { static_assert(rfl::always_false_v, "Unsupported type."); } diff --git a/include/rfl/generic/Writer.hpp b/include/rfl/generic/Writer.hpp index b4e72027..d97c05aa 100644 --- a/include/rfl/generic/Writer.hpp +++ b/include/rfl/generic/Writer.hpp @@ -101,7 +101,7 @@ struct Writer { } else if constexpr (std::is_floating_point>()) { return OutputVarType(static_cast(_var)); } else if constexpr (std::is_integral>()) { - return OutputVarType(static_cast(_var)); + return OutputVarType(static_cast(_var)); } else { static_assert(always_false_v, "Unsupported type"); }