X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a4d982a7cfcc3663c03e05fc5b62ba1b4965fae1..66c2bf7b1d9326fb650acfaae22ec50528cfbf7c:/include/wx/cpp.h diff --git a/include/wx/cpp.h b/include/wx/cpp.h index 3d9941920e..b5ce014e4d 100644 --- a/include/wx/cpp.h +++ b/include/wx/cpp.h @@ -3,7 +3,6 @@ * Purpose: Various preprocessor helpers * Author: Vadim Zeitlin * Created: 2006-09-30 - * RCS-ID: $Id$ * Copyright: (c) 2006 Vadim Zeitlin * Licence: wxWindows licence */ @@ -13,6 +12,8 @@ #ifndef _WX_CPP_H_ #define _WX_CPP_H_ +#include "wx/compiler.h" /* wxCHECK_XXX_VERSION() macros */ + /* wxCONCAT works like preprocessor ## operator but also works with macros */ #define wxCONCAT_HELPER(text, line) text ## line @@ -79,6 +80,28 @@ */ #define wxEMPTY_PARAMETER_VALUE /* Fake macro parameter value */ +/* + Helpers for defining macros that expand into a single statement. + + The standatd solution is to use "do { ... } while (0)" statement but MSVC + generates a C4127 "condition expression is constant" warning for it so we + use something which is just complicated enough to not be recognized as a + constant but still simple enough to be optimized away. + + Another solution would be to use __pragma() to temporarily disable C4127. + + Notice that wxASSERT_ARG_TYPE in wx/strvargarg.h relies on these macros + creating some kind of a loop because it uses "break". + */ +#ifdef __WATCOMC__ + #define wxFOR_ONCE(name) for(int name=0; name<1; name++) + #define wxSTATEMENT_MACRO_BEGIN wxFOR_ONCE(wxMAKE_UNIQUE_NAME(wxmacro)) { + #define wxSTATEMENT_MACRO_END } +#else + #define wxSTATEMENT_MACRO_BEGIN do { + #define wxSTATEMENT_MACRO_END } while ( (void)0, 0 ) +#endif + /* Define __WXFUNCTION__ which is like standard __FUNCTION__ but defined as NULL for the compilers which don't support the latter. @@ -102,16 +125,24 @@ #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 +/* Auto-detect variadic macros support unless explicitly disabled. */ +#if !defined(HAVE_VARIADIC_MACROS) && !defined(wxNO_VARIADIC_MACROS) + /* Any C99 or C++11 compiler should have them. */ + #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \ + (defined(__cplusplus) && __cplusplus >= 201103L) + #define HAVE_VARIADIC_MACROS + #elif wxCHECK_GCC_VERSION(3,0) + #define HAVE_VARIADIC_MACROS + #elif wxCHECK_VISUALC_VERSION(8) + #define HAVE_VARIADIC_MACROS + #elif wxCHECK_WATCOM_VERSION(1,2) + #define HAVE_VARIADIC_MACROS + #endif +#endif /* !HAVE_VARIADIC_MACROS */ + + -#ifdef wxHAS_VARIADIC_MACROS +#ifdef HAVE_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 @@ -128,8 +159,8 @@ (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_NARG(...) wxCALL_FOR_EACH_NARG_((__VA_ARGS__, wxCALL_FOR_EACH_RSEQ_N())) +#define wxCALL_FOR_EACH_NARG_(args) wxCALL_FOR_EACH_ARG_N 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 @@ -142,15 +173,15 @@ #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_(N, args) \ + wxCONCAT(wxCALL_FOR_EACH_, N) args #define wxCALL_FOR_EACH(what, ...) \ - wxCALL_FOR_EACH_(wxCALL_FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__) + 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 /* HAVE_VARIADIC_MACROS */ #endif /* _WX_CPP_H_ */