]> git.saurik.com Git - wxWidgets.git/blob - include/wx/strvararg.h
added copy ctor to wxCStrData to fix crashes when passing to vararg functions
[wxWidgets.git] / include / wx / strvararg.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/strvararg.h
3 // Purpose: macros for implementing type-safe vararg passing of strings
4 // Author: Vaclav Slavik
5 // Created: 2007-02-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_STRVARARG_H_
12 #define _WX_STRVARARG_H_
13
14 #include "wx/platform.h"
15 #if wxONLY_WATCOM_EARLIER_THAN(1,4)
16 #error "OpenWatcom version >= 1.4 is required to compile this code"
17 #endif
18
19 #include "wx/cpp.h"
20 #include "wx/chartype.h"
21 #include "wx/wxcrt.h"
22 #include "wx/strconv.h"
23 #include "wx/buffer.h"
24
25 class WXDLLIMPEXP_BASE wxCStrData;
26 class WXDLLIMPEXP_BASE wxString;
27
28 // ----------------------------------------------------------------------------
29 // WX_DEFINE_VARARG_FUNC* macros
30 // ----------------------------------------------------------------------------
31
32 // This macro is used to implement type-safe wrappers for variadic functions
33 // that accept strings as arguments. This makes it possible to pass char*,
34 // wchar_t* or even wxString (as opposed to having to use wxString::c_str())
35 // to e.g. wxPrintf().
36 //
37 // This is done by defining a set of N template function taking 1..N arguments
38 // (currently, N is set to 30 in this header). These functions are just thin
39 // wrappers around another variadic function ('impl' or 'implUtf8' arguments,
40 // see below) and the only thing the wrapper does is that it normalizes the
41 // arguments passed in so that they are of the type expected by variadic
42 // functions taking string arguments, i.e., char* or wchar_t*, depending on the
43 // build:
44 // * char* in the current locale's charset in ANSI build
45 // * char* with UTF-8 encoding if wxUSE_UNICODE_UTF8 and the app is running
46 // under an UTF-8 locale
47 // * wchar_t* if wxUSE_UNICODE_WCHAR or if wxUSE_UNICODE_UTF8 and the current
48 // locale is not UTF-8
49 //
50 // Parameters:
51 // [ there are examples in square brackets showing values of the parameters
52 // for the wxFprintf() wrapper for fprintf() function with the following
53 // prototype:
54 // int wxFprintf(FILE *stream, const wxString& format, ...); ]
55 //
56 // rettype Functions' return type [int]
57 // name Name of the function [fprintf]
58 // numfixed The number of leading "fixed" (i.e., not variadic)
59 // arguments of the function (e.g. "stream" and "format"
60 // arguments of fprintf()); their type is _not_ converted
61 // using wxArgNormalizer<T>, unlike the rest of
62 // the function's arguments [2]
63 // fixed List of types of the leading "fixed" arguments, in
64 // parenthesis [(FILE*,const wxString&)]
65 // impl Name of the variadic function that implements 'name' for
66 // the native strings representation (wchar_t* if
67 // wxUSE_UNICODE_WCHAR or wxUSE_UNICODE_UTF8 when running under
68 // non-UTF8 locale, char* in ANSI build) [wxCrt_Fprintf]
69 // implUtf8 Like 'impl', but for the UTF-8 char* version to be used
70 // if wxUSE_UNICODE_UTF8 and running under UTF-8 locale
71 // (ignored otherwise) [fprintf]
72 //
73 #define WX_DEFINE_VARARG_FUNC2(rettype, name, numfixed, fixed, impl, implUtf8)\
74 _WX_VARARG_DEFINE_FUNC_N0(rettype, name, impl, implUtf8, numfixed, fixed) \
75 WX_DEFINE_VARARG_FUNC2_SANS_N0(rettype, name, numfixed, fixed, impl, implUtf8)
76
77 // ditto, but without the version with 0 template/vararg arguments
78 #define WX_DEFINE_VARARG_FUNC2_SANS_N0(rettype, name, \
79 numfixed, fixed, impl, implUtf8) \
80 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
81 _WX_VARARG_DEFINE_FUNC, \
82 rettype, name, impl, implUtf8, numfixed, fixed)
83
84 // like WX_DEFINE_VARARG_FUNC2, but for impl=implUtf8:
85 #define WX_DEFINE_VARARG_FUNC(rettype, name, numfixed, fixed, impl) \
86 WX_DEFINE_VARARG_FUNC2(rettype, name, numfixed, fixed, impl, impl)
87
88 // Like WX_DEFINE_VARARG_FUNC2, but for variadic functions that don't return
89 // a value.
90 #define WX_DEFINE_VARARG_FUNC_VOID2(name, numfixed, fixed, impl, implUtf8) \
91 _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
92 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
93 _WX_VARARG_DEFINE_FUNC_VOID, \
94 void, name, impl, implUtf8, numfixed, fixed)
95
96 // like WX_DEFINE_VARARG_FUNC_VOID2, but for impl=implUtf8:
97 #define WX_DEFINE_VARARG_FUNC_VOID(name, numfixed, fixed, impl) \
98 WX_DEFINE_VARARG_FUNC_VOID2(name, numfixed, fixed, impl, impl)
99
100 // Like WX_DEFINE_VARARG_FUNC_VOID, but instead of wrapping an implementation
101 // function, does nothing in defined functions' bodies.
102 //
103 // Used to implement wxLogXXX functions if wxUSE_LOG=0.
104 #define WX_DEFINE_VARARG_FUNC_NOP(name, numfixed, fixed) \
105 _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
106 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
107 _WX_VARARG_DEFINE_FUNC_NOP, \
108 void, name, dummy, numfixed, fixed)
109
110 // Like WX_DEFINE_VARARG_FUNC_CTOR, but for defining template constructors
111 #define WX_DEFINE_VARARG_FUNC_CTOR(name, numfixed, fixed, impl) \
112 _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, impl, numfixed, fixed) \
113 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
114 _WX_VARARG_DEFINE_FUNC_CTOR, \
115 void, name, impl, impl, numfixed, fixed)
116
117 // ----------------------------------------------------------------------------
118 // wxArgNormalizer*<T> converters
119 // ----------------------------------------------------------------------------
120
121 // Converts an argument passed to wxPrint etc. into standard form expected,
122 // by wxXXX functions, e.g. all strings (wxString, char*, wchar_t*) are
123 // converted into wchar_t* or char* depending on the build.
124 template<typename T>
125 struct wxArgNormalizer
126 {
127 wxArgNormalizer(const T& value) : m_value(value) {}
128
129 // Returns the value in a form that can be safely passed to real vararg
130 // functions. In case of strings, this is char* in ANSI build and wchar_t*
131 // in Unicode build.
132 const T& get() const { return m_value; }
133
134 const T& m_value;
135 };
136
137 // normalizer for passing arguments to functions working with wchar_t* (and
138 // until ANSI build is removed, char* in ANSI build as well - FIXME-UTF8)
139 // string representation
140 #if !wxUSE_UTF8_LOCALE_ONLY
141 template<typename T>
142 struct wxArgNormalizerWchar : public wxArgNormalizer<T>
143 {
144 wxArgNormalizerWchar(const T& value) : wxArgNormalizer<T>(value) {}
145 };
146 #endif // !wxUSE_UTF8_LOCALE_ONLY
147
148 // normalizer for passing arguments to functions working with UTF-8 encoded
149 // char* strings
150 #if wxUSE_UNICODE_UTF8
151 template<typename T>
152 struct wxArgNormalizerUtf8 : public wxArgNormalizer<T>
153 {
154 wxArgNormalizerUtf8(const T& value) : wxArgNormalizer<T>(value) {}
155 };
156
157 #define wxArgNormalizerNative wxArgNormalizerUtf8
158 #else // wxUSE_UNICODE_WCHAR
159 #define wxArgNormalizerNative wxArgNormalizerWchar
160 #endif // wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_UTF8
161
162
163
164 // special cases for converting strings:
165
166
167 // base class for wxArgNormalizer<T> specializations that need to do conversion;
168 // CharType is either wxStringCharType or wchar_t in UTF-8 build when wrapping
169 // widechar CRT function
170 template<typename CharType>
171 struct wxArgNormalizerWithBuffer
172 {
173 typedef wxCharTypeBuffer<CharType> CharBuffer;
174
175 wxArgNormalizerWithBuffer() {}
176 wxArgNormalizerWithBuffer(const CharBuffer& buf) : m_value(buf) {}
177
178 const CharType *get() const { return m_value; }
179
180 CharBuffer m_value;
181 };
182
183 // string objects:
184 template<>
185 struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxString&>
186 {
187 wxArgNormalizerNative(const wxString& s) : m_value(s) {}
188 const wxStringCharType *get() const;
189
190 const wxString& m_value;
191 };
192
193 // c_str() values:
194 template<>
195 struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxCStrData&>
196 {
197 wxArgNormalizerNative(const wxCStrData& value) : m_value(value) {}
198 const wxStringCharType *get() const;
199
200 const wxCStrData& m_value;
201 };
202
203 // wxString/wxCStrData conversion to wchar_t* value
204 #if wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
205 template<>
206 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxString&>
207 : public wxArgNormalizerWithBuffer<wchar_t>
208 {
209 wxArgNormalizerWchar(const wxString& s);
210 };
211
212 template<>
213 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxCStrData&>
214 : public wxArgNormalizerWithBuffer<wchar_t>
215 {
216 wxArgNormalizerWchar(const wxCStrData& s);
217 };
218 #endif // wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
219
220
221 // C string pointers of the wrong type (wchar_t* for ANSI or UTF8 build,
222 // char* for wchar_t Unicode build or UTF8):
223 #if wxUSE_UNICODE_WCHAR
224
225 template<>
226 struct wxArgNormalizerWchar<const char*>
227 : public wxArgNormalizerWithBuffer<wchar_t>
228 {
229 wxArgNormalizerWchar(const char* s)
230 : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s)) {}
231 };
232
233 #elif wxUSE_UNICODE_UTF8
234
235 template<>
236 struct wxArgNormalizerUtf8<const wchar_t*>
237 : public wxArgNormalizerWithBuffer<char>
238 {
239 wxArgNormalizerUtf8(const wchar_t* s)
240 : wxArgNormalizerWithBuffer<char>(wxConvUTF8.cWC2MB(s)) {}
241 };
242
243 template<>
244 struct wxArgNormalizerUtf8<const char*>
245 : public wxArgNormalizerWithBuffer<char>
246 {
247 wxArgNormalizerUtf8(const char* s)
248 {
249 // FIXME-UTF8: optimize this if current locale is UTF-8 one
250
251 // convert to widechar string first:
252 wxWCharBuffer buf(wxConvLibc.cMB2WC(s));
253
254 // then to UTF-8:
255 if ( buf )
256 m_value = wxConvUTF8.cWC2MB(buf);
257 }
258 };
259
260 // UTF-8 build needs conversion to wchar_t* too:
261 #if !wxUSE_UTF8_LOCALE_ONLY
262 template<>
263 struct wxArgNormalizerWchar<const char*>
264 : public wxArgNormalizerWithBuffer<wchar_t>
265 {
266 wxArgNormalizerWchar(const char* s)
267 : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s)) {}
268 };
269 #endif // !wxUSE_UTF8_LOCALE_ONLY
270
271 #else // ANSI - FIXME-UTF8
272
273 template<>
274 struct wxArgNormalizerWchar<const wchar_t*>
275 : public wxArgNormalizerWithBuffer<char>
276 {
277 wxArgNormalizerWchar(const wchar_t* s)
278 : wxArgNormalizerWithBuffer<char>(wxConvLibc.cWC2MB(s)) {}
279 };
280
281 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8/ANSI
282
283
284 // this macro is used to implement specialization that are exactly same as
285 // some other specialization, i.e. to "forward" the implementation (e.g. for
286 // T=wxString and T=const wxString&). Note that the ctor takes BaseT argument,
287 // not T!
288 #if wxUSE_UNICODE_UTF8
289 #if wxUSE_UTF8_LOCALE_ONLY
290 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
291 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
292 #else // possibly non-UTF8 locales
293 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
294 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT); \
295 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
296 #endif
297 #else // wxUSE_UNICODE_WCHAR
298 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
299 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT)
300 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
301
302 #define _WX_ARG_NORMALIZER_FORWARD_IMPL(Normalizer, T, BaseT) \
303 template<> \
304 struct Normalizer<T> : public Normalizer<BaseT> \
305 { \
306 Normalizer(BaseT value) : Normalizer<BaseT>(value) {} \
307 }
308
309 // non-reference versions of specializations for string objects
310 WX_ARG_NORMALIZER_FORWARD(wxString, const wxString&);
311 WX_ARG_NORMALIZER_FORWARD(wxCStrData, const wxCStrData&);
312
313 // versions for passing non-const pointers:
314 WX_ARG_NORMALIZER_FORWARD(char*, const char*);
315 WX_ARG_NORMALIZER_FORWARD(wchar_t*, const wchar_t*);
316
317 // versions for passing wx[W]CharBuffer:
318 WX_ARG_NORMALIZER_FORWARD(wxCharBuffer, const char*);
319 WX_ARG_NORMALIZER_FORWARD(const wxCharBuffer&, const char*);
320 WX_ARG_NORMALIZER_FORWARD(wxWCharBuffer, const wchar_t*);
321 WX_ARG_NORMALIZER_FORWARD(const wxWCharBuffer&, const wchar_t*);
322
323 #undef WX_ARG_NORMALIZER_FORWARD
324 #undef _WX_ARG_NORMALIZER_FORWARD_IMPL
325
326 // ----------------------------------------------------------------------------
327 // WX_VA_ARG_STRING
328 // ----------------------------------------------------------------------------
329
330 // Replacement for va_arg() for use with strings in functions that accept
331 // strings normalized by wxArgNormalizer<T>:
332
333 struct WXDLLIMPEXP_BASE wxArgNormalizedString
334 {
335 wxArgNormalizedString(const void* ptr) : m_ptr(ptr) {}
336
337 // returns true if non-NULL string was passed in
338 bool IsValid() const { return m_ptr != NULL; }
339 operator bool() const { return IsValid(); }
340
341 // extracts the string, returns empty string if NULL was passed in
342 wxString GetString() const;
343 operator wxString() const;
344
345 private:
346 const void *m_ptr;
347 };
348
349 #define WX_VA_ARG_STRING(ap) wxArgNormalizedString(va_arg(ap, const void*))
350
351 // ----------------------------------------------------------------------------
352 // implementation of the WX_DEFINE_VARARG_* macros
353 // ----------------------------------------------------------------------------
354
355 // NB: The vararg emulation code is limited to 30 variadic and 4 fixed
356 // arguments at the moment.
357 // If you need more variadic arguments, you need to
358 // 1) increase the value of _WX_VARARG_MAX_ARGS
359 // 2) add _WX_VARARG_JOIN_* and _WX_VARARG_ITER_* up to the new
360 // _WX_VARARG_MAX_ARGS value to the lists below
361 // If you need more fixed arguments, you need to
362 // 1) increase the value of _WX_VARARG_MAX_FIXED_ARGS
363 // 2) add _WX_VARARG_FIXED_EXPAND_* and _WX_VARARG_FIXED_UNUSED_EXPAND_*
364 // macros below
365 #define _WX_VARARG_MAX_ARGS 30
366 #define _WX_VARARG_MAX_FIXED_ARGS 4
367
368 #define _WX_VARARG_JOIN_1(m) m(1)
369 #define _WX_VARARG_JOIN_2(m) _WX_VARARG_JOIN_1(m), m(2)
370 #define _WX_VARARG_JOIN_3(m) _WX_VARARG_JOIN_2(m), m(3)
371 #define _WX_VARARG_JOIN_4(m) _WX_VARARG_JOIN_3(m), m(4)
372 #define _WX_VARARG_JOIN_5(m) _WX_VARARG_JOIN_4(m), m(5)
373 #define _WX_VARARG_JOIN_6(m) _WX_VARARG_JOIN_5(m), m(6)
374 #define _WX_VARARG_JOIN_7(m) _WX_VARARG_JOIN_6(m), m(7)
375 #define _WX_VARARG_JOIN_8(m) _WX_VARARG_JOIN_7(m), m(8)
376 #define _WX_VARARG_JOIN_9(m) _WX_VARARG_JOIN_8(m), m(9)
377 #define _WX_VARARG_JOIN_10(m) _WX_VARARG_JOIN_9(m), m(10)
378 #define _WX_VARARG_JOIN_11(m) _WX_VARARG_JOIN_10(m), m(11)
379 #define _WX_VARARG_JOIN_12(m) _WX_VARARG_JOIN_11(m), m(12)
380 #define _WX_VARARG_JOIN_13(m) _WX_VARARG_JOIN_12(m), m(13)
381 #define _WX_VARARG_JOIN_14(m) _WX_VARARG_JOIN_13(m), m(14)
382 #define _WX_VARARG_JOIN_15(m) _WX_VARARG_JOIN_14(m), m(15)
383 #define _WX_VARARG_JOIN_16(m) _WX_VARARG_JOIN_15(m), m(16)
384 #define _WX_VARARG_JOIN_17(m) _WX_VARARG_JOIN_16(m), m(17)
385 #define _WX_VARARG_JOIN_18(m) _WX_VARARG_JOIN_17(m), m(18)
386 #define _WX_VARARG_JOIN_19(m) _WX_VARARG_JOIN_18(m), m(19)
387 #define _WX_VARARG_JOIN_20(m) _WX_VARARG_JOIN_19(m), m(20)
388 #define _WX_VARARG_JOIN_21(m) _WX_VARARG_JOIN_20(m), m(21)
389 #define _WX_VARARG_JOIN_22(m) _WX_VARARG_JOIN_21(m), m(22)
390 #define _WX_VARARG_JOIN_23(m) _WX_VARARG_JOIN_22(m), m(23)
391 #define _WX_VARARG_JOIN_24(m) _WX_VARARG_JOIN_23(m), m(24)
392 #define _WX_VARARG_JOIN_25(m) _WX_VARARG_JOIN_24(m), m(25)
393 #define _WX_VARARG_JOIN_26(m) _WX_VARARG_JOIN_25(m), m(26)
394 #define _WX_VARARG_JOIN_27(m) _WX_VARARG_JOIN_26(m), m(27)
395 #define _WX_VARARG_JOIN_28(m) _WX_VARARG_JOIN_27(m), m(28)
396 #define _WX_VARARG_JOIN_29(m) _WX_VARARG_JOIN_28(m), m(29)
397 #define _WX_VARARG_JOIN_30(m) _WX_VARARG_JOIN_29(m), m(30)
398
399 #define _WX_VARARG_ITER_1(m,a,b,c,d,e,f) m(1,a,b,c,d,e,f)
400 #define _WX_VARARG_ITER_2(m,a,b,c,d,e,f) _WX_VARARG_ITER_1(m,a,b,c,d,e,f) m(2,a,b,c,d,e,f)
401 #define _WX_VARARG_ITER_3(m,a,b,c,d,e,f) _WX_VARARG_ITER_2(m,a,b,c,d,e,f) m(3,a,b,c,d,e,f)
402 #define _WX_VARARG_ITER_4(m,a,b,c,d,e,f) _WX_VARARG_ITER_3(m,a,b,c,d,e,f) m(4,a,b,c,d,e,f)
403 #define _WX_VARARG_ITER_5(m,a,b,c,d,e,f) _WX_VARARG_ITER_4(m,a,b,c,d,e,f) m(5,a,b,c,d,e,f)
404 #define _WX_VARARG_ITER_6(m,a,b,c,d,e,f) _WX_VARARG_ITER_5(m,a,b,c,d,e,f) m(6,a,b,c,d,e,f)
405 #define _WX_VARARG_ITER_7(m,a,b,c,d,e,f) _WX_VARARG_ITER_6(m,a,b,c,d,e,f) m(7,a,b,c,d,e,f)
406 #define _WX_VARARG_ITER_8(m,a,b,c,d,e,f) _WX_VARARG_ITER_7(m,a,b,c,d,e,f) m(8,a,b,c,d,e,f)
407 #define _WX_VARARG_ITER_9(m,a,b,c,d,e,f) _WX_VARARG_ITER_8(m,a,b,c,d,e,f) m(9,a,b,c,d,e,f)
408 #define _WX_VARARG_ITER_10(m,a,b,c,d,e,f) _WX_VARARG_ITER_9(m,a,b,c,d,e,f) m(10,a,b,c,d,e,f)
409 #define _WX_VARARG_ITER_11(m,a,b,c,d,e,f) _WX_VARARG_ITER_10(m,a,b,c,d,e,f) m(11,a,b,c,d,e,f)
410 #define _WX_VARARG_ITER_12(m,a,b,c,d,e,f) _WX_VARARG_ITER_11(m,a,b,c,d,e,f) m(12,a,b,c,d,e,f)
411 #define _WX_VARARG_ITER_13(m,a,b,c,d,e,f) _WX_VARARG_ITER_12(m,a,b,c,d,e,f) m(13,a,b,c,d,e,f)
412 #define _WX_VARARG_ITER_14(m,a,b,c,d,e,f) _WX_VARARG_ITER_13(m,a,b,c,d,e,f) m(14,a,b,c,d,e,f)
413 #define _WX_VARARG_ITER_15(m,a,b,c,d,e,f) _WX_VARARG_ITER_14(m,a,b,c,d,e,f) m(15,a,b,c,d,e,f)
414 #define _WX_VARARG_ITER_16(m,a,b,c,d,e,f) _WX_VARARG_ITER_15(m,a,b,c,d,e,f) m(16,a,b,c,d,e,f)
415 #define _WX_VARARG_ITER_17(m,a,b,c,d,e,f) _WX_VARARG_ITER_16(m,a,b,c,d,e,f) m(17,a,b,c,d,e,f)
416 #define _WX_VARARG_ITER_18(m,a,b,c,d,e,f) _WX_VARARG_ITER_17(m,a,b,c,d,e,f) m(18,a,b,c,d,e,f)
417 #define _WX_VARARG_ITER_19(m,a,b,c,d,e,f) _WX_VARARG_ITER_18(m,a,b,c,d,e,f) m(19,a,b,c,d,e,f)
418 #define _WX_VARARG_ITER_20(m,a,b,c,d,e,f) _WX_VARARG_ITER_19(m,a,b,c,d,e,f) m(20,a,b,c,d,e,f)
419 #define _WX_VARARG_ITER_21(m,a,b,c,d,e,f) _WX_VARARG_ITER_20(m,a,b,c,d,e,f) m(21,a,b,c,d,e,f)
420 #define _WX_VARARG_ITER_22(m,a,b,c,d,e,f) _WX_VARARG_ITER_21(m,a,b,c,d,e,f) m(22,a,b,c,d,e,f)
421 #define _WX_VARARG_ITER_23(m,a,b,c,d,e,f) _WX_VARARG_ITER_22(m,a,b,c,d,e,f) m(23,a,b,c,d,e,f)
422 #define _WX_VARARG_ITER_24(m,a,b,c,d,e,f) _WX_VARARG_ITER_23(m,a,b,c,d,e,f) m(24,a,b,c,d,e,f)
423 #define _WX_VARARG_ITER_25(m,a,b,c,d,e,f) _WX_VARARG_ITER_24(m,a,b,c,d,e,f) m(25,a,b,c,d,e,f)
424 #define _WX_VARARG_ITER_26(m,a,b,c,d,e,f) _WX_VARARG_ITER_25(m,a,b,c,d,e,f) m(26,a,b,c,d,e,f)
425 #define _WX_VARARG_ITER_27(m,a,b,c,d,e,f) _WX_VARARG_ITER_26(m,a,b,c,d,e,f) m(27,a,b,c,d,e,f)
426 #define _WX_VARARG_ITER_28(m,a,b,c,d,e,f) _WX_VARARG_ITER_27(m,a,b,c,d,e,f) m(28,a,b,c,d,e,f)
427 #define _WX_VARARG_ITER_29(m,a,b,c,d,e,f) _WX_VARARG_ITER_28(m,a,b,c,d,e,f) m(29,a,b,c,d,e,f)
428 #define _WX_VARARG_ITER_30(m,a,b,c,d,e,f) _WX_VARARG_ITER_29(m,a,b,c,d,e,f) m(30,a,b,c,d,e,f)
429
430
431 #define _WX_VARARG_FIXED_EXPAND_1(t1) \
432 t1 f1
433 #define _WX_VARARG_FIXED_EXPAND_2(t1,t2) \
434 t1 f1, t2 f2
435 #define _WX_VARARG_FIXED_EXPAND_3(t1,t2,t3) \
436 t1 f1, t2 f2, t3 f3
437 #define _WX_VARARG_FIXED_EXPAND_4(t1,t2,t3,t4) \
438 t1 f1, t2 f2, t3 f3, t4 f4
439
440 #define _WX_VARARG_FIXED_UNUSED_EXPAND_1(t1) \
441 t1 WXUNUSED(f1)
442 #define _WX_VARARG_FIXED_UNUSED_EXPAND_2(t1,t2) \
443 t1 WXUNUSED(f1), t2 WXUNUSED(f2)
444 #define _WX_VARARG_FIXED_UNUSED_EXPAND_3(t1,t2,t3) \
445 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3)
446 #define _WX_VARARG_FIXED_UNUSED_EXPAND_4(t1,t2,t3,t4) \
447 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3), t4 WXUNUSED(f4)
448
449
450 // This macro expands N-items tuple of fixed arguments types into part of
451 // function's declaration. For example,
452 // "_WX_VARARG_FIXED_EXPAND(3, (int, char*, int))" expands into
453 // "int f1, char* f2, int f3".
454 #define _WX_VARARG_FIXED_EXPAND(N, args) \
455 _WX_VARARG_FIXED_EXPAND_IMPL(N, args)
456 #define _WX_VARARG_FIXED_EXPAND_IMPL(N, args) \
457 _WX_VARARG_FIXED_EXPAND_##N args
458
459 // Ditto for unused arguments
460 #define _WX_VARARG_FIXED_UNUSED_EXPAND(N, args) \
461 _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args)
462 #define _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args) \
463 _WX_VARARG_FIXED_UNUSED_EXPAND_##N args
464
465
466 // This macro calls another macro 'm' passed as second argument 'N' times,
467 // with its only argument set to 1..N, and concatenates the results using
468 // comma as separator.
469 //
470 // An example:
471 // #define foo(i) x##i
472 // // this expands to "x1,x2,x3,x4"
473 // _WX_VARARG_JOIN(4, foo)
474 //
475 //
476 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
477 #define _WX_VARARG_JOIN(N, m) _WX_VARARG_JOIN_IMPL(N, m)
478 #define _WX_VARARG_JOIN_IMPL(N, m) _WX_VARARG_JOIN_##N(m)
479
480
481 // This macro calls another macro 'm' passed as second argument 'N' times, with
482 // its first argument set to 1..N and the remaining arguments set to 'a', 'b',
483 // 'c', 'd', 'e' and 'f'. The results are separated with whitespace in the
484 // expansion.
485 //
486 // An example:
487 // // this macro expands to:
488 // // foo(1,a,b,c,d,e,f)
489 // // foo(2,a,b,c,d,e,f)
490 // // foo(3,a,b,c,d,e,f)
491 // _WX_VARARG_ITER(3, foo, a, b, c, d, e, f)
492 //
493 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
494 #define _WX_VARARG_ITER(N,m,a,b,c,d,e,f) \
495 _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f)
496 #define _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f) \
497 _WX_VARARG_ITER_##N(m,a,b,c,d,e,f)
498
499 // Generates code snippet for i-th "variadic" argument in vararg function's
500 // prototype:
501 #define _WX_VARARG_ARG(i) T##i a##i
502
503 // Like _WX_VARARG_ARG_UNUSED, but outputs argument's type with WXUNUSED:
504 #define _WX_VARARG_ARG_UNUSED(i) T##i WXUNUSED(a##i)
505
506 // Generates code snippet for i-th type in vararg function's template<...>:
507 #define _WX_VARARG_TEMPL(i) typename T##i
508
509 // Generates code snippet for passing i-th argument of vararg function
510 // wrapper to its implementation, normalizing it in the process:
511 #define _WX_VARARG_PASS_WCHAR(i) wxArgNormalizerWchar<T##i>(a##i).get()
512 #define _WX_VARARG_PASS_UTF8(i) wxArgNormalizerUtf8<T##i>(a##i).get()
513
514
515 // And the same for fixed arguments, _not_ normalizing it:
516 // FIXME-UTF8: this works, but uses wxString's implicit conversion to wxChar*
517 // for the 'format' argument (which is const wxString&) _if_ the
518 // implementation function has C sting argument; we need to
519 // have wxFixedArgNormalizer<T> here that will pass everything
520 // as-is except for wxString (for which wx_str() would be used),
521 // but OTOH, we don't want to do that if the implementation takes
522 // wxString argument
523 #define _WX_VARARG_PASS_FIXED(i) f##i
524
525 #if wxUSE_UNICODE_UTF8
526 #define _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed) \
527 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
528 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_UTF8))
529 #define _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed) \
530 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
531 #endif // wxUSE_UNICODE_UTF8
532
533 #define _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed) \
534 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
535 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WCHAR))
536 #define _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed) \
537 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
538
539 #if wxUSE_UNICODE_UTF8
540 #if wxUSE_UTF8_LOCALE_ONLY
541 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_UTF8
542 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_UTF8
543 #else // possibly non-UTF8 locales
544 #define _WX_VARARG_DO_CALL(return_kw, impl, implUtf8, N, numfixed) \
545 if ( wxLocaleIsUtf8 ) \
546 _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed);\
547 else \
548 _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed)
549
550 #define _WX_VARARG_DO_CALL0(return_kw, impl, implUtf8, numfixed) \
551 if ( wxLocaleIsUtf8 ) \
552 _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed); \
553 else \
554 _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed)
555 #endif // wxUSE_UTF8_LOCALE_ONLY or not
556 #else // wxUSE_UNICODE_WCHAR or ANSI
557 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_WCHAR
558 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_WCHAR
559 #endif // wxUSE_UNICODE_UTF8 / wxUSE_UNICODE_WCHAR
560
561
562 // Macro to be used with _WX_VARARG_ITER in the implementation of
563 // WX_DEFINE_VARARG_FUNC (see its documentation for the meaning of arguments)
564 #define _WX_VARARG_DEFINE_FUNC(N, rettype, name, \
565 impl, implUtf8, numfixed, fixed) \
566 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
567 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
568 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
569 { \
570 _WX_VARARG_DO_CALL(return, impl, implUtf8, N, numfixed); \
571 }
572
573 #define _WX_VARARG_DEFINE_FUNC_N0(rettype, name, \
574 impl, implUtf8, numfixed, fixed) \
575 inline rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
576 { \
577 _WX_VARARG_DO_CALL0(return, impl, implUtf8, numfixed); \
578 }
579
580 // Macro to be used with _WX_VARARG_ITER in the implementation of
581 // WX_DEFINE_VARARG_FUNC_VOID (see its documentation for the meaning of
582 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
583 // requirements).
584 #define _WX_VARARG_DEFINE_FUNC_VOID(N, rettype, name, \
585 impl, implUtf8, numfixed, fixed) \
586 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
587 void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
588 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
589 { \
590 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
591 impl, implUtf8, N, numfixed); \
592 }
593
594 #define _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
595 inline void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
596 { \
597 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
598 impl, implUtf8, numfixed); \
599 }
600
601 // Macro to be used with _WX_VARARG_ITER in the implementation of
602 // WX_DEFINE_VARARG_FUNC_CTOR (see its documentation for the meaning of
603 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
604 // requirements).
605 #define _WX_VARARG_DEFINE_FUNC_CTOR(N, rettype, name, \
606 impl, implUtf8, numfixed, fixed) \
607 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
608 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
609 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
610 { \
611 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
612 impl, implUtf8, N, numfixed); \
613 }
614
615 #define _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
616 inline name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
617 { \
618 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
619 impl, implUtf8, numfixed); \
620 }
621
622 // Macro to be used with _WX_VARARG_ITER in the implementation of
623 // WX_DEFINE_VARARG_FUNC_NOP, i.e. empty stub for a disabled vararg function.
624 // The rettype and impl arguments are ignored.
625 #define _WX_VARARG_DEFINE_FUNC_NOP(N, rettype, name, \
626 impl, implUtf8, numfixed, fixed) \
627 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
628 void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed), \
629 _WX_VARARG_JOIN(N, _WX_VARARG_ARG_UNUSED)) \
630 {}
631
632 #define _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
633 inline void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed)) \
634 {}
635
636 #endif // _WX_STRVARARG_H_