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>
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>
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} // namespace mgutility
152
161template <typename Enum, mgutility::detail::enable_if_t<
162 std::is_enum<Enum>::value, bool> = true>
163auto operator<<(std::ostream &os, Enum e) -> std::ostream & {
164 static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
165 os << mgutility::enum_name(e);
166 return os;
167}
168
169#if defined(__cpp_lib_format)
170
171#include <format>
172
178template <typename Enum>
179 requires std::is_enum_v<Enum>
180struct std::formatter<Enum> : formatter<std::string_view> {
181 auto constexpr format(Enum e, format_context &ctx) const {
182 return formatter<std::string_view>::format(mgutility::enum_name(e), ctx);
183 }
184};
185
186#endif
187
188#if defined(__has_include)
189#if __has_include(<fmt/ostream.h>)
190#include <fmt/ostream.h>
191#endif
192#endif
193
194#endif // MGUTILITY_ENUM_NAME_HPP
A basic string view class template.
Definition string_view.hpp:57
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
Definition meta.hpp:161