]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/cpp.h
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / include / wx / cpp.h
... / ...
CommitLineData
1/*
2 * Name: wx/cpp.h
3 * Purpose: Various preprocessor helpers
4 * Author: Vadim Zeitlin
5 * Created: 2006-09-30
6 * RCS-ID: $Id$
7 * Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
8 * Licence: wxWindows licence
9 */
10
11/* THIS IS A C FILE, DON'T USE C++ FEATURES (IN PARTICULAR COMMENTS) IN IT */
12
13#ifndef _WX_CPP_H_
14#define _WX_CPP_H_
15
16/* wxCONCAT works like preprocessor ## operator but also works with macros */
17#define wxCONCAT_HELPER(text, line) text ## line
18
19#define wxCONCAT(x1, x2) \
20 wxCONCAT_HELPER(x1, x2)
21#define wxCONCAT3(x1, x2, x3) \
22 wxCONCAT(wxCONCAT(x1, x2), x3)
23#define wxCONCAT4(x1, x2, x3, x4) \
24 wxCONCAT(wxCONCAT3(x1, x2, x3), x4)
25#define wxCONCAT5(x1, x2, x3, x4, x5) \
26 wxCONCAT(wxCONCAT4(x1, x2, x3, x4), x5)
27#define wxCONCAT6(x1, x2, x3, x4, x5, x6) \
28 wxCONCAT(wxCONCAT5(x1, x2, x3, x4, x5), x6)
29#define wxCONCAT7(x1, x2, x3, x4, x5, x6, x7) \
30 wxCONCAT(wxCONCAT6(x1, x2, x3, x4, x5, x6), x7)
31#define wxCONCAT8(x1, x2, x3, x4, x5, x6, x7, x8) \
32 wxCONCAT(wxCONCAT7(x1, x2, x3, x4, x5, x6, x7), x8)
33#define wxCONCAT9(x1, x2, x3, x4, x5, x6, x7, x8, x9) \
34 wxCONCAT(wxCONCAT8(x1, x2, x3, x4, x5, x6, x7, x8), x9)
35
36/* wxSTRINGIZE works as the preprocessor # operator but also works with macros */
37#define wxSTRINGIZE_HELPER(x) #x
38#define wxSTRINGIZE(x) wxSTRINGIZE_HELPER(x)
39
40/* a Unicode-friendly version of wxSTRINGIZE_T */
41#define wxSTRINGIZE_T(x) wxAPPLY_T(wxSTRINGIZE(x))
42
43/*
44 Special workarounds for compilers with broken "##" operator. For all the
45 other ones we can just use it directly.
46 */
47#ifdef wxCOMPILER_BROKEN_CONCAT_OPER
48 #define wxPREPEND_L(x) L ## x
49 #define wxAPPEND_i64(x) x ## i64
50 #define wxAPPEND_ui64(x) x ## ui64
51#endif /* wxCOMPILER_BROKEN_CONCAT_OPER */
52
53/*
54 Helper macros for wxMAKE_UNIQUE_NAME: normally this works by appending the
55 current line number to the given identifier to reduce the probability of the
56 conflict (it may still happen if this is used in the headers, hence you
57 should avoid doing it or provide unique prefixes then) but we have to do it
58 differently for VC++
59 */
60#if defined(__VISUALC__) && (__VISUALC__ >= 1300)
61 /*
62 __LINE__ handling is completely broken in VC++ when using "Edit and
63 Continue" (/ZI option) and results in preprocessor errors if we use it
64 inside the macros. Luckily VC7 has another standard macro which can be
65 used like this and is even better than __LINE__ because it is globally
66 unique.
67 */
68# define wxCONCAT_LINE(text) wxCONCAT(text, __COUNTER__)
69#else /* normal compilers */
70# define wxCONCAT_LINE(text) wxCONCAT(text, __LINE__)
71#endif
72
73/* Create a "unique" name with the given prefix */
74#define wxMAKE_UNIQUE_NAME(text) wxCONCAT_LINE(text)
75
76/*
77 This macro can be passed as argument to another macro when you don't have
78 anything to pass in fact.
79 */
80#define wxEMPTY_PARAMETER_VALUE /* Fake macro parameter value */
81
82/*
83 Define __WXFUNCTION__ which is like standard __FUNCTION__ but defined as
84 NULL for the compilers which don't support the latter.
85 */
86#ifndef __WXFUNCTION__
87 /* TODO: add more compilers supporting __FUNCTION__ */
88 #if defined(__DMC__)
89 /*
90 __FUNCTION__ happens to be not defined within class members
91 http://www.digitalmars.com/drn-bin/wwwnews?c%2B%2B.beta/485
92 */
93 #define __WXFUNCTION__ (NULL)
94 #elif defined(__GNUC__) || \
95 (defined(_MSC_VER) && _MSC_VER >= 1300) || \
96 defined(__FUNCTION__)
97 #define __WXFUNCTION__ __FUNCTION__
98 #else
99 /* still define __WXFUNCTION__ to avoid #ifdefs elsewhere */
100 #define __WXFUNCTION__ (NULL)
101 #endif
102#endif /* __WXFUNCTION__ already defined */
103
104
105#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
106 (defined(__cplusplus) && __cplusplus >= 201103L)
107 #define wxHAS_VARIADIC_MACROS
108#elif defined(__GNUC__) && __GNUC__ >= 3
109 #define wxHAS_VARIADIC_MACROS
110#elif defined(_MSC_VER) && _MSC_VER >= 1400
111 #define wxHAS_VARIADIC_MACROS
112#endif
113
114#ifdef wxHAS_VARIADIC_MACROS
115/*
116 wxCALL_FOR_EACH(what, ...) calls the macro from its first argument, what(pos, x),
117 for every remaining argument 'x', with 'pos' being its 1-based index in
118 *reverse* order (with the last argument being numbered 1).
119
120 For example, wxCALL_FOR_EACH(test, a, b, c) expands into this:
121
122 test(3, a) \
123 test(2, b) \
124 test(1, c)
125
126 Up to eight arguments are supported.
127
128 (With thanks to https://groups.google.com/d/topic/comp.std.c/d-6Mj5Lko_s/discussion
129 and http://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros)
130*/
131#define wxCALL_FOR_EACH_NARG(...) wxCALL_FOR_EACH_NARG_((__VA_ARGS__, wxCALL_FOR_EACH_RSEQ_N()))
132#define wxCALL_FOR_EACH_NARG_(args) wxCALL_FOR_EACH_ARG_N args
133#define wxCALL_FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
134#define wxCALL_FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
135
136#define wxCALL_FOR_EACH_1(what, x) what(1, x)
137#define wxCALL_FOR_EACH_2(what, x, ...) what(2, x) wxCALL_FOR_EACH_1(what, __VA_ARGS__)
138#define wxCALL_FOR_EACH_3(what, x, ...) what(3, x) wxCALL_FOR_EACH_2(what, __VA_ARGS__)
139#define wxCALL_FOR_EACH_4(what, x, ...) what(4, x) wxCALL_FOR_EACH_3(what, __VA_ARGS__)
140#define wxCALL_FOR_EACH_5(what, x, ...) what(5, x) wxCALL_FOR_EACH_4(what, __VA_ARGS__)
141#define wxCALL_FOR_EACH_6(what, x, ...) what(6, x) wxCALL_FOR_EACH_5(what, __VA_ARGS__)
142#define wxCALL_FOR_EACH_7(what, x, ...) what(7, x) wxCALL_FOR_EACH_6(what, __VA_ARGS__)
143#define wxCALL_FOR_EACH_8(what, x, ...) what(8, x) wxCALL_FOR_EACH_7(what, __VA_ARGS__)
144
145#define wxCALL_FOR_EACH_(N, args) \
146 wxCONCAT(wxCALL_FOR_EACH_, N) args
147
148#define wxCALL_FOR_EACH(what, ...) \
149 wxCALL_FOR_EACH_(wxCALL_FOR_EACH_NARG(__VA_ARGS__), (what, __VA_ARGS__))
150
151#else
152 #define wxCALL_FOR_EACH Error_wx_CALL_FOR_EACH_requires_variadic_macros_support
153#endif /* wxHAS_VARIADIC_MACROS */
154
155#endif /* _WX_CPP_H_ */
156