Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MSVC support by using a DEFER macro and increasing the evaluation depth. #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,23 @@ Other interesting combinations include:

Special thanks to [pfultz2](https://github.com/pfultz2/Cloak/wiki/Is-the-C-preprocessor-Turing-complete%3F) for inventing the EVAL idea.

## Usage

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:

C-Code | Evaluates to
-------| ------------
MAP(f,a,b,c) | f(a) f(b) f(c)
MAP_LIST(f,a,b,c) | f(a), f(b), f(c)
MAP_UD(f,x,a,b,c) | f(a,x) f(b,x) f(c,x)
MAP_LIST_UD(f,x,a,b,c) | f(a,x), f(b,x), f(c,x)
MAP_UD_I(f,x,a,b,c) | f(a,x,0) f(b,x,1) f(c,x,2)
MAP_LIST_UD_I(f,x,a,b,c) | f(a,x,0), f(b,x,1), f(c,x,2)

The maximum number of variadic arguments for all macros is 365.

## See Also

C++ users may be intersted in the ['visit_struct' library](https://github.com/cbeck88/visit_struct),
which uses a version of this macro to implement structure visitors in C++11.
which uses a version of this macro to implement structure visitors in C++11. The ['µSer' library](https://github.com/Erlkoenig90/uSer)
uses another variant to define serializable structs in C++17.
16 changes: 16 additions & 0 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ 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'));

/* Pass user-defined data to each invocation */
#define PRINT_STREAM(x, stream) fprintf (stream, "%d\n", x);
MAP_UD(PRINT_STREAM, stderr, 1, 2, 3, 4, 5)

/* Pass user-defined data to each list invocation */
#define INVOKE(x, fun) fun(x)
int arr [] = { MAP_LIST_UD(INVOKE, map, 1, 2, 3, 4, 5) };

/* Pass user-defined data and an index to each invocation */
#define PRINT_STREAM_I(x, stream, index) fprintf (stream, "%d: %d\n", index, x);
MAP_UD_I(PRINT_STREAM_I, stderr, 1, 2, 3, 4, 5)

/* Pass user-defined data and an index to each list invocation */
#define INVOKE_I(x, fun, index) fun(index, x)
int arr2 [] = { MAP_LIST_UD_I(INVOKE_I, map2, 1, 2, 3, 4, 5) };
Loading