mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
rfl::Generic: add support for long (int64) (#333)
This commit is contained in:
+19
-4
@@ -19,10 +19,10 @@ class Generic {
|
||||
|
||||
using Array = std::vector<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>;
|
||||
using ReflectionType = std::optional<
|
||||
std::variant<bool, int, double, std::string, Object, Array>>;
|
||||
std::variant<bool, int64_t, double, std::string, Object, Array>>;
|
||||
|
||||
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<int> to_int() const noexcept {
|
||||
if (const int* ptr = std::get_if<int>(&value_)) {
|
||||
return *ptr;
|
||||
if (const int64_t* ptr = std::get_if<int64_t>(&value_)) {
|
||||
return static_cast<int>(*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<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
|
||||
/// rfl::Error, if the underlying value is not an rfl::Generic::Object.
|
||||
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.
|
||||
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 {
|
||||
|
||||
@@ -53,8 +53,13 @@ struct Reader {
|
||||
return _var.to_double().transform(
|
||||
[](const auto& _v) { return static_cast<T>(_v); });
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return _var.to_int().transform(
|
||||
[](const auto& _v) { return static_cast<T>(_v); });
|
||||
if constexpr (sizeof(T) > sizeof(int)) {
|
||||
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 {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ struct Writer {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(static_cast<double>(_var));
|
||||
} 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 {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user