]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxCALL_FOR_EACH() macro.
authorVáclav Slavík <vslavik@fastmail.fm>
Thu, 1 Nov 2012 16:45:01 +0000 (16:45 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Thu, 1 Nov 2012 16:45:01 +0000 (16:45 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72836 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/cpp.h

index fbcbcaa1feebb242594360d48342f9abfc0f1c68..3d9941920e24742ac2a195134506654c17a4c339 100644 (file)
     #endif
 #endif /* __WXFUNCTION__ already defined */
 
+
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
+    (defined(__cplusplus) && __cplusplus >= 201103L)
+    #define wxHAS_VARIADIC_MACROS
+#elif defined(__GNUC__) && __GNUC__ >= 3
+    #define wxHAS_VARIADIC_MACROS
+#elif defined(_MSC_VER) && _MSC_VER >= 1400
+    #define wxHAS_VARIADIC_MACROS
+#endif
+
+#ifdef wxHAS_VARIADIC_MACROS
+/*
+   wxCALL_FOR_EACH(what, ...) calls the macro from its first argument, what(pos, x),
+   for every remaining argument 'x', with 'pos' being its 1-based index in
+   *reverse* order (with the last argument being numbered 1).
+
+   For example, wxCALL_FOR_EACH(test, a, b, c) expands into this:
+
+       test(3, a) \
+       test(2, b) \
+       test(1, c)
+
+   Up to eight arguments are supported.
+
+   (With thanks to https://groups.google.com/d/topic/comp.std.c/d-6Mj5Lko_s/discussion
+   and http://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros)
+*/
+#define wxCALL_FOR_EACH_NARG(...)  wxCALL_FOR_EACH_NARG_(__VA_ARGS__, wxCALL_FOR_EACH_RSEQ_N())
+#define wxCALL_FOR_EACH_NARG_(...) wxCALL_FOR_EACH_ARG_N(__VA_ARGS__)
+#define wxCALL_FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
+#define wxCALL_FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
+
+#define wxCALL_FOR_EACH_1(what, x)        what(1, x)
+#define wxCALL_FOR_EACH_2(what, x, ...)   what(2, x)  wxCALL_FOR_EACH_1(what,  __VA_ARGS__)
+#define wxCALL_FOR_EACH_3(what, x, ...)   what(3, x)  wxCALL_FOR_EACH_2(what,  __VA_ARGS__)
+#define wxCALL_FOR_EACH_4(what, x, ...)   what(4, x)  wxCALL_FOR_EACH_3(what,  __VA_ARGS__)
+#define wxCALL_FOR_EACH_5(what, x, ...)   what(5, x)  wxCALL_FOR_EACH_4(what,  __VA_ARGS__)
+#define wxCALL_FOR_EACH_6(what, x, ...)   what(6, x)  wxCALL_FOR_EACH_5(what,  __VA_ARGS__)
+#define wxCALL_FOR_EACH_7(what, x, ...)   what(7, x)  wxCALL_FOR_EACH_6(what,  __VA_ARGS__)
+#define wxCALL_FOR_EACH_8(what, x, ...)   what(8, x)  wxCALL_FOR_EACH_7(what,  __VA_ARGS__)
+
+#define wxCALL_FOR_EACH_(N, what, ...) \
+    wxCONCAT(wxCALL_FOR_EACH_, N)(what, __VA_ARGS__)
+
+#define wxCALL_FOR_EACH(what, ...) \
+    wxCALL_FOR_EACH_(wxCALL_FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)
+
+#else
+    #define wxCALL_FOR_EACH  Error_wx_CALL_FOR_EACH_requires_variadic_macros_support
+#endif /* wxHAS_VARIADIC_MACROS */
+
 #endif /* _WX_CPP_H_ */