draconisplusplus/include/rfl/internal/nt_to_ptr_named_tuple.hpp

59 lines
1.9 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_INTERNAL_NT_TO_PTR_NAMED_TUPLE_HPP_
#define RFL_INTERNAL_NT_TO_PTR_NAMED_TUPLE_HPP_
#include <tuple>
#include "../Field.hpp"
#include "../make_named_tuple.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
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_v<Fields>;
if constexpr (i == num_fields) {
return make_named_tuple(_a...);
} else {
using FieldType = typename std::tuple_element<i, Fields>::type;
using T = std::remove_cvref_t<typename FieldType::Type>;
return nt_to_ptr_named_tuple(
2024-06-08 15:53:06 -04:00
_nt, _a..., Field<FieldType::name_, T*>(&std::get<i>(_nt.values()))
);
2024-06-08 14:10:59 -04:00
}
}
/// Generates a named tuple that contains pointers to the original values in
/// the struct from a named tuple.
template <class NamedTupleType, class... AlreadyExtracted>
2024-06-08 15:53:06 -04:00
auto
nt_to_ptr_named_tuple(const NamedTupleType& _nt, AlreadyExtracted... _a) {
2024-06-08 14:10:59 -04:00
using Fields = typename NamedTupleType::Fields;
constexpr auto i = sizeof...(AlreadyExtracted);
constexpr auto num_fields = std::tuple_size_v<Fields>;
if constexpr (i == num_fields) {
return make_named_tuple(_a...);
} else {
using FieldType = typename std::tuple_element<i, Fields>::type;
using T = std::remove_cvref_t<typename FieldType::Type>;
return nt_to_ptr_named_tuple(
2024-06-09 18:55:00 -04:00
_nt,
_a...,
2024-06-08 15:53:06 -04:00
Field<FieldType::name_, const T*>(&std::get<i>(_nt.values()))
);
2024-06-08 14:10:59 -04:00
}
}
} // namespace internal
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif