draconisplusplus/include/rfl/internal/remove_fields.hpp

78 lines
2.7 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_INTERNAL_REMOVEFIELDS_HPP_
#define RFL_INTERNAL_REMOVEFIELDS_HPP_
#include <algorithm>
#include <tuple>
#include <type_traits>
#include "../NamedTuple.hpp"
#include "../define_named_tuple.hpp"
#include "StringLiteral.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace internal {
/// Recursively builds a new NamedTuple type from the FieldTypes, leaving
/// out the field signified by _name.
2024-06-16 00:13:15 -04:00
template <class _OldNamedTupleType, StringLiteral _name, class _NewNamedTupleType, int _i>
2024-06-08 14:10:59 -04:00
struct remove_single_field;
/// Special case - _i == 0
2024-06-16 00:13:15 -04:00
template <class _OldNamedTupleType, StringLiteral _name, class _NewNamedTupleType>
struct remove_single_field<_OldNamedTupleType, _name, _NewNamedTupleType, 0> {
2024-06-08 14:10:59 -04:00
using type = _NewNamedTupleType;
};
/// General case.
2024-06-16 00:13:15 -04:00
template <class _OldNamedTupleType, StringLiteral _name, class _NewNamedTupleType, int _i>
2024-06-08 14:10:59 -04:00
struct remove_single_field {
using OldNamedTupleType = std::remove_cvref_t<_OldNamedTupleType>;
2024-06-16 00:13:15 -04:00
constexpr static int num_fields = std::tuple_size_v<typename OldNamedTupleType::Fields>;
2024-06-08 14:10:59 -04:00
2024-06-16 00:13:15 -04:00
using FieldType = std::remove_cvref_t<
typename std::tuple_element<num_fields - _i, typename OldNamedTupleType::Fields>::type>;
2024-06-08 14:10:59 -04:00
using NewNamedTupleType = std::conditional_t<
2024-06-16 00:13:15 -04:00
_name == FieldType::name_,
_NewNamedTupleType,
define_named_tuple_t<_NewNamedTupleType, FieldType>>;
using type =
typename remove_single_field<OldNamedTupleType, _name, NewNamedTupleType, _i - 1>::type;
2024-06-08 14:10:59 -04:00
};
/// Recursively removes all of the fields signified by _head and _tail from
/// the NamedTupleType.
2024-06-16 00:13:15 -04:00
template <class _NamedTupleType, StringLiteral _head, StringLiteral... _tail>
2024-06-08 14:10:59 -04:00
struct remove_fields;
/// Special case - only head is left.
template <class _NamedTupleType, StringLiteral _head>
struct remove_fields<_NamedTupleType, _head> {
using NamedTupleType = std::remove_cvref_t<_NamedTupleType>;
2024-06-16 00:13:15 -04:00
constexpr static int num_fields = std::tuple_size_v<typename NamedTupleType::Fields>;
2024-06-08 14:10:59 -04:00
2024-06-16 00:13:15 -04:00
using type =
typename remove_single_field<NamedTupleType, _head, NamedTuple<>, num_fields>::type;
2024-06-08 14:10:59 -04:00
};
/// General case.
2024-06-16 00:13:15 -04:00
template <class _NamedTupleType, StringLiteral _head, StringLiteral... _tail>
2024-06-08 14:10:59 -04:00
struct remove_fields {
using NamedTupleType = std::remove_cvref_t<_NamedTupleType>;
2024-06-16 00:13:15 -04:00
constexpr static int num_fields = std::tuple_size_v<typename NamedTupleType::Fields>;
2024-06-08 14:10:59 -04:00
2024-06-16 00:13:15 -04:00
using NewNamedTupleType =
typename remove_single_field<NamedTupleType, _head, NamedTuple<>, num_fields>::type;
2024-06-08 14:10:59 -04:00
using type = typename remove_fields<NewNamedTupleType, _tail...>::type;
};
} // namespace internal
} // namespace rfl
#endif // RFL_REMOVEFIELDS_HPP_