From ddd540203ed935fc387400f98ce1987f6aeeda7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Tue, 30 May 2023 03:25:37 +0300 Subject: init --- optional.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 optional.h diff --git a/optional.h b/optional.h new file mode 100644 index 0000000..ca1f732 --- /dev/null +++ b/optional.h @@ -0,0 +1,29 @@ +/** + * optional.h - some macros for optional types + * + * 2023 Yaroslav de la Peña Smirnov + * + */ +#ifndef OPTIONAL_H +#define OPTIONAL_H + +#include + +#define OPTIONAL(T, name) struct { bool has; T data; } name + +#define OPTNONE { .has = false } +#define OPTSOME(d) { .has = true, .data = d } + +#define optional_set_none(opt) opt.has = false + +#define optional_set_some(opt, d) \ + (opt.has = true, opt.data = d) + +#define optional_has(opt) (opt.has) + +#define optional_hasnt(opt) (!opt.has) + +#define optional_unwrap(src, dst) \ + (optional_has(src) ? (dst = src.data, true) : (false)) + +#endif -- cgit v1.2.3