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 e) noexcept
41 -> detail::underlying_type_t<Enum> {
42 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
43 return static_cast<detail::underlying_type_t<Enum>>(e);
44}
45
55template <int Min, int Max, typename Enum>
56MGUTILITY_CNSTXPR auto enum_name(Enum e) noexcept
57 -> detail::string_or_view_t<Enum> {
58 static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
59 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
60 return detail::enum_name_impl<Enum, Min, Max>(e);
61}
62
72template <typename Enum, int Min = enum_range<Enum>::min,
73 int Max = enum_range<Enum>::max>
74MGUTILITY_CNSTXPR auto enum_name(Enum e) noexcept
75 -> detail::string_or_view_t<Enum> {
76 static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
77 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
78 return detail::enum_name_impl<Enum, Min, Max>(e);
79}
80
87template <typename Enum>
88auto enum_for_each<Enum>::enum_iter::operator*() const -> value_type {
89 auto value = static_cast<Enum>(m_pos);
90 return detail::enum_pair<Enum>{value, enum_name(value)};
91}
92
102template <typename Enum, int Min = enum_range<Enum>::min,
103 int Max = enum_range<Enum>::max,
104 detail::enable_if_t<!detail::has_bit_or<Enum>::value, bool> = true>
105MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
107 static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
108 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
109 return detail::to_enum_impl<Enum, Min, Max>(str);
110}
111
121template <typename Enum, int Min = enum_range<Enum>::min,
122 int Max = enum_range<Enum>::max,
123 detail::enable_if_t<detail::has_bit_or<Enum>::value, bool> = true>
124MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
126 static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
127 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
128 return detail::to_enum_bitmask_impl<Enum, Min, Max>(str);
129}
130
140template <typename Enum, int Min = enum_range<Enum>::min,
141 int Max = enum_range<Enum>::max>
142MGUTILITY_CNSTXPR auto enum_cast(int value) noexcept
144 static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
145 static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
146 if (enum_name(static_cast<Enum>(value)).empty()) {
147 return mgutility::nullopt;
148 }
149 return static_cast<Enum>(value);
150}
151
152namespace operators {
153template <typename Enum, mgutility::detail::enable_if_t<
154 std::is_enum<Enum>::value, bool> = true>
155inline constexpr auto operator&(const Enum &lhs, const Enum &rhs) -> Enum {
156 return static_cast<Enum>(mgutility::enum_to_underlying(lhs) &
158}
159
160template <typename Enum, mgutility::detail::enable_if_t<
161 std::is_enum<Enum>::value, bool> = true>
162inline constexpr auto operator|(const Enum &lhs, const Enum &rhs) -> Enum {
163 return static_cast<Enum>(mgutility::enum_to_underlying(lhs) |
165}
166} // namespace operators
167
168} // namespace mgutility
169
178template <typename Enum, mgutility::detail::enable_if_t<
179 std::is_enum<Enum>::value, bool> = true>
180auto operator<<(std::ostream &os, Enum e) -> std::ostream & {
181 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
182 os << mgutility::enum_name(e);
183 return os;
184}
185
186#if defined(__cpp_lib_format)
187
188#include <format>
189
195template <typename Enum>
196 requires std::is_enum_v<Enum>
197struct std::formatter<Enum> : formatter<std::string_view> {
198 auto constexpr format(Enum e, format_context &ctx) const {
199 return formatter<std::string_view>::format(mgutility::enum_name(e), ctx);
200 }
201};
202
203#endif
204
205#if defined(__has_include)
206#if __has_include(<fmt/ostream.h>)
207#include <fmt/ostream.h>
208#endif
209#endif
210
211#endif // MGUTILITY_ENUM_NAME_HPP
A basic string view class template.
Definition string_view.hpp:57
A class template that provides optional (nullable) objects.
Definition optional.hpp:60
Definition doctest.h:530
Checks for MSVC compiler version.
Definition enum_for_each.hpp:34
constexpr auto enum_to_underlying(Enum e) noexcept -> detail::underlying_type_t< Enum >
Converts an enum value to its underlying integer value.
Definition enum_name.hpp:40
auto nullopt
A global instance of nullopt_t to represent null optional.
Definition optional.hpp:316
MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept -> mgutility::optional< Enum >
Converts a string to an enum value.
Definition enum_name.hpp:105
MGUTILITY_CNSTXPR auto enum_name(Enum e) noexcept -> detail::string_or_view_t< Enum >
Gets the name of an enum value.
Definition enum_name.hpp:56
MGUTILITY_CNSTXPR auto enum_cast(int value) noexcept -> mgutility::optional< Enum >
Casts an integer value to an enum value.
Definition enum_name.hpp:142