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
75#ifndef MGUTILITY_GLOBAL_ENUM_BLOB_SIZE
76// NOLINTNEXTLINE [cppcoreguidelines-macro-usage]
77#define MGUTILITY_GLOBAL_ENUM_BLOB_SIZE 8192
78#endif
79
80#ifndef MGUTILITY_INLINE
81#if MGUTILITY_CPLUSPLUS > 201402L
82#define MGUTILITY_INLINE inline
83#else
84#define MGUTILITY_INLINE
85#endif
86#endif
87
93template <typename E> struct is_scoped_enum {
97 static constexpr auto value =
98 // NOLINTNEXTLINE [modernize-type-traits]
99 std::is_enum<E>::value &&
100 // NOLINTNEXTLINE [modernize-type-traits]
101 !std::is_convertible<E, typename std::underlying_type<E>::type>::value;
102};
103
110template <typename T, typename = void> struct has_bit_or : std::false_type {};
111
118template <typename T>
119struct has_bit_or<T, decltype((T{} | T{}), void())> : std::true_type {};
120
121#if MGUTILITY_CPLUSPLUS > 201103L
127template <typename E>
128static constexpr bool is_scoped_enum_v = is_scoped_enum<E>::value;
129#endif
130
141template <bool B, class T = void>
142// NOLINTNEXTLINE [modernize-type-traits]
143using enable_if_t = typename std::enable_if<B, T>::type;
144
150template <typename T>
151// NOLINTNEXTLINE [modernize-type-traits]
152using underlying_type_t = typename std::underlying_type<T>::type;
153
159template <typename T>
160// NOLINTNEXTLINE [modernize-type-traits]
161using remove_const_t = typename std::remove_const<T>::type;
162
169template <typename Enum, Enum... Values> struct enum_sequence {};
170
178template <typename Enum, int Min, typename Seq> struct enum_sequence_from_index;
179
187template <typename Enum, int Min, std::size_t... I>
188struct enum_sequence_from_index<Enum, Min, index_sequence<I...>> {
189private:
190 // NOLINTNEXTLINE [readability-identifier-length]
191 static constexpr int offset(std::size_t i) {
192 return Min + static_cast<int>(i);
193 }
194
195public:
196 using type = enum_sequence<Enum, static_cast<Enum>(offset(I))...>;
197};
198
206template <typename Enum, int Min, int Max>
207using make_enum_sequence = typename enum_sequence_from_index<
208 Enum, Min,
209 make_index_sequence<static_cast<std::size_t>(Max - Min + 1)>>::type;
210} // namespace detail
211
217template <typename T> struct enum_range {
218 static constexpr auto min{MGUTILITY_ENUM_RANGE_MIN};
219 static constexpr auto max{MGUTILITY_ENUM_RANGE_MAX};
220};
221
222template <typename T, typename U> struct pair {
223 T first;
224 U second;
225};
226
227template <typename T>
228#if MGUTILITY_CPLUSPLUS >= 201402L || defined(__GNUC__) && !defined(__clang__)
229using flat_map = std::initializer_list<pair<T, const char *>>;
230#else
231// NOLINTNEXTLINE [cppcoreguidelines-avoid-c-arrays]
233#endif
234
240template <typename T> struct custom_enum {
241 // #if MGUTILITY_CPLUSPLUS > 201402L
242 static MGUTILITY_INLINE constexpr flat_map<T> map = {};
243 // #else
244 // static constexpr flat_map<T> map() noexcept {
245 // return {}; // default: empty map
246 // }
247 // #endif
248};
249
255template <typename T> struct enum_name_buffer {
256 static constexpr auto size = MGUTILITY_ENUM_NAME_BUFFER_SIZE;
257};
258
259} // namespace mgutility
260
261#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:240
Helper for creating enum sequences from index sequences.
Definition meta.hpp:178
Represents a sequence of enumeration values.
Definition meta.hpp:169
Trait to check if a type supports the bitwise OR operator.
Definition meta.hpp:110
Represents a compile-time sequence of indices.
Definition mgutility_enum_name.hpp:251
Trait to check if a type is a scoped enumeration.
Definition meta.hpp:93
static constexpr auto value
Boolean value indicating if the type is a scoped enumeration.
Definition meta.hpp:97
Provides the name buffer size for an enumeration type.
Definition meta.hpp:255
Provides the range for an enumeration type.
Definition meta.hpp:217
Definition meta.hpp:222