rfl::Generic: add support for long (int64) (#333)

This commit is contained in:
Brett Jia
2025-02-07 21:54:13 +01:00
committed by GitHub
parent 7051ee36dc
commit de246990f9
3 changed files with 27 additions and 7 deletions
+19 -4
View File
@@ -19,10 +19,10 @@ class Generic {
using Array = std::vector<Generic>; using Array = std::vector<Generic>;
using Object = rfl::Object<Generic>; using Object = rfl::Object<Generic>;
using VariantType = std::variant<bool, int, double, std::string, Object, using VariantType = std::variant<bool, int64_t, double, std::string, Object,
Array, std::nullopt_t>; Array, std::nullopt_t>;
using ReflectionType = std::optional< using ReflectionType = std::optional<
std::variant<bool, int, double, std::string, Object, Array>>; std::variant<bool, int64_t, double, std::string, Object, Array>>;
Generic(); Generic();
@@ -115,14 +115,25 @@ class Generic {
/// Casts the underlying value to an integer or returns an rfl::Error, if the /// Casts the underlying value to an integer or returns an rfl::Error, if the
/// underlying value is not an integer. /// underlying value is not an integer.
Result<int> to_int() const noexcept { Result<int> to_int() const noexcept {
if (const int* ptr = std::get_if<int>(&value_)) { if (const int64_t* ptr = std::get_if<int64_t>(&value_)) {
return *ptr; return static_cast<int>(*ptr);
} else { } else {
return error( return error(
"rfl::Generic: Could not cast the underlying value to an integer."); "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<int64_t> to_int64() const noexcept {
if (const int64_t* ptr = std::get_if<int64_t>(&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 /// Casts the underlying value to an rfl::Generic::Object or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Object. /// rfl::Error, if the underlying value is not an rfl::Generic::Object.
Result<Object> to_object() const noexcept { Result<Object> to_object() const noexcept {
@@ -191,6 +202,10 @@ inline Result<double> to_double(const Generic& _g) noexcept {
/// underlying value is not an integer. /// underlying value is not an integer.
inline Result<int> to_int(const Generic& _g) noexcept { return _g.to_int(); } 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 /// Casts the underlying value to an rfl::Generic::Object or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Object. /// rfl::Error, if the underlying value is not an rfl::Generic::Object.
inline Result<Generic::Object> to_object(const Generic& _g) noexcept { inline Result<Generic::Object> to_object(const Generic& _g) noexcept {
+7 -2
View File
@@ -53,8 +53,13 @@ struct Reader {
return _var.to_double().transform( return _var.to_double().transform(
[](const auto& _v) { return static_cast<T>(_v); }); [](const auto& _v) { return static_cast<T>(_v); });
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
return _var.to_int().transform( if constexpr (sizeof(T) > sizeof(int)) {
[](const auto& _v) { return static_cast<T>(_v); }); return _var.to_int64().transform(
[](const auto& _v) { return static_cast<T>(_v); });
} else {
return _var.to_int().transform(
[](const auto& _v) { return static_cast<T>(_v); });
}
} else { } else {
static_assert(rfl::always_false_v<T>, "Unsupported type."); static_assert(rfl::always_false_v<T>, "Unsupported type.");
} }
+1 -1
View File
@@ -101,7 +101,7 @@ struct Writer {
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
return OutputVarType(static_cast<double>(_var)); return OutputVarType(static_cast<double>(_var));
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
return OutputVarType(static_cast<int>(_var)); return OutputVarType(static_cast<int64_t>(_var));
} else { } else {
static_assert(always_false_v<T>, "Unsupported type"); static_assert(always_false_v<T>, "Unsupported type");
} }