/
tokenum.h
294 lines (254 loc) · 17.4 KB
/
tokenum.h
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
288
289
290
291
292
293
294
#include <iostream>
#include <typeinfo>
//#include <cxxabi.h>
#include <string.h>
#include <memory>
#include <array>
using u32 = uint32_t;
using s32 = int;
using u32 = uint32_t;
using s32 = int;
// 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 {
const char string[length];
public:
// Compile-time string construction from a string array and an integer_sequence to index it.
template<unsigned long long... indexes>
constexpr string_literal (const char(&raw_string)[sizeof...(indexes)], std::integer_sequence<unsigned long long, indexes...> index_sequence) : string{ raw_string[indexes]... } {
static_cast<void>(index_sequence);
}
// Get a pointer to the string data.
constexpr const char* c_str () const { return this->string; }
// Create a substring from this string by providing a template start position, length, and integer_sequence of indexes.
template<unsigned long long substring_start, unsigned long long substring_length, unsigned long long... substring_indexes>
constexpr string_literal<substring_length + 1> substr(std::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' }, std::make_integer_sequence<unsigned long long, substring_length + 1>{});
}
// Search for a given character within this string.
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)));
}
// Search for a given character within this string in reverse order starting from the end.
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)));
}
};
// The u_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:
// 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;
};
// Template struct declaration that combines two sequences into one.
template <typename integer_type, typename lhs_sequence, typename rhs_sequence>
struct merge_sequences;
// 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)...> {};
// 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 std::make_integer_sequence<integer_type, length / 2>::sequence_type,
typename std::make_integer_sequence<integer_type, length - length / 2>::sequence_type> {};
// 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> {};
// 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> {};
public:
// 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.
template <enum_type value>
constexpr static const char* helper_type() {
// Clang compiler format: __PRETTY_FUNCTION__ = static const char *u_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]
// MSVC compiler format: __FUNCSIG__ = const char *__cdecl u_enum_name<enum ENUM_TYPE_NAME,ENUM_VALUE>::helper_type<ENUM_TYPE_NAME::ENUM_VALUE_NAME>(void)
#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__, std::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>(std::make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__)
constexpr unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr u_string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr unsigned long long type_name_start = function_name.rfind('=') + 2;
constexpr unsigned long long type_name_end = function_name.rfind(':') - 1;
constexpr unsigned long long type_name_length = type_name_end - type_name_start;
constexpr u_string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(std::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__, gtl::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>(std::make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#else
#error "Unsupported compiler."
#endif
}
// 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.
template <enum_type value>
constexpr static const char* helper_value() {
// Clang compiler format: __PRETTY_FUNCTION__ = static const char *u_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]
// MSVC compiler format: __FUNCSIG__ = const char *__cdecl u_enum_name<enum ENUM_TYPE_NAME,ENUM_VALUE>::helper_value<ENUM_TYPE_NAME::ENUM_VALUE_NAME>(void)
#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__, std::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>(std::make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__)
constexpr unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, std::make_integer_sequence<unsigned long long, function_name_length>{});
constexpr unsigned long long type_name_start = function_name.rfind(':') + 1;
constexpr unsigned long long type_name_end = function_name_length - 2;
constexpr unsigned long long type_name_length = type_name_end - type_name_start;
constexpr string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(std::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__, std::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>(std::make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#else
#error "Unsupported compiler."
#endif
}
// 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.
template <enum_type value>
constexpr static const char* helper_name() {
// Clang compiler format: __PRETTY_FUNCTION__ = static const char *u_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]
// MSVC compiler format: __FUNCSIG__ = const char *__cdecl u_enum_name<enum ENUM_TYPE_NAME,ENUM_VALUE>::helper_value<ENUM_TYPE_NAME::ENUM_VALUE_NAME>(void)
#if defined(__clang__)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const u_string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, std::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 u_string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(std::make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#elif defined(__GNUC__)
constexpr unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr u_string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, detail::make_integer_sequence<unsigned long long, function_name_length>{});
constexpr unsigned long long type_name_start = function_name.rfind('=') + 2;
constexpr unsigned long long type_name_end = function_name_length - 2;
constexpr unsigned long long type_name_length = type_name_end - type_name_start;
constexpr u_string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(std::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__, std::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>(std::make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
#else
#error "Unsupported compiler."
#endif
}
public:
constexpr static const char* name_type() { return helper_type<enum_value>(); } // Get the name of the classes template enum type as a string.
constexpr static const char* name_value() { return helper_value<enum_value>(); } // Get the name of the classes template enum type value as a string.
constexpr static const char* name() { return helper_name<enum_value>(); } // Get the name of the classes template enum type and value as a string.
};
struct token {
const char * name;
s32 id;
};
template<typename enum_type, enum_type begin, enum_type end>
struct token_holder {
constexpr static inline int count_values(enum_type type) {
return (type != end)
? count_values((enum_type)(s32(type)+1))
: (s32(type)+1);
}
static constexpr u32 N = count_values(begin);
using tokens = std::array<token, N+1>; // because have {0, 0} in last element
template<typename e_enum_type, e_enum_type enum_value>
constexpr token make_name() {
return { enum_name<e_enum_type, enum_value>().name(), enum_value };
}
template<typename e_enum_type, u32... Indices>
constexpr tokens make_tokens_helper(std::integer_sequence<u32, Indices...>) {
tokens ts = { make_name<e_enum_type, (e_enum_type)Indices>()... };
ts[N] = {0, 0};
return ts;
}
template<typename e_enum_type>
constexpr tokens make_tokens() {
return make_tokens_helper<e_enum_type>( std::make_integer_sequence<u32, N>{});
}
operator const token *() const { return values.unsafe_ptr(); }
const token *data() const { return values.unsafe_ptr(); }
constexpr token_holder() : values{make_tokens<enum_type>()} {}
tokens values;
};
struct token {
const char * name;
s32 id;
};
template<typename enum_type, enum_type begin, enum_type end>
struct token_holder {
constexpr static inline int count_values(enum_type type) {
return (type != end)
? count_values((enum_type)(s32(type)+1))
: (s32(type)+1);
}
static constexpr u32 N = count_values(begin);
using tokens = std::array<token, N+1>; // because have {0, 0} in last element
template<typename e_enum_type, e_enum_type enum_value>
constexpr token make_name() {
return { enum_name<e_enum_type, enum_value>().name(), enum_value };
}
template<typename e_enum_type, u32... Indices>
constexpr tokens make_tokens_helper(std::integer_sequence<u32, Indices...>) {
tokens ts = { make_name<e_enum_type, (e_enum_type)Indices>()... };
ts[N] = {0, 0};
return ts;
}
template<typename e_enum_type>
constexpr tokens make_tokens() {
return make_tokens_helper<e_enum_type>( std::make_integer_sequence<u32, N>{});
}
operator const token *() const { return values.unsafe_ptr(); }
const token *data() const { return values.unsafe_ptr(); }
constexpr token_holder() : values{make_tokens<enum_type>()} {}
tokens values;
};
#ifdef TOKENNUM_TEST
enum test_enum { te_first, te_second, te_third, te_count };
int main() {
const token_holder<test_enum, te_first, te_count> test_tokens;
for (auto &s: test_tokens.values)
std::cout << "{" << (s.name ? s.name : "null") << ", " << s.id << " }" << std::endl;
return 0;
}
#endif // TOKENNUM_TEST