Derive-C
Loading...
Searching...
No Matches
debug.h File Reference
#include <stdio.h>
#include <derive-c/core/attributes.h>
#include <derive-c/core/debug/fmt.h>
#include <derive-c/core/namespace.h>
#include <derive-c/core/require.h>
#include <derive-c/core/std/reflect.h>
#include <derive-c/core/compiler.h>
#include <type_traits>

Go to the source code of this file.

Macros

#define DC_TRAIT_DEBUGABLE(SELF)
 The debug trait Inspired by rust's {:?} pretty print, any data structure can be dumped to a stream.
#define DC_NO_DEBUG   PRIV(no_debug)
#define _DC_DERIVE_DEBUG_MEMBER(MEMBER_TYPE, MEMBER_NAME)
#define DC_DERIVE_DEBUG(TYPE)
#define _DC_DERIVE_STD_DEBUG(TYPE, FMT, ...)
#define _DC_DERIVE_STD_DEBUG_FLOAT(TYPE, FMT, ...)
#define _DC_DEFAULT_DEBUG_CASE(TYPE, _, FMT, STREAM)
#define _DC_DEFAULT_DEBUG(SELF, FMT, STREAM)
#define DC_DEFAULT_DEBUG   _DC_DEFAULT_DEBUG

Functions

static DC_INTERNAL void PRIV no_debug (void const *self, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC void dc_string_debug (char *const *string, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC void dc_string_const_debug (char const *const *string, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC void dc_void_ptr_debug (void *const *ptr, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC void dc_void_const_ptr_debug (void const *const *ptr, dc_debug_fmt fmt, FILE *stream)

Macro Definition Documentation

◆ _DC_DEFAULT_DEBUG

#define _DC_DEFAULT_DEBUG ( SELF,
FMT,
STREAM )
Value:
[&]<typename T>(T item) { \
DC_STD_REFLECT(_DC_DEFAULT_DEBUG_CASE, FMT, STREAM) \
DC_FLOAT_REFLECT(_DC_DEFAULT_DEBUG_CASE, FMT, STREAM) \
if constexpr (std::is_same_v<void*, std::remove_cv_t< \
std::remove_reference_t<decltype(*item)>>>) { \
dc_void_const_ptr_debug(item, FMT, STREAM); \
} else if constexpr (std::is_same_v<void const*, \
std::remove_cv_t< \
std::remove_reference_t<decltype(*item)>>>) { \
dc_void_ptr_debug(item, FMT, STREAM); \
} else if constexpr (std::is_same_v<char*, std::remove_cv_t<std::remove_reference_t< \
decltype(*item)>>>) { \
dc_string_debug(item, FMT, STREAM); \
} else if constexpr (std::is_same_v<char const*, \
std::remove_cv_t< \
std::remove_reference_t<decltype(*item)>>>) { \
dc_string_const_debug(item, FMT, STREAM); \
} else { \
DC_NO_DEBUG(item, FMT, STREAM); \
} \
}(SELF)
IV_PAIR item
Definition template.h:281
#define SELF
Definition def.h:52
#define _DC_DEFAULT_DEBUG_CASE(TYPE, _, FMT, STREAM)
Definition debug.h:96

Definition at line 102 of file debug.h.

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)

◆ _DC_DEFAULT_DEBUG_CASE

#define _DC_DEFAULT_DEBUG_CASE ( TYPE,
_,
FMT,
STREAM )
Value:
if constexpr (std::is_same_v< \
TYPE, std::remove_cv_t<std::remove_reference_t<decltype(*item)>>>) { \
NS(TYPE, debug)(item, FMT, STREAM); \
} else
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212

Definition at line 96 of file debug.h.

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

◆ _DC_DERIVE_DEBUG_MEMBER

#define _DC_DERIVE_DEBUG_MEMBER ( MEMBER_TYPE,
MEMBER_NAME )
Value:
dc_debug_fmt_print(fmt, stream, DC_STRINGIFY(MEMBER_NAME) ": "); \
NS(MEMBER_TYPE, debug)(&self->MEMBER_NAME, fmt, stream); \
fprintf(stream, ",\n");
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
#define DC_STRINGIFY(MACRO)
Definition namespace.h:5
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

Definition at line 30 of file debug.h.

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");

