enum_name
Loading...
Searching...
No Matches
enum_for_each.hpp
1/*
2MIT License
3
4Copyright (c) 2024 mguludag
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#ifndef DETAIL_ENUM_FOR_EACH_HPP
26#define DETAIL_ENUM_FOR_EACH_HPP
27
28#include "meta.hpp"
29#include "mgutility/std/fixed_string.hpp"
30
31// NOLINTNEXTLINE [unused-includes]
32#include <cstdint>
33#include <utility>
34
35namespace mgutility {
36namespace detail {
46template <typename T>
47// NOLINTNEXTLINE [modernize-type-traits]
48using string_or_view_t = typename std::conditional<
49 has_bit_or<T>::value, mgutility::fixed_string<enum_name_buffer<T>::size>,
50 mgutility::string_view>::type;
51
58template <typename Enum>
59using enum_pair = std::pair<Enum, detail::string_or_view_t<Enum>>;
60} // namespace detail
61
67template <typename Enum> class enum_for_each {
68 using value_type = const detail::enum_pair<Enum>;
69 using size_type = std::size_t;
70
74 struct enum_iter {
75 using const_iter_type = int;
76 using iter_type = detail::remove_const_t<const_iter_type>;
77 using iterator_category = std::forward_iterator_tag;
78 using value_type = const detail::enum_pair<Enum>;
79 using difference_type = std::ptrdiff_t;
80 using pointer = value_type *;
81 using reference = value_type &;
82
87 enum_iter() : m_pos{} {}
88
94 explicit enum_iter(iter_type value) : m_pos{value} {}
95
101 auto operator++() -> enum_iter & {
102 ++m_pos;
103 return *this;
104 }
105
111 auto operator++(int) -> enum_iter {
112 m_pos++;
113 return *this;
114 }
115
122 auto operator!=(const enum_iter &other) const -> bool {
123 return m_pos != other.m_pos;
124 }
125
132 auto operator==(const enum_iter &other) const -> bool {
133 return m_pos == other.m_pos;
134 }
135
141 auto operator*() const -> value_type;
142
143 private:
144 iter_type m_pos;
145 };
146
147public:
151 enum_for_each() = default;
152
158 auto begin() -> enum_iter & { return m_begin; }
159
165 auto end() -> enum_iter & { return m_end; }
166
172 auto size() -> std::size_t {
173 return static_cast<int>(enum_range<Enum>::max) -
174 static_cast<int>(enum_range<Enum>::min) + 1;
175 }
176
177private:
178 enum_iter m_begin{
179 static_cast<int>(enum_range<Enum>::min)};
180 enum_iter m_end{static_cast<int>(enum_range<Enum>::max) +
181 1};
182};
183} // namespace mgutility
184
185#endif // DETAIL_ENUM_FOR_EACH_HPP
A class template for iterating over enum values.
Definition enum_for_each.hpp:67
auto size() -> std::size_t
Returns the size of the enum range.
Definition enum_for_each.hpp:172
enum_for_each()=default
Default constructor.
auto begin() -> enum_iter &
Returns an iterator to the beginning of the enum range.
Definition enum_for_each.hpp:158
auto end() -> enum_iter &
Returns an iterator to the end of the enum range.
Definition enum_for_each.hpp:165
Checks for MSVC compiler version.
Definition enum_for_each.hpp:35
Provides the range for an enumeration type.
Definition meta.hpp:173