Remove boilerplate to rfl::internal::to_ptr_field_tuple

Also ensure the same behavioral changes that happened to
move/copy_to_field_tuple are actual for to_ptr_field_tuple by wrapping
in fields if necessary
This commit is contained in:
Txema Ynzenga
2023-12-09 21:50:37 +01:00
parent b86d2078e1
commit 28c602e195
4 changed files with 56 additions and 2341 deletions
+2 -31
View File
@@ -6,46 +6,17 @@
#include "rfl/field_names_t.hpp"
#include "rfl/internal/bind_to_tuple.hpp"
#include "rfl/internal/flattened_ptr_tuple_t.hpp"
#include "rfl/internal/has_fields.hpp"
#include "rfl/internal/is_flatten_field.hpp"
#include "rfl/internal/is_named_tuple.hpp"
#include "rfl/internal/lit_name.hpp"
#include "rfl/internal/wrap_in_fields.hpp"
namespace rfl {
namespace internal {
template <class FieldNames, int j = 0, class... Fields>
auto wrap_in_fields(auto&& _tuple, Fields&&... _fields) {
constexpr auto size = std::tuple_size_v<std::decay_t<decltype(_tuple)>>;
constexpr auto i = sizeof...(_fields);
if constexpr (i == size) {
return std::make_tuple(std::move(_fields)...);
} else {
auto value = std::move(std::get<i>(_tuple));
using Type = std::decay_t<decltype(value)>;
if constexpr (is_flatten_field_v<Type>) {
// The problem here is that the FieldNames are already flattened, but this
// is not, so we need to determine how many field names to skip.
constexpr auto n_skip = std::tuple_size_v<
std::decay_t<flattened_ptr_tuple_t<typename Type::Type>>>;
return wrap_in_fields<FieldNames, j + n_skip>(
std::move(_tuple), std::move(_fields)..., std::move(value));
} else {
const auto name_literal = FieldNames::template name_of<j>();
auto new_field =
rfl::make_field<lit_name_v<std::decay_t<decltype(name_literal)>>>(
std::move(value));
return wrap_in_fields<FieldNames, j + 1>(
std::move(_tuple), std::move(_fields)..., std::move(new_field));
}
}
}
template <class OriginalStruct>
auto move_to_field_tuple(OriginalStruct&& _t) {
using T = std::decay_t<OriginalStruct>;
if constexpr (internal::is_named_tuple_v<T>) {
if constexpr (is_named_tuple_v<T>) {
return _t.fields();
} else if constexpr (has_fields<T>()) {
return bind_to_tuple(_t, [](auto& x) { return std::move(x); });
+1 -3
View File
@@ -5,15 +5,13 @@
#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;
using ptr_field_tuple_t = decltype(to_ptr_field_tuple(std::declval<T&>()));
} // namespace internal
} // namespace rfl
File diff suppressed because it is too large Load Diff
+43
View File
@@ -0,0 +1,43 @@
#ifndef RFL_INTERNAL_WRAP_IN_FIELDS_HPP_
#define RFL_INTERNAL_WRAP_IN_FIELDS_HPP_
#include <tuple>
#include <type_traits>
#include "rfl/internal/flattened_ptr_tuple_t.hpp"
#include "rfl/internal/is_flatten_field.hpp"
#include "rfl/internal/lit_name.hpp"
namespace rfl {
namespace internal {
template <class FieldNames, int j = 0, class... Fields>
auto wrap_in_fields(auto&& _tuple, Fields&&... _fields) {
constexpr auto size = std::tuple_size_v<std::decay_t<decltype(_tuple)>>;
constexpr auto i = sizeof...(_fields);
if constexpr (i == size) {
return std::make_tuple(std::move(_fields)...);
} else {
auto value = std::move(std::get<i>(_tuple));
using Type = std::decay_t<decltype(value)>;
if constexpr (is_flatten_field_v<Type>) {
// The problem here is that the FieldNames are already flattened, but this
// is not, so we need to determine how many field names to skip.
constexpr auto n_skip = std::tuple_size_v<
std::decay_t<flattened_ptr_tuple_t<typename Type::Type>>>;
return wrap_in_fields<FieldNames, j + n_skip>(
std::move(_tuple), std::move(_fields)..., std::move(value));
} else {
const auto name_literal = FieldNames::template name_of<j>();
auto new_field =
rfl::make_field<lit_name_v<std::decay_t<decltype(name_literal)>>>(
std::move(value));
return wrap_in_fields<FieldNames, j + 1>(
std::move(_tuple), std::move(_fields)..., std::move(new_field));
}
}
}
} // namespace internal
} // namespace rfl
#endif