OGLplus  (0.59.0) a C++ wrapper for rendering APIs

enumerators.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_REFLECT_ENUMERATORS_HPP
10 #define EAGINE_REFLECT_ENUMERATORS_HPP
11 
12 #include "../selector.hpp"
13 #include "../valid_if/decl.hpp"
14 #include "map_enumerators.hpp"
15 #include <cstdint>
16 
17 namespace eagine {
18 //------------------------------------------------------------------------------
19 template <typename T, typename Selector>
20 constexpr auto enumerator_count(type_identity<T> id, Selector sel) noexcept
21  -> std::enable_if_t<has_enumerator_mapping_v<T, Selector>, span_size_t> {
22  return span_size_t(enumerator_mapping(id, sel).size());
23 }
24 //------------------------------------------------------------------------------
25 template <typename T>
26 constexpr auto enumerator_count(type_identity<T> id) noexcept {
27  return enumerator_count(id, default_selector);
28 }
29 //------------------------------------------------------------------------------
30 template <typename T>
31 constexpr auto enumerator_name(T value, type_identity<T> id = {}) noexcept {
32  return enumerator_name(value, id, default_selector);
33 }
34 //------------------------------------------------------------------------------
35 template <typename T, typename Selector>
36 inline auto
37 enumerator_name(T enumerator, type_identity<T> id, Selector sel) noexcept
38  -> std::enable_if_t<has_enumerator_mapping_v<T, Selector>, decl_name> {
39  for(const auto& info : enumerator_mapping(id, sel)) {
40  if(info.enumerator == enumerator) {
41  return info.name;
42  }
43  }
44  return {};
45 }
46 //------------------------------------------------------------------------------
47 template <typename T>
48 constexpr auto enumerator_value(T value, type_identity<T> = {}) noexcept {
49  return static_cast<std::underlying_type_t<T>>(value);
50 }
51 //------------------------------------------------------------------------------
52 template <bool Signed>
53 struct enum_int_value_and_name {
54  using value_type =
55  std::conditional_t<Signed, std::intmax_t, std::uintmax_t>;
56 
57  constexpr enum_int_value_and_name() noexcept = default;
58 
59  template <typename T>
60  constexpr enum_int_value_and_name(const enumerator_and_name<T>& src) noexcept
61  : name{src.name}
62  , value{static_cast<value_type>(src.enumerator)} {}
63 
64  const string_view name{};
65  const value_type value{0};
66 };
67 //------------------------------------------------------------------------------
68 using signed_enum_value_and_name = enum_int_value_and_name<true>;
69 using unsigned_enum_value_and_name = enum_int_value_and_name<false>;
70 //------------------------------------------------------------------------------
71 template <typename T>
72 using enum_value_and_name =
73  enum_int_value_and_name<std::is_signed_v<std::underlying_type_t<T>>>;
74 //------------------------------------------------------------------------------
75 template <typename T, typename Selector>
76 inline auto
77 enumerator_info(T enumerator, type_identity<T> id, Selector sel) noexcept
78  -> std::enable_if_t<
79  has_enumerator_mapping_v<T, Selector>,
80  optionally_valid<enum_value_and_name<T>>> {
81  for(const auto& info : enumerator_mapping(id, sel)) {
82  if(info.enumerator == enumerator) {
83  return {enum_value_and_name<T>{info}, true};
84  }
85  }
86  return {};
87 }
88 //------------------------------------------------------------------------------
89 template <typename T>
90 auto enumerator_info(T enumerator, type_identity<T> id = {}) noexcept {
91  return enumerator_info(enumerator, id, default_selector);
92 }
93 //------------------------------------------------------------------------------
94 template <typename T, typename Selector>
95 inline auto from_value(
96  std::underlying_type_t<T> value,
97  type_identity<T> id,
98  Selector sel) noexcept
99  -> std::enable_if_t<has_enumerator_mapping_v<T, Selector>, optionally_valid<T>> {
100  for(const auto& info : enumerator_mapping(id, sel)) {
101  if(enumerator_value(info.enumerator, id) == value) {
102  return {info.enumerator, true};
103  }
104  }
105  return {};
106 }
107 //------------------------------------------------------------------------------
108 template <typename T>
109 constexpr auto
110 from_value(std::underlying_type_t<T> value, type_identity<T> id = {}) noexcept {
111  return from_value(value, id, default_selector);
112 }
113 //------------------------------------------------------------------------------
114 template <typename T, typename Selector>
115 inline auto
116 from_string(string_view name, type_identity<T> id, Selector sel) noexcept
117  -> std::enable_if_t<has_enumerator_mapping_v<T, Selector>, optionally_valid<T>> {
118  for(const auto& info : enumerator_mapping(id, sel)) {
119  if(are_equal(string_view(info.name), name)) {
120  return {info.enumerator, true};
121  }
122  }
123  return {};
124 }
125 //------------------------------------------------------------------------------
126 template <typename Function, typename T, typename Selector>
127 inline auto for_each_enumerator(
128  Function& function,
129  type_identity<T> id,
130  Selector sel) noexcept
131  -> std::enable_if_t<has_enumerator_mapping_v<T, Selector>> {
132  for(const auto& info : enumerator_mapping(id, sel)) {
133  function(enum_value_and_name<T>{info});
134  }
135 }
136 //------------------------------------------------------------------------------
137 template <typename Function, typename T>
138 inline auto for_each_enumerator(Function& function, type_identity<T> id) noexcept
139  -> std::enable_if_t<has_enumerator_mapping_v<T, default_selector_t>> {
140  for_each_enumerator(function, id, default_selector);
141 }
142 //------------------------------------------------------------------------------
143 } // namespace eagine
144 
145 #endif // EAGINE_REFLECT_ENUMERATORS_HPP
value_type
Value tree value element data type enumeration.
Definition: interface.hpp:27
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
auto from_string(string_view src) noexcept
Converts the string representation in src to a value of type T.
Definition: from_string.hpp:360
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
Common code is placed in this namespace.
Definition: eagine.hpp:21
@ info
Informational log entries.
constexpr const default_selector_t default_selector
The default overload selector constant.
Definition: selector.hpp:34

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).