File: enums.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 (64 lines) | stat: -rw-r--r-- 2,339 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
#ifndef RFL_ENUMS_HPP_
#define RFL_ENUMS_HPP_

#include <string>

#include "Result.hpp"
#include "internal/enums/StringConverter.hpp"
#include "internal/enums/get_enum_names.hpp"
#include "internal/enums/is_flag_enum.hpp"
#include "internal/enums/is_scoped_enum.hpp"

namespace rfl {

// Converts an enum value to a string.
template <internal::enums::is_scoped_enum EnumType>
std::string enum_to_string(EnumType _enum) {
  return rfl::internal::enums::StringConverter<EnumType>::enum_to_string(_enum);
}

// Converts a string to a value of the given enum type.
template <internal::enums::is_scoped_enum EnumType>
rfl::Result<EnumType> string_to_enum(const std::string& _str) {
  return rfl::internal::enums::StringConverter<EnumType>::string_to_enum(_str);
}

// Returns a named tuple mapping names of enumerators of the given enum type to
// their values.
template <internal::enums::is_scoped_enum EnumType>
auto get_enumerators() {
  constexpr auto names = internal::enums::get_enum_names<
      EnumType, internal::enums::is_flag_enum<EnumType>>();
  return internal::enums::names_to_enumerator_named_tuple(names);
}

// Returns a named tuple mapping names of enumerators of the given enum type to
// their underlying values.
template <internal::enums::is_scoped_enum EnumType>
auto get_underlying_enumerators() {
  constexpr auto names = internal::enums::get_enum_names<
      EnumType, internal::enums::is_flag_enum<EnumType>>();
  return internal::enums::names_to_underlying_enumerator_named_tuple(names);
}

// Returns an std::array containing pairs of enumerator names (as
// std::string_view) and values.
template <internal::enums::is_scoped_enum EnumType>
constexpr auto get_enumerator_array() {
  constexpr auto names = internal::enums::get_enum_names<
      EnumType, internal::enums::is_flag_enum<EnumType>>();
  return internal::enums::names_to_enumerator_array(names);
}

// Returns an std::array containing pairs of enumerator names (as
// std::string_view) and underlying values.
template <internal::enums::is_scoped_enum EnumType>
constexpr auto get_underlying_enumerator_array() {
  constexpr auto names = internal::enums::get_enum_names<
      EnumType, internal::enums::is_flag_enum<EnumType>>();
  return internal::enums::names_to_underlying_enumerator_array(names);
}

}  // namespace rfl

#endif  // RFL_ENUMS_HPP_