Skip to content

Commit 241e98d

Browse files
authored
Merge pull request #7 from Erlkoenig90/msvc_varmacro_fix
Add MSVC support by using a DEFER macro and increasing the evaluation depth.
2 parents c5189e6 + ce493dd commit 241e98d

File tree

3 files changed

+470
-11
lines changed

3 files changed

+470
-11
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,23 @@ Other interesting combinations include:
134134
135135
Special thanks to [pfultz2](https://github.com/pfultz2/Cloak/wiki/Is-the-C-preprocessor-Turing-complete%3F) for inventing the EVAL idea.
136136
137+
## Usage
138+
139+
The [map.h](map.h) file defines several "map"-style macros for different use cases. The [example.c](example.c) file shows how to use them. A quick overview is:
140+
141+
C-Code | Evaluates to
142+
-------| ------------
143+
MAP(f,a,b,c) | f(a) f(b) f(c)
144+
MAP_LIST(f,a,b,c) | f(a), f(b), f(c)
145+
MAP_UD(f,x,a,b,c) | f(a,x) f(b,x) f(c,x)
146+
MAP_LIST_UD(f,x,a,b,c) | f(a,x), f(b,x), f(c,x)
147+
MAP_UD_I(f,x,a,b,c) | f(a,x,0) f(b,x,1) f(c,x,2)
148+
MAP_LIST_UD_I(f,x,a,b,c) | f(a,x,0), f(b,x,1), f(c,x,2)
149+
150+
The maximum number of variadic arguments for all macros is 365.
151+
137152
## See Also
138153
139154
C++ users may be intersted in the ['visit_struct' library](https://github.com/cbeck88/visit_struct),
140-
which uses a version of this macro to implement structure visitors in C++11.
155+
which uses a version of this macro to implement structure visitors in C++11. The ['µSer' library](https://github.com/Erlkoenig90/uSer)
156+
uses another variant to define serializable structs in C++17.

example.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@ MAP(CALL, ('a'), ('b'), ('c'))
1818
/* Test `MAP_LIST` with parentheses in the arguments: */
1919
#define CALL_LIST(x) putchar x
2020
MAP_LIST(CALL_LIST, ('a'), ('b'), ('c'));
21+
22+
/* Pass user-defined data to each invocation */
23+
#define PRINT_STREAM(x, stream) fprintf (stream, "%d\n", x);
24+
MAP_UD(PRINT_STREAM, stderr, 1, 2, 3, 4, 5)
25+
26+
/* Pass user-defined data to each list invocation */
27+
#define INVOKE(x, fun) fun(x)
28+
int arr [] = { MAP_LIST_UD(INVOKE, map, 1, 2, 3, 4, 5) };
29+
30+
/* Pass user-defined data and an index to each invocation */
31+
#define PRINT_STREAM_I(x, stream, index) fprintf (stream, "%d: %d\n", index, x);
32+
MAP_UD_I(PRINT_STREAM_I, stderr, 1, 2, 3, 4, 5)
33+
34+
/* Pass user-defined data and an index to each list invocation */
35+
#define INVOKE_I(x, fun, index) fun(index, x)
36+
int arr2 [] = { MAP_LIST_UD_I(INVOKE_I, map2, 1, 2, 3, 4, 5) };

0 commit comments

Comments
 (0)