Derive-C
Loading...
Searching...
No Matches
option.c
Go to the documentation of this file.
1
4
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
11 int x;
12 double y;
14};
15
16void complex_data_delete(struct complex_data* self) { free(self->description); }
17struct complex_data complex_data_clone(struct complex_data const* self) {
18 return (struct complex_data){
19 .x = self->x,
20 .y = self->y,
21 .description = strdup(self->description),
22 };
23}
24void complex_data_debug(struct complex_data const* self, dc_debug_fmt fmt, FILE* stream) {
25 fprintf(stream, "complex_data@%p {\n", self);
26 fmt = dc_debug_fmt_scope_begin(fmt);
27 dc_debug_fmt_print(fmt, stream, "x: %d,\n", self->x);
28 dc_debug_fmt_print(fmt, stream, "y: %lf,\n", self->y);
29 dc_debug_fmt_print(fmt, stream, "description: %lf,\n", self->description);
30 fmt = dc_debug_fmt_scope_end(fmt);
31 dc_debug_fmt_print(fmt, stream, "}");
32}
33
34#define ITEM struct complex_data
35#define ITEM_DELETE complex_data_delete
36#define ITEM_CLONE complex_data_clone
37#define ITEM_DEBUG complex_data_debug
38#define NAME complex_data_option
40
42 complex_data_option opt = complex_data_option_empty();
43 DC_ASSERT(!complex_data_option_is_present(&opt));
44
45 // when accessing a value, you get a pointer. Not present = NULL
46 DC_ASSERT(!complex_data_option_get(&opt));
47 DC_ASSERT(!complex_data_option_get_const(&opt));
48
49 complex_data_option_debug(&opt, dc_debug_fmt_new(), stdout);
50
51 bool was_present_1 = complex_data_option_replace(
52 &opt, (struct complex_data){.x = 42, .y = 3.14, .description = strdup("A complex data")});
53 DC_ASSERT(!was_present_1);
54
55 DC_ASSERT(complex_data_option_is_present(&opt));
56 DC_ASSERT(complex_data_option_get(&opt));
57
58 complex_data_option_debug(&opt, dc_debug_fmt_new(), stdout);
59
60 bool was_present_2 = complex_data_option_replace(
61 &opt,
62 (struct complex_data){.x = 100, .y = 2.71, .description = strdup("Another complex data")});
63 DC_ASSERT(was_present_2);
64
65 complex_data_option_delete(&opt);
66}
67
68int main() { option_example(); }
static void free(SELF *self, void *ptr)
Definition template.h:56
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
static dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:14
void complex_data_debug(struct complex_data const *self, dc_debug_fmt fmt, FILE *stream)
Definition option.c:24
void complex_data_delete(struct complex_data *self)
Definition option.c:16
struct complex_data complex_data_clone(struct complex_data const *self)
Definition option.c:17
void option_example()
Definition option.c:41
int main()
Definition option.c:68
#define DC_ASSERT(expr,...)
Definition panic.h:36
double y
Definition option.c:12
char * description
Definition option.c:13
Debug format helpers for debug printin data structures.
Definition fmt.h:10
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107