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
16
17#define DC_TRAIT_DEBUGABLE(SELF) \
18 DC_REQUIRE_METHOD(void, SELF, debug, (SELF const*, dc_debug_fmt fmt, FILE*));
19
20#define NO_DEBUG PRIV(no_debug)
21
22static void PRIV(no_debug)(void const* self, dc_debug_fmt fmt, FILE* stream) {
23 (void)self;
24 (void)fmt;
25 fprintf(stream, "(no debug provided)");
26}
27
28#define _DC_DERIVE_DEBUG_MEMBER(MEMBER_TYPE, MEMBER_NAME) \
29 dc_debug_fmt_print(fmt, stream, STRINGIFY(MEMBER_NAME) ": "); \
30 NS(MEMBER_TYPE, debug)(&self->MEMBER_NAME, fmt, stream); \
31 fprintf(fmt, stream, ",\n");
32
33#define DC_DERIVE_DEBUG(TYPE) \
34 static TYPE NS(TYPE, DEBUG)(TYPE const* self, dc_debug_fmt fmt, FILE* stream) { \
35 fprintf(stream, STRINGIFY(TYPE) "@%p {\n", self); \
36 fmt = dc_debug_fmt_scope_begin(fmt); \
37 NS(TYPE, REFLECT)(_DC_DERIVE_DEBUG_MEMBER); \
38 fmt = dc_debug_fmt_scope_end(fmt); \
39 }
40
41#define _DERIVE_STD_DEBUG(TYPE, FMT, ...) \
42 static void NS(TYPE, debug)(TYPE const* self, dc_debug_fmt fmt, FILE* stream) { \
43 (void)fmt; \
44 fprintf(stream, FMT, *self); \
45 }
46
48
49static void dc_string_debug(char const* const* string, dc_debug_fmt fmt, FILE* stream) {
50 (void)fmt;
51 fprintf(stream, "char*@%p \"%s\"", *string, *string);
52}
53
54#if defined __cplusplus
55 #include <type_traits>
56
57 #define _DC_DEFAULT_DEBUG_CASE(TYPE, _, FMT, STREAM) \
58 if constexpr (std::is_same_v< \
59 TYPE, std::remove_cv_t<std::remove_reference_t<decltype(*item)>>>) { \
60 NS(TYPE, debug)(item, FMT, STREAM); \
61 } else
62
63 #define _DC_DEFAULT_DEBUG(SELF, FMT, STREAM) \
64 [&]<typename T>(T item) { \
65 DC_STD_REFLECT(_DC_DEFAULT_DEBUG_CASE, FMT, STREAM) \
66 if constexpr (std::is_same_v<char*, std::remove_cv_t< \
67 std::remove_reference_t<decltype(*item)>>>) { \
68 dc_string_debug(item, FMT, STREAM); \
69 } else { \
70 NO_DEBUG(item, FMT, STREAM); \
71 } \
72 }(SELF)
73
74#else
75 #define _DC_DEFAULT_DEBUG_CASE(TYPE, _, x) \
76 TYPE: \
77 NS(TYPE, debug),
78
79 #define _DC_DEFAULT_DEBUG(SELF, FMT, STREAM) \
80 _Generic(*(SELF), \
81 DC_STD_REFLECT(_DC_DEFAULT_DEBUG_CASE, f) char const*: dc_string_debug, \
82 default: PRIV(no_debug))(SELF, FMT, STREAM);
83#endif
84
85#define DC_DEFAULT_DEBUG _DC_DEFAULT_DEBUG
#define _DERIVE_STD_DEBUG(TYPE, FMT,...)
Definition debug.h:41
static void PRIV no_debug(void const *self, dc_debug_fmt fmt, FILE *stream)
Definition debug.h:22
static void dc_string_debug(char const *const *string, dc_debug_fmt fmt, FILE *stream)
Definition debug.h:49
#define PRIV(name)
Definition namespace.h:6
#define DC_STD_REFLECT(F,...)
Definition reflect.h:11
Debug format helpers for debug printin data structures.
Definition fmt.h:10
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107