File: Names.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 (76 lines) | stat: -rw-r--r-- 2,521 bytes parent folder | download
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
#ifndef RFL_INTERNAL_ENUMS_NAMES_HPP_
#define RFL_INTERNAL_ENUMS_NAMES_HPP_

#include <algorithm>
#include <array>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

#include "../../Literal.hpp"
#include "../../define_literal.hpp"
#include "../../make_named_tuple.hpp"
#include "../StringLiteral.hpp"

namespace rfl {
namespace internal {
namespace enums {

template <class EnumType, class LiteralType, size_t N, auto... _enums>
struct Names {
  /// Contains a collection of enums as compile-time strings.
  using Literal = LiteralType;

  /// The number of possible values
  constexpr static size_t size = N;

  /// A list of all the possible enums
  constexpr static std::array<EnumType, N> enums_ =
      std::array<EnumType, N>{_enums...};

  static_assert(N == 0 || LiteralType::size() == N,
                "Size of literal and enum do not match.");

  template <class NewLiteral, auto _new_enum>
  using AddOneType = std::conditional_t<
      N == 0, Names<EnumType, NewLiteral, 1, _new_enum>,
      Names<EnumType, define_literal_t<LiteralType, NewLiteral>, N + 1,
            _enums..., _new_enum>>;
};

template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
auto names_to_enumerator_named_tuple(
    Names<EnumType, Literal<_names...>, N, _enums...>) {
  return make_named_tuple(Field<_names, EnumType>{_enums}...);
}

template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
auto names_to_underlying_enumerator_named_tuple(
    Names<EnumType, Literal<_names...>, N, _enums...>) {
  return make_named_tuple(Field<_names, std::underlying_type_t<EnumType>>{
      static_cast<std::underlying_type_t<EnumType>>(_enums)}...);
}

template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
constexpr std::array<std::pair<std::string_view, EnumType>, N>
names_to_enumerator_array(Names<EnumType, Literal<_names...>, N, _enums...>) {
  return {
      std::make_pair(LiteralHelper<_names>::name_.string_view(), _enums)...};
}

template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
constexpr std::array<
    std::pair<std::string_view, std::underlying_type_t<EnumType>>, N>
names_to_underlying_enumerator_array(
    Names<EnumType, Literal<_names...>, N, _enums...>) {
  return {
      std::make_pair(LiteralHelper<_names>::name_.string_view(),
                     static_cast<std::underlying_type_t<EnumType>>(_enums))...};
}

}  // namespace enums
}  // namespace internal
}  // namespace rfl

#endif