-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmacro_map.hpp
73 lines (57 loc) · 2.64 KB
/
macro_map.hpp
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
#pragma once
/*
* Created by William Swanson in 2012.
*
* I, William Swanson, dedicate this work to the public domain.
* I waive all rights to the work worldwide under copyright law,
* including all related and neighboring rights,
* to the extent allowed by law.
*
* You can copy, modify, distribute and perform the work,
* even for commercial purposes, all without asking permission.
*/
/*
Example usage:
// Basic `MAP` usage:
#define STRING(x) char const *x##_string = #x;
MAP(STRING, foo, bar, baz)
// Basic `MAP_LIST` usage:
#define PARAM(x) int x
void function(MAP_LIST(PARAM, foo, bar, baz));
// Test `MAP` with parentheses in the arguments:
#define CALL(x) putchar x;
MAP(CALL, ('a'), ('b'), ('c'))
// Test `MAP_LIST` with parentheses in the arguments:
#define CALL_LIST(x) putchar x
MAP_LIST(CALL_LIST, ('a'), ('b'), ('c'));
*/
#define MACRO_EVAL0(...) __VA_ARGS__
#define MACRO_EVAL1(...) MACRO_EVAL0(MACRO_EVAL0(MACRO_EVAL0(__VA_ARGS__)))
#define MACRO_EVAL2(...) MACRO_EVAL1(MACRO_EVAL1(MACRO_EVAL1(__VA_ARGS__)))
#define MACRO_EVAL3(...) MACRO_EVAL2(MACRO_EVAL2(MACRO_EVAL2(__VA_ARGS__)))
#define MACRO_EVAL4(...) MACRO_EVAL3(MACRO_EVAL3(MACRO_EVAL3(__VA_ARGS__)))
#define MACRO_EVAL(...) MACRO_EVAL4(MACRO_EVAL4(MACRO_EVAL4(__VA_ARGS__)))
#define MACRO_MAP_END(...)
#define MACRO_MAP_OUT
#define MACRO_MAP_COMMA ,
#define MACRO_MAP_GET_END2() 0, MACRO_MAP_END
#define MACRO_MAP_GET_END1(...) MACRO_MAP_GET_END2
#define MACRO_MAP_GET_END(...) MACRO_MAP_GET_END1
#define MACRO_MAP_NEXT0(test, next, ...) next MACRO_MAP_OUT
#define MACRO_MAP_NEXT1(test, next) MACRO_MAP_NEXT0(test, next, 0)
#define MACRO_MAP_NEXT(test, next) MACRO_MAP_NEXT1(MACRO_MAP_GET_END test, next)
#define MACRO_MAP0(f, x, peek, ...) f(x) MACRO_MAP_NEXT(peek, MACRO_MAP1)(f, peek, __VA_ARGS__)
#define MACRO_MAP1(f, x, peek, ...) f(x) MACRO_MAP_NEXT(peek, MACRO_MAP0)(f, peek, __VA_ARGS__)
#define MACRO_MAP_LIST_NEXT1(test, next) MACRO_MAP_NEXT0(test, MACRO_MAP_COMMA next, 0)
#define MACRO_MAP_LIST_NEXT(test, next) MACRO_MAP_LIST_NEXT1(MACRO_MAP_GET_END test, next)
#define MACRO_MAP_LIST0(f, x, peek, ...) f(x) MACRO_MAP_LIST_NEXT(peek, MACRO_MAP_LIST1)(f, peek, __VA_ARGS__)
#define MACRO_MAP_LIST1(f, x, peek, ...) f(x) MACRO_MAP_LIST_NEXT(peek, MACRO_MAP_LIST0)(f, peek, __VA_ARGS__)
/**
* Applies the function macro `f` to each of the remaining parameters.
*/
#define MACRO_MAP(f, ...) MACRO_EVAL(MACRO_MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
/**
* Applies the function macro `f` to each of the remaining parameters and
* inserts commas between the results.
*/
#define MACRO_MAP_LIST(f, ...) MACRO_EVAL(MACRO_MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))