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