-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor_ctx.h
108 lines (85 loc) · 3.79 KB
/
preprocessor_ctx.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
105
106
107
108
/*
* CREATED BY SHPEGUN60
*
* PREPROCESSOR CONTEXT CAPTURE/GETTING LIBRARY
*/
#ifndef __PREPROCESSOR_CTX_H__
#define __PREPROCESSOR_CTX_H__ 1
#include "preprocessor_map.h"
/*********************************************************************************************************************
* PREPROCESSOR CONTEXT TYPE
*
* PREPROCESSOR_CTX_TYPE(ctx) // expands to --> void* const * const ctx
*/
#define PREPROCESSOR_CTX_TYPE_GET() void* const * const
#define PREPROCESSOR_CTX_TYPE(name) PREPROCESSOR_CTX_TYPE_GET() name
#define PREPROCESSOR_CTX_MUTABLE_TYPE_GET() void**
#define PREPROCESSOR_CTX_MUTABLE_TYPE(name) PREPROCESSOR_CTX_MUTABLE_TYPE_GET() name
#define PREPROCESSOR_CTX_TYPE_CAST(name) ((void* const) name)
/*********************************************************************************************************************
* PREPROCESSOR CONTEXT CAPTURE
*
* uint8_t val1 = 254;
* uint16_t val2 = 12896;
* uint32_t val3 = 40000000;
*
* PREPROCESSOR_CTX_CAPTURE({&dta, &val1, &val2, &val3}) // expands to --> (void* []){&dta, &val1, &val2, &val3}
*/
#define PREPROCESSOR_CTX_CAPTURE(...) (void* const []) __VA_ARGS__
/*********************************************************************************************************************
* PREPROCESSOR CONTEXT GET
*
* PREPROCESSOR_CTX_GET(ctx,
* uint8_t * val1,
* uint16_t * val2,
* uint32_t * val3)
*
* // expands to-->
* uint8_t * val1 = ctx[0];
* uint16_t * val2 = ctx[1];
* uint32_t * val3 = ctx[2];
*
* * ATTENTION!!! For deep optimization you need write "const" in pointers -->
* PREPROCESSOR_CTX_GET(ctx,
* uint8_t * const val1,
* uint16_t * const val2,
* uint32_t * const val3)
* OR BETTER:
* PREPROCESSOR_CTX_GET(ctx,
* const uint8_t * const val1,
* const uint16_t * const val2,
* const uint32_t * const val3)
*
*/
#define PREPROCESSOR_CTX_GET(name, ...) PREPROCESSOR_MAP_CTX(name, PREPROCESSOR_EMPTY, __VA_ARGS__)
#define PREPROCESSOR_MAP_CTX(name, sep, ...) \
PREPROCESSOR_IF(PREPROCESSOR_ARGS_NOT_EMPTY(__VA_ARGS__))(PREPROCESSOR_EVAL(PREPROCESSOR_MAP_CTX_INNER(name, sep, 0, __VA_ARGS__)))
#define PREPROCESSOR_MAP_CTX_INNER(name, sep, counter, cur_val, ...) \
cur_val = name[counter]; \
PREPROCESSOR_IF(PREPROCESSOR_ARGS_NOT_EMPTY(__VA_ARGS__)) ( \
PREPROCESSOR_DEFER2(_PREPROCESSOR_MAP_CTX_INNER)()(name, sep, PREPROCESSOR_INC(counter), __VA_ARGS__) \
)
#define _PREPROCESSOR_MAP_CTX_INNER() PREPROCESSOR_MAP_CTX_INNER
/*********************************************************************************************************************
EXAMPLE:
#include <stdint.h>
void foo(PREPROCESSOR_CTX_TYPE(ctx))
{
PREPROCESSOR_CTX_GET(ctx,
const uint8_t * const val1,
const uint16_t * const val2,
const uint32_t * const val3)
}
int main()
{
uint8_t val1 = 254;
uint16_t val2 = 12896;
uint32_t val3 = 40000000;
foo(PREPROCESSOR_CTX_CAPTURE({
&val1,
&val2,
&val3
}));
}
*/
#endif /* __PREPROCESSOR_CTX_H__ */