enum_name
Loading...
Searching...
No Matches
meta.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_META_HPP
26#define DETAIL_META_HPP
27
28#include "mgutility/_common/definitions.hpp"
29#include "mgutility/std/utility.hpp"
30#include <initializer_list>
31#include <type_traits>
32#include <utility>
33
34namespace mgutility {
35namespace detail {
36
37#ifndef MGUTILITY_ENUM_RANGE_MIN
43// NOLINTNEXTLINE [cppcoreguidelines-macro-usage]
44#define MGUTILITY_ENUM_RANGE_MIN 0
45#endif
46
47#ifndef MGUTILITY_ENUM_RANGE_MAX
53// NOLINTNEXTLINE [cppcoreguidelines-macro-usage]
54#define MGUTILITY_ENUM_RANGE_MAX 256
55#endif
56
62#ifndef MGUTILITY_ENUM_NAME_BUFFER_SIZE
63// NOLINTNEXTLINE [cppcoreguidelines-macro-usage]
64#define MGUTILITY_ENUM_NAME_BUFFER_SIZE 32U
65#endif
66
67#ifndef MGUTILITY_INLINE
68#if MGUTILITY_CPLUSPLUS > 201402L
69#define MGUTILITY_INLINE inline
70#else
71#define MGUTILITY_INLINE
72#endif
73#endif
74
80template <typename E> struct is_scoped_enum {
84 static constexpr auto value =
85 // NOLINTNEXTLINE [modernize-type-traits]
86 std::is_enum<E>::value &&
87 // NOLINTNEXTLINE [modernize-type-traits]
88 !std::is_convertible<E, typename std::underlying_type<E>::type>::value;
89};
90
97template <typename T, typename = void> struct has_bit_or : std::false_type {};
98
105template <typename T>
106struct has_bit_or<T, decltype((T{} | T{}), void())> : std::true_type {};
107
108#if MGUTILITY_CPLUSPLUS > 201103L
114template <typename E>
115static constexpr bool is_scoped_enum_v = is_scoped_enum<E>::value;
116#endif
117
128template <bool B, class T = void>
129// NOLINTNEXTLINE [modernize-type-traits]
130using enable_if_t = typename std::enable_if<B, T>::type;
131
137template <typename T>
138// NOLINTNEXTLINE [modernize-type-traits]
139using underlying_type_t = typename std::underlying_type<T>::type;
140
146template <typename T>
147// NOLINTNEXTLINE [modernize-type-traits]
148using remove_const_t = typename std::remove_const<T>::type;
149
156template <typename Enum, Enum... Values> struct enum_sequence {};
157
165template <typename Enum, int Min, typename Seq> struct enum_sequence_from_index;
166
174template <typename Enum, int Min, std::size_t... I>
175struct enum_sequence_from_index<Enum, Min, index_sequence<I...>> {
176private:
177 // NOLINTNEXTLINE [readability-identifier-length]
178 static constexpr int offset(std::size_t i) {
179 return Min + static_cast<int>(i);
180 }
181
182public:
183 using type = enum_sequence<Enum, static_cast<Enum>(offset(I))...>;
184};
185
193template <typename Enum, int Min, int Max>
194using make_enum_sequence = typename enum_sequence_from_index<
195 Enum, Min,
196 make_index_sequence<static_cast<std::size_t>(Max - Min + 1)>>::type;
197} // namespace detail
198
204template <typename T> struct enum_range {
205 static constexpr auto min{MGUTILITY_ENUM_RANGE_MIN};
206 static constexpr auto max{MGUTILITY_ENUM_RANGE_MAX};
207};
208
209template <typename T, typename U> struct pair {
210 T first;
211 U second;
212};
213
214template <typename T>
215#if MGUTILITY_CPLUSPLUS >= 201402L || defined(__GNUC__) && !defined(__clang__)
216using flat_map = std::initializer_list<pair<T, const char *>>;
217#else
218// NOLINTNEXTLINE [cppcoreguidelines-avoid-c-arrays]
220#endif
221
227template <typename T> struct custom_enum {
228 // #if MGUTILITY_CPLUSPLUS > 201402L
229 static MGUTILITY_INLINE constexpr flat_map<T> map = {};
230 // #else
231 // static constexpr flat_map<T> map() noexcept {
232 // return {}; // default: empty map
233 // }
234 // #endif
235};
236
242template <typename T> struct enum_name_buffer {
243 static constexpr auto size = MGUTILITY_ENUM_NAME_BUFFER_SIZE;
244};
245
246} // namespace mgutility
247
248#endif // DETAIL_META_HPP
Checks for MSVC compiler version.
Definition enum_for_each.hpp:35
Provides the custom names map for an enumeration type.
Definition meta.hpp:227
Helper for creating enum sequences from index sequences.
Definition meta.hpp:165
Represents a sequence of enumeration values.
Definition meta.hpp:156
Trait to check if a type supports the bitwise OR operator.
Definition meta.hpp:97
Represents a compile-time sequence of indices.
Definition mgutility_enum_name.hpp:223
Trait to check if a type is a scoped enumeration.
Definition meta.hpp:80
static constexpr auto value
Boolean value indicating if the type is a scoped enumeration.
Definition meta.hpp:84
Provides the name buffer size for an enumeration type.
Definition meta.hpp:242
Provides the range for an enumeration type.
Definition meta.hpp:204
Definition meta.hpp:209