Began implementing support for anonymous structs

This commit is contained in:
Patrick Urbanke
2023-11-05 20:09:29 +01:00
parent 08806d1ebf
commit 57dad0df3f
8 changed files with 1271 additions and 16 deletions
+50
View File
@@ -0,0 +1,50 @@
#ifndef RFL_INTERNAL_HASFIELDS_HPP_
#define RFL_INTERNAL_HASFIELDS_HPP_
#include <tuple>
#include <type_traits>
#include <utility>
#include "rfl/internal/all_fields.hpp"
#include "rfl/internal/is_field.hpp"
#include "rfl/internal/is_flatten_field.hpp"
#include "rfl/internal/ptr_tuple_t.hpp"
namespace rfl {
namespace internal {
template <class TupleType, int _i = 0>
constexpr bool has_some_fields() {
if constexpr (_i == std::tuple_size_v<TupleType>) {
return false;
} else {
using T = std::decay_t<std::tuple_element_t<_i, TupleType>>;
return is_field_v<T> || has_some_fields<TupleType, _i + 1>();
}
}
template <class T>
constexpr bool has_fields() {
if constexpr (is_named_tuple_v<T>) {
return true;
} else {
using TupleType = ptr_tuple_t<T>;
if constexpr (has_some_fields<TupleType>()) {
static_assert(
all_fields<TupleType>(),
"If some of your fields are annotated using rfl::Field<...>, then "
"you "
"must "
"annotate all of your fields. Also, you cannot combine annotated and "
"unannotated fields using rfl::Flatten<...>.");
return true;
} else {
return false;
}
}
}
} // namespace internal
} // namespace rfl
#endif
+6 -7
View File
@@ -12,13 +12,12 @@ namespace internal {
template <class TupleType, int _i = 0>
constexpr bool has_flatten_fields() {
if constexpr (_i == std::tuple_size_v<TupleType>) {
return false;
} else {
using T = std::decay_t<std::tuple_element_t<_i, TupleType>>;
return is_flatten_field<T>::value ||
has_flatten_fields<TupleType, _i + 1>();
}
if constexpr (_i == std::tuple_size_v<TupleType>) {
return false;
} else {
using T = std::decay_t<std::tuple_element_t<_i, TupleType>>;
return is_flatten_field_v<T> || has_flatten_fields<TupleType, _i + 1>();
}
}
} // namespace internal
+6
View File
@@ -20,6 +20,12 @@ class is_field : public std::false_type {};
template <StringLiteral _name, class Type>
class is_field<Field<_name, Type>> : public std::true_type {};
template <StringLiteral _name, class Type>
class is_field<Field<_name, Type>*> : public std::true_type {};
template <StringLiteral _name, class Type>
class is_field<const Field<_name, Type>*> : public std::true_type {};
template <class T>
constexpr bool is_field_v = is_field<T>::value;
@@ -19,6 +19,12 @@ class is_flatten_field : public std::false_type {};
template <class T>
class is_flatten_field<Flatten<T>> : public std::true_type {};
template <class T>
class is_flatten_field<Flatten<T>*> : public std::true_type {};
template <class T>
class is_flatten_field<const Flatten<T>*> : public std::true_type {};
template <class T>
constexpr bool is_flatten_field_v = is_flatten_field<T>::value;
+20
View File
@@ -0,0 +1,20 @@
#ifndef RFL_INTERNAL_PTR_TUPLE_T_HPP_
#define RFL_INTERNAL_PTR_TUPLE_T_HPP_
#include <functional>
#include <tuple>
#include <type_traits>
#include "rfl/internal/to_ptr_tuple.hpp"
namespace rfl {
namespace internal {
template <class T>
using ptr_tuple_t =
typename std::invoke_result<decltype(to_ptr_tuple<T>), T>::type;
} // namespace internal
} // namespace rfl
#endif
+47
View File
@@ -0,0 +1,47 @@
#ifndef RFL_INTERNAL_TO_PTR_TUPLE_HPP_
#define RFL_INTERNAL_TO_PTR_TUPLE_HPP_
#include <tuple>
#include "rfl/internal/has_flatten_fields.hpp"
#include "rfl/internal/is_field.hpp"
#include "rfl/internal/to_unflattened_ptr_tuple.hpp"
namespace rfl {
namespace internal {
/// Generates a std::tuple that contains pointers to the original values in
/// the struct.
template <class PtrTuple, class... Args>
auto flatten_ptr_tuple(const PtrTuple& _ptr_tuple, Args&&... _args) {
constexpr auto i = sizeof...(Args);
if constexpr (i == std::tuple_size_v<std::decay_t<PtrTuple>>) {
return std::tuple_cat(std::forward<Args>(_args)...);
} else {
using T = std::tuple_element_t<i, std::decay_t<PtrTuple>>;
if constexpr (internal::is_flatten_field_v<T>) {
const auto subtuple =
to_unflattened_ptr_tuple(std::get<i>(_ptr_tuple)->get());
return flatten_ptr_tuple(_ptr_tuple, std::forward<Args>(_args)...,
flatten_ptr_tuple(subtuple));
} else {
return flatten_ptr_tuple(_ptr_tuple, std::forward<Args>(_args)...,
std::make_tuple(std::get<i>(_ptr_tuple)));
}
}
}
template <class T>
auto to_ptr_tuple(const T& _t) {
const auto ptr_tuple = to_unflattened_ptr_tuple(_t);
if constexpr (!has_flatten_fields<std::decay_t<decltype(ptr_tuple)>>()) {
return ptr_tuple;
} else {
return flatten_ptr_tuple(ptr_tuple);
}
}
} // namespace internal
} // namespace rfl
#endif
File diff suppressed because it is too large Load Diff
+13 -9
View File
@@ -31,6 +31,7 @@
#include "rfl/from_named_tuple.hpp"
#include "rfl/internal/StringLiteral.hpp"
#include "rfl/internal/all_fields.hpp"
#include "rfl/internal/has_fields.hpp"
#include "rfl/internal/has_reflection_method_v.hpp"
#include "rfl/internal/has_reflection_type_v.hpp"
#include "rfl/internal/is_basic_type.hpp"
@@ -78,15 +79,18 @@ struct Parser {
};
return Parser<R, W, ReflectionType>::read(_r, _var).and_then(wrap_in_t);
} else if constexpr (std::is_class_v<T> && std::is_aggregate_v<T>) {
using NamedTupleType = named_tuple_t<T>;
const auto to_struct = [](NamedTupleType&& _n) -> Result<T> {
try {
return from_named_tuple<T>(std::move(_n));
} catch (std::exception& e) {
return Error(e.what());
}
};
return Parser<R, W, NamedTupleType>::read(_r, _var).and_then(to_struct);
if constexpr (internal::has_fields<T>()) {
using NamedTupleType = named_tuple_t<T>;
const auto to_struct = [](NamedTupleType&& _n) -> Result<T> {
try {
return from_named_tuple<T>(std::move(_n));
} catch (std::exception& e) {
return Error(e.what());
}
};
return Parser<R, W, NamedTupleType>::read(_r, _var).and_then(
to_struct);
}
} else if constexpr (internal::is_basic_type_v<T>) {
return _r.template to_basic_type<std::decay_t<T>>(_var);
} else {