Last active
December 17, 2015 00:39
-
-
Save jbandela/5522815 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
/* This will let macros expand before concating them */ | |
#define CROSS_COMPILER_INTERFACE_PRIMITIVE_CAT(x, y) x ## y | |
#define CROSS_COMPILER_INTERFACE_CAT(x, y) CROSS_COMPILER_INTERFACE_PRIMITIVE_CAT(x, y) | |
// Adapted from http://stackoverflow.com/questions/11920577/casting-all-parameters-passed-in-macro-using-va-args | |
// With adaptations for MSVC from BOOST_PP | |
/* This counts the number of args */ | |
#define CROSS_COMPILER_INTERFACE_NARGS_SEQ(_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,N,...) N | |
#ifdef _MSC_VER | |
#define CROSS_COMPILER_INTERFACE_NARGS(...) CROSS_COMPILER_INTERFACE_CAT(CROSS_COMPILER_INTERFACE_NARGS_SEQ(__VA_ARGS__,40, 39, 38, 37, 36, 35, 34, 33, 32, 31,30 , 29, 28, 27, 26, 25, 24, 23, 22, 21, 20,19 ,18 ,17 , 16 , 15 , 14 , 13, 12, 11, 10 , 9, 8, 7, 6, 5, 4, 3, 2, 1),) | |
#else | |
#define CROSS_COMPILER_INTERFACE_NARGS(...) CROSS_COMPILER_INTERFACE_NARGS_SEQ(__VA_ARGS__,40, 39, 38, 37, 36, 35, 34, 33, 32, 31,30 , 29, 28, 27, 26, 25, 24, 23, 22, 21, 20,19 ,18 ,17 , 16 , 15 , 14 , 13, 12, 11, 10 , 9, 8, 7, 6, 5, 4, 3, 2, 1) | |
#endif | |
/* This will call a macro on each argument passed in */ | |
#ifdef _MSC_VER | |
#define CROSS_COMPILER_INTERFACE_APPLY(macro, ...) CROSS_COMPILER_INTERFACE_CAT(CROSS_COMPILER_INTERFACE_APPLY_, CROSS_COMPILER_INTERFACE_NARGS(__VA_ARGS__))CROSS_COMPILER_INTERFACE_CAT(( macro, __VA_ARGS__),) | |
#else | |
#define CROSS_COMPILER_INTERFACE_APPLY(macro, ...) CROSS_COMPILER_INTERFACE_CAT(CROSS_COMPILER_INTERFACE_APPLY_, CROSS_COMPILER_INTERFACE_NARGS(__VA_ARGS__))( macro, __VA_ARGS__) | |
#endif | |
#define CROSS_COMPILER_INTERFACE_APPLY_1(m, x1) m(x1) | |
#define CROSS_COMPILER_INTERFACE_APPLY_2(m, x1, x2) m(x1), m(x2) | |
#define CROSS_COMPILER_INTERFACE_APPLY_3(m, x1, x2, x3) m(x1), m(x2), m(x3) | |
#define CROSS_COMPILER_INTERFACE_APPLY_4(m, x1, x2, x3, x4) m(x1), m(x2), m(x3), m(x4) | |
#define CROSS_COMPILER_INTERFACE_APPLY_5(m, x1, x2, x3, x4, x5) m(x1), m(x2), m(x3), m(x4), m(x5) | |
#define CROSS_COMPILER_INTERFACE_APPLY_6(m, x1, x2, x3, x4, x5, x6) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6) | |
#define CROSS_COMPILER_INTERFACE_APPLY_7(m, x1, x2, x3, x4, x5, x6, x7) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6), m(x7) | |
#define CROSS_COMPILER_INTERFACE_APPLY_8(m, x1, x2, x3, x4, x5, x6, x7, x8) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6), m(x7), m(x8) | |
#define CROSS_COMPILER_INTERFACE_STR(x) #x | |
#define FOO_EACH(x) CROSS_COMPILER_INTERFACE_STR(x) | |
#define FOO(...) CROSS_COMPILER_INTERFACE_APPLY(FOO_EACH, __VA_ARGS__) | |
int main(){ | |
int k = CROSS_COMPILER_INTERFACE_NARGS(a,b,c,d); | |
const char* fields[] = {FOO(a,b,c,d)}; | |
for(int i =0; i<sizeof(fields)/sizeof(fields[0]); i++){ | |
std::cout << fields[i] << " "; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment