nameof.cxx 0.10.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +185 -0
  3. package/nameof.hpp +1254 -0
  4. package/package.json +29 -0
package/nameof.hpp ADDED
@@ -0,0 +1,1254 @@
1
+ // _ _ __ _____
2
+ // | \ | | / _| / ____|_ _
3
+ // | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
4
+ // | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
5
+ // | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
6
+ // |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
7
+ // https://github.com/Neargye/nameof
8
+ // version 0.10.4
9
+ //
10
+ // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
11
+ // SPDX-License-Identifier: MIT
12
+ // Copyright (c) 2016 - 2024 Daniil Goncharov <neargye@gmail.com>.
13
+ //
14
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ // of this software and associated documentation files (the "Software"), to deal
16
+ // in the Software without restriction, including without limitation the rights
17
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ // copies of the Software, and to permit persons to whom the Software is
19
+ // furnished to do so, subject to the following conditions:
20
+ //
21
+ // The above copyright notice and this permission notice shall be included in all
22
+ // copies or substantial portions of the Software.
23
+ //
24
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ // SOFTWARE.
31
+
32
+ #ifndef NEARGYE_NAMEOF_HPP
33
+ #define NEARGYE_NAMEOF_HPP
34
+
35
+ #define NAMEOF_VERSION_MAJOR 0
36
+ #define NAMEOF_VERSION_MINOR 10
37
+ #define NAMEOF_VERSION_PATCH 4
38
+
39
+ #include <array>
40
+ #include <cassert>
41
+ #include <cstdint>
42
+ #include <cstddef>
43
+ #include <iosfwd>
44
+ #include <iterator>
45
+ #include <limits>
46
+ #include <type_traits>
47
+ #include <utility>
48
+
49
+ #if !defined(NAMEOF_USING_ALIAS_STRING)
50
+ # include <string>
51
+ #endif
52
+ #if !defined(NAMEOF_USING_ALIAS_STRING_VIEW)
53
+ # include <string_view>
54
+ #endif
55
+
56
+ #if __has_include(<cxxabi.h>)
57
+ # include <cxxabi.h>
58
+ # include <cstdlib>
59
+ #endif
60
+
61
+ #if defined(__clang__)
62
+ # pragma clang diagnostic push
63
+ # pragma clang diagnostic ignored "-Wunknown-warning-option"
64
+ # pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
65
+ #elif defined(__GNUC__)
66
+ # pragma GCC diagnostic push
67
+ # pragma GCC diagnostic ignored "-Wstringop-overflow" // Missing terminating nul 'enum_name_v'.
68
+ #elif defined(_MSC_VER)
69
+ # pragma warning(push)
70
+ # pragma warning(disable : 26495) // Variable 'cstring<N>::chars_' is uninitialized.
71
+ # pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value.
72
+ # pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call.
73
+ # pragma warning(disable : 4514) // Unreferenced inline function has been removed.
74
+ #endif
75
+
76
+ // Checks nameof_type compiler compatibility.
77
+ #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && _MSC_VER >= 1910
78
+ # undef NAMEOF_TYPE_SUPPORTED
79
+ # define NAMEOF_TYPE_SUPPORTED 1
80
+ #endif
81
+
82
+ // Checks nameof_type_rtti compiler compatibility.
83
+ #if defined(__clang__)
84
+ # if __has_feature(cxx_rtti)
85
+ # undef NAMEOF_TYPE_RTTI_SUPPORTED
86
+ # define NAMEOF_TYPE_RTTI_SUPPORTED 1
87
+ # endif
88
+ #elif defined(__GNUC__)
89
+ # if defined(__GXX_RTTI)
90
+ # undef NAMEOF_TYPE_RTTI_SUPPORTED
91
+ # define NAMEOF_TYPE_RTTI_SUPPORTED 1
92
+ # endif
93
+ #elif defined(_MSC_VER)
94
+ # if defined(_CPPRTTI)
95
+ # undef NAMEOF_TYPE_RTTI_SUPPORTED
96
+ # define NAMEOF_TYPE_RTTI_SUPPORTED 1
97
+ # endif
98
+ #endif
99
+
100
+ // Checks nameof_member compiler compatibility.
101
+ #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
102
+ # undef NAMEOF_MEMBER_SUPPORTED
103
+ # define NAMEOF_MEMBER_SUPPORTED 1
104
+ #endif
105
+
106
+ // Checks nameof_pointer compiler compatibility.
107
+ #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
108
+ # undef NAMEOF_POINTER_SUPPORTED
109
+ # define NAMEOF_POINTER_SUPPORTED 1
110
+ #endif
111
+
112
+ // Checks nameof_enum compiler compatibility.
113
+ #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910
114
+ # undef NAMEOF_ENUM_SUPPORTED
115
+ # define NAMEOF_ENUM_SUPPORTED 1
116
+ #endif
117
+
118
+ // Checks nameof_enum compiler aliases compatibility.
119
+ #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920
120
+ # undef NAMEOF_ENUM_SUPPORTED_ALIASES
121
+ # define NAMEOF_ENUM_SUPPORTED_ALIASES 1
122
+ #endif
123
+
124
+ // Enum value must be greater or equals than NAMEOF_ENUM_RANGE_MIN. By default NAMEOF_ENUM_RANGE_MIN = -128.
125
+ // If need another min range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN.
126
+ #if !defined(NAMEOF_ENUM_RANGE_MIN)
127
+ # define NAMEOF_ENUM_RANGE_MIN -128
128
+ #endif
129
+
130
+ // Enum value must be less or equals than NAMEOF_ENUM_RANGE_MAX. By default NAMEOF_ENUM_RANGE_MAX = 128.
131
+ // If need another max range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MAX.
132
+ #if !defined(NAMEOF_ENUM_RANGE_MAX)
133
+ # define NAMEOF_ENUM_RANGE_MAX 128
134
+ #endif
135
+
136
+ namespace nameof {
137
+
138
+ // If need another string_view type, define the macro NAMEOF_USING_ALIAS_STRING_VIEW.
139
+ #if defined(NAMEOF_USING_ALIAS_STRING_VIEW)
140
+ NAMEOF_USING_ALIAS_STRING_VIEW
141
+ #else
142
+ using std::string_view;
143
+ #endif
144
+
145
+ // If need another string type, define the macro NAMEOF_USING_ALIAS_STRING.
146
+ #if defined(NAMEOF_USING_ALIAS_STRING)
147
+ NAMEOF_USING_ALIAS_STRING
148
+ #else
149
+ using std::string;
150
+ #endif
151
+
152
+ namespace customize {
153
+
154
+ // Enum value must be in range [NAMEOF_ENUM_RANGE_MIN, NAMEOF_ENUM_RANGE_MAX]. By default NAMEOF_ENUM_RANGE_MIN = -128, NAMEOF_ENUM_RANGE_MAX = 128.
155
+ // If you need another range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN and NAMEOF_ENUM_RANGE_MAX.
156
+ // If you need another range for specific enum type, add specialization enum_range for necessary enum type.
157
+ template <typename E>
158
+ struct enum_range {
159
+ static_assert(std::is_enum_v<E>, "nameof::customize::enum_range requires enum type.");
160
+ inline static constexpr int min = NAMEOF_ENUM_RANGE_MIN;
161
+ inline static constexpr int max = NAMEOF_ENUM_RANGE_MAX;
162
+ static_assert(max > min, "nameof::customize::enum_range requires max > min.");
163
+ };
164
+
165
+ static_assert(NAMEOF_ENUM_RANGE_MIN <= 0, "NAMEOF_ENUM_RANGE_MIN must be less or equals than 0.");
166
+ static_assert(NAMEOF_ENUM_RANGE_MIN > (std::numeric_limits<std::int16_t>::min)(), "NAMEOF_ENUM_RANGE_MIN must be greater than INT16_MIN.");
167
+
168
+ static_assert(NAMEOF_ENUM_RANGE_MAX > 0, "NAMEOF_ENUM_RANGE_MAX must be greater than 0.");
169
+ static_assert(NAMEOF_ENUM_RANGE_MAX < (std::numeric_limits<std::int16_t>::max)(), "NAMEOF_ENUM_RANGE_MAX must be less than INT16_MAX.");
170
+
171
+ static_assert(NAMEOF_ENUM_RANGE_MAX > NAMEOF_ENUM_RANGE_MIN, "NAMEOF_ENUM_RANGE_MAX must be greater than NAMEOF_ENUM_RANGE_MIN.");
172
+
173
+ // If you need custom names for enum, add specialization enum_name for necessary enum type.
174
+ template <typename E>
175
+ constexpr string_view enum_name(E) noexcept {
176
+ static_assert(std::is_enum_v<E>, "nameof::customize::enum_name requires enum type.");
177
+ return {};
178
+ }
179
+
180
+ // If you need custom name for type, add specialization type_name for necessary type.
181
+ template <typename T>
182
+ constexpr string_view type_name() noexcept {
183
+ return {};
184
+ }
185
+
186
+ // If you need custom name for member, add specialization member_name for necessary type.
187
+ template <auto V>
188
+ constexpr string_view member_name() noexcept {
189
+ return {};
190
+ }
191
+
192
+ // If you need custom name for a pointer, add specialization pointer_name for necessary type.
193
+ template <auto V>
194
+ constexpr string_view pointer_name() noexcept {
195
+ return {};
196
+ }
197
+
198
+ } // namespace nameof::customize
199
+
200
+ template <std::uint16_t N>
201
+ class [[nodiscard]] cstring {
202
+ public:
203
+ using value_type = const char;
204
+ using size_type = std::uint16_t;
205
+ using difference_type = std::ptrdiff_t;
206
+ using pointer = const char*;
207
+ using const_pointer = const char*;
208
+ using reference = const char&;
209
+ using const_reference = const char&;
210
+
211
+ using iterator = const char*;
212
+ using const_iterator = const char*;
213
+
214
+ using reverse_iterator = std::reverse_iterator<iterator>;
215
+ using const_reverse_iterator = std::reverse_iterator<const_iterator>;
216
+
217
+ constexpr explicit cstring(string_view str) noexcept : cstring{str, std::make_integer_sequence<std::uint16_t, N>{}} {
218
+ assert(str.size() > 0 && str.size() == N);
219
+ }
220
+
221
+ constexpr cstring() = delete;
222
+
223
+ constexpr cstring(const cstring&) = default;
224
+
225
+ constexpr cstring(cstring&&) = default;
226
+
227
+ ~cstring() = default;
228
+
229
+ cstring& operator=(const cstring&) = default;
230
+
231
+ cstring& operator=(cstring&&) = default;
232
+
233
+ [[nodiscard]] constexpr const_pointer data() const noexcept { return chars_; }
234
+
235
+ [[nodiscard]] constexpr size_type size() const noexcept { return N; }
236
+
237
+ [[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); }
238
+
239
+ [[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); }
240
+
241
+ [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); }
242
+
243
+ [[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); }
244
+
245
+ [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return end(); }
246
+
247
+ [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return begin(); }
248
+
249
+ [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
250
+
251
+ [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return rend(); }
252
+
253
+ [[nodiscard]] constexpr const_reference operator[](size_type i) const noexcept { return assert(i < size()), chars_[i]; }
254
+
255
+ [[nodiscard]] constexpr const_reference front() const noexcept { return chars_[0]; }
256
+
257
+ [[nodiscard]] constexpr const_reference back() const noexcept { return chars_[N]; }
258
+
259
+ [[nodiscard]] constexpr size_type length() const noexcept { return size(); }
260
+
261
+ [[nodiscard]] constexpr bool empty() const noexcept { return false; }
262
+
263
+ [[nodiscard]] constexpr int compare(string_view str) const noexcept { return string_view{data(), size()}.compare(str); }
264
+
265
+ [[nodiscard]] constexpr const char* c_str() const noexcept { return data(); }
266
+
267
+ [[nodiscard]] string str() const { return {begin(), end()}; }
268
+
269
+ [[nodiscard]] constexpr operator string_view() const noexcept { return {data(), size()}; }
270
+
271
+ [[nodiscard]] constexpr explicit operator const_pointer() const noexcept { return data(); }
272
+
273
+ [[nodiscard]] explicit operator string() const { return {begin(), end()}; }
274
+
275
+ private:
276
+ template <std::uint16_t... I>
277
+ constexpr cstring(string_view str, std::integer_sequence<std::uint16_t, I...>) noexcept : chars_{str[I]..., '\0'} {}
278
+
279
+ char chars_[static_cast<std::size_t>(N) + 1];
280
+ };
281
+
282
+ template <>
283
+ class [[nodiscard]] cstring<0> {
284
+ public:
285
+ using value_type = const char;
286
+ using size_type = std::uint16_t;
287
+ using difference_type = std::ptrdiff_t;
288
+ using pointer = const char*;
289
+ using const_pointer = const char*;
290
+ using reference = const char&;
291
+ using const_reference = const char&;
292
+
293
+ using iterator = const char*;
294
+ using const_iterator = const char*;
295
+
296
+ using reverse_iterator = std::reverse_iterator<iterator>;
297
+ using const_reverse_iterator = std::reverse_iterator<const_iterator>;
298
+
299
+ constexpr explicit cstring(string_view) noexcept {}
300
+
301
+ constexpr cstring() = default;
302
+
303
+ constexpr cstring(const cstring&) = default;
304
+
305
+ constexpr cstring(cstring&&) = default;
306
+
307
+ ~cstring() = default;
308
+
309
+ cstring& operator=(const cstring&) = default;
310
+
311
+ cstring& operator=(cstring&&) = default;
312
+
313
+ [[nodiscard]] constexpr const_pointer data() const noexcept { return nullptr; }
314
+
315
+ [[nodiscard]] constexpr size_type size() const noexcept { return 0; }
316
+
317
+ [[nodiscard]] constexpr const_iterator begin() const noexcept { return nullptr; }
318
+
319
+ [[nodiscard]] constexpr const_iterator end() const noexcept { return nullptr; }
320
+
321
+ [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return nullptr; }
322
+
323
+ [[nodiscard]] constexpr const_iterator cend() const noexcept { return nullptr; }
324
+
325
+ [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return {}; }
326
+
327
+ [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return {}; }
328
+
329
+ [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return {}; }
330
+
331
+ [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return {}; }
332
+
333
+ [[nodiscard]] constexpr size_type length() const noexcept { return 0; }
334
+
335
+ [[nodiscard]] constexpr bool empty() const noexcept { return true; }
336
+
337
+ [[nodiscard]] constexpr int compare(string_view str) const noexcept { return string_view{}.compare(str); }
338
+
339
+ [[nodiscard]] constexpr const char* c_str() const noexcept { return nullptr; }
340
+
341
+ [[nodiscard]] string str() const { return {}; }
342
+
343
+ [[nodiscard]] constexpr operator string_view() const noexcept { return {}; }
344
+
345
+ [[nodiscard]] constexpr explicit operator const_pointer() const noexcept { return nullptr; }
346
+
347
+ [[nodiscard]] explicit operator string() const { return {}; }
348
+ };
349
+
350
+ template <std::uint16_t N>
351
+ [[nodiscard]] constexpr bool operator==(const cstring<N>& lhs, string_view rhs) noexcept {
352
+ return lhs.compare(rhs) == 0;
353
+ }
354
+
355
+ template <std::uint16_t N>
356
+ [[nodiscard]] constexpr bool operator==(string_view lhs, const cstring<N>& rhs) noexcept {
357
+ return lhs.compare(rhs) == 0;
358
+ }
359
+
360
+ template <std::uint16_t N>
361
+ [[nodiscard]] constexpr bool operator!=(const cstring<N>& lhs, string_view rhs) noexcept {
362
+ return lhs.compare(rhs) != 0;
363
+ }
364
+
365
+ template <std::uint16_t N>
366
+ [[nodiscard]] constexpr bool operator!=(string_view lhs, const cstring<N>& rhs) noexcept {
367
+ return lhs.compare(rhs) != 0;
368
+ }
369
+
370
+ template <std::uint16_t N>
371
+ [[nodiscard]] constexpr bool operator>(const cstring<N>& lhs, string_view rhs) noexcept {
372
+ return lhs.compare(rhs) > 0;
373
+ }
374
+
375
+ template <std::uint16_t N>
376
+ [[nodiscard]] constexpr bool operator>(string_view lhs, const cstring<N>& rhs) noexcept {
377
+ return lhs.compare(rhs) > 0;
378
+ }
379
+
380
+ template <std::uint16_t N>
381
+ [[nodiscard]] constexpr bool operator>=(const cstring<N>& lhs, string_view rhs) noexcept {
382
+ return lhs.compare(rhs) >= 0;
383
+ }
384
+
385
+ template <std::uint16_t N>
386
+ [[nodiscard]] constexpr bool operator>=(string_view lhs, const cstring<N>& rhs) noexcept {
387
+ return lhs.compare(rhs) >= 0;
388
+ }
389
+
390
+ template <std::uint16_t N>
391
+ [[nodiscard]] constexpr bool operator<(const cstring<N>& lhs, string_view rhs) noexcept {
392
+ return lhs.compare(rhs) < 0;
393
+ }
394
+
395
+ template <std::uint16_t N>
396
+ [[nodiscard]] constexpr bool operator<(string_view lhs, const cstring<N>& rhs) noexcept {
397
+ return lhs.compare(rhs) < 0;
398
+ }
399
+
400
+ template <std::uint16_t N>
401
+ [[nodiscard]] constexpr bool operator<=(const cstring<N>& lhs, string_view rhs) noexcept {
402
+ return lhs.compare(rhs) <= 0;
403
+ }
404
+
405
+ template <std::uint16_t N>
406
+ [[nodiscard]] constexpr bool operator<=(string_view lhs, const cstring<N>& rhs) noexcept {
407
+ return lhs.compare(rhs) <= 0;
408
+ }
409
+
410
+ template <typename Char, typename Traits, std::uint16_t N>
411
+ std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, const cstring<N>& srt) {
412
+ for (const auto c : srt) {
413
+ os.put(c);
414
+ }
415
+ return os;
416
+ }
417
+
418
+ namespace detail {
419
+
420
+ constexpr string_view pretty_name(string_view name, bool remove_suffix = true) noexcept {
421
+ if (name.size() >= 1 && (name[0] == '"' || name[0] == '\'')) {
422
+ return {}; // Narrow multibyte string literal.
423
+ } else if (name.size() >= 2 && name[0] == 'R' && (name[1] == '"' || name[1] == '\'')) {
424
+ return {}; // Raw string literal.
425
+ } else if (name.size() >= 2 && name[0] == 'L' && (name[1] == '"' || name[1] == '\'')) {
426
+ return {}; // Wide string literal.
427
+ } else if (name.size() >= 2 && name[0] == 'U' && (name[1] == '"' || name[1] == '\'')) {
428
+ return {}; // UTF-32 encoded string literal.
429
+ } else if (name.size() >= 2 && name[0] == 'u' && (name[1] == '"' || name[1] == '\'')) {
430
+ return {}; // UTF-16 encoded string literal.
431
+ } else if (name.size() >= 3 && name[0] == 'u' && name[1] == '8' && (name[2] == '"' || name[2] == '\'')) {
432
+ return {}; // UTF-8 encoded string literal.
433
+ } else if (name.size() >= 1 && (name[0] >= '0' && name[0] <= '9')) {
434
+ return {}; // Invalid name.
435
+ }
436
+
437
+ for (std::size_t i = name.size(), h = 0, s = 0; i > 0; --i) {
438
+ if (name[i - 1] == ')') {
439
+ ++h;
440
+ ++s;
441
+ continue;
442
+ } else if (name[i - 1] == '(') {
443
+ --h;
444
+ ++s;
445
+ continue;
446
+ }
447
+
448
+ if (h == 0) {
449
+ name.remove_suffix(s);
450
+ break;
451
+ } else {
452
+ ++s;
453
+ continue;
454
+ }
455
+ }
456
+
457
+ std::size_t s = 0;
458
+ for (std::size_t i = name.size(), h = 0; i > 0; --i) {
459
+ if (name[i - 1] == '>') {
460
+ ++h;
461
+ ++s;
462
+ continue;
463
+ } else if (name[i - 1] == '<') {
464
+ --h;
465
+ ++s;
466
+ continue;
467
+ }
468
+
469
+ if (h == 0) {
470
+ break;
471
+ } else {
472
+ ++s;
473
+ continue;
474
+ }
475
+ }
476
+
477
+ for (std::size_t i = name.size() - s; i > 0; --i) {
478
+ if (!((name[i - 1] >= '0' && name[i - 1] <= '9') ||
479
+ (name[i - 1] >= 'a' && name[i - 1] <= 'z') ||
480
+ (name[i - 1] >= 'A' && name[i - 1] <= 'Z') ||
481
+ (name[i - 1] == '_'))) {
482
+ name.remove_prefix(i);
483
+ break;
484
+ }
485
+ }
486
+ if (remove_suffix) {
487
+ name.remove_suffix(s);
488
+ }
489
+
490
+ if (name.size() > 0 && ((name[0] >= 'a' && name[0] <= 'z') ||
491
+ (name[0] >= 'A' && name[0] <= 'Z') ||
492
+ (name[0] == '_'))) {
493
+ return name;
494
+ }
495
+
496
+ return {}; // Invalid name.
497
+ }
498
+
499
+ #if defined(__cpp_lib_array_constexpr) && __cpp_lib_array_constexpr >= 201603L
500
+ # define NAMEOF_ARRAY_CONSTEXPR 1
501
+ #else
502
+ template <typename T, std::size_t N, std::size_t... I>
503
+ constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N], std::index_sequence<I...>) noexcept {
504
+ return {{a[I]...}};
505
+ }
506
+ #endif
507
+
508
+ template <typename L, typename R>
509
+ constexpr bool cmp_less(L lhs, R rhs) noexcept {
510
+ static_assert(std::is_integral_v<L> && std::is_integral_v<R>, "nameof::detail::cmp_less requires integral type.");
511
+
512
+ if constexpr (std::is_signed_v<L> == std::is_signed_v<R>) {
513
+ // If same signedness (both signed or both unsigned).
514
+ return lhs < rhs;
515
+ } else if constexpr (std::is_same_v<L, bool>) { // bool special case
516
+ return static_cast<R>(lhs) < rhs;
517
+ } else if constexpr (std::is_same_v<R, bool>) { // bool special case
518
+ return lhs < static_cast<L>(rhs);
519
+ } else if constexpr (std::is_signed_v<R>) {
520
+ // If 'right' is negative, then result is 'false', otherwise cast & compare.
521
+ return rhs > 0 && lhs < static_cast<std::make_unsigned_t<R>>(rhs);
522
+ } else {
523
+ // If 'left' is negative, then result is 'true', otherwise cast & compare.
524
+ return lhs < 0 || static_cast<std::make_unsigned_t<L>>(lhs) < rhs;
525
+ }
526
+ }
527
+
528
+ template <typename I>
529
+ constexpr I log2(I value) noexcept {
530
+ static_assert(std::is_integral_v<I>, "nameof::detail::log2 requires integral type.");
531
+
532
+ if constexpr (std::is_same_v<I, bool>) { // bool special case
533
+ return assert(false), value;
534
+ } else {
535
+ auto ret = I{0};
536
+ for (; value > I{1}; value >>= I{1}, ++ret) {}
537
+
538
+ return ret;
539
+ }
540
+ }
541
+
542
+ template <typename T>
543
+ struct nameof_enum_supported
544
+ #if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED || defined(NAMEOF_ENUM_NO_CHECK_SUPPORT)
545
+ : std::true_type {};
546
+ #else
547
+ : std::false_type {};
548
+ #endif
549
+
550
+ template <typename T, typename R>
551
+ using enable_if_enum_t = std::enable_if_t<std::is_enum_v<std::decay_t<T>>, R>;
552
+
553
+ template <typename T>
554
+ inline constexpr bool is_enum_v = std::is_enum_v<T> && std::is_same_v<T, std::decay_t<T>>;
555
+
556
+ template <typename E, E V>
557
+ constexpr auto n() noexcept {
558
+ static_assert(is_enum_v<E>, "nameof::detail::n requires enum type.");
559
+
560
+ if constexpr (nameof_enum_supported<E>::value) {
561
+ #if defined(__clang__) || defined(__GNUC__)
562
+ constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
563
+ #elif defined(_MSC_VER)
564
+ constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
565
+ #else
566
+ constexpr auto name = string_view{};
567
+ #endif
568
+ return name;
569
+ } else {
570
+ return string_view{};
571
+ }
572
+ }
573
+
574
+ template <typename E, E V>
575
+ constexpr auto enum_name() noexcept {
576
+ [[maybe_unused]] constexpr auto custom_name = customize::enum_name<E>(V);
577
+
578
+ if constexpr (custom_name.empty()) {
579
+ constexpr auto name = n<E, V>();
580
+ return cstring<name.size()>{name};
581
+ } else {
582
+ return cstring<custom_name.size()>{custom_name};
583
+ }
584
+ }
585
+
586
+ template <typename E, E V>
587
+ inline constexpr auto enum_name_v = enum_name<E, V>();
588
+
589
+ template <typename E, auto V>
590
+ constexpr bool is_valid() noexcept {
591
+ #if defined(__clang__) && __clang_major__ >= 16
592
+ // https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
593
+ constexpr E v = __builtin_bit_cast(E, V);
594
+ #else
595
+ constexpr E v = static_cast<E>(V);
596
+ #endif
597
+ [[maybe_unused]] constexpr auto custom_name = customize::enum_name<E>(v);
598
+ if constexpr (custom_name.empty()) {
599
+ return n<E, v>().size() != 0;
600
+ } else {
601
+ return custom_name.size() != 0;
602
+ }
603
+ }
604
+
605
+ template <typename E, int O, bool IsFlags, typename U = std::underlying_type_t<E>>
606
+ constexpr U ualue(std::size_t i) noexcept {
607
+ if constexpr (std::is_same_v<U, bool>) { // bool special case
608
+ static_assert(O == 0, "nameof::detail::ualue requires valid offset.");
609
+
610
+ return static_cast<U>(i);
611
+ } else if constexpr (IsFlags) {
612
+ return static_cast<U>(U{1} << static_cast<U>(static_cast<int>(i) + O));
613
+ } else {
614
+ return static_cast<U>(static_cast<int>(i) + O);
615
+ }
616
+ }
617
+
618
+ template <typename E, int O, bool IsFlags, typename U = std::underlying_type_t<E>>
619
+ constexpr E value(std::size_t i) noexcept {
620
+ return static_cast<E>(ualue<E, O, IsFlags>(i));
621
+ }
622
+
623
+ template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
624
+ constexpr int reflected_min() noexcept {
625
+ if constexpr (IsFlags) {
626
+ return 0;
627
+ } else {
628
+ constexpr auto lhs = customize::enum_range<E>::min;
629
+ constexpr auto rhs = (std::numeric_limits<U>::min)();
630
+
631
+ if constexpr (cmp_less(rhs, lhs)) {
632
+ return lhs;
633
+ } else {
634
+ return rhs;
635
+ }
636
+ }
637
+ }
638
+
639
+ template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
640
+ constexpr int reflected_max() noexcept {
641
+ if constexpr (IsFlags) {
642
+ return std::numeric_limits<U>::digits - 1;
643
+ } else {
644
+ constexpr auto lhs = customize::enum_range<E>::max;
645
+ constexpr auto rhs = (std::numeric_limits<U>::max)();
646
+
647
+ if constexpr (cmp_less(lhs, rhs)) {
648
+ return lhs;
649
+ } else {
650
+ return rhs;
651
+ }
652
+ }
653
+ }
654
+
655
+ #define NAMEOF_FOR_EACH_256(T) \
656
+ T( 0)T( 1)T( 2)T( 3)T( 4)T( 5)T( 6)T( 7)T( 8)T( 9)T( 10)T( 11)T( 12)T( 13)T( 14)T( 15)T( 16)T( 17)T( 18)T( 19)T( 20)T( 21)T( 22)T( 23)T( 24)T( 25)T( 26)T( 27)T( 28)T( 29)T( 30)T( 31) \
657
+ T( 32)T( 33)T( 34)T( 35)T( 36)T( 37)T( 38)T( 39)T( 40)T( 41)T( 42)T( 43)T( 44)T( 45)T( 46)T( 47)T( 48)T( 49)T( 50)T( 51)T( 52)T( 53)T( 54)T( 55)T( 56)T( 57)T( 58)T( 59)T( 60)T( 61)T( 62)T( 63) \
658
+ T( 64)T( 65)T( 66)T( 67)T( 68)T( 69)T( 70)T( 71)T( 72)T( 73)T( 74)T( 75)T( 76)T( 77)T( 78)T( 79)T( 80)T( 81)T( 82)T( 83)T( 84)T( 85)T( 86)T( 87)T( 88)T( 89)T( 90)T( 91)T( 92)T( 93)T( 94)T( 95) \
659
+ T( 96)T( 97)T( 98)T( 99)T(100)T(101)T(102)T(103)T(104)T(105)T(106)T(107)T(108)T(109)T(110)T(111)T(112)T(113)T(114)T(115)T(116)T(117)T(118)T(119)T(120)T(121)T(122)T(123)T(124)T(125)T(126)T(127) \
660
+ T(128)T(129)T(130)T(131)T(132)T(133)T(134)T(135)T(136)T(137)T(138)T(139)T(140)T(141)T(142)T(143)T(144)T(145)T(146)T(147)T(148)T(149)T(150)T(151)T(152)T(153)T(154)T(155)T(156)T(157)T(158)T(159) \
661
+ T(160)T(161)T(162)T(163)T(164)T(165)T(166)T(167)T(168)T(169)T(170)T(171)T(172)T(173)T(174)T(175)T(176)T(177)T(178)T(179)T(180)T(181)T(182)T(183)T(184)T(185)T(186)T(187)T(188)T(189)T(190)T(191) \
662
+ T(192)T(193)T(194)T(195)T(196)T(197)T(198)T(199)T(200)T(201)T(202)T(203)T(204)T(205)T(206)T(207)T(208)T(209)T(210)T(211)T(212)T(213)T(214)T(215)T(216)T(217)T(218)T(219)T(220)T(221)T(222)T(223) \
663
+ T(224)T(225)T(226)T(227)T(228)T(229)T(230)T(231)T(232)T(233)T(234)T(235)T(236)T(237)T(238)T(239)T(240)T(241)T(242)T(243)T(244)T(245)T(246)T(247)T(248)T(249)T(250)T(251)T(252)T(253)T(254)T(255)
664
+
665
+ template <typename E, bool IsFlags, std::size_t Size, int Min, std::size_t I>
666
+ constexpr void valid_count(bool* valid, std::size_t& count) noexcept {
667
+ #define NAMEOF_ENUM_V(O) \
668
+ if constexpr ((I + O) < Size) { \
669
+ if constexpr (is_valid<E, ualue<E, Min, IsFlags>(I + O)>()) { \
670
+ valid[I + O] = true; \
671
+ ++count; \
672
+ } \
673
+ }
674
+
675
+ NAMEOF_FOR_EACH_256(NAMEOF_ENUM_V)
676
+
677
+ if constexpr ((I + 256) < Size) {
678
+ valid_count<E, IsFlags, Size, Min, I + 256>(valid, count);
679
+ }
680
+ #undef NAMEOF_ENUM_V
681
+ }
682
+
683
+ template <std::size_t N>
684
+ struct valid_count_t {
685
+ std::size_t count = 0;
686
+ bool valid[N] = {};
687
+ };
688
+
689
+ template <typename E, bool IsFlags, std::size_t Size, int Min>
690
+ constexpr auto valid_count() noexcept {
691
+ valid_count_t<Size> vc;
692
+ valid_count<E, IsFlags, Size, Min, 0>(vc.valid, vc.count);
693
+ return vc;
694
+ }
695
+
696
+ template <typename E, bool IsFlags, std::size_t Size, int Min>
697
+ constexpr auto values() noexcept {
698
+ constexpr auto vc = valid_count<E, IsFlags, Size, Min>();
699
+
700
+ if constexpr (vc.count > 0) {
701
+ #if defined(NAMEOF_ARRAY_CONSTEXPR)
702
+ std::array<E, vc.count> values = {};
703
+ #else
704
+ E values[vc.count] = {};
705
+ #endif
706
+ for (std::size_t i = 0, v = 0; v < vc.count; ++i) {
707
+ if (vc.valid[i]) {
708
+ values[v++] = value<E, Min, IsFlags>(i);
709
+ }
710
+ }
711
+ #if defined(NAMEOF_ARRAY_CONSTEXPR)
712
+ return values;
713
+ #else
714
+ return to_array(values, std::make_index_sequence<vc.count>{});
715
+ #endif
716
+ } else {
717
+ return std::array<E, 0>{};
718
+ }
719
+ }
720
+
721
+ template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
722
+ constexpr auto values() noexcept {
723
+ constexpr auto min = reflected_min<E, IsFlags>();
724
+ constexpr auto max = reflected_max<E, IsFlags>();
725
+ constexpr auto range_size = max - min + 1;
726
+ static_assert(range_size > 0, "nameof::enum_range requires valid size.");
727
+ static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size.");
728
+
729
+ return values<E, IsFlags, range_size, min>();
730
+ }
731
+
732
+ template <typename E, bool IsFlags = false>
733
+ inline constexpr auto values_v = values<E, IsFlags>();
734
+
735
+ template <typename E, bool IsFlags = false>
736
+ inline constexpr auto count_v = values_v<E, IsFlags>.size();
737
+
738
+ template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
739
+ inline constexpr auto min_v = (count_v<E, IsFlags> > 0) ? static_cast<U>(values_v<E, IsFlags>.front()) : U{0};
740
+
741
+ template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
742
+ inline constexpr auto max_v = (count_v<E, IsFlags> > 0) ? static_cast<U>(values_v<E, IsFlags>.back()) : U{0};
743
+
744
+ template <typename E, bool IsFlags, std::size_t... I>
745
+ constexpr auto names(std::index_sequence<I...>) noexcept {
746
+ constexpr auto names = std::array<string_view, sizeof...(I)>{{enum_name_v<E, values_v<E, IsFlags>[I]>...}};
747
+ return names;
748
+ }
749
+
750
+ template <typename E, bool IsFlags = false>
751
+ inline constexpr auto names_v = names<E, IsFlags>(std::make_index_sequence<count_v<E, IsFlags>>{});
752
+
753
+ template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
754
+ constexpr bool is_sparse() noexcept {
755
+ if constexpr (count_v<E, IsFlags> == 0) {
756
+ return false;
757
+ } else if constexpr (std::is_same_v<U, bool>) { // bool special case
758
+ return false;
759
+ } else {
760
+ constexpr auto max = IsFlags ? log2(max_v<E, IsFlags>) : max_v<E, IsFlags>;
761
+ constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
762
+ constexpr auto range_size = max - min + 1;
763
+
764
+ return range_size != count_v<E, IsFlags>;
765
+ }
766
+ }
767
+
768
+ template <typename E, bool IsFlags = false>
769
+ inline constexpr bool is_sparse_v = is_sparse<E, IsFlags>();
770
+
771
+ template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
772
+ constexpr E enum_value(std::size_t i) noexcept {
773
+ if constexpr (is_sparse_v<E, IsFlags>) {
774
+ return values_v<E, IsFlags>[i];
775
+ } else {
776
+ constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
777
+
778
+ return value<E, min, IsFlags>(i);
779
+ }
780
+ }
781
+
782
+ template <typename... T>
783
+ struct nameof_type_supported
784
+ #if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
785
+ : std::true_type {};
786
+ #else
787
+ : std::false_type {};
788
+ #endif
789
+
790
+ template <typename... T>
791
+ struct nameof_type_rtti_supported
792
+ #if defined(NAMEOF_TYPE_RTTI_SUPPORTED) && NAMEOF_TYPE_RTTI_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
793
+ : std::true_type {};
794
+ #else
795
+ : std::false_type {};
796
+ #endif
797
+
798
+ template <typename... T>
799
+ struct nameof_member_supported
800
+ #if defined(NAMEOF_MEMBER_SUPPORTED) && NAMEOF_MEMBER_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
801
+ : std::true_type {};
802
+ #else
803
+ : std::false_type {};
804
+ #endif
805
+
806
+ template <typename... T>
807
+ struct nameof_pointer_supported
808
+ #if defined(NAMEOF_POINTER_SUPPORTED) && NAMEOF_POINTER_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
809
+ : std::true_type {};
810
+ #else
811
+ : std::false_type {};
812
+ #endif
813
+
814
+ #if defined(_MSC_VER) && !defined(__clang__)
815
+ template <typename T>
816
+ struct identity {
817
+ using type = T;
818
+ };
819
+ #else
820
+ template <typename T>
821
+ using identity = T;
822
+ #endif
823
+
824
+ template <typename T>
825
+ using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
826
+
827
+ template <typename T, typename R>
828
+ using enable_if_has_short_name_t = std::enable_if_t<!std::is_array_v<T> && !std::is_pointer_v<T>, R>;
829
+
830
+ template <typename... T>
831
+ constexpr auto n() noexcept {
832
+ #if defined(_MSC_VER) && !defined(__clang__)
833
+ [[maybe_unused]] constexpr auto custom_name = customize::type_name<typename T::type...>();
834
+ #else
835
+ [[maybe_unused]] constexpr auto custom_name = customize::type_name<T...>();
836
+ #endif
837
+
838
+ if constexpr (custom_name.empty() && nameof_type_supported<T...>::value) {
839
+ #if defined(__clang__)
840
+ constexpr string_view name{__PRETTY_FUNCTION__ + 31, sizeof(__PRETTY_FUNCTION__) - 34};
841
+ #elif defined(__GNUC__)
842
+ constexpr string_view name{__PRETTY_FUNCTION__ + 46, sizeof(__PRETTY_FUNCTION__) - 49};
843
+ #elif defined(_MSC_VER)
844
+ constexpr string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)};
845
+ #else
846
+ constexpr auto name = string_view{};
847
+ #endif
848
+ return cstring<name.size()>{name};
849
+ } else {
850
+ return cstring<custom_name.size()>{custom_name};
851
+ }
852
+ }
853
+
854
+ template <typename... T>
855
+ inline constexpr auto type_name_v = n<T...>();
856
+
857
+ #if __has_include(<cxxabi.h>)
858
+ template <typename T>
859
+ string nameof_type_rtti(const char* tn) {
860
+ static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
861
+ const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr);
862
+ const auto name = string{dmg};
863
+ free(dmg);
864
+ assert(!name.empty() && "Type does not have a name.");
865
+
866
+ return name;
867
+ }
868
+
869
+ template <typename T>
870
+ string nameof_full_type_rtti(const char* tn) {
871
+ static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
872
+ const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr);
873
+ auto name = string{dmg};
874
+ free(dmg);
875
+ assert(!name.empty() && "Type does not have a name.");
876
+ if constexpr (std::is_const_v<std::remove_reference_t<T>>) {
877
+ name = string{"const "}.append(name);
878
+ }
879
+ if constexpr (std::is_volatile_v<std::remove_reference_t<T>>) {
880
+ name = string{"volatile "}.append(name);
881
+ }
882
+ if constexpr (std::is_lvalue_reference_v<T>) {
883
+ name.append(1, '&');
884
+ }
885
+ if constexpr (std::is_rvalue_reference_v<T>) {
886
+ name.append("&&");
887
+ }
888
+
889
+ return name;
890
+ }
891
+
892
+ template <typename T, enable_if_has_short_name_t<T, int> = 0>
893
+ string nameof_short_type_rtti(const char* tn) {
894
+ static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
895
+ const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr);
896
+ const auto pname = pretty_name(dmg);
897
+ const auto name = string{pname.data(), pname.size()};
898
+ free(dmg);
899
+ assert(!name.empty() && "Type does not have a short name.");
900
+
901
+ return name;
902
+ }
903
+ #else
904
+ template <typename T>
905
+ string nameof_type_rtti(const char* tn) {
906
+ static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
907
+ const auto name = string_view{tn};
908
+ assert(!name.empty() && "Type does not have a name.");
909
+ return {name.data(), name.size()};
910
+ }
911
+
912
+ template <typename T>
913
+ string nameof_full_type_rtti(const char* tn) {
914
+ static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
915
+ auto name = string{tn};
916
+ assert(!name.empty() && "Type does not have a name.");
917
+ if constexpr (std::is_const_v<std::remove_reference_t<T>>) {
918
+ name = string{"const "}.append(name);
919
+ }
920
+ if constexpr (std::is_volatile_v<std::remove_reference_t<T>>) {
921
+ name = string{"volatile "}.append(name);
922
+ }
923
+ if constexpr (std::is_lvalue_reference_v<T>) {
924
+ name.append(1, '&');
925
+ }
926
+ if constexpr (std::is_rvalue_reference_v<T>) {
927
+ name.append("&&");
928
+ }
929
+ return name;
930
+ }
931
+
932
+ template <typename T, enable_if_has_short_name_t<T, int> = 0>
933
+ string nameof_short_type_rtti(const char* tn) {
934
+ static_assert(nameof_type_rtti_supported<T>::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
935
+ const auto name = pretty_name(tn);
936
+ assert(!name.empty() && "Type does not have a short name.");
937
+ return {name.data(), name.size()};
938
+ }
939
+ #endif
940
+
941
+ template <auto V, auto U = V>
942
+ constexpr auto n() noexcept {
943
+ [[maybe_unused]] constexpr auto custom_name = customize::member_name<V>();
944
+
945
+ if constexpr (custom_name.empty() && nameof_member_supported<decltype(V)>::value) {
946
+ #if defined(__clang__) || defined(__GNUC__)
947
+ constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
948
+ #elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
949
+ constexpr auto name = pretty_name({__FUNCSIG__,
950
+ sizeof(__FUNCSIG__) - 18 + std::is_member_function_pointer_v<decltype(U)>});
951
+ #else
952
+ constexpr auto name = string_view{};
953
+ #endif
954
+ return cstring<name.size()>{name};
955
+ } else {
956
+ return cstring<custom_name.size()>{custom_name};
957
+ }
958
+ }
959
+
960
+ #if defined(__clang__) || defined(__GNUC__)
961
+ template <auto V>
962
+ inline constexpr auto member_name_v = n<V>();
963
+ #elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
964
+ template <typename From, typename Type>
965
+ From get_base_type(Type From::*);
966
+
967
+ template <typename T>
968
+ extern T nonexist_object;
969
+
970
+ template<class T>
971
+ struct Store {
972
+ T v;
973
+ };
974
+
975
+ template<class T>
976
+ Store(T) -> Store<T>;
977
+
978
+ template <auto V>
979
+ consteval auto get_member_name() noexcept {
980
+ if constexpr (std::is_member_function_pointer_v<decltype(V)>) {
981
+ return n<V>();
982
+ } else {
983
+ constexpr bool is_defined = sizeof(decltype(get_base_type(V))) != 0;
984
+ static_assert(is_defined, "nameof::nameof_member member name can use only if the struct is already fully defined. Please use NAMEOF macro, or separate definition and declaration.");
985
+ if constexpr (is_defined) {
986
+ return n<V, Store{&(nonexist_object<decltype(get_base_type(V))>.*V)}>();
987
+ } else {
988
+ return "";
989
+ }
990
+ }
991
+ }
992
+
993
+ template <auto V>
994
+ inline constexpr auto member_name_v = get_member_name<V>();
995
+ #else
996
+ template <auto V>
997
+ inline constexpr auto member_name_v = cstring<0>{};
998
+ #endif
999
+
1000
+ template <auto U, auto V>
1001
+ struct is_same : std::false_type {};
1002
+
1003
+ template <auto U>
1004
+ struct is_same<U, U> : std::true_type {};
1005
+
1006
+ template <auto P>
1007
+ constexpr bool is_nullptr_v = is_same<P, static_cast<std::remove_reference_t<decltype(P)>>(nullptr)>::value;
1008
+
1009
+ template <auto V>
1010
+ constexpr auto p() noexcept {
1011
+ [[maybe_unused]] constexpr auto custom_name = customize::pointer_name<V>().empty() && is_nullptr_v<V> ? "nullptr" : customize::pointer_name<V>();
1012
+
1013
+ if constexpr (custom_name.empty() && nameof_pointer_supported<decltype(V)>::value) {
1014
+ #if defined(__clang__)
1015
+ constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
1016
+ #elif defined(__GNUC__)
1017
+ constexpr bool has_parenthesis = __PRETTY_FUNCTION__[sizeof(__PRETTY_FUNCTION__) - 3] == ')';
1018
+ constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2 - has_parenthesis});
1019
+ #elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
1020
+ constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
1021
+ #else
1022
+ constexpr auto name = string_view{};
1023
+ #endif
1024
+ return cstring<name.size()>{name};
1025
+ } else {
1026
+ return cstring<custom_name.size()>{custom_name};
1027
+ }
1028
+ }
1029
+
1030
+ template <auto V>
1031
+ inline constexpr auto pointer_name_v = p<V>();
1032
+
1033
+ } // namespace nameof::detail
1034
+
1035
+ // Checks is nameof_type supported compiler.
1036
+ inline constexpr bool is_nameof_type_supported = detail::nameof_type_supported<void>::value;
1037
+
1038
+ // Checks is nameof_type_rtti supported compiler.
1039
+ inline constexpr bool is_nameof_type_rtti_supported = detail::nameof_type_rtti_supported<void>::value;
1040
+
1041
+ // Checks is nameof_member supported compiler.
1042
+ inline constexpr bool is_nameof_member_supported = detail::nameof_member_supported<void>::value;
1043
+
1044
+ // Checks is nameof_pointer supported compiler.
1045
+ inline constexpr bool is_nameof_pointer_supported = detail::nameof_pointer_supported<void>::value;
1046
+
1047
+ // Checks is nameof_enum supported compiler.
1048
+ inline constexpr bool is_nameof_enum_supported = detail::nameof_enum_supported<void>::value;
1049
+
1050
+ // Obtains name of enum variable.
1051
+ template <typename E>
1052
+ [[nodiscard]] constexpr auto nameof_enum(E value) noexcept -> detail::enable_if_enum_t<E, string_view> {
1053
+ using D = std::decay_t<E>;
1054
+ using U = std::underlying_type_t<D>;
1055
+ static_assert(detail::nameof_enum_supported<D>::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1056
+ static_assert(detail::count_v<D> > 0, "nameof::nameof_enum requires enum implementation and valid max and min.");
1057
+
1058
+ if constexpr (detail::is_sparse_v<D>) {
1059
+ for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
1060
+ if (detail::enum_value<D>(i) == value) {
1061
+ return detail::names_v<D>[i];
1062
+ }
1063
+ }
1064
+ } else {
1065
+ const auto v = static_cast<U>(value);
1066
+ if (v >= detail::min_v<D> && v <= detail::max_v<D>) {
1067
+ return detail::names_v<D>[static_cast<std::size_t>(v - detail::min_v<D>)];
1068
+ }
1069
+ }
1070
+ return {}; // Value out of range.
1071
+ }
1072
+
1073
+ // Obtains name of enum variable or default value if enum variable out of range.
1074
+ template <typename E>
1075
+ [[nodiscard]] auto nameof_enum_or(E value, string_view default_value) -> detail::enable_if_enum_t<E, string> {
1076
+ using D = std::decay_t<E>;
1077
+ static_assert(detail::nameof_enum_supported<D>::value, "nameof::nameof_enum_or unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1078
+ static_assert(detail::count_v<D> > 0, "nameof::nameof_enum_or requires enum implementation and valid max and min.");
1079
+
1080
+ if (auto v = nameof_enum<D>(value); !v.empty()) {
1081
+ return string{v.data(), v.size()};
1082
+ }
1083
+ return string{default_value.data(), default_value.size()};
1084
+ }
1085
+
1086
+ // Obtains name of enum-flags variable.
1087
+ template <typename E>
1088
+ [[nodiscard]] auto nameof_enum_flag(E value, char sep = '|') -> detail::enable_if_enum_t<E, string> {
1089
+ using D = std::decay_t<E>;
1090
+ using U = std::underlying_type_t<D>;
1091
+ static_assert(detail::nameof_enum_supported<D>::value, "nameof::nameof_enum_flag unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1092
+ static_assert(detail::count_v<D, true> > 0, "nameof::nameof_enum_flag requires enum-flags implementation.");
1093
+
1094
+ string name;
1095
+ auto check_value = U{0};
1096
+ for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
1097
+ if (const auto v = static_cast<U>(detail::enum_value<D, true>(i)); (static_cast<U>(value) & v) != 0) {
1098
+ if (const auto n = detail::names_v<D, true>[i]; !n.empty()) {
1099
+ check_value |= v;
1100
+ if (!name.empty()) {
1101
+ name.append(1, sep);
1102
+ }
1103
+ name.append(n.data(), n.size());
1104
+ } else {
1105
+ return {}; // Value out of range.
1106
+ }
1107
+ }
1108
+ }
1109
+
1110
+ if (check_value != 0 && check_value == static_cast<U>(value)) {
1111
+ return name;
1112
+ }
1113
+ return {}; // Invalid value or out of range.
1114
+ }
1115
+
1116
+ // Obtains name of static storage enum variable.
1117
+ // This version is much lighter on the compile times and is not restricted to the enum_range limitation.
1118
+ template <auto V, detail::enable_if_enum_t<decltype(V), int> = 0>
1119
+ [[nodiscard]] constexpr auto nameof_enum() noexcept {
1120
+ using D = std::decay_t<decltype(V)>;
1121
+ static_assert(std::is_enum_v<D>, "nameof::nameof_enum requires member enum type.");
1122
+ static_assert(detail::nameof_enum_supported<D>::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1123
+ return detail::enum_name_v<D, V>;
1124
+ }
1125
+
1126
+ // Obtains name of type, reference and cv-qualifiers are ignored.
1127
+ template <typename T>
1128
+ [[nodiscard]] constexpr string_view nameof_type() noexcept {
1129
+ using U = detail::identity<detail::remove_cvref_t<T>>;
1130
+ static_assert(detail::nameof_type_supported<U>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1131
+ return detail::type_name_v<U>;
1132
+ }
1133
+
1134
+ // Obtains full name of type, with reference and cv-qualifiers.
1135
+ template <typename T>
1136
+ [[nodiscard]] constexpr string_view nameof_full_type() noexcept {
1137
+ using U = detail::identity<T>;
1138
+ static_assert(detail::nameof_type_supported<U>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1139
+ return detail::type_name_v<U>;
1140
+ }
1141
+
1142
+ // Obtains short name of type.
1143
+ template <typename T, detail::enable_if_has_short_name_t<T, int> = 0>
1144
+ [[nodiscard]] constexpr auto nameof_short_type() noexcept {
1145
+ using U = detail::identity<detail::remove_cvref_t<T>>;
1146
+ static_assert(detail::nameof_type_supported<U>::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1147
+ static_assert(!std::is_array_v<T> && !std::is_pointer_v<T>, "nameof::nameof_member requires non array and non pointer type.");
1148
+ constexpr string_view name = detail::pretty_name(detail::type_name_v<U>);
1149
+ static_assert(!name.empty(), "Type does not have a short name.");
1150
+ return cstring<name.size()>{name};
1151
+ }
1152
+
1153
+ // Obtains name of member.
1154
+ template <auto V, std::enable_if_t<std::is_member_pointer_v<decltype(V)>, int> = 0>
1155
+ [[nodiscard]] constexpr auto nameof_member() noexcept {
1156
+ using U = decltype(V);
1157
+ static_assert(std::is_member_pointer_v<U>, "nameof::nameof_member requires member pointer type.");
1158
+ static_assert(detail::nameof_member_supported<U>::value, "nameof::nameof_member unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1159
+ return detail::member_name_v<V>;
1160
+ }
1161
+
1162
+ // Obtains name of a function, a global or class static variable.
1163
+ template <auto V, std::enable_if_t<std::is_pointer_v<decltype(V)>, int> = 0>
1164
+ [[nodiscard]] constexpr auto nameof_pointer() noexcept {
1165
+ using U = decltype(V);
1166
+ static_assert(std::is_pointer_v<U>, "nameof::nameof_pointer requires pointer type.");
1167
+ static_assert(detail::nameof_pointer_supported<U>::value, "nameof::nameof_pointer unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility).");
1168
+ return detail::pointer_name_v<V>;
1169
+ }
1170
+
1171
+ } // namespace nameof
1172
+
1173
+ // Obtains name of variable, function, macro.
1174
+ #define NAMEOF(...) []() constexpr noexcept { \
1175
+ ::std::void_t<decltype(__VA_ARGS__)>(); \
1176
+ constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__); \
1177
+ static_assert(!_name.empty(), "Expression does not have a name."); \
1178
+ constexpr auto _size = _name.size(); \
1179
+ constexpr auto _nameof = ::nameof::cstring<_size>{_name}; \
1180
+ return _nameof; }()
1181
+
1182
+ // Obtains full name of variable, function, macro.
1183
+ #define NAMEOF_FULL(...) []() constexpr noexcept { \
1184
+ ::std::void_t<decltype(__VA_ARGS__)>(); \
1185
+ constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__, false); \
1186
+ static_assert(!_name.empty(), "Expression does not have a name."); \
1187
+ constexpr auto _size = _name.size(); \
1188
+ constexpr auto _nameof_full = ::nameof::cstring<_size>{_name}; \
1189
+ return _nameof_full; }()
1190
+
1191
+ // Obtains raw name of variable, function, macro.
1192
+ #define NAMEOF_RAW(...) []() constexpr noexcept { \
1193
+ ::std::void_t<decltype(__VA_ARGS__)>(); \
1194
+ constexpr auto _name = ::nameof::string_view{#__VA_ARGS__}; \
1195
+ static_assert(!_name.empty(), "Expression does not have a name."); \
1196
+ constexpr auto _size = _name.size(); \
1197
+ constexpr auto _nameof_raw = ::nameof::cstring<_size>{_name}; \
1198
+ return _nameof_raw; }()
1199
+
1200
+ // Obtains name of enum variable.
1201
+ #define NAMEOF_ENUM(...) ::nameof::nameof_enum<::std::decay_t<decltype(__VA_ARGS__)>>(__VA_ARGS__)
1202
+
1203
+ // Obtains name of enum variable or default value if enum variable out of range.
1204
+ #define NAMEOF_ENUM_OR(...) ::nameof::nameof_enum_or(__VA_ARGS__)
1205
+
1206
+ // Obtains name of static storage enum variable.
1207
+ // This version is much lighter on the compile times and is not restricted to the enum_range limitation.
1208
+ #define NAMEOF_ENUM_CONST(...) ::nameof::nameof_enum<__VA_ARGS__>()
1209
+
1210
+ // Obtains name of enum-flags variable.
1211
+ #define NAMEOF_ENUM_FLAG(...) ::nameof::nameof_enum_flag<::std::decay_t<decltype(__VA_ARGS__)>>(__VA_ARGS__)
1212
+
1213
+ // Obtains type name, reference and cv-qualifiers are ignored.
1214
+ #define NAMEOF_TYPE(...) ::nameof::nameof_type<__VA_ARGS__>()
1215
+
1216
+ // Obtains full type name, with reference and cv-qualifiers.
1217
+ #define NAMEOF_FULL_TYPE(...) ::nameof::nameof_full_type<__VA_ARGS__>()
1218
+
1219
+ // Obtains short type name.
1220
+ #define NAMEOF_SHORT_TYPE(...) ::nameof::nameof_short_type<__VA_ARGS__>()
1221
+
1222
+ // Obtains type name of expression, reference and cv-qualifiers are ignored.
1223
+ #define NAMEOF_TYPE_EXPR(...) ::nameof::nameof_type<decltype(__VA_ARGS__)>()
1224
+
1225
+ // Obtains full type name of expression, with reference and cv-qualifiers.
1226
+ #define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type<decltype(__VA_ARGS__)>()
1227
+
1228
+ // Obtains short type name of expression.
1229
+ #define NAMEOF_SHORT_TYPE_EXPR(...) ::nameof::nameof_short_type<decltype(__VA_ARGS__)>()
1230
+
1231
+ // Obtains type name, with reference and cv-qualifiers, using RTTI.
1232
+ #define NAMEOF_TYPE_RTTI(...) ::nameof::detail::nameof_type_rtti<::std::void_t<decltype(__VA_ARGS__)>>(typeid(__VA_ARGS__).name())
1233
+
1234
+ // Obtains full type name, using RTTI.
1235
+ #define NAMEOF_FULL_TYPE_RTTI(...) ::nameof::detail::nameof_full_type_rtti<decltype(__VA_ARGS__)>(typeid(__VA_ARGS__).name())
1236
+
1237
+ // Obtains short type name, using RTTI.
1238
+ #define NAMEOF_SHORT_TYPE_RTTI(...) ::nameof::detail::nameof_short_type_rtti<decltype(__VA_ARGS__)>(typeid(__VA_ARGS__).name())
1239
+
1240
+ // Obtains name of member.
1241
+ #define NAMEOF_MEMBER(...) ::nameof::nameof_member<__VA_ARGS__>()
1242
+
1243
+ // Obtains name of a function, a global or class static variable.
1244
+ #define NAMEOF_POINTER(...) ::nameof::nameof_pointer<__VA_ARGS__>()
1245
+
1246
+ #if defined(__clang__)
1247
+ # pragma clang diagnostic pop
1248
+ #elif defined(__GNUC__)
1249
+ # pragma GCC diagnostic pop
1250
+ #elif defined(_MSC_VER)
1251
+ # pragma warning(pop)
1252
+ #endif
1253
+
1254
+ #endif // NEARGYE_NAMEOF_HPP