Last active
February 19, 2016 15:40
-
-
Save kbauer/09e2a4fb916a9524374f to your computer and use it in GitHub Desktop.
A c preprocessor macro allowing to iterate over an argument list `A1, A2, ...` to produce `M(1, A1), M(2, A2), ...` or the reverse. Depends on ONEARG.h, see https://gist.github.com/kbauer/d651bae52ab2f72b8d1e . Tested with `gcc -E -std=c99`.
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
// Usage: _FORVARARGS(M,A0[,A1[,A2[...]]]) | |
// | |
// Equivalent to M(0,A0) M(1,A1) M(2,A2) ... | |
// Supports up to 32 Varargs. Emacs lisp code for generating included. | |
// Define the macro M(N, ARG) to generate useful code. | |
// | |
// Usage: _FORVARARGS_R(M,A0[,A1[,A2[...]]]) | |
// | |
// Equivalent to ... M(2,A2) M(1,A1) M(0,A0), i.e. reverse of _FORVARARGS. | |
// | |
// Fixed to work with standard C macros, but as a consequence needs | |
// at least one argument. | |
#define _FORVARARGS(M,...) _IFONEARG(M(0,__VA_ARGS__),_FORVARARGS_00_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_R(M,...) _IFONEARG(M(0,__VA_ARGS__),_FORVARARGS_00_R(M,__VA_ARGS__),__VA_ARGS__) | |
/* Generating code (emacs lisp=: | |
(cl-loop initially (progn (search-forward "*/") (push-mark) (search-forward "/*END*/") | |
(delete-region (mark) (point))) with initpoint = (point) | |
for i from 0 to 31 do (insert (format (concat | |
"\n#define _FORVARARGS_%02d_F(M,A,...) M(%2d,A) _IFONEARG(M(%2d,__VA_ARGS__),_FORVARARGS_%02d_F(M,__VA_ARGS__),__VA_ARGS__)" | |
"\n#define _FORVARARGS_%02d_R(M,A,...) _IFONEARG(M(%2d,__VA_ARGS__),_FORVARARGS_%02d_R(M,__VA_ARGS__),__VA_ARGS__) M(%2d,A)") | |
i i (+ 1 i) (+ 1 i) i (+ 1 i) (+ 1 i) i)) finally do (insert (format (concat | |
"\n#define _FORVARARGS_%02d_F(...) __FORVARARGS_TOO_MANY_VARARGS__" | |
"\n#define _FORVARARGS_%02d_R(...) __FORVARARGS_TOO_MANY_VARARGS__" | |
"\n/*END*/") i i)) finally do (goto-char initpoint)) */ | |
#define _FORVARARGS_00_F(M,A,...) M( 0,A) _IFONEARG(M( 1,__VA_ARGS__),_FORVARARGS_01_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_00_R(M,A,...) _IFONEARG(M( 1,__VA_ARGS__),_FORVARARGS_01_R(M,__VA_ARGS__),__VA_ARGS__) M( 0,A) | |
#define _FORVARARGS_01_F(M,A,...) M( 1,A) _IFONEARG(M( 2,__VA_ARGS__),_FORVARARGS_02_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_01_R(M,A,...) _IFONEARG(M( 2,__VA_ARGS__),_FORVARARGS_02_R(M,__VA_ARGS__),__VA_ARGS__) M( 1,A) | |
#define _FORVARARGS_02_F(M,A,...) M( 2,A) _IFONEARG(M( 3,__VA_ARGS__),_FORVARARGS_03_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_02_R(M,A,...) _IFONEARG(M( 3,__VA_ARGS__),_FORVARARGS_03_R(M,__VA_ARGS__),__VA_ARGS__) M( 2,A) | |
#define _FORVARARGS_03_F(M,A,...) M( 3,A) _IFONEARG(M( 4,__VA_ARGS__),_FORVARARGS_04_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_03_R(M,A,...) _IFONEARG(M( 4,__VA_ARGS__),_FORVARARGS_04_R(M,__VA_ARGS__),__VA_ARGS__) M( 3,A) | |
#define _FORVARARGS_04_F(M,A,...) M( 4,A) _IFONEARG(M( 5,__VA_ARGS__),_FORVARARGS_05_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_04_R(M,A,...) _IFONEARG(M( 5,__VA_ARGS__),_FORVARARGS_05_R(M,__VA_ARGS__),__VA_ARGS__) M( 4,A) | |
#define _FORVARARGS_05_F(M,A,...) M( 5,A) _IFONEARG(M( 6,__VA_ARGS__),_FORVARARGS_06_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_05_R(M,A,...) _IFONEARG(M( 6,__VA_ARGS__),_FORVARARGS_06_R(M,__VA_ARGS__),__VA_ARGS__) M( 5,A) | |
#define _FORVARARGS_06_F(M,A,...) M( 6,A) _IFONEARG(M( 7,__VA_ARGS__),_FORVARARGS_07_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_06_R(M,A,...) _IFONEARG(M( 7,__VA_ARGS__),_FORVARARGS_07_R(M,__VA_ARGS__),__VA_ARGS__) M( 6,A) | |
#define _FORVARARGS_07_F(M,A,...) M( 7,A) _IFONEARG(M( 8,__VA_ARGS__),_FORVARARGS_08_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_07_R(M,A,...) _IFONEARG(M( 8,__VA_ARGS__),_FORVARARGS_08_R(M,__VA_ARGS__),__VA_ARGS__) M( 7,A) | |
#define _FORVARARGS_08_F(M,A,...) M( 8,A) _IFONEARG(M( 9,__VA_ARGS__),_FORVARARGS_09_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_08_R(M,A,...) _IFONEARG(M( 9,__VA_ARGS__),_FORVARARGS_09_R(M,__VA_ARGS__),__VA_ARGS__) M( 8,A) | |
#define _FORVARARGS_09_F(M,A,...) M( 9,A) _IFONEARG(M(10,__VA_ARGS__),_FORVARARGS_10_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_09_R(M,A,...) _IFONEARG(M(10,__VA_ARGS__),_FORVARARGS_10_R(M,__VA_ARGS__),__VA_ARGS__) M( 9,A) | |
#define _FORVARARGS_10_F(M,A,...) M(10,A) _IFONEARG(M(11,__VA_ARGS__),_FORVARARGS_11_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_10_R(M,A,...) _IFONEARG(M(11,__VA_ARGS__),_FORVARARGS_11_R(M,__VA_ARGS__),__VA_ARGS__) M(10,A) | |
#define _FORVARARGS_11_F(M,A,...) M(11,A) _IFONEARG(M(12,__VA_ARGS__),_FORVARARGS_12_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_11_R(M,A,...) _IFONEARG(M(12,__VA_ARGS__),_FORVARARGS_12_R(M,__VA_ARGS__),__VA_ARGS__) M(11,A) | |
#define _FORVARARGS_12_F(M,A,...) M(12,A) _IFONEARG(M(13,__VA_ARGS__),_FORVARARGS_13_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_12_R(M,A,...) _IFONEARG(M(13,__VA_ARGS__),_FORVARARGS_13_R(M,__VA_ARGS__),__VA_ARGS__) M(12,A) | |
#define _FORVARARGS_13_F(M,A,...) M(13,A) _IFONEARG(M(14,__VA_ARGS__),_FORVARARGS_14_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_13_R(M,A,...) _IFONEARG(M(14,__VA_ARGS__),_FORVARARGS_14_R(M,__VA_ARGS__),__VA_ARGS__) M(13,A) | |
#define _FORVARARGS_14_F(M,A,...) M(14,A) _IFONEARG(M(15,__VA_ARGS__),_FORVARARGS_15_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_14_R(M,A,...) _IFONEARG(M(15,__VA_ARGS__),_FORVARARGS_15_R(M,__VA_ARGS__),__VA_ARGS__) M(14,A) | |
#define _FORVARARGS_15_F(M,A,...) M(15,A) _IFONEARG(M(16,__VA_ARGS__),_FORVARARGS_16_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_15_R(M,A,...) _IFONEARG(M(16,__VA_ARGS__),_FORVARARGS_16_R(M,__VA_ARGS__),__VA_ARGS__) M(15,A) | |
#define _FORVARARGS_16_F(M,A,...) M(16,A) _IFONEARG(M(17,__VA_ARGS__),_FORVARARGS_17_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_16_R(M,A,...) _IFONEARG(M(17,__VA_ARGS__),_FORVARARGS_17_R(M,__VA_ARGS__),__VA_ARGS__) M(16,A) | |
#define _FORVARARGS_17_F(M,A,...) M(17,A) _IFONEARG(M(18,__VA_ARGS__),_FORVARARGS_18_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_17_R(M,A,...) _IFONEARG(M(18,__VA_ARGS__),_FORVARARGS_18_R(M,__VA_ARGS__),__VA_ARGS__) M(17,A) | |
#define _FORVARARGS_18_F(M,A,...) M(18,A) _IFONEARG(M(19,__VA_ARGS__),_FORVARARGS_19_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_18_R(M,A,...) _IFONEARG(M(19,__VA_ARGS__),_FORVARARGS_19_R(M,__VA_ARGS__),__VA_ARGS__) M(18,A) | |
#define _FORVARARGS_19_F(M,A,...) M(19,A) _IFONEARG(M(20,__VA_ARGS__),_FORVARARGS_20_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_19_R(M,A,...) _IFONEARG(M(20,__VA_ARGS__),_FORVARARGS_20_R(M,__VA_ARGS__),__VA_ARGS__) M(19,A) | |
#define _FORVARARGS_20_F(M,A,...) M(20,A) _IFONEARG(M(21,__VA_ARGS__),_FORVARARGS_21_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_20_R(M,A,...) _IFONEARG(M(21,__VA_ARGS__),_FORVARARGS_21_R(M,__VA_ARGS__),__VA_ARGS__) M(20,A) | |
#define _FORVARARGS_21_F(M,A,...) M(21,A) _IFONEARG(M(22,__VA_ARGS__),_FORVARARGS_22_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_21_R(M,A,...) _IFONEARG(M(22,__VA_ARGS__),_FORVARARGS_22_R(M,__VA_ARGS__),__VA_ARGS__) M(21,A) | |
#define _FORVARARGS_22_F(M,A,...) M(22,A) _IFONEARG(M(23,__VA_ARGS__),_FORVARARGS_23_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_22_R(M,A,...) _IFONEARG(M(23,__VA_ARGS__),_FORVARARGS_23_R(M,__VA_ARGS__),__VA_ARGS__) M(22,A) | |
#define _FORVARARGS_23_F(M,A,...) M(23,A) _IFONEARG(M(24,__VA_ARGS__),_FORVARARGS_24_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_23_R(M,A,...) _IFONEARG(M(24,__VA_ARGS__),_FORVARARGS_24_R(M,__VA_ARGS__),__VA_ARGS__) M(23,A) | |
#define _FORVARARGS_24_F(M,A,...) M(24,A) _IFONEARG(M(25,__VA_ARGS__),_FORVARARGS_25_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_24_R(M,A,...) _IFONEARG(M(25,__VA_ARGS__),_FORVARARGS_25_R(M,__VA_ARGS__),__VA_ARGS__) M(24,A) | |
#define _FORVARARGS_25_F(M,A,...) M(25,A) _IFONEARG(M(26,__VA_ARGS__),_FORVARARGS_26_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_25_R(M,A,...) _IFONEARG(M(26,__VA_ARGS__),_FORVARARGS_26_R(M,__VA_ARGS__),__VA_ARGS__) M(25,A) | |
#define _FORVARARGS_26_F(M,A,...) M(26,A) _IFONEARG(M(27,__VA_ARGS__),_FORVARARGS_27_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_26_R(M,A,...) _IFONEARG(M(27,__VA_ARGS__),_FORVARARGS_27_R(M,__VA_ARGS__),__VA_ARGS__) M(26,A) | |
#define _FORVARARGS_27_F(M,A,...) M(27,A) _IFONEARG(M(28,__VA_ARGS__),_FORVARARGS_28_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_27_R(M,A,...) _IFONEARG(M(28,__VA_ARGS__),_FORVARARGS_28_R(M,__VA_ARGS__),__VA_ARGS__) M(27,A) | |
#define _FORVARARGS_28_F(M,A,...) M(28,A) _IFONEARG(M(29,__VA_ARGS__),_FORVARARGS_29_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_28_R(M,A,...) _IFONEARG(M(29,__VA_ARGS__),_FORVARARGS_29_R(M,__VA_ARGS__),__VA_ARGS__) M(28,A) | |
#define _FORVARARGS_29_F(M,A,...) M(29,A) _IFONEARG(M(30,__VA_ARGS__),_FORVARARGS_30_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_29_R(M,A,...) _IFONEARG(M(30,__VA_ARGS__),_FORVARARGS_30_R(M,__VA_ARGS__),__VA_ARGS__) M(29,A) | |
#define _FORVARARGS_30_F(M,A,...) M(30,A) _IFONEARG(M(31,__VA_ARGS__),_FORVARARGS_31_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_30_R(M,A,...) _IFONEARG(M(31,__VA_ARGS__),_FORVARARGS_31_R(M,__VA_ARGS__),__VA_ARGS__) M(30,A) | |
#define _FORVARARGS_31_F(M,A,...) M(31,A) _IFONEARG(M(32,__VA_ARGS__),_FORVARARGS_32_F(M,__VA_ARGS__),__VA_ARGS__) | |
#define _FORVARARGS_31_R(M,A,...) _IFONEARG(M(32,__VA_ARGS__),_FORVARARGS_32_R(M,__VA_ARGS__),__VA_ARGS__) M(31,A) | |
#define _FORVARARGS_32_F(...) __FORVARARGS_TOO_MANY_VARARGS__ | |
#define _FORVARARGS_32_R(...) __FORVARARGS_TOO_MANY_VARARGS__ | |
/*END*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment