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 DC_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) { return *self; }
23 #define ITEM_EQ item_eq
24static bool ITEM_EQ(item_t const* a, item_t const* b) { return a->x == b->x; }
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
49DC_PUBLIC static SELF NS(SELF, from)(ITEM value) { return (SELF){.item = value, .present = true}; }
50
51DC_PUBLIC static SELF NS(SELF, empty)() { return (SELF){.present = false}; }
52
53DC_PUBLIC static 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
61DC_PUBLIC static ITEM* NS(SELF, get)(SELF* self) {
62 DC_ASSUME(self);
63 if (self->present) {
64 return &self->item;
65 }
66 return NULL;
67}
68
69DC_PUBLIC static 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
77DC_PUBLIC static 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
85DC_PUBLIC static 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
93DC_PUBLIC static bool NS(SELF, is_present)(SELF const* self) {
94 DC_ASSUME(self);
95 return self->present;
96}
97
98DC_PUBLIC static void NS(SELF, delete)(SELF* self) {
99 DC_ASSUME(self);
100 if (self->present) {
101 ITEM_DELETE(&self->item);
102 }
103}
104
105DC_PUBLIC static 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
119DC_PUBLIC static void NS(SELF, debug)(SELF* self, dc_debug_fmt fmt, FILE* stream) {
120 if (self->present) {
121 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p { ", (void*)self);
122 fmt = dc_debug_fmt_scope_begin(fmt);
123 ITEM_DEBUG(&self->item, fmt, stream);
124 fprintf(stream, " }");
125 } else {
126 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p { NONE }", (void*)self);
127 }
128}
129
130#undef ITEM_DEBUG
131#undef ITEM_EQ
132#undef ITEM_CLONE
133#undef ITEM_DELETE
134#undef ITEM
135
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212
#define ITEM
Definition template.h:36
static DC_PUBLIC bool empty(ITER const *iter)
Definition template.h:349
static DC_PUBLIC SELF clone(SELF const *self)
Definition template.h:215
static DC_PUBLIC bool get(SELF const *self, INDEX_TYPE index)
Definition template.h:80
#define ITEM_DELETE
Definition template.h:20
#define ITEM_CLONE
Definition template.h:22
#define ITEM_DEBUG
Definition template.h:24
#define SELF
Definition def.h:52
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define DC_PUBLIC
Definition namespace.h:25
#define NS(pre, post)
Definition namespace.h:14
#define DC_EXPAND_STRING(NAME)
Definition namespace.h:6
#define DC_ASSUME(expr,...)
Definition panic.h:57
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:11
A queue comprised of an extendable circular buffer.
Definition template.h:16
int x
Definition template.h:17
static DC_PUBLIC ITEM get_value_or(SELF const *self, ITEM const default_value)
Definition template.h:85
static DC_PUBLIC bool replace(SELF *self, ITEM value)
Definition template.h:105
static DC_PUBLIC ITEM const * get_const_or(SELF const *self, ITEM const *default_value)
Definition template.h:77
static DC_PUBLIC bool is_present(SELF const *self)
Definition template.h:93
static DC_PUBLIC SELF from(ITEM value)
Definition template.h:49
#define ITEM_EQ
Definition template.h:23
static DC_PUBLIC ITEM const * get_const(SELF const *self)
Definition template.h:69
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108