Derive-C
Loading...
Searching...
No Matches
template.h
Go to the documentation of this file.
1
2// for access to errors or successes
3
6#if !defined(SKIP_INCLUDES)
7 #include "includes.h"
8#endif
9
11
12#if !defined OK
13 #if !defined DC_PLACEHOLDERS
14TEMPLATE_ERROR("No OK")
15 #endif
16 #define OK ok_t
17typedef struct {
18 int x;
19} OK;
20#endif
21
22#if !defined OK_DELETE
23 #define OK_DELETE DC_NO_DELETE
24#endif
25
26#if !defined OK_EQ
27 #define OK_EQ DC_MEM_EQ
28#endif
29
30#if !defined OK_CLONE
31 #define OK_CLONE DC_COPY_CLONE
32#endif
33
34#if !defined OK_DEBUG
35 #define OK_DEBUG DC_DEFAULT_DEBUG
36#endif
37
38#if !defined ERROR
39 #if !defined DC_PLACEHOLDERS
40TEMPLATE_ERROR("No ERROR")
41 #endif
42 #define ERROR error_t
43typedef struct {
44 int x;
45} ERROR;
46#endif
47
48#if !defined ERROR_THROW
49 #define ERROR_THROW(_) DC_PANIC("Unexpected error in " DC_EXPAND_STRING(SELF))
50#endif
51
52#if !defined ERROR_DELETE
53 #define ERROR_DELETE DC_NO_DELETE
54#endif
55
56#if !defined ERROR_EQ
57 #define ERROR_EQ DC_MEM_EQ
58#endif
59
60#if !defined ERROR_CLONE
61 #define ERROR_CLONE DC_COPY_CLONE
62#endif
63
64#if !defined ERROR_DEBUG
65 #define ERROR_DEBUG DC_DEFAULT_DEBUG
66#endif
67
68typedef struct {
69 bool success;
70 union {
73 } result;
74} SELF;
75
76DC_PUBLIC static SELF NS(SELF, from_ok)(OK value) {
77 return (SELF){
78 .success = true,
79 .result =
80 {
81 .ok = value,
82 },
83 };
84}
85
87 return (SELF){
88 .success = false,
89 .result =
90 {
91 .error = value,
92 },
93 };
94}
95
96DC_PUBLIC static bool NS(SELF, is_error)(SELF const* self) { return !self->success; }
97
98DC_PUBLIC static OK const* NS(SELF, strict_get_const)(SELF const* self) {
99 if (!self->success) {
100 ERROR_THROW(&self->result.error);
101 DC_UNREACHABLE("Error has already thrown");
102 }
103 return &self->result.ok;
104}
105
106DC_PUBLIC static OK const* NS(SELF, get_okay)(SELF const* self) {
107 if (self->success) {
108 return &self->result.ok;
109 }
110 return NULL;
111}
112
113DC_PUBLIC static ERROR const* NS(SELF, get_error)(SELF const* self) {
114 if (!self->success) {
115 return &self->result.error;
116 }
117 return NULL;
118}
119
120DC_PUBLIC static void NS(SELF, debug)(SELF const* self, dc_debug_fmt fmt, FILE* stream) {
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}
135
136DC_PUBLIC static void NS(SELF, delete)(SELF* self) {
137 if (self->success) {
138 OK_DELETE(&self->result.ok);
139 } else {
140 ERROR_DELETE(&self->result.error);
141 }
142}
143
144#undef ERROR_DEBUG
145#undef ERROR_CLONE
146#undef ERROR_EQ
147#undef ERROR_DELETE
148#undef ERROR_THROW
149#undef ERROR
150
151#undef OK_DEBUG
152#undef OK_CLONE
153#undef OK_EQ
154#undef OK_DELETE
155#undef OK
156
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212
#define TEMPLATE_ERROR(...)
With the user provided name, even in nested templates.
Definition def.h:56
#define SELF
Definition def.h:52
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 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_UNREACHABLE(...)
Definition panic.h:44
int x
Definition template.h:44
int x
Definition template.h:18
bool success
Definition template.h:69
OK ok
Definition template.h:71
ERROR error
Definition template.h:72
Debug format helpers for debug printin data structures.
Definition fmt.h:11
#define ERROR_THROW(_)
Definition template.h:49
#define ERROR_DELETE
Definition template.h:53
static DC_PUBLIC OK const * strict_get_const(SELF const *self)
Definition template.h:98
static DC_PUBLIC SELF from_error(ERROR value)
Definition template.h:86
static DC_PUBLIC bool is_error(SELF const *self)
Definition template.h:96
#define ERROR_DEBUG
Definition template.h:65
#define OK_DELETE
Definition template.h:23
static DC_PUBLIC OK const * get_okay(SELF const *self)
Definition template.h:106
#define ERROR
Definition template.h:42
#define OK_DEBUG
Definition template.h:35
#define OK
A simple result type, using the (already) optional pointer type.
Definition template.h:16
static DC_PUBLIC SELF from_ok(OK value)
Definition template.h:76
static DC_PUBLIC ERROR const * get_error(SELF const *self)
Definition template.h:113
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108