From 13cf037953a79def598b7842ebdc41a50275bbd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Thu, 22 Jun 2023 03:28:46 +0300 Subject: README, LICENSE text, simple example --- example.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 example.c (limited to 'example.c') diff --git a/example.c b/example.c new file mode 100644 index 0000000..fb71718 --- /dev/null +++ b/example.c @@ -0,0 +1,72 @@ +#include + +#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); +} -- cgit v1.2.3