draconisplusplus/include/rfl/internal/move_from_named_tuple.hpp

116 lines
3.6 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_MOVE_FROM_NAMED_TUPLE_HPP_
#define RFL_MOVE_FROM_NAMED_TUPLE_HPP_
#include <functional>
#include <type_traits>
2024-06-08 14:10:59 -04:00
#include "../named_tuple_t.hpp"
2024-05-31 22:59:00 -04:00
#include "is_field.hpp"
#include "is_named_tuple.hpp"
#include "nt_to_ptr_named_tuple.hpp"
#include "ptr_field_tuple_t.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace internal {
template <class PtrFieldTupleType, class PtrNamedTupleType, class... Args>
auto make_ptr_fields(PtrNamedTupleType& _n, Args... _args) {
constexpr auto i = sizeof...(Args);
constexpr auto size =
std::tuple_size_v<std::remove_cvref_t<PtrFieldTupleType>>;
if constexpr (i == size) {
return std::make_tuple(_args...);
} else {
using Field =
std::remove_cvref_t<std::tuple_element_t<i, PtrFieldTupleType>>;
using T =
std::remove_cvref_t<std::remove_pointer_t<typename Field::Type>>;
if constexpr (is_named_tuple_v<T>) {
2024-06-08 15:53:06 -04:00
using SubPtrNamedTupleType = typename std::invoke_result<
decltype(nt_to_ptr_named_tuple<T>), T>::type;
2024-06-08 14:10:59 -04:00
return make_ptr_fields<PtrFieldTupleType>(
2024-06-08 15:53:06 -04:00
_n, _args..., SubPtrNamedTupleType(_n).fields()
);
2024-06-08 14:10:59 -04:00
} else if constexpr (is_flatten_field<Field>::value) {
using SubPtrFieldTupleType =
std::remove_cvref_t<ptr_field_tuple_t<T>>;
return make_ptr_fields<PtrFieldTupleType>(
2024-06-08 15:53:06 -04:00
_n, _args..., make_ptr_fields<SubPtrFieldTupleType>(_n)
);
2024-06-08 14:10:59 -04:00
} else {
return make_ptr_fields<PtrFieldTupleType>(
2024-06-08 15:53:06 -04:00
_n, _args..., _n.template get_field<Field::name_>()
);
2024-06-08 14:10:59 -04:00
}
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
template <class T, class Pointers, class... Args>
auto move_from_ptr_fields(Pointers& _ptrs, Args&&... _args) {
constexpr auto i = sizeof...(Args);
if constexpr (i == std::tuple_size_v<std::remove_cvref_t<Pointers>>) {
return T {std::move(_args)...};
} else {
using FieldType =
std::tuple_element_t<i, std::remove_cvref_t<Pointers>>;
if constexpr (is_field_v<FieldType>) {
return move_from_ptr_fields<T>(
_ptrs, std::move(_args)...,
rfl::make_field<FieldType::name_>(
2024-06-08 15:53:06 -04:00
std::move(*std::get<i>(_ptrs).value())
)
);
2024-06-08 14:10:59 -04:00
} else {
using PtrFieldTupleType = std::remove_cvref_t<ptr_field_tuple_t<T>>;
using U = std::remove_cvref_t<std::remove_pointer_t<
typename std::tuple_element_t<i, PtrFieldTupleType>::Type>>;
return move_from_ptr_fields<T>(
_ptrs, std::move(_args)...,
2024-06-08 15:53:06 -04:00
move_from_ptr_fields<U>(std::get<i>(_ptrs))
);
2024-06-08 14:10:59 -04:00
}
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
/// 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::remove_cvref_t<named_tuple_t<T>>;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
if constexpr (is_named_tuple_v<std::remove_cvref_t<T>>) {
return std::move(_n);
2024-05-31 22:59:00 -04:00
2024-06-08 15:53:06 -04:00
} else if constexpr (std::is_same<
std::remove_cvref_t<NamedTupleType>,
RequiredType>()) {
2024-06-08 14:10:59 -04:00
auto ptr_named_tuple = nt_to_ptr_named_tuple(_n);
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
using PtrFieldTupleType = std::remove_cvref_t<ptr_field_tuple_t<T>>;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
auto pointers = make_ptr_fields<PtrFieldTupleType>(ptr_named_tuple);
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
return move_from_ptr_fields<T>(pointers);
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} else {
2024-06-08 15:53:06 -04:00
return move_from_named_tuple<T, RequiredType>(RequiredType(std::move(_n)
));
2024-06-08 14:10:59 -04:00
}
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace internal
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif