File: has_fields.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (79 lines) | stat: -rw-r--r-- 2,245 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef RFL_INTERNAL_HASFIELDS_HPP_
#define RFL_INTERNAL_HASFIELDS_HPP_

#include <tuple>
#include <type_traits>
#include <utility>

#include "../Tuple.hpp"
#include "all_fields.hpp"
#include "is_field.hpp"
#include "is_flatten_field.hpp"
#include "ptr_tuple_t.hpp"

namespace rfl {
namespace internal {

template <class TupleType>
constexpr bool all_fields_or_flatten() {
  const auto is_true_for_one =
      []<int _i>(std::integral_constant<int, _i>) -> bool {
    using T = std::remove_cvref_t<tuple_element_t<_i, TupleType>>;
    if constexpr (is_flatten_field_v<T>) {
      return all_fields_or_flatten<
          ptr_tuple_t<typename std::remove_pointer_t<T>::Type>>();
    } else {
      return is_field_v<T>;
    }
  };

  return [&]<int... _is>(std::integer_sequence<int, _is...>) {
    return (true && ... && is_true_for_one(std::integral_constant<int, _is>{}));
  }
  (std::make_integer_sequence<int, rfl::tuple_size_v<TupleType>>());
}

template <class TupleType>
constexpr bool some_fields_or_flatten() {
  const auto is_true_for_one =
      []<int _i>(std::integral_constant<int, _i>) -> bool {
    using T = std::remove_cvref_t<tuple_element_t<_i, TupleType>>;
    if constexpr (is_flatten_field_v<T>) {
      return some_fields_or_flatten<
          ptr_tuple_t<typename std::remove_pointer_t<T>::Type>>();
    } else {
      return is_field_v<T>;
    }
  };

  return [&]<int... _is>(std::integer_sequence<int, _is...>) {
    return (false || ... ||
            is_true_for_one(std::integral_constant<int, _is>{}));
  }
  (std::make_integer_sequence<int, rfl::tuple_size_v<TupleType>>());
}

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 (some_fields_or_flatten<TupleType>()) {
      static_assert(
          all_fields_or_flatten<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