Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/rfl/Generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, long, int, double, std::string, Object,
Array, std::nullopt_t>;
using ReflectionType = std::optional<
std::variant<bool, int, double, std::string, Object, Array>>;
std::variant<bool, long, int, double, std::string, Object, Array>>;

Generic();

Expand Down Expand Up @@ -94,6 +94,10 @@ class Generic {
/// underlying value is not an integer.
Result<int> to_int() const noexcept;

/// Casts the underlying value to a long or returns an rfl::Error, if the
/// underlying value is not a long.
Result<long> to_long() const noexcept;

/// 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;
Expand Down Expand Up @@ -139,6 +143,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 a long or returns an rfl::Error, if the
/// underlying value is not a long.
inline Result<long> to_long(const Generic& _g) noexcept { return _g.to_long(); }

/// 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 {
Expand Down
9 changes: 7 additions & 2 deletions include/rfl/generic/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,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_long().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.");
}
Expand Down
6 changes: 5 additions & 1 deletion include/rfl/generic/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ 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));
if constexpr (sizeof(T) > sizeof(int)) {
return OutputVarType(static_cast<long>(_var));
} else {
return OutputVarType(static_cast<int>(_var));
}
} else {
static_assert(always_false_v<T>, "Unsupported type");
}
Expand Down
13 changes: 13 additions & 0 deletions src/rfl/Generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,25 @@ Result<double> Generic::to_double() const noexcept {
Result<int> Generic::to_int() const noexcept {
if (const int* ptr = std::get_if<int>(&value_)) {
return *ptr;
} else if (const long* ptr = std::get_if<long>(&value_)) {
return static_cast<int>(*ptr);
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an integer.");
}
}

Result<long> Generic::to_long() const noexcept {
if (const long* ptr = std::get_if<long>(&value_)) {
return *ptr;
} else if (const int* ptr = std::get_if<int>(&value_)) {
return static_cast<long>(*ptr);
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a long.");
}
}

Result<Generic::Object> Generic::to_object() const noexcept {
if (const auto* ptr = std::get_if<Object>(&value_)) {
return *ptr;
Expand Down
Loading