-
Notifications
You must be signed in to change notification settings - Fork 0
/
serde-macros.h
40 lines (36 loc) · 1.46 KB
/
serde-macros.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
#pragma once
// Source:
// https://codecraft.co/2014/11/25/variadic-macros-tricks/
// Accept any number of args >= N, but expand to just the Nth one.
// Here, N == 8.
#define _GET_NTH_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
// Define some macros to help us create overrides based on the
// arity of a for-each-style macro.
#define _fe_0(_call, ...)
#define _fe_1(_call, x) _call(x)
#define _fe_2(_call, x, ...) _call(x) _fe_1(_call, __VA_ARGS__)
#define _fe_3(_call, x, ...) _call(x) _fe_2(_call, __VA_ARGS__)
#define _fe_4(_call, x, ...) _call(x) _fe_3(_call, __VA_ARGS__)
#define _fe_5(_call, x, ...) _call(x) _fe_4(_call, __VA_ARGS__)
#define _fe_6(_call, x, ...) _call(x) _fe_5(_call, __VA_ARGS__)
#define _fe_7(_call, x, ...) _call(x) _fe_6(_call, __VA_ARGS__)
#define _fe_8(_call, x, ...) _call(x) _fe_7(_call, __VA_ARGS__)
/**
* Provide a for-each construct for variadic macros. Supports up
* to 8 args.
*
* Example usage1:
* #define FWD_DECLARE_CLASS(cls) class cls;
* CALL_MACRO_X_FOR_EACH(FWD_DECLARE_CLASS, Foo, Bar)
*
* Example usage 2:
* #define START_NS(ns) namespace ns {
* #define END_NS(ns) }
* #define MY_NAMESPACES System, Net, Http
* CALL_MACRO_X_FOR_EACH(START_NS, MY_NAMESPACES)
* typedef foo int;
* CALL_MACRO_X_FOR_EACH(END_NS, MY_NAMESPACES)
*/
#define CALL_MACRO_X_FOR_EACH(x, ...) \
_GET_NTH_ARG("ignored", ##__VA_ARGS__, \
_fe_8, _fe_7, _fe_6, _fe_5, _fe_4, _fe_3, _fe_2, _fe_1, _fe_0)(x, ##__VA_ARGS__)