]> git.saurik.com Git - wxWidgets.git/blob - include/wx/strvararg.h
fix wxStringOutputStream::Write() in Unicode build when the output overlaps a boundar...
[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, 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 // versions for std::[w]string:
324 #if wxUSE_STD_STRING
325
326 #include "wx/stringimpl.h"
327
328 #if !wxUSE_UTF8_LOCALE_ONLY
329 template<>
330 struct wxArgNormalizerWchar<const std::string&>
331 : public wxArgNormalizerWchar<const char*>
332 {
333 wxArgNormalizerWchar(const std::string& s)
334 : wxArgNormalizerWchar<const char*>(s.c_str()) {}
335 };
336
337 template<>
338 struct wxArgNormalizerWchar<const wxStdWideString&>
339 : public wxArgNormalizerWchar<const wchar_t*>
340 {
341 wxArgNormalizerWchar(const wxStdWideString& s)
342 : wxArgNormalizerWchar<const wchar_t*>(s.c_str()) {}
343 };
344 #endif // !wxUSE_UTF8_LOCALE_ONLY
345
346 #if wxUSE_UNICODE_UTF8
347 template<>
348 struct wxArgNormalizerUtf8<const std::string&>
349 : public wxArgNormalizerUtf8<const char*>
350 {
351 wxArgNormalizerUtf8(const std::string& s)
352 : wxArgNormalizerUtf8<const char*>(s.c_str()) {}
353 };
354
355 template<>
356 struct wxArgNormalizerUtf8<const wxStdWideString&>
357 : public wxArgNormalizerUtf8<const wchar_t*>
358 {
359 wxArgNormalizerUtf8(const wxStdWideString& s)
360 : wxArgNormalizerUtf8<const wchar_t*>(s.c_str()) {}
361 };
362 #endif // wxUSE_UNICODE_UTF8
363
364 WX_ARG_NORMALIZER_FORWARD(std::string, const std::string&);
365 WX_ARG_NORMALIZER_FORWARD(wxStdWideString, const wxStdWideString&);
366
367 #endif // wxUSE_STD_STRING
368
369
370 #undef WX_ARG_NORMALIZER_FORWARD
371 #undef _WX_ARG_NORMALIZER_FORWARD_IMPL
372
373 // ----------------------------------------------------------------------------
374 // WX_VA_ARG_STRING
375 // ----------------------------------------------------------------------------
376
377 // Replacement for va_arg() for use with strings in functions that accept
378 // strings normalized by wxArgNormalizer<T>:
379
380 struct WXDLLIMPEXP_BASE wxArgNormalizedString
381 {
382 wxArgNormalizedString(const void* ptr) : m_ptr(ptr) {}
383
384 // returns true if non-NULL string was passed in
385 bool IsValid() const { return m_ptr != NULL; }
386 operator bool() const { return IsValid(); }
387
388 // extracts the string, returns empty string if NULL was passed in
389 wxString GetString() const;
390 operator wxString() const;
391
392 private:
393 const void *m_ptr;
394 };
395
396 #define WX_VA_ARG_STRING(ap) wxArgNormalizedString(va_arg(ap, const void*))
397
398 // ----------------------------------------------------------------------------
399 // implementation of the WX_DEFINE_VARARG_* macros
400 // ----------------------------------------------------------------------------
401
402 // NB: The vararg emulation code is limited to 30 variadic and 4 fixed
403 // arguments at the moment.
404 // If you need more variadic arguments, you need to
405 // 1) increase the value of _WX_VARARG_MAX_ARGS
406 // 2) add _WX_VARARG_JOIN_* and _WX_VARARG_ITER_* up to the new
407 // _WX_VARARG_MAX_ARGS value to the lists below
408 // If you need more fixed arguments, you need to
409 // 1) increase the value of _WX_VARARG_MAX_FIXED_ARGS
410 // 2) add _WX_VARARG_FIXED_EXPAND_* and _WX_VARARG_FIXED_UNUSED_EXPAND_*
411 // macros below
412 #define _WX_VARARG_MAX_ARGS 30
413 #define _WX_VARARG_MAX_FIXED_ARGS 4
414
415 #define _WX_VARARG_JOIN_1(m) m(1)
416 #define _WX_VARARG_JOIN_2(m) _WX_VARARG_JOIN_1(m), m(2)
417 #define _WX_VARARG_JOIN_3(m) _WX_VARARG_JOIN_2(m), m(3)
418 #define _WX_VARARG_JOIN_4(m) _WX_VARARG_JOIN_3(m), m(4)
419 #define _WX_VARARG_JOIN_5(m) _WX_VARARG_JOIN_4(m), m(5)
420 #define _WX_VARARG_JOIN_6(m) _WX_VARARG_JOIN_5(m), m(6)
421 #define _WX_VARARG_JOIN_7(m) _WX_VARARG_JOIN_6(m), m(7)
422 #define _WX_VARARG_JOIN_8(m) _WX_VARARG_JOIN_7(m), m(8)
423 #define _WX_VARARG_JOIN_9(m) _WX_VARARG_JOIN_8(m), m(9)
424 #define _WX_VARARG_JOIN_10(m) _WX_VARARG_JOIN_9(m), m(10)
425 #define _WX_VARARG_JOIN_11(m) _WX_VARARG_JOIN_10(m), m(11)
426 #define _WX_VARARG_JOIN_12(m) _WX_VARARG_JOIN_11(m), m(12)
427 #define _WX_VARARG_JOIN_13(m) _WX_VARARG_JOIN_12(m), m(13)
428 #define _WX_VARARG_JOIN_14(m) _WX_VARARG_JOIN_13(m), m(14)
429 #define _WX_VARARG_JOIN_15(m) _WX_VARARG_JOIN_14(m), m(15)
430 #define _WX_VARARG_JOIN_16(m) _WX_VARARG_JOIN_15(m), m(16)
431 #define _WX_VARARG_JOIN_17(m) _WX_VARARG_JOIN_16(m), m(17)
432 #define _WX_VARARG_JOIN_18(m) _WX_VARARG_JOIN_17(m), m(18)
433 #define _WX_VARARG_JOIN_19(m) _WX_VARARG_JOIN_18(m), m(19)
434 #define _WX_VARARG_JOIN_20(m) _WX_VARARG_JOIN_19(m), m(20)
435 #define _WX_VARARG_JOIN_21(m) _WX_VARARG_JOIN_20(m), m(21)
436 #define _WX_VARARG_JOIN_22(m) _WX_VARARG_JOIN_21(m), m(22)
437 #define _WX_VARARG_JOIN_23(m) _WX_VARARG_JOIN_22(m), m(23)
438 #define _WX_VARARG_JOIN_24(m) _WX_VARARG_JOIN_23(m), m(24)
439 #define _WX_VARARG_JOIN_25(m) _WX_VARARG_JOIN_24(m), m(25)
440 #define _WX_VARARG_JOIN_26(m) _WX_VARARG_JOIN_25(m), m(26)
441 #define _WX_VARARG_JOIN_27(m) _WX_VARARG_JOIN_26(m), m(27)
442 #define _WX_VARARG_JOIN_28(m) _WX_VARARG_JOIN_27(m), m(28)
443 #define _WX_VARARG_JOIN_29(m) _WX_VARARG_JOIN_28(m), m(29)
444 #define _WX_VARARG_JOIN_30(m) _WX_VARARG_JOIN_29(m), m(30)
445
446 #define _WX_VARARG_ITER_1(m,a,b,c,d,e,f) m(1,a,b,c,d,e,f)
447 #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)
448 #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)
449 #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)
450 #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)
451 #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)
452 #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)
453 #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)
454 #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)
455 #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)
456 #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)
457 #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)
458 #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)
459 #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)
460 #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)
461 #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)
462 #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)
463 #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)
464 #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)
465 #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)
466 #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)
467 #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)
468 #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)
469 #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)
470 #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)
471 #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)
472 #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)
473 #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)
474 #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)
475 #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)
476
477
478 #define _WX_VARARG_FIXED_EXPAND_1(t1) \
479 t1 f1
480 #define _WX_VARARG_FIXED_EXPAND_2(t1,t2) \
481 t1 f1, t2 f2
482 #define _WX_VARARG_FIXED_EXPAND_3(t1,t2,t3) \
483 t1 f1, t2 f2, t3 f3
484 #define _WX_VARARG_FIXED_EXPAND_4(t1,t2,t3,t4) \
485 t1 f1, t2 f2, t3 f3, t4 f4
486
487 #define _WX_VARARG_FIXED_UNUSED_EXPAND_1(t1) \
488 t1 WXUNUSED(f1)
489 #define _WX_VARARG_FIXED_UNUSED_EXPAND_2(t1,t2) \
490 t1 WXUNUSED(f1), t2 WXUNUSED(f2)
491 #define _WX_VARARG_FIXED_UNUSED_EXPAND_3(t1,t2,t3) \
492 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3)
493 #define _WX_VARARG_FIXED_UNUSED_EXPAND_4(t1,t2,t3,t4) \
494 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3), t4 WXUNUSED(f4)
495
496
497 // This macro expands N-items tuple of fixed arguments types into part of
498 // function's declaration. For example,
499 // "_WX_VARARG_FIXED_EXPAND(3, (int, char*, int))" expands into
500 // "int f1, char* f2, int f3".
501 #define _WX_VARARG_FIXED_EXPAND(N, args) \
502 _WX_VARARG_FIXED_EXPAND_IMPL(N, args)
503 #define _WX_VARARG_FIXED_EXPAND_IMPL(N, args) \
504 _WX_VARARG_FIXED_EXPAND_##N args
505
506 // Ditto for unused arguments
507 #define _WX_VARARG_FIXED_UNUSED_EXPAND(N, args) \
508 _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args)
509 #define _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args) \
510 _WX_VARARG_FIXED_UNUSED_EXPAND_##N args
511
512
513 // This macro calls another macro 'm' passed as second argument 'N' times,
514 // with its only argument set to 1..N, and concatenates the results using
515 // comma as separator.
516 //
517 // An example:
518 // #define foo(i) x##i
519 // // this expands to "x1,x2,x3,x4"
520 // _WX_VARARG_JOIN(4, foo)
521 //
522 //
523 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
524 #define _WX_VARARG_JOIN(N, m) _WX_VARARG_JOIN_IMPL(N, m)
525 #define _WX_VARARG_JOIN_IMPL(N, m) _WX_VARARG_JOIN_##N(m)
526
527
528 // This macro calls another macro 'm' passed as second argument 'N' times, with
529 // its first argument set to 1..N and the remaining arguments set to 'a', 'b',
530 // 'c', 'd', 'e' and 'f'. The results are separated with whitespace in the
531 // expansion.
532 //
533 // An example:
534 // // this macro expands to:
535 // // foo(1,a,b,c,d,e,f)
536 // // foo(2,a,b,c,d,e,f)
537 // // foo(3,a,b,c,d,e,f)
538 // _WX_VARARG_ITER(3, foo, a, b, c, d, e, f)
539 //
540 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
541 #define _WX_VARARG_ITER(N,m,a,b,c,d,e,f) \
542 _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f)
543 #define _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f) \
544 _WX_VARARG_ITER_##N(m,a,b,c,d,e,f)
545
546 // Generates code snippet for i-th "variadic" argument in vararg function's
547 // prototype:
548 #define _WX_VARARG_ARG(i) T##i a##i
549
550 // Like _WX_VARARG_ARG_UNUSED, but outputs argument's type with WXUNUSED:
551 #define _WX_VARARG_ARG_UNUSED(i) T##i WXUNUSED(a##i)
552
553 // Generates code snippet for i-th type in vararg function's template<...>:
554 #define _WX_VARARG_TEMPL(i) typename T##i
555
556 // Generates code snippet for passing i-th argument of vararg function
557 // wrapper to its implementation, normalizing it in the process:
558 #define _WX_VARARG_PASS_WCHAR(i) wxArgNormalizerWchar<T##i>(a##i).get()
559 #define _WX_VARARG_PASS_UTF8(i) wxArgNormalizerUtf8<T##i>(a##i).get()
560
561
562 // And the same for fixed arguments, _not_ normalizing it:
563 // FIXME-UTF8: this works, but uses wxString's implicit conversion to wxChar*
564 // for the 'format' argument (which is const wxString&) _if_ the
565 // implementation function has C sting argument; we need to
566 // have wxFixedArgNormalizer<T> here that will pass everything
567 // as-is except for wxString (for which wx_str() would be used),
568 // but OTOH, we don't want to do that if the implementation takes
569 // wxString argument
570 #define _WX_VARARG_PASS_FIXED(i) f##i
571
572 #if wxUSE_UNICODE_UTF8
573 #define _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed) \
574 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
575 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_UTF8))
576 #define _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed) \
577 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
578 #endif // wxUSE_UNICODE_UTF8
579
580 #define _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed) \
581 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
582 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WCHAR))
583 #define _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed) \
584 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
585
586 #if wxUSE_UNICODE_UTF8
587 #if wxUSE_UTF8_LOCALE_ONLY
588 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_UTF8
589 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_UTF8
590 #else // possibly non-UTF8 locales
591 #define _WX_VARARG_DO_CALL(return_kw, impl, implUtf8, N, numfixed) \
592 if ( wxLocaleIsUtf8 ) \
593 _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed);\
594 else \
595 _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed)
596
597 #define _WX_VARARG_DO_CALL0(return_kw, impl, implUtf8, numfixed) \
598 if ( wxLocaleIsUtf8 ) \
599 _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed); \
600 else \
601 _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed)
602 #endif // wxUSE_UTF8_LOCALE_ONLY or not
603 #else // wxUSE_UNICODE_WCHAR or ANSI
604 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_WCHAR
605 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_WCHAR
606 #endif // wxUSE_UNICODE_UTF8 / wxUSE_UNICODE_WCHAR
607
608
609 // Macro to be used with _WX_VARARG_ITER in the implementation of
610 // WX_DEFINE_VARARG_FUNC (see its documentation for the meaning of arguments)
611 #define _WX_VARARG_DEFINE_FUNC(N, rettype, name, \
612 impl, implUtf8, numfixed, fixed) \
613 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
614 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
615 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
616 { \
617 _WX_VARARG_DO_CALL(return, impl, implUtf8, N, numfixed); \
618 }
619
620 #define _WX_VARARG_DEFINE_FUNC_N0(rettype, name, \
621 impl, implUtf8, numfixed, fixed) \
622 inline rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
623 { \
624 _WX_VARARG_DO_CALL0(return, impl, implUtf8, numfixed); \
625 }
626
627 // Macro to be used with _WX_VARARG_ITER in the implementation of
628 // WX_DEFINE_VARARG_FUNC_VOID (see its documentation for the meaning of
629 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
630 // requirements).
631 #define _WX_VARARG_DEFINE_FUNC_VOID(N, rettype, name, \
632 impl, implUtf8, numfixed, fixed) \
633 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
634 void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
635 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
636 { \
637 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
638 impl, implUtf8, N, numfixed); \
639 }
640
641 #define _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
642 inline void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
643 { \
644 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
645 impl, implUtf8, numfixed); \
646 }
647
648 // Macro to be used with _WX_VARARG_ITER in the implementation of
649 // WX_DEFINE_VARARG_FUNC_CTOR (see its documentation for the meaning of
650 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
651 // requirements).
652 #define _WX_VARARG_DEFINE_FUNC_CTOR(N, rettype, name, \
653 impl, implUtf8, numfixed, fixed) \
654 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
655 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
656 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
657 { \
658 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
659 impl, implUtf8, N, numfixed); \
660 }
661
662 #define _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
663 inline name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
664 { \
665 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
666 impl, implUtf8, numfixed); \
667 }
668
669 // Macro to be used with _WX_VARARG_ITER in the implementation of
670 // WX_DEFINE_VARARG_FUNC_NOP, i.e. empty stub for a disabled vararg function.
671 // The rettype and impl arguments are ignored.
672 #define _WX_VARARG_DEFINE_FUNC_NOP(N, rettype, name, \
673 impl, implUtf8, numfixed, fixed) \
674 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
675 void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed), \
676 _WX_VARARG_JOIN(N, _WX_VARARG_ARG_UNUSED)) \
677 {}
678
679 #define _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
680 inline void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed)) \
681 {}
682
683 #endif // _WX_STRVARARG_H_