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/string_view.hpp"
30
31#include <cstdint>
32#include <utility>
33
34namespace mgutility {
35namespace detail {
45template <typename T>
46using string_or_view_t =
47 typename std::conditional<has_bit_or<T>::value, std::string,
49
56template <typename Enum>
57using enum_pair = std::pair<Enum, detail::string_or_view_t<Enum>>;
58} // namespace detail
59
65template <typename Enum> class enum_for_each {
66 using value_type = const detail::enum_pair<Enum>;
67 using size_type = std::size_t;
68
72 struct enum_iter {
73 using const_iter_type = decltype(enum_range<Enum>::min);
74 using iter_type = detail::remove_const_t<const_iter_type>;
75 using iterator_category = std::forward_iterator_tag;
76 using value_type = const detail::enum_pair<Enum>;
77 using difference_type = std::ptrdiff_t;
78 using pointer = value_type *;
79 using reference = value_type &;
80
85 enum_iter() : m_pos{} {}
86
92 enum_iter(iter_type value) : m_pos{value} {}
93
99 auto operator++() -> enum_iter & {
100 ++m_pos;
101 return *this;
102 }
103
109 auto operator++(int) -> enum_iter {
110 m_pos++;
111 return *this;
112 }
113
120 auto operator!=(const enum_iter &other) const -> bool {
121 return m_pos != other.m_pos;
122 }
123
130 auto operator==(const enum_iter &other) const -> bool {
131 return m_pos == other.m_pos;
132 }
133
139 auto operator*() const -> value_type;
140
141 private:
142 iter_type m_pos;
143 };
144
145public:
149 enum_for_each() = default;
150
156 auto begin() -> enum_iter & { return m_begin; }
157
163 auto end() -> enum_iter & { return m_end; }
164
170 auto size() -> std::size_t {
172 }
173
174private:
175 enum_iter m_begin{enum_range<Enum>::min};
176 enum_iter m_end{enum_range<Enum>::max};
177};
178} // namespace mgutility
179
180#endif // DETAIL_ENUM_FOR_EACH_HPP
A basic string view class template.
Definition string_view.hpp:57
A class template for iterating over enum values.
Definition enum_for_each.hpp:65
auto size() -> std::size_t
Returns the size of the enum range.
Definition enum_for_each.hpp:170
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:156
auto end() -> enum_iter &
Returns an iterator to the end of the enum range.
Definition enum_for_each.hpp:163
Checks for MSVC compiler version.
Definition enum_for_each.hpp:34
Provides the range for an enumeration type.
Definition meta.hpp:156
Definition meta.hpp:161