GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
debug_formatting.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DEBUG_FORMATTING_HPP
5#define GREM_CORE_DEBUG_FORMATTING_HPP
6
8
23
24#include <cstdio> // stdout, stderr, std::FILE
25#include <iterator> // std::begin, std::end, std::iterator_traits, std::forward_iterator_tag
26#include <type_traits> // std::type_identity_t, std::is_empty_v
27#include <utility> // std::index_sequence, std::make_index_sequence, std::declval, std::remove_cvref_t
28
29namespace grem {
30
31template <typename T>
33 constexpr DebugFormatter(size_t = 0) {}
34
35 [[nodiscard]] constexpr size_t getRecursiveSize(const T&) const noexcept {
36 return 1;
37 }
38
39 void setCompact(bool) {}
40
41 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
42 if (*p == 'c') {
43 ++p;
44 }
45 return p;
46 }
47
48 void formatTo(FormatOutput& output, const T& value) const {
49 constexpr CStringView HEXADECIMAL_DIGITS = "0123456789ABCDEF";
50 output.append("<hex");
51 for (const byte b : asBytes(Span{&value, 1})) {
52 output.append(" ");
53 output.append(StringView{&HEXADECIMAL_DIGITS[(bit_cast<uint8_t>(b) >> 4) & 0x0F], 1});
54 output.append(StringView{&HEXADECIMAL_DIGITS[(bit_cast<uint8_t>(b) & 0x0F)], 1});
55 }
56 output.append(">");
57 }
58};
59
60template <>
61struct DebugFormatter<byte> {
62 constexpr DebugFormatter(size_t = 0) {}
63
64 [[nodiscard]] constexpr size_t getRecursiveSize(const byte&) const noexcept {
65 return 1;
66 }
67
68 void setCompact(bool) {}
69
70 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
71 if (*p == 'c') {
72 ++p;
73 }
74 return p;
75 }
76
77 void formatTo(FormatOutput& output, const byte& value) const {
78 constexpr CStringView HEXADECIMAL_DIGITS = "0123456789ABCDEF";
79 output.append("<hex ");
80 output.append(StringView{&HEXADECIMAL_DIGITS[(bit_cast<uint8_t>(value) >> 4) & 0x0F], 1});
81 output.append(StringView{&HEXADECIMAL_DIGITS[(bit_cast<uint8_t>(value) & 0x0F)], 1});
82 output.append(">");
83 }
84};
85
86template <>
88 char quote = '\"';
89
90 constexpr DebugFormatter(size_t = 0) {}
91
92 [[nodiscard]] constexpr size_t getRecursiveSize(const StringView&) const noexcept {
93 return 1;
94 }
95
96 void setCompact(bool) {}
97
98 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
99 if (*p == 'c') {
100 ++p;
101 }
102 return p;
103 }
104
105 void formatTo(FormatOutput& output, StringView value) const {
106 constexpr CStringView HEXADECIMAL_DIGITS = "0123456789ABCDEF";
107 output.append(StringView{&quote, 1});
108 size_t begin = 0;
109 for (size_t i = 0; i < value.size(); ++i) {
110 const char ch = value[i];
111 if (ch < ' ' || ch > '~' || ch == quote || ch == '\\') {
112 output.append(value.substr(begin, i - begin));
113 switch (ch) {
114 case '\"': output.append("\\\"'"); break;
115 case '\'': output.append("\\\''"); break;
116 case '\\': output.append("\\\\'"); break;
117 case '\b': output.append("\\b'"); break;
118 case '\f': output.append("\\f'"); break;
119 case '\n': output.append("\\n'"); break;
120 case '\r': output.append("\\r'"); break;
121 case '\t': output.append("\\t'"); break;
122 case '\v': output.append("\\v'"); break;
123 case '\0': output.append("\\0'"); break;
124 default:
125 output.append("\\x");
126 output.append(StringView{&HEXADECIMAL_DIGITS[(bit_cast<uint8_t>(ch) >> 4) & 0x0F], 1});
127 output.append(StringView{&HEXADECIMAL_DIGITS[(bit_cast<uint8_t>(ch) & 0x0F)], 1});
128 break;
129 }
130 begin = i + 1;
131 }
132 }
133 if (begin < value.size()) {
134 output.append(value.substr(begin));
135 }
136 output.append(StringView{&quote, 1});
137 }
138};
139
140template <>
148
149template <>
150struct DebugFormatter<grem::String> : DebugFormatter<StringView> {
152
153 GREM_ALWAYS_INLINE void formatTo(FormatOutput& output, const String& value) const {
155 }
156};
157
158template <grem::size_t Length>
166
167template <>
168struct DebugFormatter<char> : DebugFormatter<StringView> {
169 constexpr DebugFormatter(size_t = 0) {
170 quote = '\'';
171 }
172
173 GREM_ALWAYS_INLINE void formatTo(FormatOutput& output, char value) const {
175 }
176};
177
178template <>
181
182 GREM_ALWAYS_INLINE void formatTo(FormatOutput& output, const char* value) const {
184 }
185};
186
187template <>
189 using DebugFormatter<const char*>::DebugFormatter;
190
191 GREM_ALWAYS_INLINE void formatTo(FormatOutput& output, char* value) const {
192 DebugFormatter<const char*>::formatTo(output, static_cast<const char*>(value));
193 }
194};
195
196template <grem::size_t N>
197struct DebugFormatter<char[N]> : DebugFormatter<const char*> {
198 using DebugFormatter<const char*>::DebugFormatter;
199
200 GREM_ALWAYS_INLINE void formatTo(FormatOutput& output, const char (&value)[N]) const {
201 DebugFormatter<const char*>::formatTo(output, static_cast<const char*>(value));
202 }
203};
204
205template <formattable T>
207 constexpr DebugFormatter(size_t = 0) {}
208
209 [[nodiscard]] constexpr size_t getRecursiveSize(const T&) const noexcept {
210 return 1;
211 }
212
213 void setCompact(bool) {}
214
215 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
216 if (*p == 'c') {
217 ++p;
218 }
219 return p;
220 }
221};
222
223template <typename T>
226 size_t indentation = 0;
227 bool compact = false;
228
232
233 [[nodiscard]] constexpr size_t getRecursiveSize(const auto& value) const noexcept {
234 if constexpr (convertible_to<typename std::iterator_traits<decltype(std::begin(value))>::iterator_category, std::forward_iterator_tag>) {
235 size_t result = 1;
236 for (const auto& element : value) {
237 result += underlying.getRecursiveSize(element);
238 }
239 return result;
240 } else {
241 return 1;
242 }
243 }
244
245 void setCompact(bool newCompact) {
246 compact = newCompact;
247 underlying.setCompact(newCompact);
248 }
249
250 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
251 if (*p == 'c') {
252 setCompact(true);
253 ++p;
254 }
255 return p;
256 }
257
258 void formatTo(FormatOutput& output, const auto& value) const {
259 if (compact || getRecursiveSize(value) - 1 <= 4) {
260 output.append("[");
261 auto it = std::begin(value);
262 auto end = std::end(value);
263 if (it != end) {
264 underlying.formatTo(output, *it++);
265 while (it != end) {
266 output.append(", ");
267 underlying.formatTo(output, *it++);
268 }
269 }
270 output.append("]");
271 } else {
272 output.append("[\n");
273 auto it = std::begin(value);
274 auto end = std::end(value);
275 if (it != end) {
276 for (size_t i = 0; i < indentation + 1; ++i) {
277 output.append(" ");
278 }
279 underlying.formatTo(output, *it++);
280 while (it != end) {
281 output.append(",\n");
282 for (size_t i = 0; i < indentation + 1; ++i) {
283 output.append(" ");
284 }
285 underlying.formatTo(output, *it++);
286 }
287 }
288 output.append("\n");
289 for (size_t i = 0; i < indentation; ++i) {
290 output.append(" ");
291 }
292 output.append("]");
293 }
294 }
295};
296
297template <input_range Range>
298requires(!formattable<Range> || requires { typename Formatter<Range>::GREM_private_DefaultRangeFormatterTag; })
302
303template <typename... Ts>
304requires(!formattable<Ts> || ...) struct DebugFormatter<Tuple<Ts...>> {
305 size_t indentation = 0;
306 bool compact = false;
307
308 constexpr DebugFormatter(size_t indentation = 0)
310
311 [[nodiscard]] constexpr size_t getRecursiveSize(const auto& value) const noexcept {
312 size_t result = 1;
313 meta::forEach(value, [&]<typename T>(const T& element) -> void { result += DebugFormatter<std::remove_cvref_t<T>>{}.getRecursiveSize(element); });
314 return result;
315 }
316
317 void setCompact(bool newCompact) {
318 compact = newCompact;
319 }
320
321 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
322 if (*p == 'c') {
323 setCompact(true);
324 ++p;
325 }
326 return p;
327 }
328
329 void formatTo(FormatOutput& output, const auto& value) const {
330 if constexpr (sizeof...(Ts) > 0) {
331 if (compact || getRecursiveSize(value) - 1 <= 4) {
332 output.append("(");
333 meta::forEachIndex<sizeof...(Ts)>([&](auto index) -> void {
334 if constexpr (index != 0) {
335 output.append(", ");
336 }
337 const auto& field = get<index>(value);
338 DebugFormatter<std::remove_cvref_t<decltype(field)>> underlying{indentation + 1};
339 underlying.setCompact(compact);
340 underlying.formatTo(output, field);
341 });
342 output.append(")");
343 } else {
344 output.append("(\n");
345 meta::forEachIndex<sizeof...(Ts)>([&](auto index) -> void {
346 if constexpr (index != 0) {
347 output.append(",\n");
348 }
349 for (size_t i = 0; i < indentation + 1; ++i) {
350 output.append(" ");
351 }
352 const auto& field = get<index>(value);
353 DebugFormatter<std::remove_cvref_t<decltype(field)>> underlying{indentation + 1};
354 underlying.setCompact(compact);
355 underlying.formatTo(output, field);
356 });
357 output.append("\n");
358 for (size_t i = 0; i < indentation; ++i) {
359 output.append(" ");
360 }
361 output.append(")");
362 }
363 } else {
364 output.append("()");
365 }
366 }
367};
368
369template <typename T1, typename T2>
373
374template <typename T>
377
378 constexpr DebugFormatter(size_t indentation = 0)
379 : underlying(indentation) {}
380
381 [[nodiscard]] constexpr size_t getRecursiveSize(const Optional<T>& value) const noexcept {
382 return (value) ? underlying.getRecursiveSize(*value) : 1;
383 }
384
385 void setCompact(bool newCompact) {
386 underlying.setCompact(newCompact);
387 }
388
389 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
390 return underlying.parseFormatSpecification(p);
391 }
392
393 void formatTo(FormatOutput& output, const Optional<T>& value) const {
394 if (value) {
395 underlying.formatTo(output, *value);
396 } else {
397 output.append("null");
398 }
399 }
400};
401
402template <typename T>
403requires(!std::is_void_v<T>) struct DebugFormatter<T*> {
405
406 constexpr DebugFormatter(size_t indentation = 0)
407 : underlying(indentation) {}
408
409 [[nodiscard]] constexpr size_t getRecursiveSize(T* value) const noexcept {
410 return (value) ? underlying.getRecursiveSize(*value) : 1;
411 }
412
413 void setCompact(bool newCompact) {
414 underlying.setCompact(newCompact);
415 }
416
417 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
418 return underlying.parseFormatSpecification(p);
419 }
420
421 void formatTo(FormatOutput& output, T* value) const {
422 if (value) {
423 underlying.formatTo(output, *value);
424 } else {
425 output.append("null");
426 }
427 }
428};
429
430template <typename T>
433
434 constexpr DebugFormatter(size_t indentation = 0)
435 : underlying(indentation) {}
436
437 [[nodiscard]] constexpr size_t getRecursiveSize(const UniquePointer<T>& value) const noexcept {
438 return (value) ? underlying.getRecursiveSize(*value) : 1;
439 }
440
441 void setCompact(bool newCompact) {
442 underlying.setCompact(newCompact);
443 }
444
445 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
446 return underlying.parseFormatSpecification(p);
447 }
448
449 void formatTo(FormatOutput& output, const UniquePointer<T>& value) const {
450 if (value) {
451 underlying.formatTo(output, *value);
452 } else {
453 output.append("null");
454 }
455 }
456};
457
458template <typename T>
461
462 constexpr DebugFormatter(size_t indentation = 0)
463 : underlying(indentation) {}
464
465 [[nodiscard]] constexpr size_t getRecursiveSize(const UniquePointer<T>& value) const noexcept {
466 return (value) ? underlying.getRecursiveSize(*value) : 1;
467 }
468
469 void setCompact(bool newCompact) {
470 underlying.setCompact(newCompact);
471 }
472
473 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
474 return underlying.parseFormatSpecification(p);
475 }
476
477 void formatTo(FormatOutput& output, const UniquePointer<T>& value) const {
478 if (value) {
479 output.append("[");
480 underlying.formatTo(output, *value);
481 output.append(", ... (unknown size)]");
482 } else {
483 output.append("null");
484 }
485 }
486};
487
488template <typename T>
491
492 constexpr DebugFormatter(size_t indentation = 0)
493 : underlying(indentation) {}
494
495 [[nodiscard]] constexpr size_t getRecursiveSize(const SharedPointer<T>& value) const noexcept {
496 return (value) ? underlying.getRecursiveSize(*value) : 1;
497 }
498
499 void setCompact(bool newCompact) {
500 underlying.setCompact(newCompact);
501 }
502
503 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
504 return underlying.parseFormatSpecification(p);
505 }
506
507 void formatTo(FormatOutput& output, const SharedPointer<T>& value) const {
508 if (value) {
509 underlying.formatTo(output, *value);
510 } else {
511 output.append("null");
512 }
513 }
514};
515
516template <typename T>
519
520 constexpr DebugFormatter(size_t indentation = 0)
521 : underlying(indentation) {}
522
523 [[nodiscard]] constexpr size_t getRecursiveSize(const SharedPointer<T[]>& value) const noexcept {
524 if (value) {
525 return underlying.getRecursiveSize(Span{value.get(), value.size()});
526 }
527 return 1;
528 }
529
530 void setCompact(bool newCompact) {
531 underlying.setCompact(newCompact);
532 }
533
534 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
535 return underlying.parseFormatSpecification(p);
536 }
537
538 void formatTo(FormatOutput& output, const SharedPointer<T[]>& value) const {
539 if (value) {
540 underlying.formatTo(output, Span{value.get(), value.size()});
541 } else {
542 output.append("null");
543 }
544 }
545};
546
547template <typename T>
550
551 constexpr DebugFormatter(size_t indentation = 0)
552 : underlying(indentation) {}
553
554 [[nodiscard]] constexpr size_t getRecursiveSize(const WeakPointer<T>& value) const noexcept {
555 if (value) {
556 if (const SharedPointer<T> p = value.lock()) {
557 return underlying.getRecursiveSize(*p);
558 }
559 }
560 return 1;
561 }
562
563 void setCompact(bool newCompact) {
564 underlying.setCompact(newCompact);
565 }
566
567 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
568 return underlying.parseFormatSpecification(p);
569 }
570
571 void formatTo(FormatOutput& output, const WeakPointer<T>& value) const {
572 if (value) {
573 if (const SharedPointer<T> p = value.lock()) {
574 underlying.formatTo(output, *p);
575 } else {
576 output.append("<expired>");
577 }
578 } else {
579 output.append("null");
580 }
581 }
582};
583
584template <typename T>
587
588 constexpr DebugFormatter(size_t indentation = 0)
589 : underlying(indentation) {}
590
591 [[nodiscard]] constexpr size_t getRecursiveSize(const WeakPointer<T[]>& value) const noexcept {
592 if (value) {
593 if (const SharedPointer<T> p = value.lock()) {
594 return underlying.getRecursiveSize(Span{p.get(), p.size()});
595 }
596 }
597 return 1;
598 }
599
600 void setCompact(bool newCompact) {
601 underlying.setCompact(newCompact);
602 }
603
604 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
605 return underlying.parseFormatSpecification(p);
606 }
607
608 void formatTo(FormatOutput& output, const WeakPointer<T>& value) const {
609 if (value) {
610 if (const SharedPointer<T> p = value.lock()) {
611 underlying.formatTo(output, Span{p.get(), p.size()});
612 } else {
613 output.append("<expired>");
614 }
615 } else {
616 output.append("null");
617 }
618 }
619};
620
621template <typename... Ts>
623 size_t indentation = 0;
624 bool compact = false;
625
626 constexpr DebugFormatter(size_t indentation = 0)
628
629 [[nodiscard]] constexpr size_t getRecursiveSize(const Variant<Ts...>& value) const noexcept {
630 if (value.valueless_by_exception()) {
631 return 1;
632 }
633 return match(value)([&]<typename T>(const T& v) -> size_t { return DebugFormatter<std::remove_cvref_t<T>>{indentation}.getRecursiveSize(v); });
634 }
635
636 void setCompact(bool newCompact) {
637 compact = newCompact;
638 }
639
640 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
641 if (*p == 'c') {
642 setCompact(true);
643 ++p;
644 }
645 return p;
646 }
647
648 void formatTo(FormatOutput& output, const Variant<Ts...>& value) const {
649 if (value.valueless_by_exception()) {
650 output.append("<valueless by exception>");
651 } else {
652 match(value)([&]<typename T>(const T& v) -> void {
654 output.append("(");
656 underlying.setCompact(compact);
657 underlying.formatTo(output, v);
658 output.append(")");
659 });
660 }
661 }
662};
663
664template <enumeration Enum>
666 constexpr DebugFormatter(size_t = 0) {}
667
668 [[nodiscard]] constexpr size_t getRecursiveSize(const Enum&) const noexcept {
669 return 1;
670 }
671
672 void setCompact(bool) {}
673
674 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
675 return p;
676 }
677
678 void formatTo(FormatOutput& output, const Enum& value) const {
679 bool found = false;
680 meta::forEachNamedEnumerand<Enum>([&](StringView name, auto type) -> void {
681 if (!found && value == type) {
683 output.append("::");
684 output.append(name);
685 found = true;
686 }
687 });
688 }
689};
690
691namespace detail {
692
693template <typename... Ts>
694consteval Variant<Ts...> getVariant(const Variant<Ts...>&);
695
696} // namespace detail
697
698template <aggregate Aggregate>
699requires(!formattable<Aggregate> && !requires(const Aggregate t) { detail::getVariant(t); }) struct DebugFormatter<Aggregate> {
700 size_t indentation = 0;
701 bool compact = false;
702
703 constexpr DebugFormatter(size_t indentation = 0)
705
706 [[nodiscard]] constexpr size_t getRecursiveSize(const Aggregate& value) const noexcept {
707 size_t result = 1;
708 meta::forEachField(value,
709 [&]<typename T>(const T& field) -> void { result += DebugFormatter<std::remove_cvref_t<decltype(field)>>{indentation + 1}.getRecursiveSize(field); });
710 return result;
711 }
712
713 void setCompact(bool newCompact) {
714 compact = newCompact;
715 }
716
717 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
718 if (*p == 'c') {
719 setCompact(true);
720 ++p;
721 }
722 return p;
723 }
724
725 void formatTo(FormatOutput& output, const Aggregate& value) const {
726 if constexpr (meta::aggregate_size_v<Aggregate> > 0) {
727 if (compact || getRecursiveSize(value) - 1 <= 4) {
728 output.append("{ ");
729 meta::forEachIndexedNamedField(value, [&](auto index, StringView name, const auto& field) -> void {
730 if constexpr (index != 0) {
731 output.append(", ");
732 }
733 output.append(name);
734 output.append(": ");
735 DebugFormatter<std::remove_cvref_t<decltype(field)>> underlying{indentation + 1};
736 underlying.setCompact(compact);
737 underlying.formatTo(output, field);
738 });
739 output.append(" }");
740 } else {
741 output.append("{\n");
742 meta::forEachIndexedNamedField(value, [&](auto index, StringView name, const auto& field) -> void {
743 if constexpr (index != 0) {
744 output.append(",\n");
745 }
746 for (size_t i = 0; i < indentation + 1; ++i) {
747 output.append(" ");
748 }
749 output.append(name);
750 output.append(": ");
751 DebugFormatter<std::remove_cvref_t<decltype(field)>> underlying{indentation + 1};
752 underlying.setCompact(compact);
753 underlying.formatTo(output, field);
754 });
755 output.append("\n");
756 for (size_t i = 0; i < indentation; ++i) {
757 output.append(" ");
758 }
759 output.append("}");
760 }
761 } else {
762 output.append("{}");
763 }
764 }
765};
766
767template <typename VariantDerived>
768requires(!formattable<VariantDerived> && !aggregate<VariantDerived> && requires(const VariantDerived t) { detail::getVariant(t); })
769struct DebugFormatter<VariantDerived> : DebugFormatter<decltype(detail::getVariant(std::declval<const VariantDerived&>()))> {
770 using DebugFormatter<decltype(detail::getVariant(std::declval<const VariantDerived&>()))>::DebugFormatter;
771};
772
773template <aggregate VariantDerivedAggregate>
774requires(!formattable<VariantDerivedAggregate> && requires(const VariantDerivedAggregate t) { detail::getVariant(t); })
775struct DebugFormatter<VariantDerivedAggregate> : DebugFormatter<decltype(detail::getVariant(std::declval<const VariantDerivedAggregate&>()))> {
776 size_t indentation = 0;
777 bool compact = false;
778
779 constexpr DebugFormatter(size_t indentation = 0)
780 : DebugFormatter<decltype(detail::getVariant(std::declval<const VariantDerivedAggregate&>()))>(indentation + 1)
782
783 [[nodiscard]] constexpr size_t getRecursiveSize(const VariantDerivedAggregate& value) const noexcept {
785 meta::forEachField(value,
786 [&]<typename T>(const T& field) -> void { result += DebugFormatter<std::remove_cvref_t<decltype(field)>>{indentation + 1}.getRecursiveSize(field); });
787 return result;
788 }
789
794
795 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
796 if (*p == 'c') {
797 setCompact(true);
798 ++p;
799 }
800 return p;
801 }
802
803 void formatTo(FormatOutput& output, const VariantDerivedAggregate& value) const {
805 if (compact || getRecursiveSize(value) - 1 <= 4) {
806 output.append("{ ");
808 meta::forEachNamedField(value, [&](StringView name, const auto& field) -> void {
809 output.append(", ");
810 output.append(name);
811 output.append(": ");
812 DebugFormatter<std::remove_cvref_t<decltype(field)>> underlying{indentation + 1};
813 underlying.setCompact(compact);
814 underlying.formatTo(output, field);
815 });
816 output.append(" }");
817 } else {
818 output.append("{\n");
820 meta::forEachNamedField(value, [&](StringView name, const auto& field) -> void {
821 output.append(",\n");
822 for (size_t i = 0; i < indentation + 1; ++i) {
823 output.append(" ");
824 }
825 output.append(name);
826 output.append(": ");
827 DebugFormatter<std::remove_cvref_t<decltype(field)>> underlying{indentation + 1};
828 underlying.setCompact(compact);
829 underlying.formatTo(output, field);
830 });
831 output.append("\n");
832 for (size_t i = 0; i < indentation; ++i) {
833 output.append(" ");
834 }
835 output.append("}");
836 }
837 } else {
839 }
840 }
841};
842
843template <typename... Args>
845public:
846 template <typename T>
847 consteval DebugFormatStringBase(const T& format) requires(convertible_to<T, CStringView>)
848 : format(format) {
849 detail::FormatSizeCounter sizeCounter{};
851 }
852
853 constexpr DebugFormatStringBase(RuntimeFormat format) noexcept
854 : format(format) {}
855
856 [[nodiscard]] constexpr CStringView get() const noexcept {
857 return format;
858 }
859
860private:
861 CStringView format;
862};
863
864template <typename... Args>
866
867template <typename... Args>
868[[nodiscard]] constexpr size_t getFormattedSizeDebug(DebugFormatString<Args...> format, const Args&... args) {
869 detail::FormatSizeCounter sizeCounter{};
870 formatToV(sizeCounter, format.get(), {FormatArgument::create<Args, DebugFormatter<Args>>(args)...});
871 return sizeCounter.output;
872}
873
874template <typename Output, typename... Args>
875constexpr void formatToDebug(Output& output, DebugFormatString<Args...> format, const Args&... args) {
876 if constexpr (requires(Output out, const size_t n) {
877 out.data();
878 out.size();
879 out.resize(n);
880 }) {
881 detail::FormatSizeCounter sizeCounter{};
882 formatToV(sizeCounter, format.get(), {FormatArgument::create<Args, DebugFormatter<Args>>(args)...});
883 const size_t formattedSize = sizeCounter.output;
884 const size_t offset = output.size();
885 output.resize(offset + formattedSize);
886 try {
887 char* p = output.data() + offset;
888 formatToDebug(p, format, args...);
889 } catch (...) {
890 output.resize(offset);
891 throw;
892 }
893 } else if constexpr (requires(Output out, const size_t n) {
894 out.size();
895 out.reserve(n);
896 }) {
897 detail::FormatSizeCounter sizeCounter{};
898 formatToV(sizeCounter, format.get(), {FormatArgument::create<Args, DebugFormatter<Args>>(args)...});
899 const size_t formattedSize = sizeCounter.output;
900 output.reserve(output.size() + formattedSize);
901 detail::AppendingFormatWriter writer{output};
902 formatToV(writer, format.get(), {FormatArgument::create<Args, DebugFormatter<Args>>(args)...});
903 } else if constexpr (requires { *output++ = '\0'; }) {
904 detail::IteratorFormatWriter writer{output};
905 formatToV(writer, format.get(), {FormatArgument::create<Args, DebugFormatter<Args>>(args)...});
906 output = writer.output;
907 } else {
908 detail::AppendingFormatWriter writer{output};
909 formatToV(writer, format.get(), {FormatArgument::create<Args, DebugFormatter<Args>>(args)...});
910 }
911}
912
913template <typename... Args>
914GREM_ALWAYS_INLINE void printDebug(std::FILE* output, DebugFormatString<Args...> format, const Args&... args) {
915 detail::CFileFormatWriter writer{output};
916 formatToDebug(writer, format, args...);
917}
918
919GREM_ALWAYS_INLINE void printlnDebug(std::FILE* output) {
920 println(output);
921}
922
923template <typename... Args>
924GREM_ALWAYS_INLINE void printlnDebug(std::FILE* output, DebugFormatString<Args...> format, const Args&... args) {
925 printDebug(output, format, args...);
926 printlnDebug(output);
927}
928
929template <typename... Args>
931 printDebug(stdout, format, args...);
932}
933
937
938template <typename... Args>
940 printlnDebug(stdout, format, args...);
941}
942
943template <typename... Args>
945 printDebug(stderr, format, args...);
946}
947
951
952template <typename... Args>
954 printlnDebug(stderr, format, args...);
955}
956
957template <typename... Args>
958[[nodiscard]] inline String formatStringDebug(DebugFormatString<Args...> format, const Args&... args) {
959 String result{};
960 formatToDebug(result, format, args...);
961 return result;
962}
963
964template <size_t N, typename... Args>
965inline detail::SmallFormattedString<N> formatSmallStringDebug(DebugFormatString<Args...> format, const Args&... args) {
966 detail::SmallFormattedString<N> result{};
967 formatToDebug(result, format, args...);
968 return result;
969}
970
971} // namespace grem
972
973#endif
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Definition ConstantString.hpp:40
Definition debug_formatting.hpp:844
constexpr CStringView get() const noexcept
Definition debug_formatting.hpp:856
consteval DebugFormatStringBase(const T &format)
Definition debug_formatting.hpp:847
constexpr DebugFormatStringBase(RuntimeFormat format) noexcept
Definition debug_formatting.hpp:853
static constexpr FormatArgument createEmpty() noexcept
Definition formatting.hpp:133
Definition formatting.hpp:41
virtual constexpr void append(StringView string)=0
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Definition SharedPointer.hpp:65
Definition Span.hpp:146
Definition Tuple.hpp:210
Nullable RAII data pointer with exclusive ownership of an object that is automatically destroyed on p...
Definition UniquePointer.hpp:77
Tagged union value type that holds a value of one of the given types.
Definition Variant.hpp:293
constexpr bool valueless_by_exception() const noexcept
Check if the variant is in the valueless by exception state.
Definition Variant.hpp:726
Definition SharedPointer.hpp:588
SharedPointer< T > lock() const noexcept
Definition SharedPointer.hpp:715
Definition concepts.hpp:47
Definition formatting.hpp:125
constexpr void forEachField(Aggregate &&value, auto fn)
Execute a function for each field of an aggregate.
Definition metaprogramming.hpp:1297
constexpr void forEachIndexedNamedField(Aggregate &&value, auto fn)
Execute a function for each field of an aggregate, also including the field index and name.
Definition metaprogramming.hpp:1690
constexpr void forEachNamedField(Aggregate &&value, auto fn)
Execute a function for each field of an aggregate, also including the field name.
Definition metaprogramming.hpp:1664
constexpr void forEach(auto &&tuple, auto fn)
Execute a function for each element in a given tuple, sequentially.
Definition metaprogramming.hpp:181
constexpr std::size_t aggregate_size_v
The number of fields in a given aggregate type.
Definition metaprogramming.hpp:921
constexpr void forEachNamedEnumerand(auto fn)
Execute a function for each consecutive valid enumerand starting at value 0 in a given enum type,...
Definition metaprogramming.hpp:1878
constexpr auto unqualified_type_name_v
Unqualified name of a type.
Definition metaprogramming.hpp:1526
constexpr void forEachIndex(auto fn)
Execute a function for each index in the sequence from 0 up to, but not including,...
Definition metaprogramming.hpp:229
Definition aliases.hpp:9
constexpr tuple_element_t< Index, Pair< T1, T2 > > & get(Pair< T1, T2 > &t) noexcept
Definition Pair.hpp:202
constexpr size_t getFormattedSizeDebug(DebugFormatString< Args... > format, const Args &... args)
Definition debug_formatting.hpp:868
constexpr void formatToDebug(Output &output, DebugFormatString< Args... > format, const Args &... args)
Definition debug_formatting.hpp:875
detail::SmallFormattedString< N > formatSmallStringDebug(DebugFormatString< Args... > format, const Args &... args)
Definition debug_formatting.hpp:965
iter_value_t< iterator_t< T > > range_value_t
Definition concepts.hpp:222
constexpr void formatToV(FormatOutput &output, CStringView format, FormatArguments arguments)
Definition formatting.hpp:200
GREM_ALWAYS_INLINE void println()
Definition formatting.hpp:380
GREM_ALWAYS_INLINE void eprintlnDebug()
Definition debug_formatting.hpp:948
GREM_ALWAYS_INLINE void printDebug(std::FILE *output, DebugFormatString< Args... > format, const Args &... args)
Definition debug_formatting.hpp:914
GREM_ALWAYS_INLINE void printlnDebug()
Definition debug_formatting.hpp:934
GREM_ALWAYS_INLINE void eprintDebug(DebugFormatString< Args... > format, const Args &... args)
Definition debug_formatting.hpp:944
DebugFormatStringBase< std::type_identity_t< Args >... > DebugFormatString
Definition debug_formatting.hpp:865
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
constexpr detail::Matcher< V > match(V &&variant)
Choose a function overload to execute based on the active alternative of a variant.
Definition Variant.hpp:1922
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
String formatStringDebug(DebugFormatString< Args... > format, const Args &... args)
Definition debug_formatting.hpp:958
auto asBytes(Span< T, N > s) noexcept
Definition Span.hpp:359
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:703
bool compact
Definition debug_formatting.hpp:701
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:717
size_t indentation
Definition debug_formatting.hpp:700
void formatTo(FormatOutput &output, const Aggregate &value) const
Definition debug_formatting.hpp:725
void setCompact(bool newCompact)
Definition debug_formatting.hpp:713
constexpr size_t getRecursiveSize(const Aggregate &value) const noexcept
Definition debug_formatting.hpp:706
constexpr DebugFormatter(size_t=0)
Definition debug_formatting.hpp:666
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:674
void formatTo(FormatOutput &output, const Enum &value) const
Definition debug_formatting.hpp:678
constexpr size_t getRecursiveSize(const Enum &) const noexcept
Definition debug_formatting.hpp:668
void setCompact(bool)
Definition debug_formatting.hpp:672
void setCompact(bool newCompact)
Definition debug_formatting.hpp:385
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:378
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:389
DebugFormatter< std::remove_cvref_t< T > > underlying
Definition debug_formatting.hpp:376
void formatTo(FormatOutput &output, const Optional< T > &value) const
Definition debug_formatting.hpp:393
constexpr size_t getRecursiveSize(const Optional< T > &value) const noexcept
Definition debug_formatting.hpp:381
void setCompact(bool newCompact)
Definition debug_formatting.hpp:499
DebugFormatter< std::remove_cvref_t< T > > underlying
Definition debug_formatting.hpp:490
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:492
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:503
void formatTo(FormatOutput &output, const SharedPointer< T > &value) const
Definition debug_formatting.hpp:507
constexpr size_t getRecursiveSize(const SharedPointer< T > &value) const noexcept
Definition debug_formatting.hpp:495
constexpr size_t getRecursiveSize(const SharedPointer< T[]> &value) const noexcept
Definition debug_formatting.hpp:523
DebugRangeFormatter< std::remove_cvref_t< T > > underlying
Definition debug_formatting.hpp:518
void formatTo(FormatOutput &output, const SharedPointer< T[]> &value) const
Definition debug_formatting.hpp:538
void setCompact(bool newCompact)
Definition debug_formatting.hpp:530
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:534
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:520
void formatTo(FormatOutput &output, StringView value) const
Definition debug_formatting.hpp:105
constexpr size_t getRecursiveSize(const StringView &) const noexcept
Definition debug_formatting.hpp:92
char quote
Definition debug_formatting.hpp:88
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:98
constexpr DebugFormatter(size_t=0)
Definition debug_formatting.hpp:90
void setCompact(bool)
Definition debug_formatting.hpp:96
void setCompact(bool)
Definition debug_formatting.hpp:213
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:215
constexpr size_t getRecursiveSize(const T &) const noexcept
Definition debug_formatting.hpp:209
constexpr DebugFormatter(size_t=0)
Definition debug_formatting.hpp:207
void formatTo(FormatOutput &output, T *value) const
Definition debug_formatting.hpp:421
void setCompact(bool newCompact)
Definition debug_formatting.hpp:413
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:406
DebugFormatter< std::remove_cvref_t< T > > underlying
Definition debug_formatting.hpp:404
constexpr size_t getRecursiveSize(T *value) const noexcept
Definition debug_formatting.hpp:409
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:417
void formatTo(FormatOutput &output, const auto &value) const
Definition debug_formatting.hpp:329
constexpr size_t getRecursiveSize(const auto &value) const noexcept
Definition debug_formatting.hpp:311
bool compact
Definition debug_formatting.hpp:306
size_t indentation
Definition debug_formatting.hpp:305
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:308
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:321
void setCompact(bool newCompact)
Definition debug_formatting.hpp:317
DebugFormatter< std::remove_cvref_t< T > > underlying
Definition debug_formatting.hpp:432
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:445
void setCompact(bool newCompact)
Definition debug_formatting.hpp:441
constexpr size_t getRecursiveSize(const UniquePointer< T > &value) const noexcept
Definition debug_formatting.hpp:437
void formatTo(FormatOutput &output, const UniquePointer< T > &value) const
Definition debug_formatting.hpp:449
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:434
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:462
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:473
DebugFormatter< std::remove_cvref_t< T > > underlying
Definition debug_formatting.hpp:460
void setCompact(bool newCompact)
Definition debug_formatting.hpp:469
void formatTo(FormatOutput &output, const UniquePointer< T > &value) const
Definition debug_formatting.hpp:477
constexpr size_t getRecursiveSize(const UniquePointer< T > &value) const noexcept
Definition debug_formatting.hpp:465
void formatTo(FormatOutput &output, const VariantDerivedAggregate &value) const
Definition debug_formatting.hpp:803
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:779
size_t indentation
Definition debug_formatting.hpp:776
constexpr size_t getRecursiveSize(const VariantDerivedAggregate &value) const noexcept
Definition debug_formatting.hpp:783
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:795
void setCompact(bool newCompact)
Definition debug_formatting.hpp:790
bool compact
Definition debug_formatting.hpp:777
constexpr size_t getRecursiveSize(const Variant< Ts... > &value) const noexcept
Definition debug_formatting.hpp:629
void formatTo(FormatOutput &output, const Variant< Ts... > &value) const
Definition debug_formatting.hpp:648
void setCompact(bool newCompact)
Definition debug_formatting.hpp:636
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:640
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:626
bool compact
Definition debug_formatting.hpp:624
size_t indentation
Definition debug_formatting.hpp:623
DebugFormatter< std::remove_cvref_t< T > > underlying
Definition debug_formatting.hpp:549
void setCompact(bool newCompact)
Definition debug_formatting.hpp:563
void formatTo(FormatOutput &output, const WeakPointer< T > &value) const
Definition debug_formatting.hpp:571
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:551
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:567
constexpr size_t getRecursiveSize(const WeakPointer< T > &value) const noexcept
Definition debug_formatting.hpp:554
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:604
void setCompact(bool newCompact)
Definition debug_formatting.hpp:600
void formatTo(FormatOutput &output, const WeakPointer< T > &value) const
Definition debug_formatting.hpp:608
DebugRangeFormatter< std::remove_cvref_t< T > > underlying
Definition debug_formatting.hpp:586
constexpr size_t getRecursiveSize(const WeakPointer< T[]> &value) const noexcept
Definition debug_formatting.hpp:591
constexpr DebugFormatter(size_t indentation=0)
Definition debug_formatting.hpp:588
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:70
constexpr DebugFormatter(size_t=0)
Definition debug_formatting.hpp:62
void setCompact(bool)
Definition debug_formatting.hpp:68
constexpr size_t getRecursiveSize(const byte &) const noexcept
Definition debug_formatting.hpp:64
void formatTo(FormatOutput &output, const byte &value) const
Definition debug_formatting.hpp:77
constexpr DebugFormatter(size_t=0)
Definition debug_formatting.hpp:169
GREM_ALWAYS_INLINE void formatTo(FormatOutput &output, char value) const
Definition debug_formatting.hpp:173
GREM_ALWAYS_INLINE void formatTo(FormatOutput &output, char *value) const
Definition debug_formatting.hpp:191
GREM_ALWAYS_INLINE void formatTo(FormatOutput &output, const char(&value)[N]) const
Definition debug_formatting.hpp:200
GREM_ALWAYS_INLINE void formatTo(FormatOutput &output, const char *value) const
Definition debug_formatting.hpp:182
GREM_ALWAYS_INLINE void formatTo(FormatOutput &output, CStringView value) const
Definition debug_formatting.hpp:144
GREM_ALWAYS_INLINE void formatTo(FormatOutput &output, const ConstantString< char, Length > &value) const
Definition debug_formatting.hpp:162
GREM_ALWAYS_INLINE void formatTo(FormatOutput &output, const String &value) const
Definition debug_formatting.hpp:153
Definition debug_formatting.hpp:32
void setCompact(bool)
Definition debug_formatting.hpp:39
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:41
void formatTo(FormatOutput &output, const T &value) const
Definition debug_formatting.hpp:48
constexpr size_t getRecursiveSize(const T &) const noexcept
Definition debug_formatting.hpp:35
constexpr DebugFormatter(size_t=0)
Definition debug_formatting.hpp:33
Definition debug_formatting.hpp:224
bool compact
Definition debug_formatting.hpp:227
size_t indentation
Definition debug_formatting.hpp:226
constexpr size_t getRecursiveSize(const auto &value) const noexcept
Definition debug_formatting.hpp:233
constexpr const char * parseFormatSpecification(const char *p)
Definition debug_formatting.hpp:250
constexpr DebugRangeFormatter(size_t indentation=0)
Definition debug_formatting.hpp:229
void setCompact(bool newCompact)
Definition debug_formatting.hpp:245
DebugFormatter< T > underlying
Definition debug_formatting.hpp:225
void formatTo(FormatOutput &output, const auto &value) const
Definition debug_formatting.hpp:258
Definition formatting.hpp:47
Definition Pair.hpp:53
Definition formatting.hpp:266