◆ _DC_DERIVE_STD_DEBUG

#define _DC_DERIVE_STD_DEBUG ( TYPE,
FMT,
... )
Value:
DC_PUBLIC static void NS(TYPE, debug)(TYPE const* self, dc_debug_fmt fmt, FILE* stream) { \
(void)fmt; \
fprintf(stream, FMT, *self); \
}
#define DC_PUBLIC
Definition namespace.h:25
#define NS(pre, post)
Definition namespace.h:14
Debug format helpers for debug printin data structures.
Definition fmt.h:11

Definition at line 43 of file debug.h.

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 }

◆ _DC_DERIVE_STD_DEBUG_FLOAT

#define _DC_DERIVE_STD_DEBUG_FLOAT ( TYPE,
FMT,
... )
Value:
DC_PUBLIC static void NS(TYPE, debug)(TYPE const* self, dc_debug_fmt fmt, FILE* stream) { \
(void)fmt; \
fprintf(stream, FMT, (double)*self); \
}

Definition at line 49 of file debug.h.

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 }

◆ DC_DEFAULT_DEBUG

#define DC_DEFAULT_DEBUG   _DC_DEFAULT_DEBUG

Definition at line 126 of file debug.h.

◆ DC_DERIVE_DEBUG

#define DC_DERIVE_DEBUG ( TYPE)
Value:
DC_PUBLIC static TYPE NS(TYPE, DEBUG)(TYPE const* self, dc_debug_fmt fmt, FILE* stream) { \
fprintf(stream, DC_STRINGIFY(TYPE) "@%p {\n", self); \
NS(TYPE, REFLECT)(_DC_DERIVE_DEBUG_MEMBER); \
fmt = dc_debug_fmt_scope_end(fmt); \
}
#define _DC_DERIVE_DEBUG_MEMBER(MEMBER_TYPE, MEMBER_NAME)
Definition debug.h:30
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50

Definition at line 35 of file debug.h.

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 }

◆ DC_NO_DEBUG

#define DC_NO_DEBUG   PRIV(no_debug)

Definition at line 22 of file debug.h.

◆ DC_TRAIT_DEBUGABLE

#define DC_TRAIT_DEBUGABLE ( SELF)
Value:
DC_REQUIRE_METHOD(void, SELF, debug, (SELF const*, dc_debug_fmt fmt, FILE*))
#define DC_REQUIRE_METHOD(ret, obj, name, args)
Definition require.h:18

The debug trait Inspired by rust's {:?} pretty print, any data structure can be dumped to a stream.

  • For debugging (not display) (so contains ptr information, internals)
  • Prints the types (so users can cross reference)
  • Print allocators (for debugging memory issues)
  • Overrideable per data structure

Definition at line 19 of file debug.h.

19#define DC_TRAIT_DEBUGABLE(SELF) \
20 DC_REQUIRE_METHOD(void, SELF, debug, (SELF const*, dc_debug_fmt fmt, FILE*))

Function Documentation

◆ dc_string_const_debug()

DC_PUBLIC void dc_string_const_debug ( char const *const * string,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 63 of file debug.h.

64 {
65 (void)fmt;
66 fprintf(stream, "char*@%p \"%s\"", *string, *string);
67}

◆ dc_string_debug()

DC_PUBLIC void dc_string_debug ( char *const * string,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 58 of file debug.h.

58 {
59 (void)fmt;
60 fprintf(stream, "char*@%p \"%s\"", *string, *string);
61}

◆ dc_void_const_ptr_debug()

DC_PUBLIC void dc_void_const_ptr_debug ( void const *const * ptr,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 74 of file debug.h.

75 {
76 (void)fmt;
77 fprintf(stream, "void const*@%p", *ptr);
78}

◆ dc_void_ptr_debug()

DC_PUBLIC void dc_void_ptr_debug ( void *const * ptr,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 69 of file debug.h.

69 {
70 (void)fmt;
71 fprintf(stream, "void*@%p", *ptr);
72}

◆ no_debug()

DC_INTERNAL void PRIV no_debug ( void const * self,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 24 of file debug.h.

24 {
25 (void)self;
26 (void)fmt;
27 fprintf(stream, "(no DEBUG function provided)");
28}