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

enum_map.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_ENUM_MAP_HPP
10 #define EAGINE_ENUM_MAP_HPP
11 
12 #include "bitfield.hpp"
13 #include "selector.hpp"
14 #include "types.hpp"
15 #include <type_traits>
16 
17 namespace eagine {
18 //------------------------------------------------------------------------------
19 template <typename Enum, template <Enum> class Unit, Enum Key>
20 struct static_enum_map_unit : Unit<Key> {
21 
22  template <typename Visitor>
23  auto _accept(Enum key, Visitor& visitor) -> bool {
24  if(Key == key) {
25  visitor(Key, *static_cast<Unit<Key>*>(this));
26  return true;
27  }
28  return false;
29  }
30 
31  template <typename Visitor>
32  auto _accept(Enum key, Visitor& visitor) const -> bool {
33  if(Key == key) {
34  visitor(Key, *static_cast<const Unit<Key>*>(this));
35  return true;
36  }
37  return false;
38  }
39 
40  template <typename Visitor>
41  auto _accept(bitfield<Enum> keys, Visitor& visitor) -> span_size_t {
42  if(keys.has(Key)) {
43  visitor(Key, *static_cast<Unit<Key>*>(this));
44  return 1;
45  }
46  return 0;
47  }
48 
49  template <typename Visitor>
50  auto _accept(bitfield<Enum> keys, Visitor& visitor) const -> span_size_t {
51  if(keys.has(Key)) {
52  visitor(Key, *static_cast<const Unit<Key>*>(this));
53  return 1;
54  }
55  return 0;
56  }
57 
58  template <typename Visitor>
59  auto _accept(Visitor& visitor) -> bool {
60  visitor(Key, *static_cast<Unit<Key>*>(this));
61  return true;
62  }
63 
64  template <typename Visitor>
65  auto _accept(Visitor& visitor) const -> bool {
66  visitor(Key, *static_cast<const Unit<Key>*>(this));
67  return true;
68  }
69 };
70 //------------------------------------------------------------------------------
73 template <typename Enum, template <Enum> class Unit, Enum... Keys>
74 class static_enum_map : private static_enum_map_unit<Enum, Unit, Keys>... {
75 
76 public:
78  static_enum_map() = default;
79 
81  template <
82  typename... Args,
83  typename = std::enable_if_t<(sizeof...(Args) > 1)>>
84  explicit static_enum_map(construct_from_t, const Args&... args)
85  : static_enum_map_unit<Enum, Unit, Keys>{args...}... {}
86 
89  template <Enum Key>
90  auto get() noexcept -> Unit<Key>& {
91  return _base<Key>();
92  }
93 
96  template <Enum Key>
97  auto get() const noexcept -> const Unit<Key>& {
98  return _base<Key>();
99  }
100 
104  template <typename Visitor>
105  auto visit(Enum key, Visitor visitor) noexcept -> bool {
106  return (false || ... || _base<Keys>()._accept(key, visitor));
107  }
108 
112  template <typename Visitor>
113  auto visit(Enum key, Visitor visitor) const noexcept -> bool {
114  return (false || ... || _base<Keys>()._accept(key, visitor));
115  }
116 
120  template <typename Visitor>
121  auto visit(bitfield<Enum> keys, Visitor visitor) noexcept -> span_size_t {
122  return (0 + ... + _base<Keys>()._accept(keys, visitor));
123  }
124 
128  template <typename Visitor>
129  auto visit(bitfield<Enum> keys, Visitor visitor) const noexcept
130  -> span_size_t {
131  return (0 + ... + _base<Keys>()._accept(keys, visitor));
132  }
133 
136  template <typename Visitor>
137  auto visit_all(Visitor visitor) noexcept -> bool {
138  return (true && ... && _base<Keys>()._accept(visitor));
139  }
140 
143  template <typename Visitor>
144  auto visit_all(Visitor visitor) const noexcept -> bool {
145  return (true && ... && _base<Keys>()._accept(visitor));
146  }
147 
148 private:
149  template <Enum Key>
150  auto _base() noexcept -> static_enum_map_unit<Enum, Unit, Key>& {
151  return *this;
152  }
153 
154  template <Enum Key>
155  auto _base() const noexcept
156  -> const static_enum_map_unit<Enum, Unit, Key>& {
157  return *this;
158  }
159 };
160 //------------------------------------------------------------------------------
161 } // namespace eagine
162 
163 #endif // EAGINE_ENUM_MAP_HPP
auto get() const noexcept -> const Unit< Key > &
Returns a const reference to the unit with the specified enumerator key.
Definition: enum_map.hpp:97
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
static_enum_map()=default
Default constructor.
Common code is placed in this namespace.
Definition: eagine.hpp:21
Class for manipulating and testing a group of enumeration-based bits.
Definition: bitfield.hpp:19
Template used to construct tag-types used mostly in tag-dispatching.
Definition: selector.hpp:21
auto visit(bitfield< Enum > keys, Visitor visitor) noexcept -> span_size_t
Calls a visitor function on the unit with the specified keys.
Definition: enum_map.hpp:121
static_enum_map(construct_from_t, const Args &... args)
Explicit construction from arguments that are passed to the units.
Definition: enum_map.hpp:84
auto visit_all(Visitor visitor) const noexcept -> bool
Calls a visitor function on all units.
Definition: enum_map.hpp:144
Class mapping from an enumerator of an instantiation of a template.
Definition: enum_map.hpp:74
auto visit_all(Visitor visitor) noexcept -> bool
Calls a visitor function on all units.
Definition: enum_map.hpp:137
auto visit(Enum key, Visitor visitor) const noexcept -> bool
Calls a visitor function on the unit with the specified key.
Definition: enum_map.hpp:113
auto get() noexcept -> Unit< Key > &
Returns a reference to the unit with the specified enumerator key.
Definition: enum_map.hpp:90
auto visit(Enum key, Visitor visitor) noexcept -> bool
Calls a visitor function on the unit with the specified key.
Definition: enum_map.hpp:105
auto visit(bitfield< Enum > keys, Visitor visitor) const noexcept -> span_size_t
Calls a visitor function on the unit with the specified keys.
Definition: enum_map.hpp:129

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