Derive-C
Loading...
Searching...
No Matches
template.h
Go to the documentation of this file.
1
2// for access
3
5#if !defined(SKIP_INCLUDES)
6 #include "includes.h"
7#endif
8
10
11#if !defined ITEM
12 #if !defined PLACEHOLDERS
13 #error "The contained type must be defined"
14 #endif
15typedef struct {
16 int x;
17} item_t;
18 #define ITEM item_t
19 #define ITEM_DELETE item_delete
20static void ITEM_DELETE(item_t* self);
21 #define ITEM_CLONE item_clone
22static item_t ITEM_CLONE(item_t const* self);
23 #define ITEM_EQ item_eq
24static bool ITEM_EQ(item_t const* a, item_t const* b);
25 #define ITEM_DEBUG item_debug
26static void ITEM_DEBUG(ITEM const* self, dc_debug_fmt fmt, FILE* stream);
27#endif
28
29#if !defined ITEM_DELETE
30 #define ITEM_DELETE(value)
31#endif
32
33#if !defined ITEM_CLONE
34 #define ITEM_CLONE(value) (*(value))
35#endif
36
37#if !defined ITEM_DEBUG
38 #define ITEM_DEBUG DC_DEFAULT_DEBUG
39#endif
40
41typedef struct {
42 union {
44 };
45 bool present;
46 dc_gdb_marker derive_c_option;
47} SELF;
48
49static SELF NS(SELF, from)(ITEM value) { return (SELF){.item = value, .present = true}; }
50
51static SELF NS(SELF, empty)() { return (SELF){.present = false}; }
52
53static SELF NS(SELF, clone)(SELF const* self) {
54 DC_ASSUME(self);
55 if (self->present) {
56 return NS(SELF, from)(ITEM_CLONE(&self->item));
57 }
58 return NS(SELF, empty)();
59}
60
61static ITEM* NS(SELF, get)(SELF* self) {
62 DC_ASSUME(self);
63 if (self->present) {
64 return &self->item;
65 }
66 return NULL;
67}
68
69static ITEM const* NS(SELF, get_const)(SELF const* self) {
70 DC_ASSUME(self);
71 if (self->present) {
72 return &self->item;
73 }
74 return NULL;
75}
76
77static ITEM const* NS(SELF, get_const_or)(SELF const* self, ITEM const* default_value) {
78 DC_ASSUME(self);
79 if (self->present) {
80 return &self->item;
81 }
82 return default_value;
83}
84
85static ITEM NS(SELF, get_value_or)(SELF const* self, ITEM const default_value) {
86 DC_ASSUME(self);
87 if (self->present) {
88 return self->item;
89 }
90 return default_value;
91}
92
93static bool NS(SELF, is_present)(SELF const* self) {
94 DC_ASSUME(self);
95 return self->present;
96}
97
98static void NS(SELF, delete)(SELF* self) {
99 DC_ASSUME(self);
100 if (self->present) {
101 ITEM_DELETE(&self->item);
102 }
103}
104
105static bool NS(SELF, replace)(SELF* self, ITEM value) {
106 DC_ASSUME(self);
107 bool was_present;
108 if (self->present) {
109 ITEM_DELETE(&self->item);
110 was_present = true;
111 } else {
112 was_present = false;
113 }
114 self->item = value;
115 self->present = true;
116 return was_present;
117}
118
119static void NS(SELF, debug)(SELF* self, dc_debug_fmt fmt, FILE* stream) {
120 if (self->present) {
121 fprintf(stream, EXPAND_STRING(SELF) "@%p {\n", self);
122 fmt = dc_debug_fmt_scope_begin(fmt);
123 dc_debug_fmt_print(fmt, stream, "value: ");
124 ITEM_DEBUG(&self->item, fmt, stream);
125 fprintf(stream, ",\n");
126 fmt = dc_debug_fmt_scope_end(fmt);
127 dc_debug_fmt_print(fmt, stream, "}");
128 } else {
129 fprintf(stream, EXPAND_STRING(SELF) "@%p { NONE }", self);
130 }
131}
132
133#undef ITEM_DEBUG
134#undef ITEM_EQ
135#undef ITEM_CLONE
136#undef ITEM_DELETE
137#undef ITEM
138
static void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:62
#define ITEM
Definition template.h:63
static bool empty(ITER const *iter)
Definition template.h:346
static SELF clone(SELF const *self)
Definition template.h:215
#define ITEM_DELETE
Definition template.h:20
#define ITEM_CLONE
Definition template.h:22
#define ITEM_DEBUG
Definition template.h:24
dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:39
dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:33
static void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:22
#define EXPAND_STRING(NAME)
Definition namespace.h:8
#define NS(pre, post)
Definition namespace.h:4
static nullalloc get()
Definition null.h:14
#define DC_ASSUME(expr,...)
Definition panic.h:56
#define SELF
Definition def.h:52
bool present
Definition template.h:45
dc_gdb_marker derive_c_option
Definition template.h:46
ITEM item
Definition template.h:43
Debug format helpers for debug printin data structures.
Definition fmt.h:10
A queue comprised of an extendable circular buffer.
Definition template.h:16
static ITEM get_value_or(SELF const *self, ITEM const default_value)
Definition template.h:85
static bool is_present(SELF const *self)
Definition template.h:93
static bool replace(SELF *self, ITEM value)
Definition template.h:105
static ITEM const * get_const_or(SELF const *self, ITEM const *default_value)
Definition template.h:77
#define ITEM_EQ
Definition template.h:23
static SELF from(ITEM value)
Definition template.h:49
static ITEM const * get_const(SELF const *self)
Definition template.h:69
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107