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 DC_PUBLIC SELF from_ok (OK value)
static DC_PUBLIC SELF from_error (ERROR value)
static DC_PUBLIC bool is_error (SELF const *self)
static DC_PUBLIC OK const * strict_get_const (SELF const *self)
static DC_PUBLIC OK const * get_okay (SELF const *self)
static DC_PUBLIC ERROR const * get_error (SELF const *self)
static DC_PUBLIC void debug (SELF const *self, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC void delete (SELF *self)

Macro Definition Documentation

◆ ERROR

#define ERROR   error_t

Definition at line 42 of file template.h.

◆ ERROR_CLONE

#define ERROR_CLONE   DC_COPY_CLONE

Definition at line 61 of file template.h.

◆ ERROR_DEBUG

#define ERROR_DEBUG   DC_DEFAULT_DEBUG

Definition at line 65 of file template.h.

◆ ERROR_DELETE

#define ERROR_DELETE   DC_NO_DELETE

Definition at line 53 of file template.h.

◆ ERROR_EQ

#define ERROR_EQ   DC_MEM_EQ

Definition at line 57 of file template.h.

◆ ERROR_THROW

#define ERROR_THROW ( _)
Value:
DC_PANIC("Unexpected error in " DC_EXPAND_STRING(SELF))
#define SELF
Definition def.h:52
#define DC_EXPAND_STRING(NAME)
Definition namespace.h:6
#define DC_PANIC(str,...)
Definition panic.h:29

Definition at line 49 of file template.h.

◆ OK

#define OK   ok_t

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

Definition at line 16 of file template.h.

◆ OK_CLONE

#define OK_CLONE   DC_COPY_CLONE

Definition at line 31 of file template.h.

◆ OK_DEBUG

#define OK_DEBUG   DC_DEFAULT_DEBUG

Definition at line 35 of file template.h.

◆ OK_DELETE

#define OK_DELETE   DC_NO_DELETE

Definition at line 23 of file template.h.

◆ OK_EQ

#define OK_EQ   DC_MEM_EQ

Definition at line 27 of file template.h.

Function Documentation

◆ debug()

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

Definition at line 120 of file template.h.

120 {
121 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p {\n", (void*)self);
122 fmt = dc_debug_fmt_scope_begin(fmt);
123 if (self->success) {
124 dc_debug_fmt_print(fmt, stream, "ok: ");
125 OK_DEBUG(&self->result.ok, fmt, stream);
126 fprintf(stream, ",\n");
127 } else {
128 dc_debug_fmt_print(fmt, stream, "error: ");
129 ERROR_DEBUG(&self->result.error, fmt, stream);
130 fprintf(stream, ",\n");
131 }
132 fmt = dc_debug_fmt_scope_end(fmt);
133 dc_debug_fmt_print(fmt, stream, "}");
134}
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define ERROR_DEBUG
Definition template.h:65
#define OK_DEBUG
Definition template.h:35
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ delete()

DC_PUBLIC void delete ( SELF * self)
static

Definition at line 136 of file template.h.

136 {
137 if (self->success) {
138 OK_DELETE(&self->result.ok);
139 } else {
140 ERROR_DELETE(&self->result.error);
141 }
142}
bool success
Definition template.h:69
union SELF::@126272076271243306031034335105312155373132214073 result
OK ok
Definition template.h:71
ERROR error
Definition template.h:72
#define ERROR_DELETE
Definition template.h:53
#define OK_DELETE
Definition template.h:23

◆ from_error()

DC_PUBLIC SELF from_error ( ERROR value)
static

Definition at line 86 of file template.h.

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

◆ from_ok()

DC_PUBLIC SELF from_ok ( OK value)
static

Definition at line 76 of file template.h.

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

◆ get_error()

DC_PUBLIC ERROR const * get_error ( SELF const * self)
static

Definition at line 113 of file template.h.

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

◆ get_okay()

DC_PUBLIC OK const * get_okay ( SELF const * self)
static

Definition at line 106 of file template.h.

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

◆ is_error()

DC_PUBLIC bool is_error ( SELF const * self)
static

Definition at line 96 of file template.h.

96{ return !self->success; }

◆ strict_get_const()

DC_PUBLIC OK const * strict_get_const ( SELF const * self)
static

Definition at line 98 of file template.h.

98 {
99 if (!self->success) {
100 ERROR_THROW(&self->result.error);
101 DC_UNREACHABLE("Error has already thrown");
102 }
103 return &self->result.ok;
104}
#define DC_UNREACHABLE(...)
Definition panic.h:44
#define ERROR_THROW(_)
Definition template.h:49