#ifndef RFL_GENERIC_HPP_ #define RFL_GENERIC_HPP_ #include #include #include #include "Object.hpp" #include "Result.hpp" namespace rfl { class Generic { public: using Array = std::vector; using Object = rfl::Object; using ReflectionType = std::variant; Generic() : value_(false) {} Generic(Generic&& _other) noexcept = default; Generic(const Generic& _other) = default; Generic(const ReflectionType& _value) : value_(_value) {} Generic(ReflectionType&& _value) noexcept : value_(std::move(_value)) {} template , bool>::type = true> Generic(const T& _value) : value_(_value) {} template , bool>::type = true> Generic(T&& _value) noexcept : value_(std::forward(_value)) {} ~Generic() = default; /// Returns the underlying object. const ReflectionType& get() const { return value_; } /// Assigns the underlying object. auto& operator=(const ReflectionType& _value) { value_ = _value; return *this; } /// Assigns the underlying object. auto& operator=(ReflectionType&& _value) noexcept { value_ = std::move(_value); return *this; } /// Assigns the underlying object. template , bool>::type = true> auto& operator=(const T& _value) { value_ = _value; return *this; } /// Assigns the underlying object. Generic& operator=(const Generic& _other) = default; /// Assigns the underlying object. Generic& operator=(Generic&& _other) = default; /// Returns the underlying object, necessary for the serialization to work. const ReflectionType& reflection() const { return value_; }; /// Casts the underlying value to an rfl::Generic::Array or returns an /// rfl::Error, if the underlying value is not an rfl::Generic::Array. Result to_array() const noexcept { if (const auto* ptr = std::get_if(&value_)) { return *ptr; } else { return Error( "rfl::Generic: Could not cast the underlying value to an " "rfl::Generic::Array."); } } /// Casts the underlying value to a boolean or returns an rfl::Error, if the /// underlying value is not a boolean. Result to_bool() const noexcept { if (const auto* ptr = std::get_if(&value_)) { return *ptr; } else { return Error( "rfl::Generic: Could not cast the underlying value to a boolean."); } } /// Casts the underlying value to a double or returns an rfl::Error, if the /// underlying value is not a double. Result to_double() const noexcept { if (const auto* ptr = std::get_if(&value_)) { return *ptr; } else { return Error( "rfl::Generic: Could not cast the underlying value to a double."); } } /// 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 auto* ptr = std::get_if(&value_)) { return *ptr; } else { return Error( "rfl::Generic: Could not cast the underlying value to an integer."); } } /// 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 { if (const auto* ptr = std::get_if(&value_)) { return *ptr; } else { return Error( "rfl::Generic: Could not cast the underlying value to an " "rfl::Generic::Object."); } } /// Casts the underlying value to a string or returns an rfl::Error, if the /// underlying value is not a string. Result to_string() const noexcept { if (const auto* ptr = std::get_if(&value_)) { return *ptr; } else { return Error( "rfl::Generic: Could not cast the underlying value to a string."); } } private: ReflectionType value_; }; /// Casts the underlying value to an rfl::Generic::Array or returns an /// rfl::Error, if the underlying value is not an rfl::Generic::Array. inline Result to_array(const Generic& _g) noexcept { return _g.to_array(); } /// Casts the underlying value to a boolean or returns an rfl::Error, if the /// underlying value is not a boolean. inline Result to_bool(const Generic& _g) noexcept { return _g.to_bool(); } /// Casts the underlying value to a double or returns an rfl::Error, if the /// underlying value is not a double. inline Result to_double(const Generic& _g) noexcept { return _g.to_double(); } /// Casts the underlying value to an integer or returns an rfl::Error, if the /// underlying value is not an integer. inline Result to_int(const Generic& _g) noexcept { return _g.to_int(); } /// 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 { return _g.to_object(); } /// Casts the underlying value to a string or returns an rfl::Error, if the /// underlying value is not a string. inline Result to_string(const Generic& _g) noexcept { return _g.to_string(); } } // namespace rfl #endif