]>
Commit | Line | Data |
---|---|---|
cdd8d745 VZ |
1 | /* |
2 | * Name: wx/cpp.h | |
3 | * Purpose: Various preprocessor helpers | |
4 | * Author: Vadim Zeitlin | |
5 | * Created: 2006-09-30 | |
cdd8d745 VZ |
6 | * Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org> |
7 | * Licence: wxWindows licence | |
8 | */ | |
9 | ||
10 | /* THIS IS A C FILE, DON'T USE C++ FEATURES (IN PARTICULAR COMMENTS) IN IT */ | |
11 | ||
12 | #ifndef _WX_CPP_H_ | |
13 | #define _WX_CPP_H_ | |
14 | ||
20125017 VZ |
15 | #include "wx/compiler.h" /* wxCHECK_XXX_VERSION() macros */ |
16 | ||
cdd8d745 VZ |
17 | /* wxCONCAT works like preprocessor ## operator but also works with macros */ |
18 | #define wxCONCAT_HELPER(text, line) text ## line | |
cdd8d745 | 19 | |
47eab2f2 VZ |
20 | #define wxCONCAT(x1, x2) \ |
21 | wxCONCAT_HELPER(x1, x2) | |
22 | #define wxCONCAT3(x1, x2, x3) \ | |
23 | wxCONCAT(wxCONCAT(x1, x2), x3) | |
24 | #define wxCONCAT4(x1, x2, x3, x4) \ | |
25 | wxCONCAT(wxCONCAT3(x1, x2, x3), x4) | |
26 | #define wxCONCAT5(x1, x2, x3, x4, x5) \ | |
27 | wxCONCAT(wxCONCAT4(x1, x2, x3, x4), x5) | |
28 | #define wxCONCAT6(x1, x2, x3, x4, x5, x6) \ | |
29 | wxCONCAT(wxCONCAT5(x1, x2, x3, x4, x5), x6) | |
30 | #define wxCONCAT7(x1, x2, x3, x4, x5, x6, x7) \ | |
31 | wxCONCAT(wxCONCAT6(x1, x2, x3, x4, x5, x6), x7) | |
32 | #define wxCONCAT8(x1, x2, x3, x4, x5, x6, x7, x8) \ | |
33 | wxCONCAT(wxCONCAT7(x1, x2, x3, x4, x5, x6, x7), x8) | |
34 | #define wxCONCAT9(x1, x2, x3, x4, x5, x6, x7, x8, x9) \ | |
35 | wxCONCAT(wxCONCAT8(x1, x2, x3, x4, x5, x6, x7, x8), x9) | |
1894e49f | 36 | |
cdd8d745 VZ |
37 | /* wxSTRINGIZE works as the preprocessor # operator but also works with macros */ |
38 | #define wxSTRINGIZE_HELPER(x) #x | |
39 | #define wxSTRINGIZE(x) wxSTRINGIZE_HELPER(x) | |
40 | ||
84206bbb VZ |
41 | /* a Unicode-friendly version of wxSTRINGIZE_T */ |
42 | #define wxSTRINGIZE_T(x) wxAPPLY_T(wxSTRINGIZE(x)) | |
43 | ||
25859335 VZ |
44 | /* |
45 | Special workarounds for compilers with broken "##" operator. For all the | |
46 | other ones we can just use it directly. | |
47 | */ | |
48 | #ifdef wxCOMPILER_BROKEN_CONCAT_OPER | |
49 | #define wxPREPEND_L(x) L ## x | |
50 | #define wxAPPEND_i64(x) x ## i64 | |
51 | #define wxAPPEND_ui64(x) x ## ui64 | |
52 | #endif /* wxCOMPILER_BROKEN_CONCAT_OPER */ | |
53 | ||
cdd8d745 VZ |
54 | /* |
55 | Helper macros for wxMAKE_UNIQUE_NAME: normally this works by appending the | |
56 | current line number to the given identifier to reduce the probability of the | |
57 | conflict (it may still happen if this is used in the headers, hence you | |
58 | should avoid doing it or provide unique prefixes then) but we have to do it | |
59 | differently for VC++ | |
60 | */ | |
61 | #if defined(__VISUALC__) && (__VISUALC__ >= 1300) | |
62 | /* | |
63 | __LINE__ handling is completely broken in VC++ when using "Edit and | |
64 | Continue" (/ZI option) and results in preprocessor errors if we use it | |
65 | inside the macros. Luckily VC7 has another standard macro which can be | |
66 | used like this and is even better than __LINE__ because it is globally | |
67 | unique. | |
68 | */ | |
69 | # define wxCONCAT_LINE(text) wxCONCAT(text, __COUNTER__) | |
70 | #else /* normal compilers */ | |
71 | # define wxCONCAT_LINE(text) wxCONCAT(text, __LINE__) | |
72 | #endif | |
73 | ||
74 | /* Create a "unique" name with the given prefix */ | |
75 | #define wxMAKE_UNIQUE_NAME(text) wxCONCAT_LINE(text) | |
76 | ||
bde626ce VZ |
77 | /* |
78 | This macro can be passed as argument to another macro when you don't have | |
79 | anything to pass in fact. | |
80 | */ | |
81 | #define wxEMPTY_PARAMETER_VALUE /* Fake macro parameter value */ | |
82 | ||
4d3845c0 VZ |
83 | /* |
84 | Helpers for defining macros that expand into a single statement. | |
85 | ||
86 | The standatd solution is to use "do { ... } while (0)" statement but MSVC | |
87 | generates a C4127 "condition expression is constant" warning for it so we | |
88 | use something which is just complicated enough to not be recognized as a | |
89 | constant but still simple enough to be optimized away. | |
90 | ||
91 | Another solution would be to use __pragma() to temporarily disable C4127. | |
92 | ||
93 | Notice that wxASSERT_ARG_TYPE in wx/strvargarg.h relies on these macros | |
94 | creating some kind of a loop because it uses "break". | |
95 | */ | |
96 | #ifdef __WATCOMC__ | |
97 | #define wxFOR_ONCE(name) for(int name=0; name<1; name++) | |
98 | #define wxSTATEMENT_MACRO_BEGIN wxFOR_ONCE(wxMAKE_UNIQUE_NAME(wxmacro)) { | |
99 | #define wxSTATEMENT_MACRO_END } | |
100 | #else | |
101 | #define wxSTATEMENT_MACRO_BEGIN do { | |
102 | #define wxSTATEMENT_MACRO_END } while ( (void)0, 0 ) | |
103 | #endif | |
104 | ||
a6ebdba6 VZ |
105 | /* |
106 | Define __WXFUNCTION__ which is like standard __FUNCTION__ but defined as | |
107 | NULL for the compilers which don't support the latter. | |
108 | */ | |
109 | #ifndef __WXFUNCTION__ | |
110 | /* TODO: add more compilers supporting __FUNCTION__ */ | |
111 | #if defined(__DMC__) | |
112 | /* | |
113 | __FUNCTION__ happens to be not defined within class members | |
114 | http://www.digitalmars.com/drn-bin/wwwnews?c%2B%2B.beta/485 | |
115 | */ | |
116 | #define __WXFUNCTION__ (NULL) | |
117 | #elif defined(__GNUC__) || \ | |
118 | (defined(_MSC_VER) && _MSC_VER >= 1300) || \ | |
119 | defined(__FUNCTION__) | |
120 | #define __WXFUNCTION__ __FUNCTION__ | |
121 | #else | |
122 | /* still define __WXFUNCTION__ to avoid #ifdefs elsewhere */ | |
123 | #define __WXFUNCTION__ (NULL) | |
124 | #endif | |
125 | #endif /* __WXFUNCTION__ already defined */ | |
126 | ||
a4d982a7 | 127 | |
168d1f65 VZ |
128 | /* Auto-detect variadic macros support unless explicitly disabled. */ |
129 | #if !defined(HAVE_VARIADIC_MACROS) && !defined(wxNO_VARIADIC_MACROS) | |
130 | /* Any C99 or C++11 compiler should have them. */ | |
131 | #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \ | |
132 | (defined(__cplusplus) && __cplusplus >= 201103L) | |
133 | #define HAVE_VARIADIC_MACROS | |
20125017 | 134 | #elif wxCHECK_GCC_VERSION(3,0) |
168d1f65 VZ |
135 | #define HAVE_VARIADIC_MACROS |
136 | #elif wxCHECK_VISUALC_VERSION(8) | |
137 | #define HAVE_VARIADIC_MACROS | |
138 | #elif wxCHECK_WATCOM_VERSION(1,2) | |
139 | #define HAVE_VARIADIC_MACROS | |
140 | #endif | |
141 | #endif /* !HAVE_VARIADIC_MACROS */ | |
142 | ||
143 | ||
a4d982a7 | 144 | |
168d1f65 | 145 | #ifdef HAVE_VARIADIC_MACROS |
a4d982a7 VS |
146 | /* |
147 | wxCALL_FOR_EACH(what, ...) calls the macro from its first argument, what(pos, x), | |
148 | for every remaining argument 'x', with 'pos' being its 1-based index in | |
149 | *reverse* order (with the last argument being numbered 1). | |
150 | ||
151 | For example, wxCALL_FOR_EACH(test, a, b, c) expands into this: | |
152 | ||
153 | test(3, a) \ | |
154 | test(2, b) \ | |
155 | test(1, c) | |
156 | ||
157 | Up to eight arguments are supported. | |
158 | ||
159 | (With thanks to https://groups.google.com/d/topic/comp.std.c/d-6Mj5Lko_s/discussion | |
160 | and http://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros) | |
161 | */ | |
e08d449f VS |
162 | #define wxCALL_FOR_EACH_NARG(...) wxCALL_FOR_EACH_NARG_((__VA_ARGS__, wxCALL_FOR_EACH_RSEQ_N())) |
163 | #define wxCALL_FOR_EACH_NARG_(args) wxCALL_FOR_EACH_ARG_N args | |
a4d982a7 VS |
164 | #define wxCALL_FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N |
165 | #define wxCALL_FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0 | |
166 | ||
167 | #define wxCALL_FOR_EACH_1(what, x) what(1, x) | |
168 | #define wxCALL_FOR_EACH_2(what, x, ...) what(2, x) wxCALL_FOR_EACH_1(what, __VA_ARGS__) | |
169 | #define wxCALL_FOR_EACH_3(what, x, ...) what(3, x) wxCALL_FOR_EACH_2(what, __VA_ARGS__) | |
170 | #define wxCALL_FOR_EACH_4(what, x, ...) what(4, x) wxCALL_FOR_EACH_3(what, __VA_ARGS__) | |
171 | #define wxCALL_FOR_EACH_5(what, x, ...) what(5, x) wxCALL_FOR_EACH_4(what, __VA_ARGS__) | |
172 | #define wxCALL_FOR_EACH_6(what, x, ...) what(6, x) wxCALL_FOR_EACH_5(what, __VA_ARGS__) | |
173 | #define wxCALL_FOR_EACH_7(what, x, ...) what(7, x) wxCALL_FOR_EACH_6(what, __VA_ARGS__) | |
174 | #define wxCALL_FOR_EACH_8(what, x, ...) what(8, x) wxCALL_FOR_EACH_7(what, __VA_ARGS__) | |
175 | ||
e08d449f VS |
176 | #define wxCALL_FOR_EACH_(N, args) \ |
177 | wxCONCAT(wxCALL_FOR_EACH_, N) args | |
a4d982a7 VS |
178 | |
179 | #define wxCALL_FOR_EACH(what, ...) \ | |
e08d449f | 180 | wxCALL_FOR_EACH_(wxCALL_FOR_EACH_NARG(__VA_ARGS__), (what, __VA_ARGS__)) |
a4d982a7 VS |
181 | |
182 | #else | |
183 | #define wxCALL_FOR_EACH Error_wx_CALL_FOR_EACH_requires_variadic_macros_support | |
168d1f65 | 184 | #endif /* HAVE_VARIADIC_MACROS */ |
a4d982a7 | 185 | |
28efe654 | 186 | #endif /* _WX_CPP_H_ */ |
cdd8d745 | 187 |