Derive-C
Loading...
Searching...
No Matches
option.c
Go to the documentation of this file.
1
4
5#include <derive-c/prelude.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10struct example_data {
11 int value;
12 char* description;
13};
14
15static void example_data_delete(struct example_data* self) { free(self->description); }
16
17static struct example_data example_data_clone(struct example_data const* self) {
18 return (struct example_data){
19 .value = self->value,
20 .description = strdup(self->description),
21 };
22}
23
24static void example_data_debug(struct example_data const* self, dc_debug_fmt fmt, FILE* stream) {
25 fprintf(stream, "example_data@%p {\n", (void*)self);
26 fmt = dc_debug_fmt_scope_begin(fmt);
27 dc_debug_fmt_print(fmt, stream, "value: %d,\n", self->value);
28 dc_debug_fmt_print(fmt, stream, "description: %s,\n", self->description);
29 fmt = dc_debug_fmt_scope_end(fmt);
30 dc_debug_fmt_print(fmt, stream, "}");
31}
32
33#define ITEM struct example_data
34#define ITEM_DELETE example_data_delete
35#define ITEM_CLONE example_data_clone
36#define ITEM_DEBUG example_data_debug
37#define NAME example_option
39
42
43 DC_SCOPED(example_option)
44 present_option = example_option_from(
45 (struct example_data){.value = 42, .description = strdup("A present option")});
46
47 DC_SCOPED(example_option) empty_option = example_option_empty();
48
49 example_option_debug(&present_option, dc_debug_fmt_new(), stdout);
50 fprintf(stdout, "\n");
51 example_option_debug(&empty_option, dc_debug_fmt_new(), stdout);
52 fprintf(stdout, "\n");
53
54 DC_ASSERT(example_option_is_present(&present_option));
55 DC_ASSERT(!example_option_is_present(&empty_option));
56
57 struct example_data const* present_data = example_option_get_const(&present_option);
58 DC_ASSERT(present_data != NULL);
59 DC_ASSERT(present_data->value == 42);
60 DC_ASSERT(strcmp(present_data->description, "A present option") == 0);
61
62 struct example_data const* empty_data = example_option_get_const(&empty_option);
63 DC_ASSERT(empty_data == NULL);
64
65 bool was_present = example_option_replace(
66 &empty_option,
67 (struct example_data){.value = 100, .description = strdup("Replaced value")});
68 DC_ASSERT(!was_present);
69 DC_ASSERT(example_option_is_present(&empty_option));
70
71 struct example_data const* replaced_data = example_option_get_const(&empty_option);
72 DC_ASSERT(replaced_data != NULL);
73 DC_ASSERT(replaced_data->value == 100);
74 DC_ASSERT(strcmp(replaced_data->description, "Replaced value") == 0);
75}
76
77int main() {
79 return 0;
80}
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_new()
Definition fmt.h:15
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
static void example_data_debug(struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
Definition option.c:24
static struct example_data example_data_clone(struct example_data const *self)
Definition option.c:17
static void example_data_delete(struct example_data *self)
Definition option.c:15
int main()
Definition option.c:77
static void example_option_example()
Definition option.c:40
#define DC_ASSERT(expr,...)
Definition panic.h:37
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
Debug format helpers for debug printin data structures.
Definition fmt.h:11
int value
Definition arena.c:14
char * description
Definition arena.c:13
#define DC_DEBUG_TRACE
Definition debug.h:17
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108