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,
40 // NOLINTNEXTLINE [modernize-type-traits]
41 detail::enable_if_t<std::is_enum<Enum>::value, bool> = true>
42constexpr auto to_underlying(Enum enumValue) noexcept
43 -> detail::underlying_type_t<Enum> {
44 // NOLINTNEXTLINE [modernize-type-traits]
45 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
46 return static_cast<detail::underlying_type_t<Enum>>(enumValue);
47}
48
58template <int Min, int Max, typename Enum>
60 -> detail::string_or_view_t<Enum> {
61 static_assert(Min < Max, "Max must be greater than Min!");
62 // NOLINTNEXTLINE [modernize-type-traits]
63 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
64 return detail::enum_name_impl<Enum, Min, Max>(enumValue);
65}
66
76template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
77 int Max = static_cast<int>(enum_range<Enum>::max)>
78MGUTILITY_CNSTXPR auto enum_name(Enum enumValue) noexcept
79 -> detail::string_or_view_t<Enum> {
80 static_assert(Min < Max, "Max must be greater than Min!");
81 // NOLINTNEXTLINE [modernize-type-traits]
82 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
83 return detail::enum_name_impl<Enum, Min, Max>(enumValue);
84}
85
92template <typename Enum>
93auto enum_for_each<Enum>::enum_iter::operator*() const -> value_type {
94 auto value = static_cast<Enum>(m_pos);
95 return detail::enum_pair<Enum>{value, enum_name(value)};
96}
97
107template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
108 int Max = static_cast<int>(enum_range<Enum>::max),
109 detail::enable_if_t<!detail::has_bit_or<Enum>::value, bool> = true>
112 static_assert(Min < Max, "Max must be greater than Min!");
113 // NOLINTNEXTLINE [modernize-type-traits]
114 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
115 return detail::to_enum_impl<Enum, Min, Max>(str);
116}
117
127template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
128 int Max = static_cast<int>(enum_range<Enum>::max),
129 detail::enable_if_t<detail::has_bit_or<Enum>::value, bool> = true>
130MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
132 static_assert(Min < Max, "Max must be greater than Min!");
133 // NOLINTNEXTLINE [modernize-type-traits]
134 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
135 return detail::to_enum_bitmask_impl<Enum, Min, Max>(str);
136}
137
147template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
148 int Max = static_cast<int>(enum_range<Enum>::max)>
149MGUTILITY_CNSTXPR auto enum_cast(int value) noexcept
151 static_assert(Min < Max, "Max must be greater than Min!");
152 // NOLINTNEXTLINE [modernize-type-traits]
153 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
154 if (enum_name(static_cast<Enum>(value)).empty()) {
155 return mgutility::nullopt;
156 }
157 return static_cast<Enum>(value);
158}
159
160namespace operators {
161template <typename Enum, mgutility::detail::enable_if_t<
162 // NOLINTNEXTLINE [modernize-type-traits]
163 std::is_enum<Enum>::value, bool> = true>
164constexpr auto operator&(const Enum &lhs, const Enum &rhs) -> Enum {
165 return static_cast<Enum>(mgutility::to_underlying(lhs) &
167}
168
169template <typename Enum, mgutility::detail::enable_if_t<
170 // NOLINTNEXTLINE [modernize-type-traits]
171 std::is_enum<Enum>::value, bool> = true>
172constexpr auto operator|(const Enum &lhs, const Enum &rhs) -> Enum {
173 return static_cast<Enum>(mgutility::to_underlying(lhs) |
175}
176} // namespace operators
177
178} // namespace mgutility
179
188template <typename Enum, mgutility::detail::enable_if_t<
189 // NOLINTNEXTLINE [modernize-type-traits]
190 std::is_enum<Enum>::value, bool> = true>
191auto operator<<(std::ostream &outStream, Enum enumVal) -> std::ostream & {
192 // NOLINTNEXTLINE [modernize-type-traits]
193 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
194 outStream << mgutility::enum_name(enumVal);
195 return outStream;
196}
197
198#if defined(__cpp_lib_format)
199
200#include <format>
201
207template <typename Enum>
208 requires std::is_enum_v<Enum>
209struct std::formatter<Enum> : formatter<std::string_view> {
210 // NOLINTNEXTLINE [readability-identifier-length]
211 auto constexpr format(Enum e, format_context &ctx) const {
212 return formatter<std::string_view>::format(mgutility::enum_name(e), ctx);
213 }
214};
215
216#endif
217
218#if defined(ENUM_NAME_USE_FMT) || \
219 (defined(MGUTILITY_HAS_HAS_INCLUDE) && __has_include(<fmt/format.h>))
220#include <fmt/format.h>
221
222template <class Enum>
223struct fmt::formatter<Enum, char,
224 // NOLINTNEXTLINE [modernize-type-traits]
225 mgutility::detail::enable_if_t<std::is_enum<Enum>::value>>
226 : formatter<string_view> {
227 // NOLINTNEXTLINE [readability-identifier-length]
228 auto format(const Enum e, format_context &ctx) const -> appender {
229 return formatter<string_view>::format(
230 static_cast<mgutility::string_view>(mgutility::enum_name(e)).data(),
231 ctx);
232 }
233};
234#endif // MGUTILITY_USE_FMT || __has_include(<fmt/format.h>)
235
236#endif // MGUTILITY_ENUM_NAME_HPP
Checks for MSVC compiler version.
Definition enum_for_each.hpp:35
MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept -> mgutility::optional< Enum >
Converts a string to an enum value.
Definition enum_name.hpp:110
MGUTILITY_CNSTXPR auto enum_cast(int value) noexcept -> mgutility::optional< Enum >
Casts an integer value to an enum value.
Definition enum_name.hpp:149
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:59
constexpr auto to_underlying(Enum enumValue) noexcept -> detail::underlying_type_t< Enum >
Converts an enum value to its underlying integer value.
Definition enum_name.hpp:42
Definition meta.hpp:178