2006-09-30 10:55:06 -04:00
|
|
|
/*
|
|
|
|
* Name: wx/cpp.h
|
|
|
|
* Purpose: Various preprocessor helpers
|
|
|
|
* Author: Vadim Zeitlin
|
|
|
|
* Created: 2006-09-30
|
2019-04-22 08:12:05 -04:00
|
|
|
* Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwidgets.org>
|
2006-09-30 10:55:06 -04:00
|
|
|
* Licence: wxWindows licence
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* THIS IS A C FILE, DON'T USE C++ FEATURES (IN PARTICULAR COMMENTS) IN IT */
|
|
|
|
|
|
|
|
#ifndef _WX_CPP_H_
|
|
|
|
#define _WX_CPP_H_
|
|
|
|
|
2013-07-12 22:30:21 -04:00
|
|
|
#include "wx/compiler.h" /* wxCHECK_XXX_VERSION() macros */
|
|
|
|
|
2006-09-30 10:55:06 -04:00
|
|
|
/* wxCONCAT works like preprocessor ## operator but also works with macros */
|
|
|
|
#define wxCONCAT_HELPER(text, line) text ## line
|
|
|
|
|
2012-05-04 16:35:37 -04:00
|
|
|
#define wxCONCAT(x1, x2) \
|
|
|
|
wxCONCAT_HELPER(x1, x2)
|
|
|
|
#define wxCONCAT3(x1, x2, x3) \
|
|
|
|
wxCONCAT(wxCONCAT(x1, x2), x3)
|
|
|
|
#define wxCONCAT4(x1, x2, x3, x4) \
|
|
|
|
wxCONCAT(wxCONCAT3(x1, x2, x3), x4)
|
|
|
|
#define wxCONCAT5(x1, x2, x3, x4, x5) \
|
|
|
|
wxCONCAT(wxCONCAT4(x1, x2, x3, x4), x5)
|
|
|
|
#define wxCONCAT6(x1, x2, x3, x4, x5, x6) \
|
|
|
|
wxCONCAT(wxCONCAT5(x1, x2, x3, x4, x5), x6)
|
|
|
|
#define wxCONCAT7(x1, x2, x3, x4, x5, x6, x7) \
|
|
|
|
wxCONCAT(wxCONCAT6(x1, x2, x3, x4, x5, x6), x7)
|
|
|
|
#define wxCONCAT8(x1, x2, x3, x4, x5, x6, x7, x8) \
|
|
|
|
wxCONCAT(wxCONCAT7(x1, x2, x3, x4, x5, x6, x7), x8)
|
|
|
|
#define wxCONCAT9(x1, x2, x3, x4, x5, x6, x7, x8, x9) \
|
|
|
|
wxCONCAT(wxCONCAT8(x1, x2, x3, x4, x5, x6, x7, x8), x9)
|
2008-01-27 09:10:53 -05:00
|
|
|
|
2006-09-30 10:55:06 -04:00
|
|
|
/* wxSTRINGIZE works as the preprocessor # operator but also works with macros */
|
|
|
|
#define wxSTRINGIZE_HELPER(x) #x
|
|
|
|
#define wxSTRINGIZE(x) wxSTRINGIZE_HELPER(x)
|
|
|
|
|
2006-10-08 07:07:55 -04:00
|
|
|
/* a Unicode-friendly version of wxSTRINGIZE_T */
|
|
|
|
#define wxSTRINGIZE_T(x) wxAPPLY_T(wxSTRINGIZE(x))
|
|
|
|
|
2010-11-07 08:16:20 -05:00
|
|
|
/*
|
|
|
|
Special workarounds for compilers with broken "##" operator. For all the
|
|
|
|
other ones we can just use it directly.
|
|
|
|
*/
|
|
|
|
#ifdef wxCOMPILER_BROKEN_CONCAT_OPER
|
|
|
|
#define wxPREPEND_L(x) L ## x
|
|
|
|
#define wxAPPEND_i64(x) x ## i64
|
|
|
|
#define wxAPPEND_ui64(x) x ## ui64
|
|
|
|
#endif /* wxCOMPILER_BROKEN_CONCAT_OPER */
|
|
|
|
|
2006-09-30 10:55:06 -04:00
|
|
|
/*
|
|
|
|
Helper macros for wxMAKE_UNIQUE_NAME: normally this works by appending the
|
|
|
|
current line number to the given identifier to reduce the probability of the
|
|
|
|
conflict (it may still happen if this is used in the headers, hence you
|
|
|
|
should avoid doing it or provide unique prefixes then) but we have to do it
|
|
|
|
differently for VC++
|
|
|
|
*/
|
2014-05-15 18:32:17 -04:00
|
|
|
#if defined(__VISUALC__)
|
2006-09-30 10:55:06 -04:00
|
|
|
/*
|
|
|
|
__LINE__ handling is completely broken in VC++ when using "Edit and
|
|
|
|
Continue" (/ZI option) and results in preprocessor errors if we use it
|
|
|
|
inside the macros. Luckily VC7 has another standard macro which can be
|
|
|
|
used like this and is even better than __LINE__ because it is globally
|
|
|
|
unique.
|
|
|
|
*/
|
|
|
|
# define wxCONCAT_LINE(text) wxCONCAT(text, __COUNTER__)
|
|
|
|
#else /* normal compilers */
|
|
|
|
# define wxCONCAT_LINE(text) wxCONCAT(text, __LINE__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create a "unique" name with the given prefix */
|
|
|
|
#define wxMAKE_UNIQUE_NAME(text) wxCONCAT_LINE(text)
|
|
|
|
|
2006-11-03 16:06:57 -05:00
|
|
|
/*
|
|
|
|
This macro can be passed as argument to another macro when you don't have
|
|
|
|
anything to pass in fact.
|
|
|
|
*/
|
|
|
|
#define wxEMPTY_PARAMETER_VALUE /* Fake macro parameter value */
|
|
|
|
|
2012-12-02 18:48:36 -05:00
|
|
|
/*
|
|
|
|
Helpers for defining macros that expand into a single statement.
|
|
|
|
|
2014-05-16 09:01:06 -04:00
|
|
|
The standard solution is to use "do { ... } while (0)" statement but MSVC
|
2012-12-02 18:48:36 -05:00
|
|
|
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".
|
|
|
|
*/
|
2014-05-16 09:01:06 -04:00
|
|
|
#define wxSTATEMENT_MACRO_BEGIN do {
|
|
|
|
#define wxSTATEMENT_MACRO_END } while ( (void)0, 0 )
|
2012-12-02 18:48:36 -05:00
|
|
|
|
2020-03-09 11:44:13 -04:00
|
|
|
/*
|
|
|
|
Helper for executing the following statement conditionally without using
|
|
|
|
conditional statements.
|
|
|
|
|
|
|
|
This strange macro is needed in the first place to avoid the problems due
|
|
|
|
to nested if/else inside macros. E.g. if some MACRO started with "if", then
|
|
|
|
|
|
|
|
if ( cond )
|
|
|
|
MACRO();
|
|
|
|
else
|
|
|
|
...
|
|
|
|
|
|
|
|
would be broken because "..." would bind to the wrong "if" inside the macro
|
|
|
|
rather than the visible one. So we use wxDO_IF() inside the macro instead
|
|
|
|
to avoid this problem.
|
|
|
|
*/
|
|
|
|
#define wxDO_IF_HELPER(loopvar, condition) \
|
|
|
|
for ( bool loopvar = false; !loopvar && condition; loopvar = true )
|
|
|
|
|
|
|
|
#define wxDO_IF(condition) wxDO_IF_HELPER(wxMAKE_UNIQUE_NAME(wxdoif), condition)
|
|
|
|
|
2009-03-21 13:18:01 -04:00
|
|
|
/*
|
|
|
|
Define __WXFUNCTION__ which is like standard __FUNCTION__ but defined as
|
|
|
|
NULL for the compilers which don't support the latter.
|
|
|
|
*/
|
|
|
|
#ifndef __WXFUNCTION__
|
2014-05-16 09:01:23 -04:00
|
|
|
#if defined(__GNUC__) || \
|
2014-05-15 18:32:17 -04:00
|
|
|
defined(__VISUALC__) || \
|
2009-03-21 13:18:01 -04:00
|
|
|
defined(__FUNCTION__)
|
|
|
|
#define __WXFUNCTION__ __FUNCTION__
|
|
|
|
#else
|
|
|
|
/* still define __WXFUNCTION__ to avoid #ifdefs elsewhere */
|
|
|
|
#define __WXFUNCTION__ (NULL)
|
|
|
|
#endif
|
|
|
|
#endif /* __WXFUNCTION__ already defined */
|
|
|
|
|
2012-11-01 12:45:01 -04:00
|
|
|
|
2013-07-11 18:40:47 -04:00
|
|
|
/* 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)
|
2014-01-02 20:12:58 -05:00
|
|
|
#define HAVE_VARIADIC_MACROS 1
|
2014-05-16 09:01:35 -04:00
|
|
|
#elif defined(__GNUC__)
|
2014-01-02 20:12:58 -05:00
|
|
|
#define HAVE_VARIADIC_MACROS 1
|
2013-07-11 18:40:47 -04:00
|
|
|
#elif wxCHECK_VISUALC_VERSION(8)
|
2014-01-02 20:12:58 -05:00
|
|
|
#define HAVE_VARIADIC_MACROS 1
|
2013-07-11 18:40:47 -04:00
|
|
|
#endif
|
|
|
|
#endif /* !HAVE_VARIADIC_MACROS */
|
|
|
|
|
|
|
|
|
2012-11-01 12:45:01 -04:00
|
|
|
|
2013-07-11 18:40:47 -04:00
|
|
|
#ifdef HAVE_VARIADIC_MACROS
|
2014-09-10 12:52:08 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
This is a hack to make it possible to use variadic macros with g++ 3.x even
|
|
|
|
when using -pedantic[-errors] option: without this, it would complain that
|
|
|
|
|
|
|
|
"anonymous variadic macros were introduced in C99"
|
|
|
|
|
|
|
|
and the option disabling this warning (-Wno-variadic-macros) is only
|
|
|
|
available in gcc 4.0 and later, so until then this hack is the only thing we
|
|
|
|
can do.
|
|
|
|
*/
|
|
|
|
#if defined(__GNUC__) && __GNUC__ == 3
|
|
|
|
#pragma GCC system_header
|
|
|
|
#endif /* gcc-3.x */
|
|
|
|
|
2012-11-01 12:45:01 -04:00
|
|
|
/*
|
|
|
|
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
|
2017-07-04 15:15:14 -04:00
|
|
|
and https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros)
|
2012-11-01 12:45:01 -04:00
|
|
|
*/
|
2012-11-01 14:25:07 -04:00
|
|
|
#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
|
2012-11-01 12:45:01 -04:00
|
|
|
#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
|
|
|
|
|
2014-09-13 20:59:53 -04:00
|
|
|
#define wxCALL_FOR_EACH_1_(args) wxCALL_FOR_EACH_1 args
|
|
|
|
#define wxCALL_FOR_EACH_2_(args) wxCALL_FOR_EACH_2 args
|
|
|
|
#define wxCALL_FOR_EACH_3_(args) wxCALL_FOR_EACH_3 args
|
|
|
|
#define wxCALL_FOR_EACH_4_(args) wxCALL_FOR_EACH_4 args
|
|
|
|
#define wxCALL_FOR_EACH_5_(args) wxCALL_FOR_EACH_5 args
|
|
|
|
#define wxCALL_FOR_EACH_6_(args) wxCALL_FOR_EACH_6 args
|
|
|
|
#define wxCALL_FOR_EACH_7_(args) wxCALL_FOR_EACH_7 args
|
|
|
|
#define wxCALL_FOR_EACH_8_(args) wxCALL_FOR_EACH_8 args
|
|
|
|
|
2012-11-01 12:45:01 -04:00
|
|
|
#define wxCALL_FOR_EACH_1(what, x) what(1, x)
|
2014-09-13 20:59:53 -04:00
|
|
|
#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__))
|
2012-11-01 12:45:01 -04:00
|
|
|
|
2012-11-01 14:25:07 -04:00
|
|
|
#define wxCALL_FOR_EACH_(N, args) \
|
|
|
|
wxCONCAT(wxCALL_FOR_EACH_, N) args
|
2012-11-01 12:45:01 -04:00
|
|
|
|
|
|
|
#define wxCALL_FOR_EACH(what, ...) \
|
2012-11-01 14:25:07 -04:00
|
|
|
wxCALL_FOR_EACH_(wxCALL_FOR_EACH_NARG(__VA_ARGS__), (what, __VA_ARGS__))
|
2012-11-01 12:45:01 -04:00
|
|
|
|
|
|
|
#else
|
|
|
|
#define wxCALL_FOR_EACH Error_wx_CALL_FOR_EACH_requires_variadic_macros_support
|
2013-07-11 18:40:47 -04:00
|
|
|
#endif /* HAVE_VARIADIC_MACROS */
|
2012-11-01 12:45:01 -04:00
|
|
|
|
2007-06-25 11:07:20 -04:00
|
|
|
#endif /* _WX_CPP_H_ */
|
2006-09-30 10:55:06 -04:00
|
|
|
|