Derive-C
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <stdio.h>
11
18
19#define DC_TRAIT_DEBUGABLE(SELF) \
20 DC_REQUIRE_METHOD(void, SELF, debug, (SELF const*, dc_debug_fmt fmt, FILE*))
21
22#define DC_NO_DEBUG PRIV(no_debug)
23
24DC_INTERNAL static void PRIV(no_debug)(void const* self, dc_debug_fmt fmt, FILE* stream) {
25 (void)self;
26 (void)fmt;
27 fprintf(stream, "(no DEBUG function provided)");
28}
29
30#define _DC_DERIVE_DEBUG_MEMBER(MEMBER_TYPE, MEMBER_NAME) \
31 dc_debug_fmt_print(fmt, stream, DC_STRINGIFY(MEMBER_NAME) ": "); \
32 NS(MEMBER_TYPE, debug)(&self->MEMBER_NAME, fmt, stream); \
33 fprintf(stream, ",\n");
34
35#define DC_DERIVE_DEBUG(TYPE) \
36 DC_PUBLIC static TYPE NS(TYPE, DEBUG)(TYPE const* self, dc_debug_fmt fmt, FILE* stream) { \
37 fprintf(stream, DC_STRINGIFY(TYPE) "@%p {\n", self); \
38 fmt = dc_debug_fmt_scope_begin(fmt); \
39 NS(TYPE, REFLECT)(_DC_DERIVE_DEBUG_MEMBER); \
40 fmt = dc_debug_fmt_scope_end(fmt); \
41 }
42
43#define _DC_DERIVE_STD_DEBUG(TYPE, FMT, ...) \
44 DC_PUBLIC static void NS(TYPE, debug)(TYPE const* self, dc_debug_fmt fmt, FILE* stream) { \
45 (void)fmt; \
46 fprintf(stream, FMT, *self); \
47 }
48
49#define _DC_DERIVE_STD_DEBUG_FLOAT(TYPE, FMT, ...) \
50 DC_PUBLIC static void NS(TYPE, debug)(TYPE const* self, dc_debug_fmt fmt, FILE* stream) { \
51 (void)fmt; \
52 fprintf(stream, FMT, (double)*self); \
53 }
54
57
58DC_PUBLIC static void dc_string_debug(char* const* string, dc_debug_fmt fmt, FILE* stream) {
59 (void)fmt;
60 fprintf(stream, "char*@%p \"%s\"", *string, *string);
61}
62
63DC_PUBLIC static void dc_string_const_debug(char const* const* string, dc_debug_fmt fmt,
64 FILE* stream) {
65 (void)fmt;
66 fprintf(stream, "char*@%p \"%s\"", *string, *string);
67}
68
69DC_PUBLIC static void dc_void_ptr_debug(void* const* ptr, dc_debug_fmt fmt, FILE* stream) {
70 (void)fmt;
71 fprintf(stream, "void*@%p", *ptr);
72}
73
74DC_PUBLIC static void dc_void_const_ptr_debug(void const* const* ptr, dc_debug_fmt fmt,
75 FILE* stream) {
76 (void)fmt;
77 fprintf(stream, "void const*@%p", *ptr);
78}
79
80#if defined DC_GENERIC_KEYWORD_SUPPORTED
81 #define _DC_DEFAULT_DEBUG_CASE(TYPE, _, x) \
82 TYPE: \
83 NS(TYPE, debug),
84
85 #define _DC_DEFAULT_DEBUG(SELF, FMT, STREAM) \
86 _Generic(*(SELF), \
87 DC_STD_REFLECT(_DC_DEFAULT_DEBUG_CASE, f) \
88 DC_FLOAT_REFLECT(_DC_DEFAULT_DEBUG_CASE, f) char const*: dc_string_const_debug, \
89 char*: dc_string_debug, \
90 void const*: dc_void_const_ptr_debug, \
91 void*: dc_void_ptr_debug, \
92 default: PRIV(no_debug))(SELF, FMT, STREAM);
93#else
94 #include <type_traits>
95
96 #define _DC_DEFAULT_DEBUG_CASE(TYPE, _, FMT, STREAM) \
97 if constexpr (std::is_same_v< \
98 TYPE, std::remove_cv_t<std::remove_reference_t<decltype(*item)>>>) { \
99 NS(TYPE, debug)(item, FMT, STREAM); \
100 } else
101
102 #define _DC_DEFAULT_DEBUG(SELF, FMT, STREAM) \
103 [&]<typename T>(T item) { \
104 DC_STD_REFLECT(_DC_DEFAULT_DEBUG_CASE, FMT, STREAM) \
105 DC_FLOAT_REFLECT(_DC_DEFAULT_DEBUG_CASE, FMT, STREAM) \
106 if constexpr (std::is_same_v<void*, std::remove_cv_t< \
107 std::remove_reference_t<decltype(*item)>>>) { \
108 dc_void_const_ptr_debug(item, FMT, STREAM); \
109 } else if constexpr (std::is_same_v<void const*, \
110 std::remove_cv_t< \
111 std::remove_reference_t<decltype(*item)>>>) { \
112 dc_void_ptr_debug(item, FMT, STREAM); \
113 } else if constexpr (std::is_same_v<char*, std::remove_cv_t<std::remove_reference_t< \
114 decltype(*item)>>>) { \
115 dc_string_debug(item, FMT, STREAM); \
116 } else if constexpr (std::is_same_v<char const*, \
117 std::remove_cv_t< \
118 std::remove_reference_t<decltype(*item)>>>) { \
119 dc_string_const_debug(item, FMT, STREAM); \
120 } else { \
121 DC_NO_DEBUG(item, FMT, STREAM); \
122 } \
123 }(SELF)
124#endif
125
126#define DC_DEFAULT_DEBUG _DC_DEFAULT_DEBUG
static DC_PUBLIC void dc_void_const_ptr_debug(void const *const *ptr, dc_debug_fmt fmt, FILE *stream)
Definition debug.h:74
#define _DC_DERIVE_STD_DEBUG(TYPE, FMT,...)
Definition debug.h:43
static DC_PUBLIC void dc_void_ptr_debug(void *const *ptr, dc_debug_fmt fmt, FILE *stream)
Definition debug.h:69
static DC_PUBLIC void dc_string_debug(char *const *string, dc_debug_fmt fmt, FILE *stream)
Definition debug.h:58
#define _DC_DERIVE_STD_DEBUG_FLOAT(TYPE, FMT,...)
Definition debug.h:49
static DC_INTERNAL void PRIV no_debug(void const *self, dc_debug_fmt fmt, FILE *stream)
Definition debug.h:24
static DC_PUBLIC void dc_string_const_debug(char const *const *string, dc_debug_fmt fmt, FILE *stream)
Definition debug.h:63
#define DC_PUBLIC
Definition namespace.h:25
#define PRIV(name)
Definition namespace.h:20
#define DC_INTERNAL
Definition namespace.h:30
#define DC_STD_REFLECT(F,...)
Definition reflect.h:32
#define DC_FLOAT_REFLECT(F,...)
Definition reflect.h:25
Debug format helpers for debug printin data structures.
Definition fmt.h:11
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108