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