-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor_cycle.h
104 lines (84 loc) · 3.58 KB
/
preprocessor_cycle.h
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
99
100
101
102
103
104
/*
* CREATED BY SHPEGUN60
*
* MACROCES FOR, WHILE REPEAT
*/
#ifndef __PREPROCESSOR_CYCLE_H__
#define __PREPROCESSOR_CYCLE_H__ 1
/* -- Headers -- */
#include "preprocessor_base.h"
#include "preprocessor_map.h"
#include "preprocessor_if.h"
#include "preprocessor_arguments.h"
#include "preprocessor_arithmetic.h"
#ifdef __cplusplus
extern "C" {
#endif
/* -- Macros -- */
/************************************************************************************************************************************************************
*
* #define FOO(x) x
* PREPROCESSOR_FOR_EACH(FOO, PREPROCESSOR_COMMA_POINT, A,B,C,D)
*
* EXPANDS TO:
* A ; B ; C ; D
*
*
*/
#define PREPROCESSOR_FOR_EACH(op, sep, ...) PREPROCESSOR_MAP(op, sep, __VA_ARGS__)
/************************************************************************************************************************************************************
*
* #define FOO(par, x) par + x
* PREPROCESSOR_FOR_EACH_PARAMETER(USER, FOO, PREPROCESSOR_COMMA_POINT, A,B,C,D)
*
* EXPANDS TO:
* USER + A ; USER + B ; USER + C ; USER + D
*
*/
#define PREPROCESSOR_FOR_EACH_PARAMETER(par, op, sep, ...) PREPROCESSOR_MAP_PARAMETER(par, op, sep, __VA_ARGS__)
/************************************************************************************************************************************************************
* repeat some macro
*
* USE:
* #define M(i, ...) i
* PREPROCESSOR_REPEAT_MACRO(8, M, ~)
*
* Which expands to:
* 0 1 2 3 4 5 6 7
***********************************************************************************************************************************************************
*/
#define PREPROCESSOR_REPEAT_MACRO(...) \
PREPROCESSOR_IF(PREPROCESSOR_ARGS_NOT_EMPTY(__VA_ARGS__))(PREPROCESSOR_EVAL(PREPROCESSOR_REPEAT_MACRO_INNER(__VA_ARGS__)))
#define PREPROCESSOR_REPEAT_MACRO_INNER(count, macro, ...) \
PREPROCESSOR_IF(count) ( \
PREPROCESSOR_DEFER2(_PREPROCESSOR_REPEAT_MACRO_INNER) () (PREPROCESSOR_DEC(count), macro, ##__VA_ARGS__) \
PREPROCESSOR_DEFER2(macro) (PREPROCESSOR_DEC(count), ##__VA_ARGS__) \
)
#define _PREPROCESSOR_REPEAT_MACRO_INNER() PREPROCESSOR_REPEAT_MACRO_INNER
/************************************************************************************************************************************************************
* WHILE macro with predicate
*
* WHILE will keep on applying the op macro until the pred returns true.
*
* USE:
* #define PRED(state, ...) PREPROCESSOR_BOOL(state)
* #define OP(state, ...) PREPROCESSOR_DEC(state), state, __VA_ARGS__
* PREPROCESSOR_WHILE_MACRO(PRED, OP, 8)
*
* Which expands to
* 0, 1, 2, 3, 4, 5, 6, 7, 8,
*
***********************************************************************************************************************************************************
*/
#define PREPROCESSOR_WHILE_MACRO(...) \
PREPROCESSOR_IF(PREPROCESSOR_ARGS_NOT_EMPTY(__VA_ARGS__))(PREPROCESSOR_EVAL(PREPROCESSOR_WHILE_MACRO_INNER(__VA_ARGS__)))
#define PREPROCESSOR_WHILE_MACRO_INNER(pred, op, ...)\
PREPROCESSOR_IF_ELSE(pred(__VA_ARGS__)) ( \
PREPROCESSOR_DEFER2(_PREPROCESSOR_WHILE_MACRO_INNER) () (pred, op, op(__VA_ARGS__)), \
__VA_ARGS__ \
)
#define _PREPROCESSOR_WHILE_MACRO_INNER() PREPROCESSOR_WHILE_MACRO_INNER
#ifdef __cplusplus
}
#endif
#endif /* __PREPROCESSOR_CYCLE_H__ */