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

Go to the source code of this file.

Data Structures

struct  OK
struct  ERROR
struct  SELF
 An allocator that prints to stdout when it allocates or frees memory. More...

Macros

#define OK   ok_t
 A simple result type, using the (already) optional pointer type.
#define OK_DELETE   DC_NO_DELETE
#define OK_EQ   DC_MEM_EQ
#define OK_CLONE   DC_COPY_CLONE
#define OK_DEBUG   DC_DEFAULT_DEBUG
#define ERROR   error_t
#define ERROR_THROW(_)
#define ERROR_DELETE   DC_NO_DELETE
#define ERROR_EQ   DC_MEM_EQ
#define ERROR_CLONE   DC_COPY_CLONE
#define ERROR_DEBUG   DC_DEFAULT_DEBUG

Functions

static SELF from_ok (OK value)
static SELF from_error (ERROR value)
static bool is_error (SELF const *self)
static OK const * strict_get_const (SELF const *self)
static OK const * get_okay (SELF const *self)
static ERROR const * get_error (SELF const *self)
static void debug (SELF const *self, dc_debug_fmt fmt, FILE *stream)
static void delete (SELF *self)

Macro Definition Documentation

◆ ERROR

#define ERROR   error_t

Definition at line 41 of file template.h.

◆ ERROR_CLONE

#define ERROR_CLONE   DC_COPY_CLONE

Definition at line 60 of file template.h.

◆ ERROR_DEBUG

#define ERROR_DEBUG   DC_DEFAULT_DEBUG

Definition at line 64 of file template.h.

◆ ERROR_DELETE

#define ERROR_DELETE   DC_NO_DELETE

Definition at line 52 of file template.h.

◆ ERROR_EQ

#define ERROR_EQ   DC_MEM_EQ

Definition at line 56 of file template.h.

◆ ERROR_THROW

#define ERROR_THROW ( _)
Value:
DC_PANIC("Unexpected error in " EXPAND_STRING(SELF))
#define EXPAND_STRING(NAME)
Definition namespace.h:8
#define DC_PANIC(...)
Definition panic.h:28
#define SELF
Definition def.h:52

Definition at line 48 of file template.h.

◆ OK

#define OK   ok_t

A simple result type, using the (already) optional pointer type.

Definition at line 15 of file template.h.

◆ OK_CLONE

#define OK_CLONE   DC_COPY_CLONE

Definition at line 30 of file template.h.

◆ OK_DEBUG

#define OK_DEBUG   DC_DEFAULT_DEBUG

Definition at line 34 of file template.h.

◆ OK_DELETE

#define OK_DELETE   DC_NO_DELETE

Definition at line 22 of file template.h.

◆ OK_EQ

#define OK_EQ   DC_MEM_EQ

Definition at line 26 of file template.h.

Function Documentation

◆ debug()

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

Definition at line 118 of file template.h.

118 {
119 fprintf(stream, EXPAND_STRING(SELF) "@%p {\n", self);
120 fmt = dc_debug_fmt_scope_begin(fmt);
121 if (self->success) {
122 dc_debug_fmt_print(fmt, stream, "ok: ");
123 OK_DEBUG(&self->result.ok, fmt, stream);
124 fprintf(stream, ",\n");
125 } else {
126 dc_debug_fmt_print(fmt, stream, "error: ");
127 ERROR_DEBUG(&self->result.error, fmt, stream);
128 fprintf(stream, ",\n");
129 }
130 fmt = dc_debug_fmt_scope_end(fmt);
131 dc_debug_fmt_print(fmt, stream, "}");
132}
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 ERROR_DEBUG
Definition template.h:64
#define OK_DEBUG
Definition template.h:34
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107

◆ delete()

void delete ( SELF * self)
static

Definition at line 134 of file template.h.

134 {
135 if (self->success) {
136 OK_DELETE(&self->result.ok);
137 } else {
138 ERROR_DELETE(&self->result.error);
139 }
140}
bool success
Definition template.h:68
union SELF::@126272076271243306031034335105312155373132214073 result
OK ok
Definition template.h:70
ERROR error
Definition template.h:71
#define ERROR_DELETE
Definition template.h:52
#define OK_DELETE
Definition template.h:22

◆ from_error()

SELF from_error ( ERROR value)
static

Definition at line 85 of file template.h.

85 {
86 return (SELF){
87 .success = false,
88 .result =
89 {
90 .error = value,
91 },
92 };
93}

◆ from_ok()

SELF from_ok ( OK value)
static

Definition at line 75 of file template.h.

75 {
76 return (SELF){
77 .success = true,
78 .result =
79 {
80 .ok = value,
81 },
82 };
83}

◆ get_error()

ERROR const * get_error ( SELF const * self)
static

Definition at line 111 of file template.h.

111 {
112 if (!self->success) {
113 return &self->result.error;
114 }
115 return NULL;
116}

◆ get_okay()

OK const * get_okay ( SELF const * self)
static

Definition at line 104 of file template.h.

104 {
105 if (self->success) {
106 return &self->result.ok;
107 }
108 return NULL;
109}

◆ is_error()

bool is_error ( SELF const * self)
static

Definition at line 95 of file template.h.

95{ return !self->success; }

◆ strict_get_const()

OK const * strict_get_const ( SELF const * self)
static

Definition at line 97 of file template.h.

97 {
98 if (!self->success) {
99 ERROR_THROW(&self->result.error);
100 }
101 return &self->result.ok;
102}
#define ERROR_THROW(_)
Definition template.h:48