Derive-C
Loading...
Searching...
No Matches
template.h File Reference

Go to the source code of this file.

Data Structures

struct  item_t
 A queue comprised of an extendable circular buffer. More...
struct  SELF
 An allocator that prints to stdout when it allocates or frees memory. More...

Macros

#define ITEM   item_t
#define ITEM_DELETE   item_delete
#define ITEM_CLONE   item_clone
#define ITEM_EQ   item_eq
#define ITEM_DEBUG   item_debug

Functions

static void ITEM_DELETE (item_t *self)
static item_t ITEM_CLONE (item_t const *self)
static bool ITEM_EQ (item_t const *a, item_t const *b)
static void ITEM_DEBUG (ITEM const *self, dc_debug_fmt fmt, FILE *stream)
static SELF from (ITEM value)
static SELF empty ()
static SELF clone (SELF const *self)
static ITEMget (SELF *self)
static ITEM const * get_const (SELF const *self)
static ITEM const * get_const_or (SELF const *self, ITEM const *default_value)
static ITEM get_value_or (SELF const *self, ITEM const default_value)
static bool is_present (SELF const *self)
static void delete (SELF *self)
static bool replace (SELF *self, ITEM value)
static void debug (SELF *self, dc_debug_fmt fmt, FILE *stream)

Macro Definition Documentation

◆ ITEM

#define ITEM   item_t

Definition at line 18 of file template.h.

◆ ITEM_CLONE

#define ITEM_CLONE   item_clone

Definition at line 21 of file template.h.

◆ ITEM_DEBUG

#define ITEM_DEBUG   item_debug

Definition at line 25 of file template.h.

◆ ITEM_DELETE

#define ITEM_DELETE   item_delete

Definition at line 19 of file template.h.

◆ ITEM_EQ

#define ITEM_EQ   item_eq

Definition at line 23 of file template.h.

Function Documentation

◆ clone()

SELF clone ( SELF const * self)
static

Definition at line 53 of file template.h.

53 {
54 DC_ASSUME(self);
55 if (self->present) {
56 return NS(SELF, from)(ITEM_CLONE(&self->item));
57 }
58 return NS(SELF, empty)();
59}
static bool empty(ITER const *iter)
Definition template.h:346
#define ITEM_CLONE
Definition template.h:22
#define NS(pre, post)
Definition namespace.h:4
#define DC_ASSUME(expr,...)
Definition panic.h:56
#define SELF
Definition def.h:52
static SELF from(ITEM value)
Definition template.h:49

◆ debug()

void debug ( SELF * self,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 119 of file template.h.

119 {
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}
#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
bool present
Definition template.h:45
ITEM item
Definition template.h:43
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107

◆ delete()

void delete ( SELF * self)
static

Definition at line 98 of file template.h.

98 {
99 DC_ASSUME(self);
100 if (self->present) {
101 ITEM_DELETE(&self->item);
102 }
103}
#define ITEM_DELETE
Definition template.h:20

◆ empty()

SELF empty ( )
static

Definition at line 51 of file template.h.

51{ return (SELF){.present = false}; }

◆ from()

SELF from ( ITEM value)
static

Definition at line 49 of file template.h.

49{ return (SELF){.item = value, .present = true}; }

◆ get()

ITEM * get ( SELF * self)
static

Definition at line 61 of file template.h.

61 {
62 DC_ASSUME(self);
63 if (self->present) {
64 return &self->item;
65 }
66 return NULL;
67}

◆ get_const()

ITEM const * get_const ( SELF const * self)
static

Definition at line 69 of file template.h.

69 {
70 DC_ASSUME(self);
71 if (self->present) {
72 return &self->item;
73 }
74 return NULL;
75}

◆ get_const_or()

ITEM const * get_const_or ( SELF const * self,
ITEM const * default_value )
static

Definition at line 77 of file template.h.

77 {
78 DC_ASSUME(self);
79 if (self->present) {
80 return &self->item;
81 }
82 return default_value;
83}

◆ get_value_or()

ITEM get_value_or ( SELF const * self,
ITEM const default_value )
static

Definition at line 85 of file template.h.

85 {
86 DC_ASSUME(self);
87 if (self->present) {
88 return self->item;
89 }
90 return default_value;
91}

◆ is_present()

bool is_present ( SELF const * self)
static

Definition at line 93 of file template.h.

93 {
94 DC_ASSUME(self);
95 return self->present;
96}

◆ ITEM_CLONE()

item_t ITEM_CLONE ( item_t const * self)
static

◆ ITEM_DEBUG()

void ITEM_DEBUG ( ITEM const * self,
dc_debug_fmt fmt,
FILE * stream )
static

◆ ITEM_DELETE()

void ITEM_DELETE ( item_t * self)
static

◆ ITEM_EQ()

bool ITEM_EQ ( item_t const * a,
item_t const * b )
static

◆ replace()

bool replace ( SELF * self,
ITEM value )
static

Definition at line 105 of file template.h.

105 {
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}