taco-db  0.1.0
macro_map.h
Go to the documentation of this file.
1 /*
2  * Created by William Swanson in 2012.
3  *
4  * I, William Swanson, dedicate this work to the public domain.
5  * I waive all rights to the work worldwide under copyright law,
6  * including all related and neighboring rights,
7  * to the extent allowed by law.
8  *
9  * You can copy, modify, distribute and perform the work,
10  * even for commercial purposes, all without asking permission.
11  */
12 
13 #ifndef EXTERNAL_MACRO_MAP_H_INCLUDED
14 #define EXTERNAL_MACRO_MAP_H_INCLUDED
15 
16 #define EVAL0(...) __VA_ARGS__
17 #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
18 #define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
19 #define EVAL3(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
20 #define EVAL4(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
21 #define EVAL(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
22 
23 #define MAP_END(...)
24 #define MAP_OUT
25 #define MAP_COMMA ,
26 
27 #define MAP_GET_END2() 0, MAP_END
28 #define MAP_GET_END1(...) MAP_GET_END2
29 #define MAP_GET_END(...) MAP_GET_END1
30 #define MAP_NEXT0(test, next, ...) next MAP_OUT
31 #define MAP_NEXT1(test, next) MAP_NEXT0(test, next, 0)
32 #define MAP_NEXT(test, next) MAP_NEXT1(MAP_GET_END test, next)
33 
34 #define MAP0(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP1)(f, peek, __VA_ARGS__)
35 #define MAP1(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP0)(f, peek, __VA_ARGS__)
36 
37 #define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0)
38 #define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next)
39 
40 #define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__)
41 #define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__)
42 
46 #define MAP(f, ...) EVAL(MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
47 
52 #define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
53 
54 #endif // EXTERNAL_MACRO_MAP_H_INCLUDED