mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
Added support for std::unique_ptr and rfl::Box
This commit is contained in:
@@ -109,7 +109,7 @@ reflect-cpp supports the following containers from the C++ standard library:
|
||||
- `std::shared_ptr`
|
||||
- `std::string`
|
||||
- `std::tuple`
|
||||
- `std::unique_ptr` (TODO)
|
||||
- `std::unique_ptr`
|
||||
- `std::unordered_map`
|
||||
- `std::unordered_set`
|
||||
- `std::variant`
|
||||
@@ -119,7 +119,7 @@ reflect-cpp supports the following containers from the C++ standard library:
|
||||
|
||||
In addition, it includes the following custom containers:
|
||||
|
||||
- `rfl::Box`: Similar to `std::unique_ptr`, but guaranteed to never be null (TODO).
|
||||
- `rfl::Box`: Similar to `std::unique_ptr`, but guaranteed to never be null.
|
||||
- `rfl::Literal`: An explicitly enumerated string.
|
||||
- `rfl::NamedTuple`: Similar to `std::tuple`, but with named fields that can be retrieved via their name at compile time.
|
||||
- `rfl::Ref`: Similar to `std::shared_ptr`, but guaranteed to never be null.
|
||||
|
||||
+3
-2
@@ -18,6 +18,8 @@ class Box {
|
||||
return Box<T>(std::make_unique<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
|
||||
Box(Box<T>&& _other) : ptr_(std::move(_other.ptr_)) {}
|
||||
|
||||
~Box() = default;
|
||||
|
||||
/// Returns a pointer to the underlying object
|
||||
@@ -34,8 +36,7 @@ class Box {
|
||||
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Box(std::unique_ptr<T>&& _ptr)
|
||||
: ptr_(std::forward<std::unique_ptr<T>>(_ptr)) {}
|
||||
explicit Box(std::unique_ptr<T>&& _ptr) : ptr_(std::move(_ptr)) {}
|
||||
|
||||
private:
|
||||
/// The underlying unique_ptr_
|
||||
|
||||
@@ -26,11 +26,11 @@ struct Field {
|
||||
|
||||
Field(Type&& _value) noexcept : value_(std::move(_value)) {}
|
||||
|
||||
Field(const Field<_name, Type>& _field) : value_(_field.get()) {}
|
||||
|
||||
Field(Field<_name, Type>&& _field) noexcept
|
||||
: value_(std::move(_field.value_)) {}
|
||||
|
||||
Field(const Field<_name, Type>& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class T>
|
||||
Field(const Field<_name, T>& _field) : value_(_field.get()) {}
|
||||
|
||||
@@ -130,6 +130,11 @@ struct Field {
|
||||
Type value_;
|
||||
};
|
||||
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
inline auto make_field(_Type&& _value) {
|
||||
return Field<_name, _Type>(std::forward<_Type>(_value));
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
inline auto make_field(const _Type& _value) {
|
||||
return Field<_name, _Type>(_value);
|
||||
|
||||
@@ -49,11 +49,15 @@ struct Flatten {
|
||||
inline const Type& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(const Type& _value) { value_ = _value; }
|
||||
inline Flatten<Type> operator=(const Type& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline void operator=(Type&& _value) {
|
||||
inline Flatten<Type> operator=(Type&& _value) {
|
||||
value_ = std::forward<Type>(_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
|
||||
@@ -22,11 +22,26 @@ template <class... FieldTypes>
|
||||
class NamedTuple {
|
||||
public:
|
||||
using Fields = std::tuple<std::decay_t<FieldTypes>...>;
|
||||
using Values = std::tuple<typename std::decay<FieldTypes>::type::Type...>;
|
||||
|
||||
public:
|
||||
/// Construct from the fields.
|
||||
NamedTuple(FieldTypes... _fields)
|
||||
: values_(std::forward_as_tuple(_fields.value_...)) {
|
||||
NamedTuple(FieldTypes&&... _fields)
|
||||
: values_(std::make_tuple(std::move(_fields.value_)...)) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
/// Construct from the fields.
|
||||
NamedTuple(const FieldTypes&... _fields)
|
||||
: values_(std::make_tuple(_fields.value_...)) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
/// Construct from a tuple containing fields.
|
||||
NamedTuple(std::tuple<FieldTypes...>&& _tup)
|
||||
: NamedTuple(std::make_from_tuple<NamedTuple<FieldTypes...>>(_tup)) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
@@ -250,10 +265,10 @@ class NamedTuple {
|
||||
}
|
||||
|
||||
/// Returns the underlying std::tuple.
|
||||
auto& values() { return values_; }
|
||||
Values& values() { return values_; }
|
||||
|
||||
/// Returns the underlying std::tuple.
|
||||
const auto& values() const { return values_; }
|
||||
const Values& values() const { return values_; }
|
||||
|
||||
private:
|
||||
/// Adds the elements of a tuple to a newly created named tuple,
|
||||
@@ -367,7 +382,7 @@ class NamedTuple {
|
||||
/// As you can see, a NamedTuple is just a normal tuple under-the-hood,
|
||||
/// everything else is resolved at compile time. It should have no
|
||||
/// runtime overhead over a normal std::tuple.
|
||||
std::tuple<typename std::decay<FieldTypes>::type::Type...> values_;
|
||||
Values values_;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
+1
-2
@@ -57,8 +57,7 @@ class Ref {
|
||||
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Ref(std::shared_ptr<T>&& _ptr)
|
||||
: ptr_(std::forward<std::shared_ptr<T>>(_ptr)) {}
|
||||
explicit Ref(std::shared_ptr<T>&& _ptr) : ptr_(std::move(_ptr)) {}
|
||||
|
||||
private:
|
||||
/// The underlying shared_ptr_
|
||||
|
||||
+54
-226
@@ -30,31 +30,6 @@ class Error {
|
||||
/// successful.
|
||||
struct Nothing {};
|
||||
|
||||
/// Helper class to be used to within the Result class to support the operator*.
|
||||
/// This is necessary, because we only want this class to be unwrapped, not
|
||||
/// normal std::tuple.
|
||||
template <class... Args>
|
||||
struct HelperTuple {
|
||||
using TupleType = std::tuple<Args...>;
|
||||
TupleType tuple_;
|
||||
};
|
||||
|
||||
template <class>
|
||||
struct is_helper_tuple : std::false_type {};
|
||||
|
||||
template <class... Args>
|
||||
struct is_helper_tuple<HelperTuple<Args...>> : std::true_type {};
|
||||
|
||||
template <class T>
|
||||
struct value_type {
|
||||
using Type = T;
|
||||
};
|
||||
|
||||
template <class... Args>
|
||||
struct value_type<HelperTuple<Args...>> {
|
||||
using Type = HelperTuple<Args...>::TupleType;
|
||||
};
|
||||
|
||||
/// The Result class is used for monadic error handling.
|
||||
template <class T>
|
||||
class Result {
|
||||
@@ -62,15 +37,13 @@ class Result {
|
||||
"The result type cannot be Error.");
|
||||
|
||||
public:
|
||||
using ValueType = typename value_type<T>::Type;
|
||||
|
||||
Result(const T& _val) : t_or_err_(_val) {}
|
||||
|
||||
Result(T&& _val) noexcept : t_or_err_(std::forward<T>(_val)) {}
|
||||
Result(T&& _val) noexcept : t_or_err_(std::move(_val)) {}
|
||||
|
||||
Result(const Error& _err) : t_or_err_(_err) {}
|
||||
|
||||
Result(Error&& _err) noexcept : t_or_err_(std::forward<Error>(_err)) {}
|
||||
Result(Error&& _err) noexcept : t_or_err_(std::move(_err)) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
@@ -91,48 +64,32 @@ class Result {
|
||||
/// Monadic operation - F must be a function of type T -> Result<U>.
|
||||
template <class F>
|
||||
auto and_then(const F& _f) {
|
||||
const auto apply = [&_f](auto&& _t) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
using TupleType = std::decay_t<decltype(_t.tuple)>;
|
||||
return std::apply(_f, std::forward<TupleType>(_t.tuple_));
|
||||
} else {
|
||||
return _f(_t);
|
||||
}
|
||||
};
|
||||
|
||||
/// Result_U is expected to be of type Result<U>.
|
||||
using Result_U = typename std::invoke_result<decltype(apply), T>::type;
|
||||
using Result_U = typename std::invoke_result<F, T>::type;
|
||||
|
||||
const auto handle_variant =
|
||||
[apply]<class TOrError>(TOrError&& _t_or_err) -> Result_U {
|
||||
[&]<class TOrError>(TOrError&& _t_or_err) -> Result_U {
|
||||
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
|
||||
return apply(std::forward<TOrError>(_t_or_err));
|
||||
return _f(std::forward<TOrError>(_t_or_err));
|
||||
} else {
|
||||
return _t_or_err;
|
||||
return std::forward<TOrError>(_t_or_err);
|
||||
}
|
||||
};
|
||||
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
return std::visit(handle_variant,
|
||||
std::forward<std::variant<T, Error>>(t_or_err_));
|
||||
}
|
||||
|
||||
/// Monadic operation - F must be a function of type T -> Result<U>.
|
||||
template <class F>
|
||||
auto and_then(const F& _f) const {
|
||||
const auto apply = [&_f](const auto& _t) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return std::apply(_f, _t.tuple_);
|
||||
} else {
|
||||
return _f(_t);
|
||||
}
|
||||
};
|
||||
|
||||
/// Result_U is expected to be of type Result<U>.
|
||||
using Result_U = typename std::invoke_result<decltype(apply), T>::type;
|
||||
using Result_U = typename std::invoke_result<F, T>::type;
|
||||
|
||||
const auto handle_variant =
|
||||
[apply]<class TOrError>(const TOrError& _t_or_err) -> Result_U {
|
||||
[&]<class TOrError>(const TOrError& _t_or_err) -> Result_U {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
return apply(_t_or_err);
|
||||
return _f(_t_or_err);
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
@@ -143,15 +100,11 @@ class Result {
|
||||
|
||||
/// Results types can be iterated over, which even make it possible to use
|
||||
/// them within a std::range.
|
||||
ValueType* begin() noexcept {
|
||||
T* begin() noexcept {
|
||||
const auto get_ptr =
|
||||
[this]<class TOrError>(const TOrError& _t_or_err) -> ValueType* {
|
||||
[this]<class TOrError>(const TOrError& _t_or_err) -> T* {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return &_t_or_err.tuple_;
|
||||
} else {
|
||||
return &_t_or_err;
|
||||
}
|
||||
return &_t_or_err;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -161,16 +114,11 @@ class Result {
|
||||
|
||||
/// Results types can be iterated over, which even make it possible to use
|
||||
/// them within a std::range.
|
||||
const ValueType* begin() const noexcept {
|
||||
const T* begin() const noexcept {
|
||||
const auto get_ptr =
|
||||
[this]<class TOrError>(
|
||||
const TOrError& _t_or_err) -> const ValueType* {
|
||||
[this]<class TOrError>(const TOrError& _t_or_err) -> const T* {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return &_t_or_err.tuple_;
|
||||
} else {
|
||||
return &_t_or_err;
|
||||
}
|
||||
return &_t_or_err;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -180,15 +128,11 @@ class Result {
|
||||
|
||||
/// Results types can be iterated over, which even make it possible to use
|
||||
/// them within a std::range.
|
||||
ValueType* end() noexcept {
|
||||
T* end() noexcept {
|
||||
const auto get_ptr =
|
||||
[this]<class TOrError>(const TOrError& _t_or_err) -> ValueType* {
|
||||
[this]<class TOrError>(const TOrError& _t_or_err) -> T* {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return &_t_or_err.tuple_ + 1;
|
||||
} else {
|
||||
return &_t_or_err + 1;
|
||||
}
|
||||
return &_t_or_err + 1;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -198,16 +142,11 @@ class Result {
|
||||
|
||||
/// Results types can be iterated over, which even make it possible to use
|
||||
/// them within a std::range.
|
||||
const ValueType* end() const noexcept {
|
||||
const T* end() const noexcept {
|
||||
const auto get_ptr =
|
||||
[this]<class TOrError>(
|
||||
const TOrError& _t_or_err) -> const ValueType* {
|
||||
[this]<class TOrError>(const TOrError& _t_or_err) -> const T* {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return &_t_or_err.tuple_ + 1;
|
||||
} else {
|
||||
return &_t_or_err + 1;
|
||||
}
|
||||
return &_t_or_err + 1;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -251,6 +190,22 @@ class Result {
|
||||
t_or_err_ = _other.transform(to_t).t_or_err_;
|
||||
}
|
||||
|
||||
/// Expects a function that takes of type Error -> Result<T> and returns
|
||||
/// Result<T>.
|
||||
template <class F>
|
||||
Result<T> or_else(const F& _f) {
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(TOrError&& _t_or_err) -> Result<T> {
|
||||
if constexpr (std::is_same<TOrError, Error>()) {
|
||||
return _f(std::forward<TOrError>(_t_or_err));
|
||||
} else {
|
||||
return std::forward<TOrError>(_t_or_err);
|
||||
}
|
||||
};
|
||||
return std::visit(handle_variant,
|
||||
std::forward<std::variant<T, Error>>(t_or_err_));
|
||||
}
|
||||
|
||||
/// Expects a function that takes of type Error -> Result<T> and returns
|
||||
/// Result<T>.
|
||||
template <class F>
|
||||
@@ -276,49 +231,32 @@ class Result {
|
||||
/// Functor operation - F must be a function of type T -> U.
|
||||
template <class F>
|
||||
auto transform(const F& _f) {
|
||||
const auto apply = [&_f](auto&& _t) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
using TupleType = std::decay_t<decltype(_t.tuple)>;
|
||||
return std::apply(_f, std::forward<TupleType>(_t.tuple_));
|
||||
} else {
|
||||
return _f(std::forward<T>(_t));
|
||||
}
|
||||
};
|
||||
|
||||
/// Result_U is expected to be of type Result<U>.
|
||||
using U = typename std::invoke_result<decltype(apply), T>::type;
|
||||
using U = typename std::invoke_result<F, T>::type;
|
||||
|
||||
const auto handle_variant =
|
||||
[apply]<class TOrError>(TOrError&& _t_or_err) -> rfl::Result<U> {
|
||||
[&]<class TOrError>(TOrError&& _t_or_err) -> rfl::Result<U> {
|
||||
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
|
||||
return apply(std::forward<TOrError>(_t_or_err));
|
||||
return _f(std::forward<TOrError>(_t_or_err));
|
||||
} else {
|
||||
return _t_or_err;
|
||||
return std::forward<TOrError>(_t_or_err);
|
||||
}
|
||||
};
|
||||
|
||||
return std::visit(handle_variant, t_or_err_);
|
||||
return std::visit(handle_variant,
|
||||
std::forward<std::variant<T, Error>>(t_or_err_));
|
||||
}
|
||||
|
||||
/// Functor operation - F must be a function of type T -> U.
|
||||
template <class F>
|
||||
auto transform(const F& _f) const {
|
||||
const auto apply = [&_f](const auto& _t) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return std::apply(_f, _t.tuple_);
|
||||
} else {
|
||||
return _f(_t);
|
||||
}
|
||||
};
|
||||
|
||||
/// Result_U is expected to be of type Result<U>.
|
||||
using U = typename std::invoke_result<decltype(apply), T>::type;
|
||||
using U = typename std::invoke_result<F, T>::type;
|
||||
|
||||
const auto handle_variant =
|
||||
[apply]<class TOrError>(
|
||||
const TOrError& _t_or_err) -> rfl::Result<U> {
|
||||
[&]<class TOrError>(const TOrError& _t_or_err) -> rfl::Result<U> {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
return apply(_t_or_err);
|
||||
return _f(_t_or_err);
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
@@ -329,15 +267,11 @@ class Result {
|
||||
|
||||
/// Returns the value if the result does not contain an error, throws an
|
||||
/// exceptions if not. Similar to .unwrap() in Rust.
|
||||
ValueType& value() {
|
||||
T& value() {
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(TOrError& _t_or_err) -> ValueType& {
|
||||
[&]<class TOrError>(TOrError& _t_or_err) -> T& {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return _t_or_err.tuple_;
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
return _t_or_err;
|
||||
} else {
|
||||
throw std::runtime_error(_t_or_err.what());
|
||||
}
|
||||
@@ -347,15 +281,11 @@ class Result {
|
||||
|
||||
/// Returns the value if the result does not contain an error, throws an
|
||||
/// exceptions if not. Similar to .unwrap() in Rust.
|
||||
const ValueType& value() const {
|
||||
const T& value() const {
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(const TOrError& _t_or_err) -> const ValueType& {
|
||||
[&]<class TOrError>(const TOrError& _t_or_err) -> const T& {
|
||||
if constexpr (!std::is_same<TOrError, Error>()) {
|
||||
if constexpr (is_helper_tuple<T>()) {
|
||||
return _t_or_err.tuple_;
|
||||
} else {
|
||||
return _t_or_err;
|
||||
}
|
||||
return _t_or_err;
|
||||
} else {
|
||||
throw std::runtime_error(_t_or_err.what());
|
||||
}
|
||||
@@ -381,108 +311,6 @@ class Result {
|
||||
std::variant<T, Error> t_or_err_;
|
||||
};
|
||||
|
||||
/// operator* allows to combine result types as a product type.
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(const Result<T>& _rt, const Result<U>& _ru) {
|
||||
const auto f1 = [&_ru](const T& _t) {
|
||||
const auto f2 = [&_t](const U& _u) {
|
||||
return HelperTuple<T, U>(std::make_tuple(_t, _u));
|
||||
};
|
||||
return _ru.transform(f2);
|
||||
};
|
||||
return _rt.and_then(f1);
|
||||
}
|
||||
|
||||
template <class... T, class... U>
|
||||
inline auto operator*(const Result<HelperTuple<T...>>& _rt,
|
||||
const Result<HelperTuple<U...>>& _ru) {
|
||||
const auto f1 = [&_ru](const T&... _t) {
|
||||
const auto f2 = [&](const U&... _u) {
|
||||
return HelperTuple<T..., U...>(std::make_tuple(_t..., _u...));
|
||||
};
|
||||
return _ru.transform(f2);
|
||||
};
|
||||
return _rt.and_then(f1);
|
||||
}
|
||||
|
||||
template <class... T, class U>
|
||||
inline auto operator*(const Result<const HelperTuple<T...>>& _rt,
|
||||
const Result<U>& _ru) {
|
||||
const auto f = [](const U& _u) {
|
||||
return HelperTuple<U>(std::make_tuple(_u));
|
||||
};
|
||||
return _rt * _ru.transform(f);
|
||||
}
|
||||
|
||||
template <class T, class... U>
|
||||
inline auto operator*(const Result<T>& _rt,
|
||||
const Result<HelperTuple<U...>>& _ru) {
|
||||
const auto f = [](const T& _t) {
|
||||
return HelperTuple<T>(std::make_tuple(_t));
|
||||
};
|
||||
return _rt.transform(f) * _ru;
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(const Result<T>& _rt, const U& _u) {
|
||||
return _rt * Result<U>(_u);
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(const T& _t, const Result<U>& _ru) {
|
||||
return Result<T>(_t) * _ru;
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(Result<T>&& _rt, Result<U>&& _ru) {
|
||||
const auto f1 = [&_ru](T&& _t) {
|
||||
const auto f2 = [&_t](U&& _u) {
|
||||
return HelperTuple<T, U>(std::forward_as_tuple(_t, _u));
|
||||
};
|
||||
return _ru.transform(f2);
|
||||
};
|
||||
return _rt.and_then(f1);
|
||||
}
|
||||
|
||||
template <class... T, class... U>
|
||||
inline auto operator*(Result<HelperTuple<T...>>&& _rt,
|
||||
Result<HelperTuple<U...>>&& _ru) {
|
||||
const auto f1 = [&_ru](T&&... _t) {
|
||||
const auto f2 = [&](U&&... _u) {
|
||||
return HelperTuple<T..., U...>(std::forward_as_tuple(_t..., _u...));
|
||||
};
|
||||
return _ru.transform(f2);
|
||||
};
|
||||
return _rt.and_then(f1);
|
||||
}
|
||||
|
||||
template <class... T, class U>
|
||||
inline auto operator*(Result<HelperTuple<T...>>&& _rt, Result<U>&& _ru) {
|
||||
const auto f = [](U&& _u) {
|
||||
return HelperTuple<U>(std::forward_as_tuple(_u));
|
||||
};
|
||||
return _rt * _ru.transform(f);
|
||||
}
|
||||
|
||||
template <class T, class... U>
|
||||
inline auto operator*(Result<T>&& _rt, Result<HelperTuple<U...>>&& _ru) {
|
||||
const auto f = [](T&& _t) {
|
||||
return HelperTuple<T>(std::forward_as_tuple(_t));
|
||||
};
|
||||
return _rt.transform(f) * _ru;
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(Result<T>&& _rt, U&& _u) {
|
||||
return _rt * Result<U>(_u);
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
inline auto operator*(T&& _t, Result<U>&& _ru) {
|
||||
return Result<T>(_t) * _ru;
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,15 +4,15 @@
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/field_tuple_t.hpp"
|
||||
#include "rfl/internal/has_flatten_fields.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/ptr_field_tuple_t.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class FieldTupleType, class NamedTupleType, class... Args>
|
||||
FieldTupleType concat_named_tuple(const NamedTupleType& _n, Args&&... _args);
|
||||
FieldTupleType concat_named_tuple(NamedTupleType&& _n, Args&&... _args);
|
||||
|
||||
/// Creates a struct of type T from a named tuple.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
@@ -24,17 +24,20 @@ T from_named_tuple(NamedTupleType&& _n) {
|
||||
return std::forward<NamedTupleType>(_n);
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
const auto make = []<class... FieldTypes>(FieldTypes&&... _fields) {
|
||||
return T{std::forward<FieldTypes>(_fields)...};
|
||||
const auto make = []<class... ValueTypes>(ValueTypes&&... _values) {
|
||||
return T{std::forward<ValueTypes>(_values)...};
|
||||
};
|
||||
|
||||
using FieldTupleType = internal::field_tuple_t<std::decay_t<T>>;
|
||||
using PtrFieldTupleType = internal::ptr_field_tuple_t<std::decay_t<T>>;
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<FieldTupleType>()) {
|
||||
return std::apply(make, _n.fields());
|
||||
if constexpr (!internal::has_flatten_fields<PtrFieldTupleType>()) {
|
||||
return std::apply(
|
||||
make,
|
||||
std::forward<typename NamedTupleType::Values>(_n.values()));
|
||||
} else {
|
||||
const auto fields = concat_named_tuple<FieldTupleType>(_n);
|
||||
return std::apply(make, fields);
|
||||
auto fields = concat_named_tuple<PtrFieldTupleType>(_n);
|
||||
using FieldsType = std::decay_t<decltype(fields)>;
|
||||
return std::apply(make, std::forward<FieldsType>(fields));
|
||||
}
|
||||
|
||||
} else {
|
||||
@@ -45,7 +48,7 @@ T from_named_tuple(NamedTupleType&& _n) {
|
||||
|
||||
/// Creates a struct of type T from a named tuple.
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T, class NamedTupleType>
|
||||
/*template <class T, class NamedTupleType>
|
||||
T from_named_tuple(const NamedTupleType& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
@@ -53,7 +56,8 @@ T from_named_tuple(const NamedTupleType& _n) {
|
||||
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
const auto make = [](const auto&... _fields) { return T{_fields...}; };
|
||||
const auto make = [](const auto&... _fields) { return T{_fields...};
|
||||
};
|
||||
|
||||
using FieldTupleType = internal::field_tuple_t<T>;
|
||||
|
||||
@@ -67,10 +71,10 @@ T from_named_tuple(const NamedTupleType& _n) {
|
||||
} else {
|
||||
return from_named_tuple<T, RequiredType>(RequiredType(_n));
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
template <class FieldTupleType, class NamedTupleType, class... Args>
|
||||
FieldTupleType concat_named_tuple(const NamedTupleType& _n, Args&&... _args) {
|
||||
FieldTupleType concat_named_tuple(NamedTupleType&& _n, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTupleType>>) {
|
||||
return std::make_tuple(std::forward<Args>(_args)...);
|
||||
@@ -78,12 +82,12 @@ FieldTupleType concat_named_tuple(const NamedTupleType& _n, Args&&... _args) {
|
||||
using T = std::tuple_element_t<i, FieldTupleType>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
return concat_named_tuple<FieldTupleType>(
|
||||
_n, std::forward<Args>(_args)...,
|
||||
_n, std::move(_args)...,
|
||||
from_named_tuple<typename T::Type>(_n));
|
||||
} else {
|
||||
return concat_named_tuple<FieldTupleType>(
|
||||
_n, std::forward<Args>(_args)...,
|
||||
_n.template get_field<T::name_>());
|
||||
std::move(_n.template get_field<T::name_>()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
#ifndef RFL_MOVE_FROM_NAMED_TUPLE_HPP_
|
||||
#define RFL_MOVE_FROM_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/is_field.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/nt_to_ptr_named_tuple.hpp"
|
||||
#include "rfl/internal/ptr_field_tuple_t.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class PtrFieldTupleType, class PtrNamedTupleType, class... Args>
|
||||
auto make_pointers(PtrNamedTupleType& _n, Args... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
|
||||
constexpr auto size = std::tuple_size_v<std::decay_t<PtrFieldTupleType>>;
|
||||
|
||||
if constexpr (i == size) {
|
||||
return std::make_tuple(_args...);
|
||||
} else {
|
||||
using Field = std::decay_t<std::tuple_element_t<i, PtrFieldTupleType>>;
|
||||
using T = std::decay_t<std::remove_pointer_t<typename Field::Type>>;
|
||||
|
||||
if constexpr (is_named_tuple_v<T>) {
|
||||
using SubPtrNamedTupleType =
|
||||
typename std::invoke_result<decltype(nt_to_ptr_named_tuple<T>),
|
||||
T>::type;
|
||||
|
||||
return make_pointers<PtrFieldTupleType>(
|
||||
_n, _args..., SubPtrNamedTupleType(_n).fields());
|
||||
|
||||
} else if constexpr (is_flatten_field<Field>::value) {
|
||||
using SubPtrFieldTupleType = std::decay_t<ptr_field_tuple_t<T>>;
|
||||
|
||||
return make_pointers<PtrFieldTupleType>(
|
||||
_n, _args..., make_pointers<SubPtrFieldTupleType>(_n));
|
||||
|
||||
} else {
|
||||
return make_pointers<PtrFieldTupleType>(
|
||||
_n, _args..., _n.template get_field<Field::name_>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class T, class Pointers, class... Args>
|
||||
auto move_from_pointers(Pointers& _ptrs, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<Pointers>>) {
|
||||
return T(std::move(_args)...);
|
||||
} else {
|
||||
using FieldType = std::tuple_element_t<i, std::decay_t<Pointers>>;
|
||||
|
||||
if constexpr (is_field_v<FieldType>) {
|
||||
return move_from_pointers<T>(
|
||||
_ptrs, std::move(_args)...,
|
||||
rfl::make_field<FieldType::name_>(
|
||||
std::move(*std::get<i>(_ptrs).value())));
|
||||
|
||||
} else {
|
||||
using PtrFieldTupleType = std::decay_t<ptr_field_tuple_t<T>>;
|
||||
|
||||
using U = std::decay_t<std::remove_pointer_t<
|
||||
typename std::tuple_element_t<i, PtrFieldTupleType>::Type>>;
|
||||
|
||||
return move_from_pointers<T>(
|
||||
_ptrs, std::move(_args)...,
|
||||
move_from_pointers<U>(std::get<i>(_ptrs)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a struct of type T from a named tuple by moving the underlying
|
||||
/// fields.
|
||||
template <class T, class NamedTupleType>
|
||||
T move_from_named_tuple(NamedTupleType& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
|
||||
if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
return std::move(_n);
|
||||
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
auto ptr_named_tuple = nt_to_ptr_named_tuple(_n);
|
||||
|
||||
using PtrFieldTupleType = std::decay_t<ptr_field_tuple_t<T>>;
|
||||
|
||||
auto pointers = make_pointers<PtrFieldTupleType>(ptr_named_tuple);
|
||||
|
||||
return move_from_pointers<T>(pointers);
|
||||
|
||||
} else {
|
||||
return move_from_named_tuple<T, RequiredType>(
|
||||
RequiredType(std::move(_n)));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef RFL_INTERNAL_NT_TO_PTR_NAMED_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_NT_TO_PTR_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <tuple>
|
||||
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/make_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// Generates a named tuple that contains pointers to the original values in
|
||||
/// the struct from a named tuple.
|
||||
template <class NamedTupleType, class... AlreadyExtracted>
|
||||
auto nt_to_ptr_named_tuple(NamedTupleType& _nt, AlreadyExtracted... _a) {
|
||||
using Fields = typename NamedTupleType::Fields;
|
||||
|
||||
constexpr auto i = sizeof...(AlreadyExtracted);
|
||||
constexpr auto num_fields = std::tuple_size<Fields>{};
|
||||
|
||||
if constexpr (i == num_fields) {
|
||||
return make_named_tuple(_a...);
|
||||
} else {
|
||||
using FieldType = typename std::tuple_element<i, Fields>::type;
|
||||
using T = std::decay_t<typename FieldType::Type>;
|
||||
return nt_to_ptr_named_tuple(
|
||||
_nt, _a...,
|
||||
Field<FieldType::name_, T*>(&std::get<i>(_nt.values())));
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a named tuple that contains pointers to the original values in
|
||||
/// the struct from a named tuple.
|
||||
template <class NamedTupleType, class... AlreadyExtracted>
|
||||
auto nt_to_ptr_named_tuple(const NamedTupleType& _nt, AlreadyExtracted... _a) {
|
||||
using Fields = typename NamedTupleType::Fields;
|
||||
|
||||
constexpr auto i = sizeof...(AlreadyExtracted);
|
||||
constexpr auto num_fields = std::tuple_size<Fields>{};
|
||||
|
||||
if constexpr (i == num_fields) {
|
||||
return make_named_tuple(_a...);
|
||||
} else {
|
||||
using FieldType = typename std::tuple_element<i, Fields>::type;
|
||||
using T = std::decay_t<typename FieldType::Type>;
|
||||
return nt_to_ptr_named_tuple(
|
||||
_nt, _a...,
|
||||
Field<FieldType::name_, const T*>(&std::get<i>(_nt.values())));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef RFL_INTERNAL_PTR_NAMED_TUPLE_T_HPP_
|
||||
#define RFL_INTERNAL_PTR_NAMED_TUPLE_T_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/to_ptr_field_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
using ptr_field_tuple_t =
|
||||
typename std::invoke_result<decltype(to_ptr_field_tuple<T>), T>::type;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef RFL_INTERNAL_PTR_FIELD_TUPLE_T_HPP_
|
||||
#define RFL_INTERNAL_PTR_FIELD_TUPLE_T_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/to_ptr_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
using ptr_named_tuple_t =
|
||||
typename std::invoke_result<decltype(to_ptr_named_tuple<T>), T>::type;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/internal/has_n_fields.hpp"
|
||||
@@ -33,13 +34,7 @@ if constexpr (internal::is_named_tuple_v<T>) {
|
||||
|
||||
main_part = "".join((make_field_template(i) for i in range(101)))
|
||||
|
||||
end = """} else {
|
||||
static_assert(rfl::always_false_v<T>, "Only up to 100 fields are
|
||||
supported.");
|
||||
}
|
||||
"""
|
||||
|
||||
code = beginning + main_part + end
|
||||
code = beginning + main_part
|
||||
|
||||
with open("generated_code2.cpp", "w", encoding="utf-8") as codefile:
|
||||
codefile.write(code)
|
||||
@@ -1085,9 +1080,23 @@ auto to_field_tuple(const T& _t) {
|
||||
f68, f69, f70, f71, f72, f73, f74, f75, f76, f77, f78, f79, f80,
|
||||
f81, f82, f83, f84, f85, f86, f87, f88, f89, f90, f91, f92, f93,
|
||||
f94, f95, f96, f97, f98, f99, f100);
|
||||
|
||||
// ---------------------------------
|
||||
// End of generated boilerplate code
|
||||
// ---------------------------------
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>,
|
||||
"Only up to 100 fields are supported.");
|
||||
static_assert(
|
||||
rfl::always_false_v<T>,
|
||||
"\n\nThis error occurs for one of two reasons:\n\n"
|
||||
"1) You have created a struct with more than 100 fields, which is "
|
||||
"unsupported. Please split up your struct into several "
|
||||
"smaller structs and then use rfl::Flatten<...> to combine them. "
|
||||
"Refer "
|
||||
"to the documentation on rfl::Flatten<...> for details.\n\n"
|
||||
"2) You have added a custom constructor to your struct, which you "
|
||||
"shouldn't do either. Please refer to the sections on custom "
|
||||
"classes or custom parsers in the documentation "
|
||||
"for solutions to this problem.\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef RFL_INTERNAL_TOPTRFIELD_HPP_
|
||||
#define RFL_INTERNAL_TOPTRFIELD_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
inline auto to_ptr_field(Field<_name, _Type>& _field) {
|
||||
using T = std::remove_reference_t<_Type>;
|
||||
return Field<_name, T*>(&_field.value_);
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
inline auto to_ptr_field(const Field<_name, _Type>& _field) {
|
||||
using T = std::remove_cvref_t<_Type>;
|
||||
return Field<_name, const T*>(&_field.value_);
|
||||
}
|
||||
|
||||
template <class _Type>
|
||||
inline auto to_ptr_field(Flatten<_Type>& _field) {
|
||||
using T = std::remove_reference_t<_Type>;
|
||||
return Flatten<T*>(&_field.value_);
|
||||
}
|
||||
|
||||
template <class _Type>
|
||||
inline auto to_ptr_field(const Flatten<_Type>& _field) {
|
||||
using T = std::remove_cvref_t<_Type>;
|
||||
return Flatten<const T*>(&_field.value_);
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,64 @@
|
||||
#ifndef RFL_INTERNAL_TO_PTR_NAMED_TUPLE_HPP_
|
||||
#define RFL_INTERNAL_TO_PTR_NAMED_TUPLE_HPP_
|
||||
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/internal/has_flatten_fields.hpp"
|
||||
#include "rfl/internal/is_field.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/to_ptr_field_tuple.hpp"
|
||||
#include "rfl/make_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class PtrFieldTuple, class... Args>
|
||||
auto flatten_ptr_field_tuple(const PtrFieldTuple& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<PtrFieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
return flatten_ptr_field_tuple(
|
||||
_t, std::forward<Args>(_args)...,
|
||||
flatten_ptr_field_tuple(
|
||||
internal::to_ptr_field_tuple(*std::get<i>(_t).get())));
|
||||
} else {
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a named tuple that contains pointers to the original values in
|
||||
/// the struct.
|
||||
template <class T>
|
||||
auto to_ptr_named_tuple(const T& _t) {
|
||||
if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
return nt_to_ptr_named_tuple(_t);
|
||||
} else {
|
||||
const auto ptr_field_tuple = to_ptr_field_tuple(_t);
|
||||
|
||||
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
|
||||
using PtrFieldTuple = std::decay_t<decltype(ptr_field_tuple)>;
|
||||
|
||||
if constexpr (!has_flatten_fields<PtrFieldTuple>()) {
|
||||
return std::apply(ft_to_nt, std::move(ptr_field_tuple));
|
||||
} else {
|
||||
const auto flattened_tuple =
|
||||
flatten_ptr_field_tuple(ptr_field_tuple);
|
||||
return std::apply(ft_to_nt, flattened_tuple);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -17,12 +17,12 @@ using InputVarType = typename Reader::InputVarType;
|
||||
/// Parses an object from JSON using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::string& _json_str) {
|
||||
using PtrType = std::unique_ptr<yyjson_doc, void (*)(yyjson_doc*)>;
|
||||
yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0);
|
||||
const auto ptr = PtrType(doc, yyjson_doc_free);
|
||||
InputVarType root = InputVarType(yyjson_doc_get_root(doc));
|
||||
const auto r = Reader();
|
||||
const auto result = Parser<T>::read(r, &root);
|
||||
yyjson_doc_free(doc);
|
||||
return result;
|
||||
return Parser<T>::read(r, &root);
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
|
||||
@@ -5,17 +5,36 @@
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
#include "rfl/internal/is_named_tuple.hpp"
|
||||
#include "rfl/internal/ptr_named_tuple_t.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class T>
|
||||
struct remove_ptr;
|
||||
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
struct remove_ptr<Field<_name, _Type>> {
|
||||
using FieldType = Field<_name, std::decay_t<std::remove_pointer_t<_Type>>>;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct remove_ptrs_nt;
|
||||
|
||||
template <class... FieldTypes>
|
||||
struct remove_ptrs_nt<NamedTuple<FieldTypes...>> {
|
||||
using NamedTupleType =
|
||||
NamedTuple<typename remove_ptr<FieldTypes>::FieldType...>;
|
||||
};
|
||||
|
||||
/// Generates the named tuple that is equivalent to the struct T.
|
||||
/// This is the result you would expect from calling to_named_tuple(my_struct).
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T>
|
||||
using named_tuple_t =
|
||||
typename std::invoke_result<decltype(to_named_tuple<T>), T>::type;
|
||||
typename remove_ptrs_nt<internal::ptr_named_tuple_t<T>>::NamedTupleType;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
|
||||
+120
-85
@@ -24,13 +24,16 @@
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/Result.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/all_fields.hpp"
|
||||
#include "rfl/internal/has_reflection_method_v.hpp"
|
||||
#include "rfl/internal/has_reflection_type_v.hpp"
|
||||
#include "rfl/internal/move_from_named_tuple.hpp"
|
||||
#include "rfl/internal/no_duplicate_field_names.hpp"
|
||||
#include "rfl/internal/to_ptr_named_tuple.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
#include "rfl/parsing/is_required.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
@@ -71,10 +74,9 @@ struct Parser {
|
||||
} else if constexpr (std::is_class_v<T> &&
|
||||
!std::is_same<T, std::string>()) {
|
||||
using NamedTupleType = named_tuple_t<T>;
|
||||
const auto to_struct =
|
||||
[](const NamedTupleType& _n) -> Result<T> {
|
||||
const auto to_struct = [](NamedTupleType&& _n) -> Result<T> {
|
||||
try {
|
||||
return from_named_tuple<T, NamedTupleType>(_n);
|
||||
return internal::move_from_named_tuple<T>(_n);
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
@@ -102,16 +104,41 @@ struct Parser {
|
||||
}
|
||||
} else if constexpr (std::is_class_v<T> &&
|
||||
!std::is_same<T, std::string>()) {
|
||||
const auto named_tuple = to_named_tuple(_var);
|
||||
using NamedTupleType = std::decay_t<decltype(named_tuple)>;
|
||||
return Parser<ReaderType, WriterType, NamedTupleType>::write(
|
||||
_w, named_tuple);
|
||||
const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var);
|
||||
using PtrNamedTupleType = std::decay_t<decltype(ptr_named_tuple)>;
|
||||
return Parser<ReaderType, WriterType, PtrNamedTupleType>::write(
|
||||
_w, ptr_named_tuple);
|
||||
} else {
|
||||
return _w.from_basic_type(_var);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, T*> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<T*> read(const ReaderType& _r, InputVarType* _var) noexcept {
|
||||
static_assert(
|
||||
always_false_v<T>,
|
||||
"Reading into raw pointers is dangerous and therefore unsupported. "
|
||||
"Please consider using std::unique_ptr, rfl::Box, std::shared_ptr, "
|
||||
"rfl::Ref or std::optional instead.");
|
||||
return Error("Unsupported.");
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const WriterType& _w, const T* _ptr) noexcept {
|
||||
if (!_ptr) {
|
||||
return _w.empty_var();
|
||||
}
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w,
|
||||
*_ptr);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class T, size_t _size>
|
||||
@@ -195,7 +222,9 @@ struct Parser<ReaderType, WriterType, Box<T>> {
|
||||
/// Expresses the variables as type T.
|
||||
static Result<Box<T>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto to_box = [&](auto&& _t) { return Box<T>::make(_t); };
|
||||
const auto to_box = [&](auto&& _t) {
|
||||
return Box<T>::make(std::move(_t));
|
||||
};
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, _var)
|
||||
.transform(to_box);
|
||||
}
|
||||
@@ -309,7 +338,7 @@ struct Parser<ReaderType, WriterType, NamedTuple<FieldTypes...>> {
|
||||
static Result<NamedTuple<FieldTypes...>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
const auto to_map = [&](auto _obj) { return _r.to_map(&_obj); };
|
||||
const auto build = [&](const auto& _map) {
|
||||
const auto build = [&](auto _map) {
|
||||
return build_named_tuple_recursively(_r, _map);
|
||||
};
|
||||
return _r.to_object(_var).transform(to_map).and_then(build);
|
||||
@@ -534,7 +563,7 @@ struct Parser<ReaderType, WriterType,
|
||||
return get_discriminator(_r, &_obj);
|
||||
};
|
||||
|
||||
const auto to_result = [&_r, _var](std::string _disc_value) {
|
||||
const auto to_result = [&_r, _var](const std::string& _disc_value) {
|
||||
return find_matching_alternative(_r, _disc_value, _var);
|
||||
};
|
||||
|
||||
@@ -562,9 +591,27 @@ struct Parser<ReaderType, WriterType,
|
||||
return Error("Could not parse tagged union, could not match " +
|
||||
_discriminator.str() + " '" + _disc_value + "'.");
|
||||
} else {
|
||||
const auto optional = try_option<_i>(_r, _disc_value, _var);
|
||||
if (optional) {
|
||||
return *optional;
|
||||
using AlternativeType = std::decay_t<std::variant_alternative_t<
|
||||
_i, std::variant<AlternativeTypes...>>>;
|
||||
|
||||
if (contains_disc_value<AlternativeType>(_disc_value)) {
|
||||
const auto to_tagged_union = [](auto&& _val) {
|
||||
return TaggedUnion<_discriminator, AlternativeTypes...>(
|
||||
std::move(_val));
|
||||
};
|
||||
|
||||
const auto embellish_error = [&](Error&& _e) {
|
||||
return Error(
|
||||
"Could not parse tagged union with discrimininator " +
|
||||
_discriminator.str() + " '" + _disc_value +
|
||||
"': " + _e.what());
|
||||
};
|
||||
|
||||
return Parser<ReaderType, WriterType, AlternativeType>::read(
|
||||
_r, _var)
|
||||
.transform(to_tagged_union)
|
||||
.or_else(embellish_error);
|
||||
|
||||
} else {
|
||||
return find_matching_alternative<_i + 1>(_r, _disc_value, _var);
|
||||
}
|
||||
@@ -603,36 +650,6 @@ struct Parser<ReaderType, WriterType,
|
||||
return LiteralType::contains(_disc_value);
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to parse one particular option.
|
||||
template <int _i>
|
||||
static std::optional<ResultType> try_option(const ReaderType& _r,
|
||||
const std::string& _disc_value,
|
||||
InputVarType* _var) noexcept {
|
||||
using AlternativeType = std::decay_t<
|
||||
std::variant_alternative_t<_i, std::variant<AlternativeTypes...>>>;
|
||||
|
||||
if (contains_disc_value<AlternativeType>(_disc_value)) {
|
||||
const auto to_tagged_union = [](const auto& _val) {
|
||||
return TaggedUnion<_discriminator, AlternativeTypes...>(_val);
|
||||
};
|
||||
|
||||
const auto embellish_error = [&](const Error& _e) {
|
||||
return Error(
|
||||
"Could not parse tagged union with discrimininator " +
|
||||
_discriminator.str() + " '" + _disc_value +
|
||||
"': " + _e.what());
|
||||
};
|
||||
|
||||
return Parser<ReaderType, WriterType, AlternativeType>::read(_r,
|
||||
_var)
|
||||
.transform(to_tagged_union)
|
||||
.or_else(embellish_error);
|
||||
|
||||
} else {
|
||||
return std::optional<ResultType>();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -725,6 +742,36 @@ struct Parser<ReaderType, WriterType, std::tuple<Ts...>> {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::unique_ptr<T>> {
|
||||
using InputVarType = typename ReaderType::InputVarType;
|
||||
using OutputVarType = typename WriterType::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::unique_ptr<T>> read(const ReaderType& _r,
|
||||
InputVarType* _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
return std::unique_ptr<T>();
|
||||
}
|
||||
const auto to_ptr = [](auto&& _t) {
|
||||
return std::make_unique<T>(std::move(_t));
|
||||
};
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, _var)
|
||||
.transform(to_ptr);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const WriterType& _w,
|
||||
const std::unique_ptr<T>& _s) noexcept {
|
||||
if (!_s) {
|
||||
return _w.empty_var();
|
||||
}
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::write(_w, *_s);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// To be used when all options of the variants are rfl::Field. Essentially,
|
||||
/// this is an externally tagged union.
|
||||
template <class ReaderType, class WriterType, class... FieldTypes>
|
||||
@@ -770,9 +817,11 @@ struct FieldVariantParser {
|
||||
"Externally tagged variants cannot have duplicate field names.");
|
||||
|
||||
const auto handle = [&](const auto& _field) {
|
||||
using NamedTupleType = NamedTuple<std::decay_t<decltype(_field)>>;
|
||||
const auto named_tuple =
|
||||
make_named_tuple(internal::to_ptr_field(_field));
|
||||
using NamedTupleType = std::decay_t<decltype(named_tuple)>;
|
||||
return Parser<ReaderType, WriterType, NamedTupleType>::write(
|
||||
_w, NamedTupleType(_field));
|
||||
_w, named_tuple);
|
||||
};
|
||||
|
||||
return std::visit(handle, _v);
|
||||
@@ -788,46 +837,32 @@ struct FieldVariantParser {
|
||||
"Could not parse std::variant, could not match field named '" +
|
||||
_disc_value + "'.");
|
||||
} else {
|
||||
const auto optional = try_option<_i>(_r, _disc_value, _var);
|
||||
if (optional) {
|
||||
return *optional;
|
||||
using FieldType = std::decay_t<typename std::tuple_element<
|
||||
_i, std::tuple<FieldTypes...>>::type>;
|
||||
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
|
||||
const auto key = FieldType::name_.str();
|
||||
|
||||
if (key == _disc_value) {
|
||||
const auto to_variant = [](ValueType&& _val) {
|
||||
return std::variant<FieldTypes...>(
|
||||
FieldType(std::move(_val)));
|
||||
};
|
||||
|
||||
const auto embellish_error = [&](const Error& _e) {
|
||||
return Error("Could not parse std::variant with field '" +
|
||||
_disc_value + "': " + _e.what());
|
||||
};
|
||||
|
||||
return Parser<ReaderType, WriterType, ValueType>::read(_r, _var)
|
||||
.transform(to_variant)
|
||||
.or_else(embellish_error);
|
||||
} else {
|
||||
return find_matching_alternative<_i + 1>(_r, _disc_value, _var);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to parse one particular option.
|
||||
template <int _i>
|
||||
static std::optional<ResultType> try_option(const ReaderType& _r,
|
||||
const std::string& _disc_value,
|
||||
InputVarType* _var) noexcept {
|
||||
using FieldType = std::decay_t<
|
||||
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type>;
|
||||
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
|
||||
const auto key = FieldType::name_.str();
|
||||
|
||||
if (key == _disc_value) {
|
||||
const auto to_variant = [](ValueType&& _val) {
|
||||
return std::variant<FieldTypes...>(
|
||||
FieldType(std::forward<ValueType>(_val)));
|
||||
};
|
||||
|
||||
const auto embellish_error = [&](const Error& _e) {
|
||||
return Error("Could not parse std::variant with field '" +
|
||||
_disc_value + "': " + _e.what());
|
||||
};
|
||||
|
||||
return Parser<ReaderType, WriterType, ValueType>::read(_r, _var)
|
||||
.transform(to_variant)
|
||||
.or_else(embellish_error);
|
||||
|
||||
} else {
|
||||
return std::optional<ResultType>();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -849,8 +884,8 @@ struct Parser<ReaderType, WriterType, std::variant<FieldTypes...>> {
|
||||
} else if constexpr (_i == sizeof...(FieldTypes)) {
|
||||
return Error("Could not parse variant: " + _errors);
|
||||
} else {
|
||||
const auto to_variant = [](const auto& _val) {
|
||||
return std::variant<FieldTypes...>(_val);
|
||||
const auto to_variant = [](auto&& _val) {
|
||||
return std::variant<FieldTypes...>(std::move(_val));
|
||||
};
|
||||
|
||||
const auto try_next = [&_r, _var, &_errors](const auto& _err) {
|
||||
@@ -905,9 +940,9 @@ struct VectorParser {
|
||||
using namespace std::ranges::views;
|
||||
|
||||
const auto get_result = [&](auto& _v) {
|
||||
return Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r,
|
||||
&_v)
|
||||
.value();
|
||||
return std::move(
|
||||
Parser<ReaderType, WriterType, std::decay_t<T>>::read(_r, &_v)
|
||||
.value());
|
||||
};
|
||||
|
||||
const auto to_vec = [&](InputArrayType _arr) -> Result<VecType> {
|
||||
|
||||
@@ -16,12 +16,18 @@ class is_required;
|
||||
template <class T>
|
||||
class is_required : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
class is_required<T*> : public std::false_type {};
|
||||
|
||||
template <class T>
|
||||
class is_required<std::optional<T>> : public std::false_type {};
|
||||
|
||||
template <class T>
|
||||
class is_required<std::shared_ptr<T>> : public std::false_type {};
|
||||
|
||||
template <class T>
|
||||
class is_required<std::unique_ptr<T>> : public std::false_type {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ auto to_named_tuple(const T& _t) {
|
||||
using FieldTuple = std::decay_t<decltype(field_tuple)>;
|
||||
|
||||
const auto ft_to_nt = []<class... Fields>(Fields&&... _fields) {
|
||||
return make_named_tuple(std::forward<Fields>(_fields)...);
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
|
||||
if constexpr (!internal::has_flatten_fields<FieldTuple>()) {
|
||||
|
||||
@@ -14,4 +14,4 @@ add_executable(
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2 -ftemplate-backtrace-limit=0")
|
||||
|
||||
@@ -13,20 +13,21 @@ void test_array() {
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::shared_ptr<std::array<Person, 3>>>
|
||||
rfl::Field<"children", std::unique_ptr<std::array<Person, 3>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa"};
|
||||
auto lisa = Person{.first_name = "Lisa"};
|
||||
|
||||
const auto maggie = Person{.first_name = "Maggie"};
|
||||
auto maggie = Person{.first_name = "Maggie"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.children = std::make_shared<std::array<Person, 3>>(
|
||||
std::array<Person, 3>{bart, lisa, maggie})};
|
||||
const auto homer = Person{
|
||||
.first_name = "Homer",
|
||||
.children =
|
||||
std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
|
||||
std::move(bart), std::move(lisa), std::move(maggie)})};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_box() {
|
||||
std::cout << "test_box" << std::endl;
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Box<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node =
|
||||
DecisionTree::Node{.critical_value = 10.0,
|
||||
.lesser = rfl::make_box<DecisionTree>(leaf1),
|
||||
.greater = rfl::make_box<DecisionTree>(leaf2)};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(
|
||||
tree,
|
||||
"{\"leafOrNode\":{\"type\":\"Node\",\"criticalValue\":10.0,\"left\":{"
|
||||
"\"leafOrNode\":{\"type\":\"Leaf\",\"value\":3.0}},\"right\":{"
|
||||
"\"leafOrNode\":{\"type\":\"Leaf\",\"value\":5.0}}}}");
|
||||
}
|
||||
@@ -25,7 +25,7 @@ void test_field_variant() {
|
||||
|
||||
using Shapes = std::variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", Square>>;
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
|
||||
const Shapes r =
|
||||
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_flatten() {
|
||||
std::cout << "test_flatten" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Field<"employer", rfl::Box<std::string>> employer;
|
||||
rfl::Field<"salary", float> salary;
|
||||
};
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(
|
||||
employee,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})");
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unique_ptr() {
|
||||
std::cout << "test_unique_ptr" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::vector<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
"{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{"
|
||||
"\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{"
|
||||
"\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{"
|
||||
"\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}");
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unique_ptr2() {
|
||||
std::cout << "test_unique_ptr2" << std::endl;
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", std::unique_ptr<DecisionTree>> lesser;
|
||||
rfl::Field<"right", std::unique_ptr<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node =
|
||||
DecisionTree::Node{.critical_value = 10.0,
|
||||
.lesser = std::make_unique<DecisionTree>(leaf1),
|
||||
.greater = std::make_unique<DecisionTree>(leaf2)};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(
|
||||
tree,
|
||||
"{\"leafOrNode\":{\"type\":\"Node\",\"criticalValue\":10.0,\"left\":{"
|
||||
"\"leafOrNode\":{\"type\":\"Leaf\",\"value\":3.0}},\"right\":{"
|
||||
"\"leafOrNode\":{\"type\":\"Leaf\",\"value\":5.0}}}}");
|
||||
}
|
||||
@@ -24,7 +24,7 @@ void test_variant() {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
using Shapes = std::variant<Circle, Rectangle, Square>;
|
||||
using Shapes = std::variant<Circle, Rectangle, std::unique_ptr<Square>>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
|
||||
@@ -1,31 +1,39 @@
|
||||
#include "test_array.hpp"
|
||||
#include "test_box.hpp"
|
||||
#include "test_custom_class1.hpp"
|
||||
#include "test_custom_class2.hpp"
|
||||
#include "test_custom_class3.hpp"
|
||||
#include "test_default_values.hpp"
|
||||
#include "test_field_variant.hpp"
|
||||
#include "test_flatten.hpp"
|
||||
#include "test_literal.hpp"
|
||||
#include "test_optional_fields.hpp"
|
||||
#include "test_readme_example.hpp"
|
||||
#include "test_ref.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
#include "test_unique_ptr2.hpp"
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
test_readme_example();
|
||||
test_default_values();
|
||||
test_optional_fields();
|
||||
test_unique_ptr();
|
||||
test_unique_ptr2();
|
||||
test_literal();
|
||||
test_variant();
|
||||
test_tagged_union();
|
||||
test_field_variant();
|
||||
test_ref();
|
||||
test_box();
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
test_custom_class3();
|
||||
test_array();
|
||||
test_timestamp();
|
||||
test_flatten();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,13 @@ void write_and_read(const T& _struct, const std::string& _expected) {
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
const auto struct2 = rfl::json::read<T>(json_string1).value();
|
||||
const auto json_string2 = rfl::json::write(struct2);
|
||||
const auto res = rfl::json::read<T>(json_string1);
|
||||
if (!res) {
|
||||
std::cout << "Test failed on read. Error: "
|
||||
<< res.error().value().what() << std::endl;
|
||||
return;
|
||||
}
|
||||
const auto json_string2 = rfl::json::write(res.value());
|
||||
if (json_string2 != _expected) {
|
||||
std::cout << "Test failed on read. Expected:" << std::endl
|
||||
<< _expected << std::endl
|
||||
|
||||
Reference in New Issue
Block a user