Derive-C
Loading...
Searching...
No Matches
option.c File Reference
#include <derive-c/prelude.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <derive-c/utils/option/template.h>

Go to the source code of this file.

Data Structures

struct  example_data

Macros

#define ITEM   struct example_data
#define ITEM_DELETE   example_data_delete
#define ITEM_CLONE   example_data_clone
#define ITEM_DEBUG   example_data_debug
#define NAME   example_option

Functions

static void example_data_delete (struct example_data *self)
static struct example_data example_data_clone (struct example_data const *self)
static void example_data_debug (struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
static void example_option_example (DC_LOGGER *parent)
int main ()

Macro Definition Documentation

◆ ITEM

#define ITEM   struct example_data

Definition at line 33 of file option.c.

◆ ITEM_CLONE

#define ITEM_CLONE   example_data_clone

Definition at line 35 of file option.c.

◆ ITEM_DEBUG

#define ITEM_DEBUG   example_data_debug

Definition at line 36 of file option.c.

◆ ITEM_DELETE

#define ITEM_DELETE   example_data_delete

Definition at line 34 of file option.c.

◆ NAME

#define NAME   example_option

Definition at line 37 of file option.c.

Function Documentation

◆ example_data_clone()

struct example_data example_data_clone ( struct example_data const * self)
static
Examples
utils/option.c.

Definition at line 17 of file option.c.

17 {
18 return (struct example_data){
19 .value = self->value,
20 .description = strdup(self->description),
21 };
22}

◆ example_data_debug()

void example_data_debug ( struct example_data const * self,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 24 of file option.c.

24 {
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}
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_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 DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ example_data_delete()

void example_data_delete ( struct example_data * self)
static

Definition at line 15 of file option.c.

15{ free(self->description); }
char * description
Definition arena.c:13

◆ example_option_example()

void example_option_example ( DC_LOGGER * parent)
static
Examples
utils/option.c.

Definition at line 40 of file option.c.

40 {
41 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
42
43 DC_LOG(log, DC_INFO, "creating present option");
44 DC_SCOPED(example_option)
45 present_option = example_option_from(
46 (struct example_data){.value = 42, .description = strdup("A present option")});
47
48 DC_LOG(log, DC_INFO, "creating empty option");
49 DC_SCOPED(example_option) empty_option = example_option_empty();
50
51 DC_LOG(log, DC_INFO, "present: %s", DC_DEBUG(example_option_debug, &present_option));
52 DC_LOG(log, DC_INFO, "empty: %s", DC_DEBUG(example_option_debug, &empty_option));
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 DC_LOG(log, DC_INFO, "replacing empty option");
66 bool was_present = example_option_replace(
67 &empty_option,
68 (struct example_data){.value = 100, .description = strdup("Replaced value")});
69 DC_ASSERT(!was_present);
70 DC_ASSERT(example_option_is_present(&empty_option));
71
72 struct example_data const* replaced_data = example_option_get_const(&empty_option);
73 DC_ASSERT(replaced_data != NULL);
74 DC_ASSERT(replaced_data->value == 100);
75 DC_ASSERT(strcmp(replaced_data->description, "Replaced value") == 0);
76
77 DC_LOG(log, DC_INFO, "all assertions passed");
78}
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
#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
int value
Definition arena.c:14
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8

◆ main()

int main ( )

Definition at line 80 of file option.c.

80 {
82 root = NS(DC_LOGGER,
83 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
84 (dc_log_id){"option"});
85
87 return 0;
88}
#define NS(pre, post)
Definition namespace.h:14
static void example_option_example(DC_LOGGER *parent)
Definition option.c:40