summaryrefslogtreecommitdiff
path: root/lib/macro.h
blob: 24fc3393da02543bdc451587349239acd16f1b97 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* 
 *	BIRD Macro Tricks
 *
 *	(c) 2018 Jan Maria Matejka <mq@jmq.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 *
 *	Contains useful but dirty macro tricks:
 *		MACRO_CONCAT(a, b)	-> concatenates a##b
 *		MACRO_BOOL(x)		-> convert 0 to 0, anything else to 1
 *		MACRO_IFELSE(b)(true-branch)(false-branch)
 *					-> b shall be 0 or 1; expands to the appropriate branch
 *		MACRO_ISEMPTY(...)	-> 1 for empty argument list, 0 otherwise
 *		MACRO_FOREACH(func, ...)
 *					-> calling FOREACH(func, a, b, c, d) expands to
 *						func(a) func(b) func(c) func(d)
 *		MACRO_RPACK(func, terminator, ...)
 *					-> packs the list into recursive calls:
 *						func(func(func(func(terminator, a), b), c), d)
 */

#ifndef _BIRD_MACRO_H_
#define _BIRD_MACRO_H_

/* What to do with args */
#define MACRO_DROP(...)
#define MACRO_UNPAREN(...) __VA_ARGS__
#define MACRO_SEP(a, b, sep)  a sep b

/* Aliases for some special chars */
#define MACRO_COMMA ,
#define MACRO_LPAREN (
#define MACRO_RPAREN )
#define MACRO_LPAREN_() (
#define MACRO_RPAREN_() )

/* Multiple expansion trick */
#define MACRO_EXPAND0(...) __VA_ARGS__
#define MACRO_EXPAND1(...) MACRO_EXPAND0(MACRO_EXPAND0(__VA_ARGS__))
#define MACRO_EXPAND2(...) MACRO_EXPAND1(MACRO_EXPAND1(__VA_ARGS__))
#define MACRO_EXPAND3(...) MACRO_EXPAND2(MACRO_EXPAND2(__VA_ARGS__))
#define MACRO_EXPAND(...) MACRO_EXPAND3(MACRO_EXPAND3(__VA_ARGS__))

/* Deferring expansion in the expansion trick */
#define MACRO_EMPTY()
#define MACRO_DEFER(t) t MACRO_EMPTY()
#define MACRO_DEFER2(t) t MACRO_EMPTY MACRO_EMPTY()()
#define MACRO_DEFER3(t) t MACRO_EMPTY MACRO_EMPTY MACRO_EMPTY()()()

/* Token concatenation */
#define MACRO_CONCAT(prefix, ...) prefix##__VA_ARGS__
#define MACRO_CONCAT_AFTER(...) MACRO_CONCAT(__VA_ARGS__)

/* Get first or second argument only */
#define MACRO_FIRST(a, ...) a
#define MACRO_SECOND(a, b, ...) b
#define MACRO_SECOND_OR_ZERO(...) MACRO_SECOND(__VA_ARGS__, 0,)

/* Macro Boolean auxiliary macros */
#define MACRO_BOOL_CHECK_0 ~, 1
#define MACRO_BOOL_NEG(x) MACRO_SECOND_OR_ZERO(MACRO_CONCAT(MACRO_BOOL_CHECK_, x))

#define MACRO_BOOL_NOT_0  1
#define MACRO_BOOL_NOT_1  0

/* Macro Boolean negation */
#define MACRO_NOT(x) MACRO_CONCAT(MACRO_BOOL_NOT_, x)

/* Convert anything to bool (anything -> 1, 0 -> 0) */
#define MACRO_BOOL(x) MACRO_NOT(MACRO_BOOL_NEG(x))

/*
 * Macro If/Else condition
 * Usage: MACRO_IFELSE(condition)(true-branch)(false-branch)
 * Expands to true-branch if condition is true, otherwise to false-branch.
 */
#define MACRO_IFELSE(b) MACRO_CONCAT(MACRO_IFELSE_, b)
#define MACRO_IFELSE_0(...) MACRO_UNPAREN
#define MACRO_IFELSE_1(...) __VA_ARGS__ MACRO_DROP

/* Auxiliary macros for MACRO_FOREACH */
#define MACRO_ISLAST(...) MACRO_BOOL_NEG(MACRO_FIRST(MACRO_ISLAST_CHECK __VA_ARGS__)())
#define MACRO_ISLAST_CHECK() 0

#define MACRO_FOREACH_EXPAND(call, a, ...) MACRO_IFELSE(MACRO_ISLAST(__VA_ARGS__))(call(a))(call(a) MACRO_DEFER2(MACRO_FOREACH_PAREN)()(call, __VA_ARGS__))
#define MACRO_FOREACH_PAREN() MACRO_FOREACH_EXPAND

#define MACRO_RPACK_EXPAND(call, terminator, a, ...) MACRO_IFELSE(MACRO_ISLAST(__VA_ARGS__))(call(terminator, a))(call(MACRO_DEFER2(MACRO_RPACK_PAREN)()(call, terminator, __VA_ARGS__), a))
#define MACRO_RPACK_PAREN() MACRO_RPACK_EXPAND
/*
 * Call the first argument for each following:
 * MACRO_FOREACH(func, a, b, c, d) expands to func(a) func(b) func(c) func(d).
 * It supports also macros as func.
 */
#define MACRO_FOREACH(call, ...) MACRO_EXPAND(MACRO_FOREACH_EXPAND(call, __VA_ARGS__))
#define MACRO_RPACK(call, terminator, ...) MACRO_EXPAND(MACRO_RPACK_EXPAND(call, terminator, __VA_ARGS__))

#endif