enum_name
Loading...
Searching...
No Matches
enum_name.hpp
1/*
2MIT License
3
4Copyright (c) 2023 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 MGUTILITY_ENUM_NAME_HPP
26#define MGUTILITY_ENUM_NAME_HPP
27
28#include "detail/enum_name_impl.hpp"
29
30namespace mgutility {
31
39template <typename Enum>
40constexpr auto enum_to_underlying(Enum enumValue) noexcept
41 -> detail::underlying_type_t<Enum> {
42 // NOLINTNEXTLINE [modernize-type-traits]
43 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
44 return static_cast<detail::underlying_type_t<Enum>>(enumValue);
45}
46
56template <int Min, int Max, typename Enum>
57MGUTILITY_CNSTXPR auto enum_name(Enum enumValue) noexcept
58 -> detail::string_or_view_t<Enum> {
59 static_assert(Min < Max, "Max must be greater than Min!");
60 // NOLINTNEXTLINE [modernize-type-traits]
61 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
62 return detail::enum_name_impl<Enum, Min, Max>(enumValue);
63}
64
74template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
75 int Max = static_cast<int>(enum_range<Enum>::max)>
76MGUTILITY_CNSTXPR auto enum_name(Enum enumValue) noexcept
77 -> detail::string_or_view_t<Enum> {
78 static_assert(Min < Max, "Max must be greater than Min!");
79 // NOLINTNEXTLINE [modernize-type-traits]
80 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
81 return detail::enum_name_impl<Enum, Min, Max>(enumValue);
82}
83
90template <typename Enum>
91auto enum_for_each<Enum>::enum_iter::operator*() const -> value_type {
92 auto value = static_cast<Enum>(m_pos);
93 return detail::enum_pair<Enum>{value, enum_name(value)};
94}
95
105template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
106 int Max = static_cast<int>(enum_range<Enum>::max),
107 detail::enable_if_t<!detail::has_bit_or<Enum>::value, bool> = true>
108MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
109 -> mgutility::optional<Enum> {
110 static_assert(Min < Max, "Max must be greater than Min!");
111 // NOLINTNEXTLINE [modernize-type-traits]
112 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
113 return detail::to_enum_impl<Enum, Min, Max>(str);
114}
115
125template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
126 int Max = static_cast<int>(enum_range<Enum>::max),
127 detail::enable_if_t<detail::has_bit_or<Enum>::value, bool> = true>
128MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
129 -> mgutility::optional<Enum> {
130 static_assert(Min < Max, "Max must be greater than Min!");
131 // NOLINTNEXTLINE [modernize-type-traits]
132 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
133 return detail::to_enum_bitmask_impl<Enum, Min, Max>(str);
134}
135
145template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
146 int Max = static_cast<int>(enum_range<Enum>::max)>
147MGUTILITY_CNSTXPR auto enum_cast(int value) noexcept
148 -> mgutility::optional<Enum> {
149 static_assert(Min < Max, "Max must be greater than Min!");
150 // NOLINTNEXTLINE [modernize-type-traits]
151 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
152 if (enum_name(static_cast<Enum>(value)).empty()) {
153 return mgutility::nullopt;
154 }
155 return static_cast<Enum>(value);
156}
157
158namespace operators {
159template <typename Enum, mgutility::detail::enable_if_t<
160 // NOLINTNEXTLINE [modernize-type-traits]
161 std::is_enum<Enum>::value, bool> = true>
162constexpr auto operator&(const Enum &lhs, const Enum &rhs) -> Enum {
163 return static_cast<Enum>(mgutility::enum_to_underlying(lhs) &
165}
166
167template <typename Enum, mgutility::detail::enable_if_t<
168 // NOLINTNEXTLINE [modernize-type-traits]
169 std::is_enum<Enum>::value, bool> = true>
170constexpr auto operator|(const Enum &lhs, const Enum &rhs) -> Enum {
171 return static_cast<Enum>(mgutility::enum_to_underlying(lhs) |
173}
174} // namespace operators
175
176} // namespace mgutility
177
186template <typename Enum, mgutility::detail::enable_if_t<
187 // NOLINTNEXTLINE [modernize-type-traits]
188 std::is_enum<Enum>::value, bool> = true>
189auto operator<<(std::ostream &outStream, Enum enumVal) -> std::ostream & {
190 // NOLINTNEXTLINE [modernize-type-traits]
191 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
192 outStream << mgutility::enum_name(enumVal);
193 return outStream;
194}
195
196#if defined(__cpp_lib_format)
197
198#include <format>
199
205template <typename Enum>
206 requires std::is_enum_v<Enum>
207struct std::formatter<Enum> : formatter<std::string_view> {
208 // NOLINTNEXTLINE [readability-identifier-length]
209 auto constexpr format(Enum e, format_context &ctx) const {
211 }
212};
213
214#endif
215
216#if defined(ENUM_NAME_USE_FMT) || \
217 (defined(MGUTILITY_HAS_HAS_INCLUDE) && __has_include(<fmt/format.h>))
218#include <fmt/format.h>
219
220template <class Enum>
221struct fmt::formatter<Enum, char,
222 mgutility::detail::enable_if_t<std::is_enum<Enum>::value>>
223 : formatter<string_view> {
224 // NOLINTNEXTLINE [readability-identifier-length]
225 auto format(const Enum e, format_context &ctx) const -> appender {
227 static_cast<mgutility::string_view>(mgutility::enum_name(e)).data(),
228 ctx);
229 }
230};
231#endif // MGUTILITY_USE_FMT || __has_include(<fmt/format.h>)
232
233#endif // MGUTILITY_ENUM_NAME_HPP
Definition core.h:1100
Definition core.h:1739
Definition doctest.h:530
Checks for MSVC compiler version.
Definition enum_for_each.hpp:35
constexpr auto enum_to_underlying(Enum enumValue) noexcept -> detail::underlying_type_t< Enum >
Converts an enum value to its underlying integer value.
Definition enum_name.hpp:40
MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept -> mgutility::optional< Enum >
Converts a string to an enum value.
Definition enum_name.hpp:108
MGUTILITY_CNSTXPR auto enum_cast(int value) noexcept -> mgutility::optional< Enum >
Casts an integer value to an enum value.
Definition enum_name.hpp:147
MGUTILITY_CNSTXPR auto enum_name(Enum enumValue) noexcept -> detail::string_or_view_t< Enum >
Gets the name of an enum value.
Definition enum_name.hpp:57
Definition core.h:1087