1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
#ifndef RFL_GENERIC_HPP_
#define RFL_GENERIC_HPP_
#include <optional>
#include <ostream>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
#include "Object.hpp"
#include "Result.hpp"
#include "Variant.hpp"
namespace rfl {
class Generic {
public:
constexpr static std::nullopt_t Null = std::nullopt;
using Array = std::vector<Generic>;
using Object = rfl::Object<Generic>;
using VariantType = std::variant<bool, int64_t, double, std::string, Object,
Array, std::nullopt_t>;
using ReflectionType = std::optional<
std::variant<bool, int64_t, double, std::string, Object, Array>>;
Generic();
Generic(Generic&& _other) noexcept;
Generic(const Generic& _other);
Generic(const VariantType& _value);
Generic(VariantType&& _value) noexcept;
Generic(const ReflectionType& _value);
template <class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>,
bool>::type = true>
Generic(const T& _value) {
value_ = _value;
}
template <class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>,
bool>::type = true>
Generic(T&& _value) noexcept : value_(std::forward<T>(_value)) {}
~Generic();
/// Returns the underlying object.
const VariantType& get() const { return value_; }
/// Whether the object contains the null value.
bool is_null() const noexcept;
/// Assigns the underlying object.
Generic& operator=(const VariantType& _value);
/// Assigns the underlying object.
Generic& operator=(VariantType&& _value) noexcept;
/// Assigns the underlying object.
template <class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>,
bool>::type = true>
auto& operator=(const T& _value) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_same_v<Type, bool>) {
value_.emplace<0>(_value);
} else if constexpr (std::is_integral_v<Type>) {
value_.emplace<1>(static_cast<int64_t>(_value));
} else if constexpr (std::is_floating_point_v<Type>) {
value_.emplace<2>(static_cast<double>(_value));
} else {
value_ = _value;
}
return *this;
}
/// Assigns the underlying object.
Generic& operator=(const Generic& _other);
/// Assigns the underlying object.
Generic& operator=(Generic&& _other);
/// Returns the underlying object, necessary for the serialization to work.
ReflectionType reflection() const noexcept;
/// 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<Array> to_array() const noexcept {
return std::visit(
[](auto _v) -> Result<Array> {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, Array>) {
return _v;
} else {
return error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Array.");
}
},
value_);
}
/// Casts the underlying value to a boolean or returns an rfl::Error, if the
/// underlying value is not a boolean.
Result<bool> to_bool() const noexcept {
return std::visit(
[](auto _v) -> Result<bool> {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, bool>) {
return _v;
} else {
return error(
"rfl::Generic: Could not cast the underlying value to a "
"boolean.");
}
},
value_);
}
/// Casts the underlying value to a double or returns an rfl::Error, if the
/// underlying value is not a double.
Result<double> to_double() const noexcept {
return std::visit(
[](auto _v) -> Result<double> {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, double>) {
return _v;
} else {
return error(
"rfl::Generic: Could not cast the underlying value to a "
"double.");
}
},
value_);
}
/// Casts the underlying value to an integer or returns an rfl::Error, if the
/// underlying value is not an integer.
Result<int> to_int() const noexcept {
return std::visit(
[](auto _v) -> Result<int> {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, int64_t>) {
return static_cast<int>(_v);
} else {
return error(
"rfl::Generic: Could not cast the underlying value to an "
"integer.");
}
},
value_);
}
/// Casts the underlying value to an int64 or returns an rfl::Error, if the
/// underlying value is not an integer.
Result<int64_t> to_int64() const noexcept {
return std::visit(
[](auto _v) -> Result<int64_t> {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, int64_t>) {
return _v;
} else {
return error(
"rfl::Generic: Could not cast the underlying value to an "
"int64.");
}
},
value_);
}
/// 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<Object> to_object() const noexcept {
return std::visit(
[](auto _v) -> Result<Object> {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, Object>) {
return _v;
} else {
return error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Object.");
}
},
value_);
}
/// Casts the underlying value to rfl::Generic::Null or returns an
/// rfl::Error, if the underlying value is not rfl::Generic::Null.
Result<std::nullopt_t> to_null() const noexcept {
return std::visit(
[](auto _v) -> Result<std::nullopt_t> {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, std::nullopt_t>) {
return _v;
} else {
return error(
"rfl::Generic: Could not cast the underlying value to "
"rfl::Generic::Null.");
}
},
value_);
}
/// Casts the underlying value to a string or returns an rfl::Error, if the
/// underlying value is not a string.
Result<std::string> to_string() const noexcept {
return std::visit(
[](auto _v) -> Result<std::string> {
using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, std::string>) {
return _v;
} else {
return error(
"rfl::Generic: Could not cast the underlying value to a "
"string.");
}
},
value_);
}
/// Returns the underlying variant.
VariantType& variant() noexcept { return value_; };
/// Returns the underlying variant.
const VariantType& variant() const noexcept { return value_; };
private:
static VariantType from_reflection_type(const ReflectionType& _r) noexcept;
private:
VariantType 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<Generic::Array> 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<bool> 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<double> 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<int> 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<long> 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<Generic::Object> to_object(const Generic& _g) noexcept {
return _g.to_object();
}
/// Casts the underlying value to a double or returns an rfl::Error, if the
/// underlying value is not a double.
inline Result<std::nullopt_t> to_null(const Generic& _g) noexcept {
return _g.to_null();
}
/// Casts the underlying value to a string or returns an rfl::Error, if the
/// underlying value is not a string.
inline Result<std::string> to_string(const Generic& _g) noexcept {
return _g.to_string();
}
} // namespace rfl
#endif
|