aboutsummaryrefslogtreecommitdiff
path: root/example.c
blob: fb717181daa131b8b4122e30b8cd18cfbe160eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>

#include "optional.h"

OPTIONALDEC(int);
OPTIONALDEC(float);

struct mine {
	OPTIONAL(int) a;
	OPTIONAL(int) b;
};

OPTIONAL(int)
ithas(void)
{
	OPTIONAL(int) r = OPTSOME(10);
	return r;
}

OPTIONAL(int)
ithasnt(void)
{
	OPTIONAL(int) r = OPTNONE;
	return r;
}

void
optput(OPTIONAL(float) *dst, float src)
{
	opt_set_some(*dst, 6.9);
}

int
main(int argc, const char *argv[])
{
	int         a, b;
	struct mine mine = {
		.a = OPTSOME(69),
		.b = OPTNONE,
	};
	OPTIONAL(float) y = OPTNONE;
	OPTIONAL(float) z = OPTSOME(1.1);
	float uy, uz, xy = opt_default(y, 6.9), xz = opt_default(z, 6.9);
	float e = y.has ? y.data : 6.9;
	float f = opt_default(y, 6.9);

	opt_set_some(mine.b, 10);
	opt_set_none(mine.a);

	if (opt_unwrap(mine.a, a)) {
		printf("has a = %d\n", a);
	} else {
		printf("doesn't have a\n");
	}
	if (opt_unwrap(mine.b, b)) {
		printf("has b = %d\n", b);
	} else {
		printf("doesn't have b\n");
	}
	if (opt_unwrap(y, uy)) {
		printf("has y = %f\n", uy);
	} else {
		printf("doesn't have y\n");
	}
	if (opt_unwrap(z, uz)) {
		printf("has z = %f\n", uz);
	} else {
		printf("doesn't have z\n");
	}

	printf("xy = %f, xz = %f\n", xy, xz);
}