-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenum_name
287 lines (257 loc) · 22.6 KB
/
enum_name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_TYPE_ENUM_NAME_HPP
#define GTL_TYPE_ENUM_NAME_HPP
// Summary: Compile-time enum value name as a string with -fno-rtti.
namespace gtl {
/// @brief The enum_name class provides a name function for a given enum type and value.
template <typename enum_type, enum_type enum_value>
class enum_name final {
private:
/// @brief Template struct that holds a sequence of integers, derived from http://stackoverflow.com/a/32223343.
template <typename integer_type, integer_type... indexes>
struct integer_sequence {
using sequence_type = integer_sequence;
};
/// @brief Template struct declaration that combines two sequences into one.
template <typename integer_type, typename lhs_sequence, typename rhs_sequence>
struct merge_sequences;
/// @brief Template struct definition and partial specialisation for merging two interger_sequences.
template <typename integer_type, integer_type... lhs_indexes, integer_type... rhs_indexes>
struct merge_sequences<integer_type, integer_sequence<integer_type, lhs_indexes...>, integer_sequence<integer_type, rhs_indexes...>>
: integer_sequence<integer_type, lhs_indexes..., (sizeof...(lhs_indexes) + rhs_indexes)...> {
};
/// @brief Template struct to create an integer_sequence from zero to the given length.
template <typename integer_type, unsigned long long length>
struct make_integer_sequence final
: merge_sequences<integer_type, typename make_integer_sequence<integer_type, length / 2>::sequence_type, typename make_integer_sequence<integer_type, length - length / 2>::sequence_type> {
};
/// @brief Template struct partial specialisation of make_integer_sequence for an integer_sequence of length zero.
template <typename integer_type>
struct make_integer_sequence<integer_type, 0> final
: integer_sequence<integer_type> {
};
/// @brief Template struct partial specialisation of make_integer_sequence for an integer_sequence of length one.
template <typename integer_type>
struct make_integer_sequence<integer_type, 1> final
: integer_sequence<integer_type, 0> {
};
private:
/// @brief The string_literal class holds a compile-time constant string and provides both substring and character finding operations.
template <unsigned long long length>
class string_literal final {
private:
/// @brief The compile-time string is held in a constant sized array.
const char string[length];
public:
/// @brief Compile-time string construction from a string array and an integer_sequence to index it.
/// @param raw_string Input string to wrap.
/// @param index_sequence Template holding the indexes of the string.
template <unsigned long long... indexes>
constexpr string_literal(const char (&raw_string)[sizeof...(indexes)], integer_sequence<unsigned long long, indexes...> index_sequence)
: string{ raw_string[indexes]... } {
static_cast<void>(index_sequence);
}
public:
/// @brief Get a pointer to the string data.
/// @return Pointer to the string data stored in this class.
constexpr const char* c_str() const {
return this->string;
}
public:
/// @brief Create a substring from this string by providing a template start position, length, and integer_sequence of indexes.
/// @param substring_index_sequence Template holding the indexes of the new string.
/// @return A string_literal holding the requested substring.
template <unsigned long long substring_start, unsigned long long substring_length, unsigned long long... substring_indexes>
constexpr string_literal<substring_length + 1> substr(integer_sequence<unsigned long long, substring_indexes...> substring_index_sequence) const {
static_cast<void>(substring_index_sequence);
return string_literal<substring_length + 1>({ this->string[substring_start + substring_indexes]..., '\0' }, make_integer_sequence<unsigned long long, substring_length + 1>{});
}
public:
/// @brief Search for a given character within this string.
/// @param character The character to find.
/// @param starting_index The index of the string to start searching from.
/// @return The index of the character if found, otherwise 0xFFFFFFFFFFFFFFFF.
constexpr unsigned long long find(const char character, const unsigned long long starting_index = 0) const {
return ((starting_index >= length) ? 0xFFFFFFFFFFFFFFFF : ((this->string[starting_index] == character) ? starting_index : this->find(character, starting_index + 1)));
}
/// @brief Search for a given character within this string in reverse order starting from the end.
/// @param character The character to find.
/// @param starting_index The index of the string to start searching from.
/// @return The index of the character if found, otherwise 0xFFFFFFFFFFFFFFFF.
constexpr unsigned long long rfind(const char character, const unsigned long long starting_index = length - 1) const {
return ((starting_index == 0) ? ((this->string[starting_index] == character) ? starting_index : 0xFFFFFFFFFFFFFFFF) : ((this->string[starting_index] == character) ? starting_index : this->rfind(character, starting_index - 1)));
}
};
private:
/// @brief Helper function that directly takes an enum_type value template parameter to get the name of the classes template enum type value as a string.
/// @return Pointer to the string representation of the classes template enum type value.
template <enum_type value>
static const char* helper_type() {
// Clang compiler format: __PRETTY_FUNCTION__ = static const char *gtl::enum_name<ENUM_TYPE_NAME, ENUM_TYPE_NAME::ENUM_VALUE_NAME>::helper_type() [enum_type = ENUM_TYPE_NAME, enum_value = ENUM_TYPE_NAME::ENUM_VALUE_NAME, value = ENUM_TYPE_NAME::ENUM_VALUE_NAME]
// GNU <=8 compiler format: __PRETTY_FUNCTION__ = static const char* gtl::enum_name<enum_type, enum_value>::helper_type() [with enum_type value = (ENUM_TYPE_NAME)ENUM_VALUE; enum_type = ENUM_TYPE_NAME; enum_type enum_value = (ENUM_TYPE_NAME)ENUM_VALUE]
// GNU >=9 compiler format: __PRETTY_FUNCTION__ = static const char* gtl::enum_name<enum_type, enum_value>::helper_type() [with enum_type value = ENUM_TYPE_NAME::ENUM_VALUE_NAME; enum_type = ENUM_TYPE_NAME; enum_type enum_value = ENUM_TYPE_NAME::ENUM_VALUE_NAME]
// MSVC compiler format: __FUNCSIG__ = const char *__cdecl gtl::enum_name<enum ENUM_TYPE_NAME,ENUM_VALUE>::helper_type<ENUM_TYPE_NAME::ENUM_VALUE_NAME>(void)
// Note from the above that ENUM_VALUE_NAME does not exist in the GNU compiler __PRETTY_FUNCTION__ string.
// Therefore it is not possible to recover the enum value name when using gcc.
// Instead the string representation of a cast to the ENUM_TYPE_NAME of the int representation is returned.
#if defined(__clang__)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('=') + 2;
constexpr static const unsigned long long type_name_end = function_name.rfind(':') - 1;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__) && (__GNUC__ <= 8)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('(') + 1;
constexpr static const unsigned long long type_name_end = function_name.rfind(')');
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__) && (__GNUC__ >= 9)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('=') + 2;
constexpr static const unsigned long long type_name_end = function_name.rfind(':') - 1;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__FUNCSIG__) || defined(_MSC_VER)
constexpr static const unsigned long long function_name_length = sizeof(__FUNCSIG__);
constexpr static const string_literal<function_name_length> function_name(__FUNCSIG__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('<') + 1;
constexpr static const unsigned long long type_name_end = function_name.rfind(':') - 1;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#else
#error "Unsupported compiler."
#endif
}
/// @brief Helper function that directly takes an enum_type value template parameter to get the name of the classes template enum type value as a string.
/// @return Pointer to the string representation of the classes template enum type value.
template <enum_type value>
static const char* helper_value() {
// Clang compiler format: __PRETTY_FUNCTION__ = static const char *gtl::enum_name<ENUM_TYPE_NAME, ENUM_TYPE_NAME::ENUM_VALUE_NAME>::helper_value() [enum_type = ENUM_TYPE_NAME, enum_value = ENUM_TYPE_NAME::ENUM_VALUE_NAME, value = ENUM_TYPE_NAME::ENUM_VALUE_NAME]
// GNU <=8 compiler format: __PRETTY_FUNCTION__ = static const char* gtl::enum_name<enum_type, enum_value>::helper_value() [with enum_type value = (ENUM_TYPE_NAME)ENUM_VALUE; enum_type = ENUM_TYPE_NAME; enum_type enum_value = (ENUM_TYPE_NAME)ENUM_VALUE]
// GNU >=9 compiler format: __PRETTY_FUNCTION__ = static const char* gtl::enum_name<enum_type, enum_value>::helper_value() [with enum_type value = ENUM_TYPE_NAME::ENUM_VALUE_NAME; enum_type = ENUM_TYPE_NAME; enum_type enum_value = ENUM_TYPE_NAME::ENUM_VALUE_NAME]
// MSVC compiler format: __FUNCSIG__ = const char *__cdecl gtl::enum_name<enum ENUM_TYPE_NAME,ENUM_VALUE>::helper_value<ENUM_TYPE_NAME::ENUM_VALUE_NAME>(void)
// Note from the above that ENUM_VALUE_NAME does not exist in the GNU compiler __PRETTY_FUNCTION__ string.
// Therefore it is not possible to recover the enum value name when using gcc.
// Instead the string representation of a cast to the ENUM_TYPE_NAME of the int representation is returned.
#if defined(__clang__)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind(':') + 1;
constexpr static const unsigned long long type_name_end = function_name_length - 2;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__) && (__GNUC__ <= 8)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind(')') + 1;
constexpr static const unsigned long long type_name_end = function_name_length - 2;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__) && (__GNUC__ >= 9)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind(':') + 1;
constexpr static const unsigned long long type_name_end = function_name_length - 2;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__FUNCSIG__) || defined(_MSC_VER)
constexpr static const unsigned long long function_name_length = sizeof(__FUNCSIG__);
constexpr static const string_literal<function_name_length> function_name(__FUNCSIG__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind(':') + 1;
constexpr static const unsigned long long type_name_end = function_name.rfind('>');
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#else
#error "Unsupported compiler."
#endif
}
/// @brief Helper function that directly takes an enum_type value template parameter to get the name of the classes template enum type value as a string.
/// @return Pointer to the string representation of the classes template enum type value.
template <enum_type value>
static const char* helper_name() {
// Clang compiler format: __PRETTY_FUNCTION__ = static const char *gtl::enum_name<ENUM_TYPE_NAME, ENUM_TYPE_NAME::ENUM_VALUE_NAME>::helper_value() [enum_type = ENUM_TYPE_NAME, enum_value = ENUM_TYPE_NAME::ENUM_VALUE_NAME, value = ENUM_TYPE_NAME::ENUM_VALUE_NAME]
// GNU <=8 compiler format: __PRETTY_FUNCTION__ = static const char* gtl::enum_name<enum_type, enum_value>::helper_value() [with enum_type value = (ENUM_TYPE_NAME)ENUM_VALUE; enum_type = ENUM_TYPE_NAME; enum_type enum_value = (ENUM_TYPE_NAME)ENUM_VALUE]
// GNU >=9 compiler format: __PRETTY_FUNCTION__ = static const char* gtl::enum_name<enum_type, enum_value>::helper_value() [with enum_type value = ENUM_TYPE_NAME::ENUM_VALUE_NAME; enum_type = ENUM_TYPE_NAME; enum_type enum_value = ENUM_TYPE_NAME::ENUM_VALUE_NAME]
// MSVC compiler format: __FUNCSIG__ = const char *__cdecl gtl::enum_name<enum ENUM_TYPE_NAME,ENUM_VALUE>::helper_value<ENUM_TYPE_NAME::ENUM_VALUE_NAME>(void)
// Note from the above that ENUM_VALUE_NAME does not exist in the GNU compiler __PRETTY_FUNCTION__ string.
// Therefore it is not possible to recover the enum value name when using gcc.
// Instead the string representation of a cast to the ENUM_TYPE_NAME of the int representation is returned.
#if defined(__clang__)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('=') + 2;
constexpr static const unsigned long long type_name_end = function_name_length - 2;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__) && (__GNUC__ <= 8)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('=') + 2;
constexpr static const unsigned long long type_name_end = function_name_length - 2;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__) && (__GNUC__ >= 9)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('=') + 2;
constexpr static const unsigned long long type_name_end = function_name_length - 2;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__FUNCSIG__) || defined(_MSC_VER)
constexpr static const unsigned long long function_name_length = sizeof(__FUNCSIG__);
constexpr static const string_literal<function_name_length> function_name(__FUNCSIG__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('<') + 1;
constexpr static const unsigned long long type_name_end = function_name.rfind('>');
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#else
#error "Unsupported compiler."
#endif
}
public:
/// @brief Get the name of the classes template enum type as a string.
/// @return Pointer to the string representation of the classes template enum type.
static const char* name_type() {
return helper_type<enum_value>();
}
/// @brief Get the name of the classes template enum type value as a string.
/// @return Pointer to the string representation of the classes template enum type value.
static const char* name_value() {
return helper_value<enum_value>();
}
/// @brief Get the name of the classes template enum type and value as a string.
/// @return Pointer to the string representation of the classes template enum type and value.
static const char* name() {
return helper_name<enum_value>();
}
};
}
#endif // GTL_TYPE_ENUM_NAME_HPP