-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdebug.hpp
2256 lines (2140 loc) · 79.7 KB
/
debug.hpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
//
// debug.hpp - prints everything!
//
// Source code available at: https://github.com/archibate/debug-hpp
//
// Usage:
// debug(), "my variable is", your_variable;
//
// Results in:
// your_file.cpp:233: my variable is {1, 2, 3}
//
// (suppose your_variable is an std::vector)
//
// **WARNING**:
// debug() only works in `Debug` build! It is automatically disabled in
// `Release` build (we do this by checking whether the NDEBUG macro is
// defined). Yeah, completely no outputs in `Release` build, this is by
// design.
//
// This is a feature for convenience, e.g. you don't have to remove all the
// debug() sentences after debug done, simply switch to `Release` build and
// everything debug is gone, no runtime overhead! And when you need debug
// simply switch back to `Debug` build and everything debug() you written
// before is back in life.
//
// If you insist to use debug() even in `Release` build, please `#define
// DEBUG_LEVEL 1` before including this header file.
//
//
// Assertion check is also supported:
// debug().check(some_variable) > 0;
//
// Will trigger a 'trap' interrupt (__debugbreak for MSVC and __builtin_trap for
// GCC, configurable, see below) for the debugger to catch when `some_variable >
// 0` is false, as well as printing human readable error message:
// your_file.cpp:233: assertion failed: 3 < 0
//
//
// After debugging complete, no need to busy removing all debug() calls! Simply:
// #define NDEBUG
// would supress all debug() prints and assertion checks, completely no runtime
// overhead. For CMake or Visual Studio users, simply switch to `Release` build
// would supress debug() prints. Since they automatically define `NDEBUG` for
// you in `Release`, `RelWithDebInfo` and `MinSizeRel` build types.
//
//
// TL;DR: This is a useful debugging utility the C++ programmers had all dreamed
// of:
//
// 1. print using the neat comma syntax, easy-to-use
// 2. supports printing STL objects including string, vector, tuple, optional,
// variant, unique_ptr, type_info, error_code, and so on.
// 3. just add a member method named `repr`, e.g. `std::string repr() const {
// ... }` to support printing your custom class!
// 4. classes that are not supported to print will be shown in something like
// `[TypeName@0xdeadbeaf]` where 0xdeadbeaf is it's address.
// 5. highly configurable, customize the behaviour by defining the DEBUG_xxx
// macros (see below)
// 6. when debug done, supress all debug messages by simply `#define NDEBUG`,
// the whole library is disabled at compile-time, no runtime overhead
// 7. Thread safe, every line of message is always distinct, no annoying
// interleaving output rushing into console (typical experience when using
// cout)
//
//
// Here is a list of configurable macros, define them **before** including this
// header file to take effect:
//
// `#define DEBUG_LEVEL 0` (default when defined NDEBUG) - disable debug output,
// completely no runtime overhead
// `#define DEBUG_LEVEL 1` (default when !defined NDEBUG) - enable debug output,
// prints everything you asked to print
//
// `#define DEBUG_STEPPING 0` (default) - no step debugging
// `#define DEBUG_STEPPING 1` - enable step debugging, stops whenever debug
// output generated, manually press ENTER to continue
// `#define DEBUG_STEPPING 2` - enable step debugging, like 1, but trigger a
// 'trap' interrupt for debugger to catch instead
//
// `#define DEBUG_SHOW_TIMESTAMP 0` - do not print timestamp
// `#define DEBUG_SHOW_TIMESTAMP 1` - enable printing a timestamp for each line
// of debug output (e.g. "09:57:32")
// `#define DEBUG_SHOW_TIMESTAMP 2` (default) - printing timestamp relative to
// program staring time rather than system real time
//
// `#define DEBUG_SHOW_THREAD_ID 0` (default) - do not print the thread id
// `#define DEBUG_SHOW_THREAD_ID 1` - print the current thread id
//
// `#define DEBUG_SHOW_LOCATION 1` (default) - show source location mark before
// each line of the debug output (e.g. "file.cpp:233")
// `#define DEBUG_SHOW_LOCATION 0` - do not show the location mark
//
// `#define DEBUG_OUTPUT std::cerr <<` (default) - controls where to output the
// debug strings (must be callable as DEBUG_OUTPUT(str))
//
// `#define DEBUG_PANIC_METHOD 0` - throws an runtime error with debug string as
// message when assertion failed
// `#define DEBUG_PANIC_METHOD 1` (default) - print the error message when
// assertion failed, then triggers a 'trap' interrupt, useful for debuggers to
// catch, if no debuggers attached, the program would terminate
// `#define DEBUG_PANIC_METHOD 2` - print the error message when assertion
// failed, and then call std::terminate
// `#define DEBUG_PANIC_METHOD 3` - print the error message when assertion
// failed, do not terminate, do not throw any exception
//
// `#define DEBUG_REPR_NAME repr` (default) - if an object x have a member
// function like `x.repr()` or a global function `repr` supporting `repr(x)`,
// then the value of `x.repr()` or `repr(x)` will be printed instead for this
// object
//
// `#define DEBUG_RANGE_BRACE "{}"` (default) - controls format for range-like
// objects (supporting begin(x) and end(x)) in "{1, 2, 3, ...}"
// `#define DEBUG_RANGE_COMMA ", "` (default) - ditto
//
// `#define DEBUG_TUPLE_BRACE "{}"` (default) - controls format for tuple-like
// objects (supporting std::tuple_size<X>) in "{1, 2, 3}"
// `#define DEBUG_TUPLE_COMMA ", "` (default) - ditto
//
// `#define DEBUG_NAMED_MEMBER_MARK ": "` (default) - used in
// debug::named_member and DEBUG_REPR, e.g. '{name: "name", age: 42}'
//
// `#define DEBUG_MAGIC_ENUM magic_enum::enum_name` - enable printing enum in
// their name rather than value, if you have magic_enum.hpp
//
// `#define DEBUG_UNSIGNED_AS_HEXADECIMAL 0` (default) - print unsigned integers
// as decimal
// `#define DEBUG_UNSIGNED_AS_HEXADECIMAL 1` - print unsigned integers as
// hexadecimal
// `#define DEBUG_UNSIGNED_AS_HEXADECIMAL 2` - print unsigned integers as
// full-width hexadecimal
//
// `#define DEBUG_HEXADECIMAL_UPPERCASE 0` (default) - print hexadecimal values
// in lowercase (a-f)
// `#define DEBUG_HEXADECIMAL_UPPERCASE 1` - print hexadecimal values in
// uppercase (A-F)
//
// `#define DEBUG_SUPRESS_NON_ASCII 0` (default) - consider non-ascii characters
// in std::string as printable (e.g. UTF-8 encoded Chinese characters)
// `#define DEBUG_SUPRESS_NON_ASCII 1` - consider non-ascii characters in
// std::string as not printable (print them in e.g. '\xfe' instead)
//
// `#define DEBUG_SHOW_SOURCE_CODE_LINE 1` - enable debug output with detailed
// source code level information (requires readable source file path)
//
// `#define DEBUG_NULLOPT_STRING "nullopt"` (default) - controls how to print
// optional-like objects (supporting *x and (bool)x) when it is nullopt
//
// `#define DEBUG_NULLPTR_STRING "nullptr"` (default) - controls how to print
// pointer-like objects (supporting static_cast<void const volatile *>(x.get()))
// when it is nullptr
//
// `#define DEBUG_SMART_POINTER_MODE 1` (default) - print smart pointer as raw
// pointer address (e.g. 0xdeadbeaf)
// `#define DEBUG_SMART_POINTER_MODE 2` - print smart pointer as content value
// unless nullptr (e.g. 42 for std::shared_ptr<int>)
// `#define DEBUG_SMART_POINTER_MODE 3` - print smart pointer as both content
// value and raw pointer address (e.g. 42@0xdeadbeaf)
//
// `#define DEBUG_NAMESPACE_BEGIN` (default) - expose debug in the global
// namespace
// `#define DEBUG_NAMESPACE_END` (default) - ditto
//
// `#define DEBUG_NAMESPACE_BEGIN namespace mydebugger {` - expose debug in the
// the namespace `mydebugger`, and use it like `mydebugger::debug()`
// `#define DEBUG_NAMESPACE_END }` - ditto
//
// `#define DEBUG_CLASS_NAME debug` (default) - the default name for the debug
// class is `debug()`, you may define your custom name here
//
#ifndef DEBUG_LEVEL
# ifdef NDEBUG
# define DEBUG_LEVEL 0
# else
# define DEBUG_LEVEL 1
# endif
#endif
#ifndef DEBUG_SHOW_LOCATION
# define DEBUG_SHOW_LOCATION 1
#endif
#ifndef DEBUG_REPR_NAME
# define DEBUG_REPR_NAME repr
#endif
#ifndef DEBUG_FORMATTER_REPR_NAME
# define DEBUG_FORMATTER_REPR_NAME _debug_formatter_repr
#endif
#ifndef DEBUG_NAMESPACE_BEGIN
# define DEBUG_NAMESPACE_BEGIN
#endif
#ifndef DEBUG_NAMESPACE_END
# define DEBUG_NAMESPACE_END
#endif
#ifndef DEBUG_OUTPUT
# define DEBUG_OUTPUT std::cerr <<
#endif
#ifndef DEBUG_ENABLE_FILES_MATCH
# define DEBUG_ENABLE_FILES_MATCH 0
#endif
#ifndef DEBUG_ERROR_CODE_SHOW_NUMBER
# define DEBUG_ERROR_CODE_SHOW_NUMBER 0
#endif
#ifndef DEBUG_PANIC_METHOD
# define DEBUG_PANIC_METHOD 1
#endif
#ifndef DEBUG_STEPPING
# define DEBUG_STEPPING 0
#endif
#ifndef DEBUG_SUPRESS_NON_ASCII
# define DEBUG_SUPRESS_NON_ASCII 0
#endif
#ifndef DEBUG_SEPARATOR_FILE
# define DEBUG_SEPARATOR_FILE ':'
#endif
#ifndef DEBUG_SEPARATOR_LINE
# define DEBUG_SEPARATOR_LINE ':'
#endif
#ifndef DEBUG_SEPARATOR_TAB
# define DEBUG_SEPARATOR_TAB '\t'
#endif
#ifndef DEBUG_SOURCE_LINE_BRACE
# define DEBUG_SOURCE_LINE_BRACE "[]"
#endif
#ifndef DEBUG_TIMESTAMP_BRACE
# define DEBUG_TIMESTAMP_BRACE "[]"
#endif
#ifndef DEBUG_RANGE_BRACE
# define DEBUG_RANGE_BRACE "{}"
#endif
#ifndef DEBUG_RANGE_COMMA
# define DEBUG_RANGE_COMMA ", "
#endif
#ifndef DEBUG_TUPLE_BRACE
# define DEBUG_TUPLE_BRACE "{}"
#endif
#ifndef DEBUG_TUPLE_COMMA
# define DEBUG_TUPLE_COMMA ", "
#endif
#ifndef DEBUG_ENUM_BRACE
# define DEBUG_ENUM_BRACE "()"
#endif
#ifndef DEBUG_NAMED_MEMBER_MARK
# define DEBUG_NAMED_MEMBER_MARK ": "
#endif
#ifndef DEBUG_NULLOPT_STRING
# define DEBUG_NULLOPT_STRING "nullopt"
#endif
#ifndef DEBUG_NULLPTR_STRING
# define DEBUG_NULLPTR_STRING "nullptr"
#endif
#ifndef DEBUG_UNKNOWN_TYPE_BRACE
# define DEBUG_UNKNOWN_TYPE_BRACE "[]"
#endif
#ifndef DEBUG_ERROR_CODE_BRACE
# define DEBUG_ERROR_CODE_BRACE "[]"
#endif
#ifndef DEBUG_ERROR_CODE_INFIX
# define DEBUG_ERROR_CODE_INFIX ""
#endif
#ifndef DEBUG_ERROR_CODE_POSTFIX
# define DEBUG_ERROR_CODE_POSTFIX ": "
#endif
#ifndef DEBUG_ERROR_CODE_NO_ERROR
# define DEBUG_ERROR_CODE_NO_ERROR std::generic_category().message(0)
#endif
#ifndef DEBUG_UNKNOWN_TYPE_AT
# define DEBUG_UNKNOWN_TYPE_AT '@'
#endif
#ifndef DEBUG_SMART_POINTER_MODE
# define DEBUG_SMART_POINTER_MODE 1
#endif
#ifndef DEBUG_SMART_POINTER_AT
# define DEBUG_SMART_POINTER_AT '@'
#endif
#ifndef DEBUG_POINTER_HEXADECIMAL_PREFIX
# define DEBUG_POINTER_HEXADECIMAL_PREFIX "0x"
#endif
#ifndef DEBUG_UNSIGNED_AS_HEXADECIMAL
# define DEBUG_UNSIGNED_AS_HEXADECIMAL 0
#endif
#ifndef DEBUG_HEXADECIMAL_UPPERCASE
# define DEBUG_HEXADECIMAL_UPPERCASE 0
#endif
#ifndef DEBUG_SHOW_SOURCE_CODE_LINE
# define DEBUG_SHOW_SOURCE_CODE_LINE 0
#endif
#ifndef DEBUG_SHOW_TIMESTAMP
# define DEBUG_SHOW_TIMESTAMP 2
#endif
#ifdef DEBUG_CLASS_NAME
# define debug DEBUG_CLASS_NAME
#endif
#if DEBUG_LEVEL
# include <cstdint>
# include <cstdlib>
# include <iomanip>
# include <iostream>
# include <limits>
# include <system_error>
# if DEBUG_SHOW_SOURCE_CODE_LINE
# include <fstream>
# include <unordered_map>
# endif
# ifndef DEBUG_SOURCE_LOCATION
# if __cplusplus >= 202002L
# if defined(__has_include)
# if __has_include(<source_location>)
# include <source_location>
# if __cpp_lib_source_location
# define DEBUG_SOURCE_LOCATION std::source_location
# endif
# endif
# endif
# endif
# endif
# ifndef DEBUG_SOURCE_LOCATION
# if __cplusplus >= 201505L
# if defined(__has_include)
# if __has_include(<experimental/source_location>)
# include <experimental/source_location>
# if __cpp_lib_experimental_source_location
# define DEBUG_SOURCE_LOCATION std::experimental::source_location
# endif
# endif
# endif
# endif
# endif
# include <memory>
# include <sstream>
# include <string>
# include <type_traits>
# include <typeinfo>
# include <utility>
# ifndef DEBUG_CUSTOM_DEMANGLE
# ifndef DEBUG_HAS_CXXABI_H
# if defined(__has_include)
# if defined(__unix__) && __has_include(<cxxabi.h>)
# include <cxxabi.h>
# define DEBUG_HAS_CXXABI_H
# endif
# endif
# else
# include <cxxabi.h>
# endif
# endif
# if DEBUG_SHOW_TIMESTAMP == 1
# if defined(__has_include)
# if defined(__unix__) && __has_include(<sys/time.h>)
# include <sys/time.h>
# define DEBUG_HAS_SYS_TIME_H
# endif
# endif
# ifndef DEBUG_HAS_SYS_TIME_H
# include <chrono>
# endif
# elif DEBUG_SHOW_TIMESTAMP == 2
# include <chrono>
# endif
# if DEBUG_SHOW_THREAD_ID
# include <thread>
# endif
# if defined(__has_include)
# if __has_include(<variant>)
# include <variant>
# endif
# endif
# ifndef DEBUG_STRING_VIEW
# if defined(__has_include)
# if __cplusplus >= 201703L
# if __has_include(<string_view>)
# include <string_view>
# if __cpp_lib_string_view
# define DEBUG_STRING_VIEW std::string_view
# endif
# endif
# endif
# endif
# endif
# ifndef DEBUG_STRING_VIEW
# include <string>
# define DEBUG_STRING_VIEW std::string
# endif
# if __cplusplus >= 202002L
# if defined(__has_cpp_attribute)
# if __has_cpp_attribute(unlikely)
# define DEBUG_UNLIKELY [[unlikely]]
# else
# define DEBUG_UNLIKELY
# endif
# if __has_cpp_attribute(likely)
# define DEBUG_LIKELY [[likely]]
# else
# define DEBUG_LIKELY
# endif
# if __has_cpp_attribute(nodiscard)
# define DEBUG_NODISCARD [[nodiscard]]
# else
# define DEBUG_NODISCARD
# endif
# else
# define DEBUG_LIKELY
# define DEBUG_UNLIKELY
# define DEBUG_NODISCARD
# endif
# else
# define DEBUG_LIKELY
# define DEBUG_UNLIKELY
# define DEBUG_NODISCARD
# endif
# if DEBUG_STEPPING == 1
# include <mutex>
# if defined(__has_include)
# if defined(__unix__)
# if __has_include(<unistd.h>) && __has_include(<termios.h>)
# include <termios.h>
# include <unistd.h>
# define DEBUG_STEPPING_HAS_TERMIOS 1
# endif
# endif
# if defined(_WIN32)
# if __has_include(<conio.h>)
# include <conio.h>
# define DEBUG_STEPPING_HAS_GETCH 1
# endif
# endif
# endif
# endif
DEBUG_NAMESPACE_BEGIN
struct DEBUG_NODISCARD debug {
private:
# ifndef DEBUG_SOURCE_LOCATION
struct debug_source_location {
char const *fn;
int ln;
int col;
char const *fun;
char const *file_name() const noexcept {
return fn;
}
int line() const noexcept {
return ln;
}
int column() const noexcept {
return col;
}
char const *function_name() const noexcept {
return fun;
}
static debug_source_location current() noexcept {
return {"???", 0, 0, "?"};
}
};
# ifndef DEBUG_SOURCE_LOCATION_FAKER
# define DEBUG_SOURCE_LOCATION_FAKER \
{ __FILE__, __LINE__, 0, __func__ }
# endif
# define DEBUG_SOURCE_LOCATION debug::debug_source_location
# endif
template <class T>
static auto debug_deref_avoid(T const &t)
-> std::enable_if_t<!std::is_void_v<decltype(*t)>, decltype(*t)> {
return *t;
}
struct debug_special_void {
char const (&repr() const)[5] {
return "void";
}
};
template <class T>
static auto debug_deref_avoid(T const &t)
-> std::enable_if_t<std::is_void_v<decltype(*t)>, debug_special_void> {
return debug_special_void();
}
static void debug_quotes(std::ostream &oss, DEBUG_STRING_VIEW sv,
char quote) {
oss << quote;
for (char c: sv) {
switch (c) {
case '\n': oss << "\\n"; break;
case '\r': oss << "\\r"; break;
case '\t': oss << "\\t"; break;
case '\\': oss << "\\\\"; break;
case '\0': oss << "\\0"; break;
default:
if ((c >= 0 && c < 0x20) || c == 0x7F
# if DEBUG_SUPRESS_NON_ASCII
|| (static_cast<unsigned char>(c) >= 0x80)
# endif
) {
auto f = oss.flags();
oss << "\\x" << std::hex << std::setfill('0')
<< std::setw(2) << static_cast<int>(c)
# if DEBUG_HEXADECIMAL_UPPERCASE
<< std::uppercase
# endif
;
oss.flags(f);
} else {
if (c == quote) {
oss << '\\';
}
oss << c;
}
break;
}
}
oss << quote;
}
template <class T>
struct debug_is_char_array : std::false_type {};
template <std::size_t N>
struct debug_is_char_array<char[N]> : std::true_type {};
# ifdef DEBUG_CUSTOM_DEMANGLE
static std::string debug_demangle(char const *name) {
return DEBUG_CUSTOM_DEMANGLE(name);
}
# else
static std::string debug_demangle(char const *name) {
# ifdef DEBUG_HAS_CXXABI_H
int status;
char *p = abi::__cxa_demangle(name, 0, 0, &status);
std::string s = p ? p : name;
std::free(p);
# else
std::string s = name;
# endif
return s;
}
# endif
public:
struct debug_formatter {
std::ostream &os;
template <class T>
debug_formatter &operator<<(T const &value) {
debug_format(os, value);
return *this;
}
};
private:
# if __cpp_if_constexpr && __cpp_concepts && \
__cpp_lib_type_trait_variable_templates
public:
template <class T, class U>
requires std::is_same<T, U>::value
static void debug_same_as(U &&) {}
private:
template <class T>
static void debug_format(std::ostream &oss, T const &t) {
using std::begin;
using std::end;
if constexpr (debug_is_char_array<T>::value) {
oss << t;
} else if constexpr ((std::is_convertible<T,
DEBUG_STRING_VIEW>::value ||
std::is_convertible<T, std::string>::value)) {
if constexpr (!std::is_convertible<T, DEBUG_STRING_VIEW>::value) {
std::string s = t;
debug_quotes(oss, s, '"');
} else {
debug_quotes(oss, t, '"');
}
} else if constexpr (std::is_same<T, bool>::value) {
auto f = oss.flags();
oss << std::boolalpha << t;
oss.flags(f);
} else if constexpr (std::is_same<T, char>::value ||
std::is_same<T, signed char>::value) {
debug_quotes(oss, {reinterpret_cast<char const *>(&t), 1}, '\'');
} else if constexpr (
# if __cpp_char8_t
std::is_same<T, char8_t>::value ||
# endif
std::is_same<T, char16_t>::value ||
std::is_same<T, char32_t>::value ||
std::is_same<T, wchar_t>::value) {
auto f = oss.flags();
oss << "'\\"
<< " xu U"[sizeof(T)] << std::hex << std::setfill('0')
<< std::setw(sizeof(T) * 2)
# if DEBUG_HEXADECIMAL_UPPERCASE
<< std::uppercase
# endif
<< static_cast<std::uint32_t>(t) << "'";
oss.flags(f);
# if DEBUG_UNSIGNED_AS_HEXADECIMAL
} else if constexpr (std::is_integral<T>::value) {
if constexpr (std::is_unsigned<T>::value) {
auto f = oss.flags();
oss << "0x" << std::hex << std::setfill('0')
# if DEBUG_UNSIGNED_AS_HEXADECIMAL >= 2
<< std::setw(sizeof(T) * 2)
# endif
# if DEBUG_HEXADECIMAL_UPPERCASE
<< std::uppercase
# endif
;
if constexpr (sizeof(T) == 1) {
oss << static_cast<unsigned int>(t);
} else {
oss << t;
}
oss.flags(f);
} else {
oss << static_cast<std::uint64_t>(
static_cast<typename std::make_unsigned<T>::type>(t));
}
# else
} else if constexpr (std::is_integral<T>::value) {
oss << static_cast<std::uint64_t>(
static_cast<typename std::make_unsigned<T>::type>(t));
# endif
} else if constexpr (std::is_floating_point<T>::value) {
auto f = oss.flags();
oss << std::fixed
<< std::setprecision(std::numeric_limits<T>::digits10) << t;
oss.flags(f);
} else if constexpr (requires(T const &t) {
static_cast<void const volatile *>(t.get());
}) {
auto const *p = t.get();
if (p != nullptr) {
# if DEBUG_SMART_POINTER_MODE == 1
debug_format(oss, *p);
# elif DEBUG_SMART_POINTER_MODE == 2
auto f = oss.flags();
oss << DEBUG_POINTER_HEXADECIMAL_PREFIX << std::hex
<< std::setfill('0')
# if DEBUG_HEXADECIMAL_UPPERCASE
<< std::uppercase
# endif
;
oss << reinterpret_cast<std::uintptr_t>(
static_cast<void const volatile *>(p));
oss.flags(f);
# else
debug_format(oss, *p);
oss << DEBUG_SMART_POINTER_AT;
auto f = oss.flags();
oss << DEBUG_POINTER_HEXADECIMAL_PREFIX << std::hex
<< std::setfill('0')
# if DEBUG_HEXADECIMAL_UPPERCASE
<< std::uppercase
# endif
;
oss << reinterpret_cast<std::uintptr_t>(
static_cast<void const volatile *>(p));
oss.flags(f);
# endif
} else {
oss << DEBUG_NULLPTR_STRING;
}
} else if constexpr (std::is_same<T, std::errc>::value) {
oss << DEBUG_ERROR_CODE_BRACE[0];
if (t != std::errc()) {
oss << std::generic_category().name() << DEBUG_ERROR_CODE_INFIX
# if DEBUG_ERROR_CODE_SHOW_NUMBER
<< ' ' << static_cast<int>(t)
# endif
<< DEBUG_ERROR_CODE_POSTFIX;
oss << std::generic_category().message(static_cast<int>(t));
} else {
oss << DEBUG_ERROR_CODE_NO_ERROR;
}
oss << DEBUG_ERROR_CODE_BRACE[1];
} else if constexpr (std::is_same<T, std::error_code>::value ||
std::is_same<T, std::error_condition>::value) {
oss << DEBUG_ERROR_CODE_BRACE[0];
if (t) {
oss << t.category().name() << DEBUG_ERROR_CODE_INFIX
# if DEBUG_ERROR_CODE_SHOW_NUMBER
<< ' ' << t.value()
# endif
<< DEBUG_ERROR_CODE_POSTFIX << t.message();
} else {
oss << DEBUG_ERROR_CODE_NO_ERROR;
}
oss << DEBUG_ERROR_CODE_BRACE[1];
} else if constexpr (requires(T const &t) {
debug_same_as<typename T::type &>(t.get());
}) {
debug_format(oss, t.get());
} else if constexpr (requires(std::ostream &oss, T const &t) {
oss << t;
}) {
oss << t;
} else if constexpr (std::is_pointer<T>::value ||
std::is_same<T, std::nullptr_t>::value) {
if (t == nullptr) {
auto f = oss.flags();
oss << DEBUG_POINTER_HEXADECIMAL_PREFIX << std::hex
<< std::setfill('0')
# if DEBUG_HEXADECIMAL_UPPERCASE
<< std::uppercase
# endif
;
oss << reinterpret_cast<std::uintptr_t>(
reinterpret_cast<void const volatile *>(t));
oss.flags(f);
} else {
oss << DEBUG_NULLPTR_STRING;
}
} else if constexpr (requires(T const &t) { begin(t) != end(t); }) {
oss << DEBUG_RANGE_BRACE[0];
bool add_comma = false;
for (auto &&i: t) {
if (add_comma) {
oss << DEBUG_RANGE_COMMA;
}
add_comma = true;
debug_format(oss, std::forward<decltype(i)>(i));
}
oss << DEBUG_RANGE_BRACE[1];
} else if constexpr (requires { std::tuple_size<T>::value; }) {
oss << DEBUG_TUPLE_BRACE[0];
bool add_comma = false;
std::apply(
[&](auto &&...args) {
(([&] {
if (add_comma) {
oss << DEBUG_TUPLE_COMMA;
}
add_comma = true;
debug_format(oss, std::forward<decltype(args)>(args));
}()),
...);
},
t);
oss << DEBUG_TUPLE_BRACE[1];
} else if constexpr (std::is_enum<T>::value) {
# ifdef DEBUG_MAGIC_ENUM
oss << DEBUG_MAGIC_ENUM(t);
# else
oss << debug_demangle(typeid(T).name()) << DEBUG_ENUM_BRACE[0];
oss << static_cast<typename std::underlying_type<T>::type>(t);
oss << DEBUG_ENUM_BRACE[1];
# endif
} else if constexpr (std::is_same<T, std::type_info>::value) {
oss << debug_demangle(t.name());
} else if constexpr (requires(T const &t) { t.DEBUG_REPR_NAME(); }) {
debug_format(oss, raw_repr_if_string(t.DEBUG_REPR_NAME()));
} else if constexpr (requires(T const &t) { t.DEBUG_REPR_NAME(oss); }) {
t.DEBUG_REPR_NAME(oss);
} else if constexpr (requires(T const &t) { DEBUG_REPR_NAME(t); }) {
debug_format(oss, raw_repr_if_string(DEBUG_REPR_NAME(t)));
} else if constexpr (requires(debug_formatter const &out, T const &t) {
t.DEBUG_FORMATTER_REPR_NAME(out);
}) {
t.DEBUG_FORMATTER_REPR_NAME(debug_formatter{oss});
} else if constexpr (requires(debug_formatter const &out, T const &t) {
DEBUG_FORMATTER_REPR_NAME(out, t);
}) {
DEBUG_FORMATTER_REPR_NAME(debug_formatter{oss}, t);
# if __cpp_lib_variant
} else if constexpr (requires { std::variant_size<T>::value; }) {
visit([&oss](auto const &t) { debug_format(oss, t); }, t);
# endif
} else if constexpr (requires(T const &t) {
(void)(*t);
(void)(bool)t;
}) {
if ((bool)t) {
debug_format(oss, debug_deref_avoid(t));
} else {
oss << DEBUG_NULLOPT_STRING;
}
} else {
oss << DEBUG_UNKNOWN_TYPE_BRACE[0]
<< debug_demangle(typeid(t).name()) << DEBUG_UNKNOWN_TYPE_AT;
debug_format(oss,
reinterpret_cast<void const *>(std::addressof(t)));
oss << DEBUG_UNKNOWN_TYPE_BRACE[1];
}
}
# else
template <class T>
struct debug_void {
using type = void;
};
template <bool v>
struct debug_bool_constant {
enum {
value = v
};
};
# define DEBUG_COND(n, ...) \
template <class T, class = void> \
struct debug_cond_##n : std::false_type {}; \
template <class T> \
struct debug_cond_##n<T, \
typename debug_void<decltype(__VA_ARGS__)>::type> \
: std::true_type {};
DEBUG_COND(is_ostream_ok, std::declval<std::ostream &>()
<< std::declval<T const &>());
DEBUG_COND(is_range, begin(std::declval<T const &>()) !=
end(std::declval<T const &>()));
DEBUG_COND(is_tuple, std::tuple_size<T>::value);
DEBUG_COND(is_member_repr, std::declval<T const &>().DEBUG_REPR_NAME());
DEBUG_COND(is_member_repr_stream, std::declval<T const &>().DEBUG_REPR_NAME(
std::declval<std::ostream &>()));
DEBUG_COND(is_adl_repr, DEBUG_REPR_NAME(std::declval<T const &>()));
DEBUG_COND(is_adl_repr_stream,
DEBUG_REPR_NAME(std::declval<std::ostream &>(),
std::declval<T const &>()));
DEBUG_COND(is_member_repr_debug,
std::declval<T const &>().DEBUG_FORMATTER_REPR_NAME(
std::declval<debug_formatter const &>()));
DEBUG_COND(is_adl_repr_debug, DEBUG_FORMATTER_REPR_NAME(
std::declval<debug_formatter const &>(),
std::declval<T const &>()));
struct variant_test_lambda {
std::ostream &oss;
template <class T>
void operator()(T const &) const {}
};
DEBUG_COND(is_variant, std::variant_size<T>::value);
DEBUG_COND(is_smart_pointer, static_cast<void const volatile *>(
std::declval<T const &>().get()));
DEBUG_COND(is_optional, (((void)*std::declval<T const &>(), (void)0),
((void)(bool)std::declval<T const &>(), (void)0)));
DEBUG_COND(
reference_wrapper,
(typename std::enable_if<
std::is_same<typename T::type &,
decltype(std::declval<T const &>().get())>::value,
int>::type)0);
# define DEBUG_CON(n, ...) \
template <class T> \
struct debug_cond_##n : debug_bool_constant<__VA_ARGS__> {};
DEBUG_CON(string, std::is_convertible<T, DEBUG_STRING_VIEW>::value ||
std::is_convertible<T, std::string>::value);
DEBUG_CON(bool, std::is_same<T, bool>::value);
DEBUG_CON(char, std::is_same<T, char>::value ||
std::is_same<T, signed char>::value);
DEBUG_CON(error_code, std::is_same<T, std::errc>::value ||
std::is_same<T, std::error_code>::value ||
std::is_same<T, std::error_condition>::value);
# if __cpp_char8_t
DEBUG_CON(unicode_char, std::is_same<T, char8_t>::value ||
std::is_same<T, char16_t>::value ||
std::is_same<T, char32_t>::value ||
std::is_same<T, wchar_t>::value);
# else
DEBUG_CON(unicode_char, std::is_same<T, char16_t>::value ||
std::is_same<T, char32_t>::value ||
std::is_same<T, wchar_t>::value);
# endif
# if DEBUG_UNSIGNED_AS_HEXADECIMAL
DEBUG_CON(integral_unsigned,
std::is_integral<T>::value &&std::is_unsigned<T>::value);
# else
DEBUG_CON(integral_unsigned, false);
# endif
DEBUG_CON(integral, std::is_integral<T>::value);
DEBUG_CON(floating_point, std::is_floating_point<T>::value);
DEBUG_CON(pointer, std::is_pointer<T>::value ||
std::is_same<T, std::nullptr_t>::value);
DEBUG_CON(enum, std::is_enum<T>::value);
template <class T, class = void>
struct debug_format_trait;
template <class T>
static void debug_format(std::ostream &oss, T const &t) {
debug_format_trait<T>()(oss, t);
}
template <class T, class>
struct debug_format_trait {
void operator()(std::ostream &oss, T const &t) const {
oss << DEBUG_UNKNOWN_TYPE_BRACE[0]
<< debug_demangle(typeid(t).name()) << DEBUG_UNKNOWN_TYPE_AT;
debug_format(oss,
reinterpret_cast<void const *>(std::addressof(t)));
oss << DEBUG_UNKNOWN_TYPE_BRACE[1];
}
};
template <class T>
struct debug_format_trait<
T, typename std::enable_if<debug_is_char_array<T>::value>::type> {
void operator()(std::ostream &oss, T const &t) const {
oss << t;
}
};
template <class T>
struct debug_format_trait<
T, typename std::enable_if<
debug_cond_string<T>::value &&
!std::is_convertible<T, DEBUG_STRING_VIEW>::value>::type> {
void operator()(std::ostream &oss, T const &t) const {
std::string s = t;
debug_quotes(oss, s, '"');
}
};
template <class T>
struct debug_format_trait<
T, typename std::enable_if<
!debug_is_char_array<T>::value && debug_cond_string<T>::value &&
std::is_convertible<T, DEBUG_STRING_VIEW>::value>::type> {
void operator()(std::ostream &oss, T const &t) const {
debug_quotes(oss, t, '"');
}
};
template <class T>
struct debug_format_trait<
T, typename std::enable_if<!debug_is_char_array<T>::value &&
!debug_cond_string<T>::value &&
debug_cond_bool<T>::value>::type> {
void operator()(std::ostream &oss, T const &t) const {
auto f = oss.flags();
oss << std::boolalpha << t;
oss.flags(f);
}
};
template <class T>
struct debug_format_trait<
T, typename std::enable_if<
!debug_is_char_array<T>::value && !debug_cond_string<T>::value &&
!debug_cond_bool<T>::value && debug_cond_char<T>::value>::type> {
void operator()(std::ostream &oss, T const &t) const {
debug_quotes(oss, {reinterpret_cast<char const *>(&t), 1}, '\'');
}
};
template <class T>
struct debug_format_trait<
T, typename std::enable_if<
!debug_is_char_array<T>::value && !debug_cond_string<T>::value &&
!debug_cond_bool<T>::value && !debug_cond_char<T>::value &&
debug_cond_unicode_char<T>::value>::type> {
void operator()(std::ostream &oss, T const &t) const {
auto f = oss.flags();
oss << "'\\"
<< " xu U"[sizeof(T)] << std::hex << std::setfill('0')
<< std::setw(sizeof(T) * 2)
# if DEBUG_HEXADECIMAL_UPPERCASE
<< std::uppercase
# endif
<< static_cast<std::uint32_t>(t) << "'";
oss.flags(f);
}
};
template <class T>
struct debug_format_trait<
T, typename std::enable_if<
!debug_is_char_array<T>::value && !debug_cond_string<T>::value &&
!debug_cond_bool<T>::value && !debug_cond_char<T>::value &&
!debug_cond_unicode_char<T>::value &&
debug_cond_integral_unsigned<T>::value>::type> {
void operator()(std::ostream &oss, T const &t) const {
auto f = oss.flags();
oss << "0x" << std::hex << std::setfill('0')
# if DEBUG_UNSIGNED_AS_HEXADECIMAL >= 2
<< std::setw(sizeof(T) * 2)
# endif
# if DEBUG_HEXADECIMAL_UPPERCASE
<< std::uppercase
# endif
;
oss << static_cast<std::uint64_t>(
static_cast<typename std::make_unsigned<T>::type>(t));
oss.flags(f);
}
};
template <class T>
struct debug_format_trait<
T, typename std::enable_if<
!debug_is_char_array<T>::value && !debug_cond_string<T>::value &&
!debug_cond_bool<T>::value && !debug_cond_char<T>::value &&
!debug_cond_unicode_char<T>::value &&
!debug_cond_integral_unsigned<T>::value &&
debug_cond_integral<T>::value>::type> {
void operator()(std::ostream &oss, T const &t) const {
oss << static_cast<std::uint64_t>(
static_cast<typename std::make_unsigned<T>::type>(t));
}
};