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 {
37
47template <typename T>
48// NOLINTNEXTLINE [modernize-type-traits]
49using string_or_view_t = typename std::conditional<
52
59template <typename Enum>
60using enum_pair = std::pair<Enum, detail::string_or_view_t<Enum>>;
61} // namespace detail
62
68template <typename Enum> class enum_for_each {
69 using value_type = const detail::enum_pair<Enum>;
70 using size_type = std::size_t;
71
75 struct enum_iter {
76 using const_iter_type = int;
77 using iter_type = detail::remove_const_t<const_iter_type>;
78 using iterator_category = std::forward_iterator_tag;
79 using value_type = const detail::enum_pair<Enum>;
80 using difference_type = std::ptrdiff_t;
81 using pointer = value_type *;
82 using reference = value_type &;
83
88 enum_iter() : m_pos{} {}
89
95 explicit enum_iter(iter_type value) : m_pos{value} {}
96
102 auto operator++() -> enum_iter & {
103 ++m_pos;
104 return *this;
105 }
106
112 auto operator++(int) -> enum_iter {
113 m_pos++;
114 return *this;
115 }
116
123 auto operator!=(const enum_iter &other) const -> bool {
124 return m_pos != other.m_pos;
125 }
126
133 auto operator==(const enum_iter &other) const -> bool {
134 return m_pos == other.m_pos;
135 }
136
142 auto operator*() const -> value_type;
143
144 private:
145 iter_type m_pos;
146 };
147
148public:
152 enum_for_each() = default;
153
159 auto begin() -> enum_iter & { return m_begin; }
160
166 auto end() -> enum_iter & { return m_end; }
167
173 auto size() -> std::size_t {
174 return static_cast<int>(enum_range<Enum>::max) -
175 static_cast<int>(enum_range<Enum>::min) + 1;
176 }
177
178private:
179 enum_iter m_begin{
180 static_cast<int>(enum_range<Enum>::min)};
181 enum_iter m_end{static_cast<int>(enum_range<Enum>::max) +
182 1};
183};
184} // namespace mgutility
185
186#endif // DETAIL_ENUM_FOR_EACH_HPP
A basic string view class template.
Definition mgutility_enum_name.hpp:462
A class template for iterating over enum values.
Definition enum_for_each.hpp:68
auto size() -> std::size_t
Returns the size of the enum range.
Definition enum_for_each.hpp:173
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:159
auto end() -> enum_iter &
Returns an iterator to the end of the enum range.
Definition enum_for_each.hpp:166
Definition mgutility_enum_name.hpp:736
Checks for MSVC compiler version.
Definition enum_for_each.hpp:35
Provides the range for an enumeration type.
Definition meta.hpp:204