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 *)
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 *, dc_debug_fmt, FILE *)
static DC_PUBLIC SELF from (ITEM value)
static DC_PUBLIC SELF empty ()
static DC_PUBLIC SELF clone (SELF const *self)
static DC_PUBLIC ITEMget (SELF *self)
static DC_PUBLIC ITEM const * get_const (SELF const *self)
static DC_PUBLIC ITEM const * get_const_or (SELF const *self, ITEM const *default_value)
static DC_PUBLIC ITEM get_value_or (SELF const *self, ITEM const default_value)
static DC_PUBLIC bool is_present (SELF const *self)
static DC_PUBLIC void delete (SELF *self)
static DC_PUBLIC bool replace (SELF *self, ITEM value)
static DC_PUBLIC 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()

DC_PUBLIC 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 DC_PUBLIC bool empty(ITER const *iter)
Definition template.h:349
#define ITEM_CLONE
Definition template.h:22
#define SELF
Definition def.h:52
#define NS(pre, post)
Definition namespace.h:14
#define DC_ASSUME(expr,...)
Definition panic.h:57
static DC_PUBLIC SELF from(ITEM value)
Definition template.h:49

◆ debug()

DC_PUBLIC 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, 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}
#define ITEM_DEBUG
Definition template.h:24
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define DC_EXPAND_STRING(NAME)
Definition namespace.h:6
bool present
Definition template.h:45
ITEM item
Definition template.h:43
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ delete()

DC_PUBLIC 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()

DC_PUBLIC SELF empty ( )
static

Definition at line 51 of file template.h.

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

◆ from()

DC_PUBLIC SELF from ( ITEM value)
static

Definition at line 49 of file template.h.

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

◆ get()

DC_PUBLIC 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()

DC_PUBLIC 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()

DC_PUBLIC 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()

DC_PUBLIC 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()

DC_PUBLIC 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

Definition at line 22 of file template.h.

22{ return *self; }

◆ ITEM_DEBUG()

void ITEM_DEBUG ( ITEM const * ,
dc_debug_fmt ,
FILE *  )
static

Definition at line 26 of file template.h.

26{}

◆ ITEM_DELETE()

void ITEM_DELETE ( item_t * )
static

Definition at line 20 of file template.h.

20{}

◆ ITEM_EQ()

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

Definition at line 24 of file template.h.

24{ return a->x == b->x; }

◆ replace()

DC_PUBLIC 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}