aboutsummaryrefslogtreecommitdiff
path: root/optional.h
blob: ca1f732a8040d86a02537614ac39fc7c59b5a1c0 (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
/**
 * optional.h - some macros for optional types
 *
 * 2023 Yaroslav de la Peña Smirnov
 *
 */
#ifndef OPTIONAL_H
#define OPTIONAL_H

#include <stdbool.h>

#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