Derive-C
Loading...
Searching...
No Matches
trait.c
Go to the documentation of this file.
1#include <stdint.h>
2
3#include <derive-c/prelude.h>
4
5#define Foo_REFLECT(F) \
6 F(uint32_t, bing) \
7 F(char, baz) \
8 F(char, zing)
9
11DC_DERIVE_EQ(Foo)
14
15static void example_trait(DC_LOGGER* parent) {
16 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
17
18 Foo f = {.bing = 23, .baz = 'c', .zing = 'z'};
19 Foo g = {.bing = 23, .baz = 'c', .zing = 'z'};
20
21 DC_LOG(log, DC_INFO, "checking equality of f and g");
22 DC_ASSERT(Foo_eq(&f, &g));
23 DC_LOG(log, DC_INFO, "checking ordering of f and g");
24 DC_ASSERT(!Foo_gt(&f, &g) && !Foo_lt(&f, &g));
25
26 DC_LOG(log, DC_INFO, "cloning f to z and incrementing bing");
27 Foo z = Foo_clone(&f);
28 z.bing += 10;
29 DC_LOG(log, DC_INFO, "checking z > f");
30 DC_ASSERT(Foo_gt(&z, &f));
31}
32
33int main() {
35 root = NS(DC_LOGGER,
36 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
37 (dc_log_id){"trait"});
38
39 example_trait(&root);
40 return 0;
41}
#define DC_DERIVE_CLONE(TYPE)
Definition clone.h:18
#define DC_DERIVE_STRUCT(TYPE,...)
Definition derive.h:13
#define DC_DERIVE_EQ(TYPE)
Definition eq.h:26
#define DC_LOGGER
Definition file.h:168
#define NS(pre, post)
Definition namespace.h:14
#define DC_DERIVE_ORD(TYPE)
Definition ord.h:26
#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
static void example_trait(DC_LOGGER *parent)
Definition trait.c:15
int main()
Definition trait.c:33
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8