Derive-C
Loading...
Searching...
No Matches
option.c
Go to the documentation of this file.
1
4
5#include <stdlib.h>
6#include <string.h>
7
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}
24
25#define ITEM struct complex_data
26#define ITEM_DELETE complex_data_delete
27#define ITEM_CLONE complex_data_clone
28#define NAME complex_data_option
30
32 complex_data_option opt = complex_data_option_empty();
33 ASSERT(!complex_data_option_is_present(&opt));
34
35 // when accessing a value, you get a pointer. Not present = NULL
36 ASSERT(!complex_data_option_get(&opt));
37 ASSERT(!complex_data_option_get_const(&opt));
38
39 bool was_present_1 = complex_data_option_replace(
40 &opt, (struct complex_data){.x = 42, .y = 3.14, .description = strdup("A complex data")});
41 ASSERT(!was_present_1);
42
43 ASSERT(complex_data_option_is_present(&opt));
44 ASSERT(complex_data_option_get(&opt));
45
46 bool was_present_2 = complex_data_option_replace(
47 &opt,
48 (struct complex_data){.x = 100, .y = 2.71, .description = strdup("Another complex data")});
49 ASSERT(was_present_2);
50
51 complex_data_option_delete(&opt);
52}
53
54int main() { option_example(); }
static void free(SELF *self, void *ptr)
Definition template.h:58
#define ASSERT(expr,...)
Definition macros.h:42
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:31
int main()
Definition option.c:54
double y
Definition option.c:12
char * description
Definition option.c:13