use wxFormatString helper class for 'vararg' functions' format argument; this prepare...
[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_FUNC(rettype, name, numfixed, fixed, impl, implUtf8) \
74 _WX_VARARG_DEFINE_FUNC_N0(rettype, name, impl, implUtf8, numfixed, fixed) \
75 WX_DEFINE_VARARG_FUNC_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_FUNC_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_FUNC, but for variadic functions that don't return
85 // a value.
86 #define WX_DEFINE_VARARG_FUNC_VOID(name, numfixed, fixed, impl, implUtf8) \
87 _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
88 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
89 _WX_VARARG_DEFINE_FUNC_VOID, \
90 void, name, impl, implUtf8, numfixed, fixed)
91
92 // Like WX_DEFINE_VARARG_FUNC_VOID, but instead of wrapping an implementation
93 // function, does nothing in defined functions' bodies.
94 //
95 // Used to implement wxLogXXX functions if wxUSE_LOG=0.
96 #define WX_DEFINE_VARARG_FUNC_NOP(name, numfixed, fixed) \
97 _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
98 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
99 _WX_VARARG_DEFINE_FUNC_NOP, \
100 void, name, dummy, dummy, numfixed, fixed)
101
102 // Like WX_DEFINE_VARARG_FUNC_CTOR, but for defining template constructors
103 #define WX_DEFINE_VARARG_FUNC_CTOR(name, numfixed, fixed, impl, implUtf8) \
104 _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
105 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
106 _WX_VARARG_DEFINE_FUNC_CTOR, \
107 void, name, impl, implUtf8, numfixed, fixed)
108
109
110 // ----------------------------------------------------------------------------
111 // wxFormatString
112 // ----------------------------------------------------------------------------
113
114 // This class should be used for format string argument of the functions
115 // defined using WX_DEFINE_VARARG_FUNC_* macros. It converts the string to
116 // char* or wchar_t* for passing to implementation function efficiently (i.e.
117 // without keeping the converted string in memory for longer than necessary,
118 // like c_str())
119 //
120 // Note that this class can _only_ be used for function arguments!
121 class WXDLLIMPEXP_BASE wxFormatString
122 {
123 public:
124 wxFormatString(const char *str)
125 : m_char(wxCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
126 wxFormatString(const wchar_t *str)
127 : m_wchar(wxWCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
128 wxFormatString(const wxString& str)
129 : m_str(&str), m_cstr(NULL) {}
130 wxFormatString(const wxCStrData& str)
131 : m_str(NULL), m_cstr(&str) {}
132 wxFormatString(const wxCharBuffer& str)
133 : m_char(str), m_str(NULL), m_cstr(NULL) {}
134 wxFormatString(const wxWCharBuffer& str)
135 : m_wchar(str), m_str(NULL), m_cstr(NULL) {}
136
137 #if !wxUSE_UNICODE_WCHAR
138 operator const char*() const
139 { return wx_const_cast(wxFormatString*, this)->AsChar(); }
140 private:
141 const char* AsChar();
142 #endif // !wxUSE_UNICODE_WCHAR
143
144 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
145 operator const wchar_t*() const
146 { return wx_const_cast(wxFormatString*, this)->AsWChar(); }
147 private:
148 const wchar_t* AsWChar();
149 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
150
151 private:
152 wxCharBuffer m_char;
153 wxWCharBuffer m_wchar;
154 // NB: we can use a pointer here, because wxFormatString is only used
155 // as function argument, so it has shorter life than the string
156 // passed to the ctor
157 const wxString * const m_str;
158 const wxCStrData * const m_cstr;
159
160 DECLARE_NO_COPY_CLASS(wxFormatString)
161 };
162
163 // ----------------------------------------------------------------------------
164 // wxArgNormalizer*<T> converters
165 // ----------------------------------------------------------------------------
166
167 // Converts an argument passed to wxPrint etc. into standard form expected,
168 // by wxXXX functions, e.g. all strings (wxString, char*, wchar_t*) are
169 // converted into wchar_t* or char* depending on the build.
170 template<typename T>
171 struct wxArgNormalizer
172 {
173 wxArgNormalizer(const T& value) : m_value(value) {}
174
175 // Returns the value in a form that can be safely passed to real vararg
176 // functions. In case of strings, this is char* in ANSI build and wchar_t*
177 // in Unicode build.
178 T get() const { return m_value; }
179
180 T m_value;
181 };
182
183 // normalizer for passing arguments to functions working with wchar_t* (and
184 // until ANSI build is removed, char* in ANSI build as well - FIXME-UTF8)
185 // string representation
186 #if !wxUSE_UTF8_LOCALE_ONLY
187 template<typename T>
188 struct wxArgNormalizerWchar : public wxArgNormalizer<T>
189 {
190 wxArgNormalizerWchar(const T& value) : wxArgNormalizer<T>(value) {}
191 };
192 #endif // !wxUSE_UTF8_LOCALE_ONLY
193
194 // normalizer for passing arguments to functions working with UTF-8 encoded
195 // char* strings
196 #if wxUSE_UNICODE_UTF8
197 template<typename T>
198 struct wxArgNormalizerUtf8 : public wxArgNormalizer<T>
199 {
200 wxArgNormalizerUtf8(const T& value) : wxArgNormalizer<T>(value) {}
201 };
202
203 #define wxArgNormalizerNative wxArgNormalizerUtf8
204 #else // wxUSE_UNICODE_WCHAR
205 #define wxArgNormalizerNative wxArgNormalizerWchar
206 #endif // wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_UTF8
207
208
209
210 // special cases for converting strings:
211
212
213 // base class for wxArgNormalizer<T> specializations that need to do conversion;
214 // CharType is either wxStringCharType or wchar_t in UTF-8 build when wrapping
215 // widechar CRT function
216 template<typename CharType>
217 struct wxArgNormalizerWithBuffer
218 {
219 typedef wxCharTypeBuffer<CharType> CharBuffer;
220
221 wxArgNormalizerWithBuffer() {}
222 wxArgNormalizerWithBuffer(const CharBuffer& buf) : m_value(buf) {}
223
224 const CharType *get() const { return m_value; }
225
226 CharBuffer m_value;
227 };
228
229 // string objects:
230 template<>
231 struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxString&>
232 {
233 wxArgNormalizerNative(const wxString& s) : m_value(s) {}
234 const wxStringCharType *get() const;
235
236 const wxString& m_value;
237 };
238
239 // c_str() values:
240 template<>
241 struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxCStrData&>
242 {
243 wxArgNormalizerNative(const wxCStrData& value) : m_value(value) {}
244 const wxStringCharType *get() const;
245
246 const wxCStrData& m_value;
247 };
248
249 // wxString/wxCStrData conversion to wchar_t* value
250 #if wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
251 template<>
252 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxString&>
253 : public wxArgNormalizerWithBuffer<wchar_t>
254 {
255 wxArgNormalizerWchar(const wxString& s);
256 };
257
258 template<>
259 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxCStrData&>
260 : public wxArgNormalizerWithBuffer<wchar_t>
261 {
262 wxArgNormalizerWchar(const wxCStrData& s);
263 };
264 #endif // wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
265
266
267 // C string pointers of the wrong type (wchar_t* for ANSI or UTF8 build,
268 // char* for wchar_t Unicode build or UTF8):
269 #if wxUSE_UNICODE_WCHAR
270
271 template<>
272 struct wxArgNormalizerWchar<const char*>
273 : public wxArgNormalizerWithBuffer<wchar_t>
274 {
275 wxArgNormalizerWchar(const char* s)
276 : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s)) {}
277 };
278
279 #elif wxUSE_UNICODE_UTF8
280
281 template<>
282 struct wxArgNormalizerUtf8<const wchar_t*>
283 : public wxArgNormalizerWithBuffer<char>
284 {
285 wxArgNormalizerUtf8(const wchar_t* s)
286 : wxArgNormalizerWithBuffer<char>(wxConvUTF8.cWC2MB(s)) {}
287 };
288
289 template<>
290 struct wxArgNormalizerUtf8<const char*>
291 : public wxArgNormalizerWithBuffer<char>
292 {
293 wxArgNormalizerUtf8(const char* s)
294 {
295 if ( wxLocaleIsUtf8 )
296 {
297 m_value = wxCharBuffer::CreateNonOwned(s);
298 }
299 else
300 {
301 // convert to widechar string first:
302 wxWCharBuffer buf(wxConvLibc.cMB2WC(s));
303
304 // then to UTF-8:
305 if ( buf )
306 m_value = wxConvUTF8.cWC2MB(buf);
307 }
308 }
309 };
310
311 // UTF-8 build needs conversion to wchar_t* too:
312 #if !wxUSE_UTF8_LOCALE_ONLY
313 template<>
314 struct wxArgNormalizerWchar<const char*>
315 : public wxArgNormalizerWithBuffer<wchar_t>
316 {
317 wxArgNormalizerWchar(const char* s)
318 : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s)) {}
319 };
320 #endif // !wxUSE_UTF8_LOCALE_ONLY
321
322 #else // ANSI - FIXME-UTF8
323
324 template<>
325 struct wxArgNormalizerWchar<const wchar_t*>
326 : public wxArgNormalizerWithBuffer<char>
327 {
328 wxArgNormalizerWchar(const wchar_t* s)
329 : wxArgNormalizerWithBuffer<char>(wxConvLibc.cWC2MB(s)) {}
330 };
331
332 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8/ANSI
333
334
335 // this macro is used to implement specialization that are exactly same as
336 // some other specialization, i.e. to "forward" the implementation (e.g. for
337 // T=wxString and T=const wxString&). Note that the ctor takes BaseT argument,
338 // not T!
339 #if wxUSE_UNICODE_UTF8
340 #if wxUSE_UTF8_LOCALE_ONLY
341 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
342 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
343 #else // possibly non-UTF8 locales
344 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
345 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT); \
346 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
347 #endif
348 #else // wxUSE_UNICODE_WCHAR
349 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
350 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT)
351 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
352
353 #define _WX_ARG_NORMALIZER_FORWARD_IMPL(Normalizer, T, BaseT) \
354 template<> \
355 struct Normalizer<T> : public Normalizer<BaseT> \
356 { \
357 Normalizer(BaseT value) : Normalizer<BaseT>(value) {} \
358 }
359
360 // non-reference versions of specializations for string objects
361 WX_ARG_NORMALIZER_FORWARD(wxString, const wxString&);
362 WX_ARG_NORMALIZER_FORWARD(wxCStrData, const wxCStrData&);
363
364 // versions for passing non-const pointers:
365 WX_ARG_NORMALIZER_FORWARD(char*, const char*);
366 WX_ARG_NORMALIZER_FORWARD(wchar_t*, const wchar_t*);
367
368 // versions for passing wx[W]CharBuffer:
369 WX_ARG_NORMALIZER_FORWARD(wxCharBuffer, const char*);
370 WX_ARG_NORMALIZER_FORWARD(const wxCharBuffer&, const char*);
371 WX_ARG_NORMALIZER_FORWARD(wxWCharBuffer, const wchar_t*);
372 WX_ARG_NORMALIZER_FORWARD(const wxWCharBuffer&, const wchar_t*);
373
374 // versions for std::[w]string:
375 #if wxUSE_STD_STRING
376
377 #include "wx/stringimpl.h"
378
379 #if !wxUSE_UTF8_LOCALE_ONLY
380 template<>
381 struct wxArgNormalizerWchar<const std::string&>
382 : public wxArgNormalizerWchar<const char*>
383 {
384 wxArgNormalizerWchar(const std::string& s)
385 : wxArgNormalizerWchar<const char*>(s.c_str()) {}
386 };
387
388 template<>
389 struct wxArgNormalizerWchar<const wxStdWideString&>
390 : public wxArgNormalizerWchar<const wchar_t*>
391 {
392 wxArgNormalizerWchar(const wxStdWideString& s)
393 : wxArgNormalizerWchar<const wchar_t*>(s.c_str()) {}
394 };
395 #endif // !wxUSE_UTF8_LOCALE_ONLY
396
397 #if wxUSE_UNICODE_UTF8
398 template<>
399 struct wxArgNormalizerUtf8<const std::string&>
400 : public wxArgNormalizerUtf8<const char*>
401 {
402 wxArgNormalizerUtf8(const std::string& s)
403 : wxArgNormalizerUtf8<const char*>(s.c_str()) {}
404 };
405
406 template<>
407 struct wxArgNormalizerUtf8<const wxStdWideString&>
408 : public wxArgNormalizerUtf8<const wchar_t*>
409 {
410 wxArgNormalizerUtf8(const wxStdWideString& s)
411 : wxArgNormalizerUtf8<const wchar_t*>(s.c_str()) {}
412 };
413 #endif // wxUSE_UNICODE_UTF8
414
415 WX_ARG_NORMALIZER_FORWARD(std::string, const std::string&);
416 WX_ARG_NORMALIZER_FORWARD(wxStdWideString, const wxStdWideString&);
417
418 #endif // wxUSE_STD_STRING
419
420
421 #undef WX_ARG_NORMALIZER_FORWARD
422 #undef _WX_ARG_NORMALIZER_FORWARD_IMPL
423
424 // ----------------------------------------------------------------------------
425 // WX_VA_ARG_STRING
426 // ----------------------------------------------------------------------------
427
428 // Replacement for va_arg() for use with strings in functions that accept
429 // strings normalized by wxArgNormalizer<T>:
430
431 struct WXDLLIMPEXP_BASE wxArgNormalizedString
432 {
433 wxArgNormalizedString(const void* ptr) : m_ptr(ptr) {}
434
435 // returns true if non-NULL string was passed in
436 bool IsValid() const { return m_ptr != NULL; }
437 operator bool() const { return IsValid(); }
438
439 // extracts the string, returns empty string if NULL was passed in
440 wxString GetString() const;
441 operator wxString() const;
442
443 private:
444 const void *m_ptr;
445 };
446
447 #define WX_VA_ARG_STRING(ap) wxArgNormalizedString(va_arg(ap, const void*))
448
449 // ----------------------------------------------------------------------------
450 // implementation of the WX_DEFINE_VARARG_* macros
451 // ----------------------------------------------------------------------------
452
453 // NB: The vararg emulation code is limited to 30 variadic and 4 fixed
454 // arguments at the moment.
455 // If you need more variadic arguments, you need to
456 // 1) increase the value of _WX_VARARG_MAX_ARGS
457 // 2) add _WX_VARARG_JOIN_* and _WX_VARARG_ITER_* up to the new
458 // _WX_VARARG_MAX_ARGS value to the lists below
459 // If you need more fixed arguments, you need to
460 // 1) increase the value of _WX_VARARG_MAX_FIXED_ARGS
461 // 2) add _WX_VARARG_FIXED_EXPAND_* and _WX_VARARG_FIXED_UNUSED_EXPAND_*
462 // macros below
463 #define _WX_VARARG_MAX_ARGS 30
464 #define _WX_VARARG_MAX_FIXED_ARGS 4
465
466 #define _WX_VARARG_JOIN_1(m) m(1)
467 #define _WX_VARARG_JOIN_2(m) _WX_VARARG_JOIN_1(m), m(2)
468 #define _WX_VARARG_JOIN_3(m) _WX_VARARG_JOIN_2(m), m(3)
469 #define _WX_VARARG_JOIN_4(m) _WX_VARARG_JOIN_3(m), m(4)
470 #define _WX_VARARG_JOIN_5(m) _WX_VARARG_JOIN_4(m), m(5)
471 #define _WX_VARARG_JOIN_6(m) _WX_VARARG_JOIN_5(m), m(6)
472 #define _WX_VARARG_JOIN_7(m) _WX_VARARG_JOIN_6(m), m(7)
473 #define _WX_VARARG_JOIN_8(m) _WX_VARARG_JOIN_7(m), m(8)
474 #define _WX_VARARG_JOIN_9(m) _WX_VARARG_JOIN_8(m), m(9)
475 #define _WX_VARARG_JOIN_10(m) _WX_VARARG_JOIN_9(m), m(10)
476 #define _WX_VARARG_JOIN_11(m) _WX_VARARG_JOIN_10(m), m(11)
477 #define _WX_VARARG_JOIN_12(m) _WX_VARARG_JOIN_11(m), m(12)
478 #define _WX_VARARG_JOIN_13(m) _WX_VARARG_JOIN_12(m), m(13)
479 #define _WX_VARARG_JOIN_14(m) _WX_VARARG_JOIN_13(m), m(14)
480 #define _WX_VARARG_JOIN_15(m) _WX_VARARG_JOIN_14(m), m(15)
481 #define _WX_VARARG_JOIN_16(m) _WX_VARARG_JOIN_15(m), m(16)
482 #define _WX_VARARG_JOIN_17(m) _WX_VARARG_JOIN_16(m), m(17)
483 #define _WX_VARARG_JOIN_18(m) _WX_VARARG_JOIN_17(m), m(18)
484 #define _WX_VARARG_JOIN_19(m) _WX_VARARG_JOIN_18(m), m(19)
485 #define _WX_VARARG_JOIN_20(m) _WX_VARARG_JOIN_19(m), m(20)
486 #define _WX_VARARG_JOIN_21(m) _WX_VARARG_JOIN_20(m), m(21)
487 #define _WX_VARARG_JOIN_22(m) _WX_VARARG_JOIN_21(m), m(22)
488 #define _WX_VARARG_JOIN_23(m) _WX_VARARG_JOIN_22(m), m(23)
489 #define _WX_VARARG_JOIN_24(m) _WX_VARARG_JOIN_23(m), m(24)
490 #define _WX_VARARG_JOIN_25(m) _WX_VARARG_JOIN_24(m), m(25)
491 #define _WX_VARARG_JOIN_26(m) _WX_VARARG_JOIN_25(m), m(26)
492 #define _WX_VARARG_JOIN_27(m) _WX_VARARG_JOIN_26(m), m(27)
493 #define _WX_VARARG_JOIN_28(m) _WX_VARARG_JOIN_27(m), m(28)
494 #define _WX_VARARG_JOIN_29(m) _WX_VARARG_JOIN_28(m), m(29)
495 #define _WX_VARARG_JOIN_30(m) _WX_VARARG_JOIN_29(m), m(30)
496
497 #define _WX_VARARG_ITER_1(m,a,b,c,d,e,f) m(1,a,b,c,d,e,f)
498 #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)
499 #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)
500 #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)
501 #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)
502 #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)
503 #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)
504 #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)
505 #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)
506 #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)
507 #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)
508 #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)
509 #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)
510 #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)
511 #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)
512 #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)
513 #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)
514 #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)
515 #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)
516 #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)
517 #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)
518 #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)
519 #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)
520 #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)
521 #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)
522 #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)
523 #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)
524 #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)
525 #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)
526 #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)
527
528
529 #define _WX_VARARG_FIXED_EXPAND_1(t1) \
530 t1 f1
531 #define _WX_VARARG_FIXED_EXPAND_2(t1,t2) \
532 t1 f1, t2 f2
533 #define _WX_VARARG_FIXED_EXPAND_3(t1,t2,t3) \
534 t1 f1, t2 f2, t3 f3
535 #define _WX_VARARG_FIXED_EXPAND_4(t1,t2,t3,t4) \
536 t1 f1, t2 f2, t3 f3, t4 f4
537
538 #define _WX_VARARG_FIXED_UNUSED_EXPAND_1(t1) \
539 t1 WXUNUSED(f1)
540 #define _WX_VARARG_FIXED_UNUSED_EXPAND_2(t1,t2) \
541 t1 WXUNUSED(f1), t2 WXUNUSED(f2)
542 #define _WX_VARARG_FIXED_UNUSED_EXPAND_3(t1,t2,t3) \
543 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3)
544 #define _WX_VARARG_FIXED_UNUSED_EXPAND_4(t1,t2,t3,t4) \
545 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3), t4 WXUNUSED(f4)
546
547
548 // This macro expands N-items tuple of fixed arguments types into part of
549 // function's declaration. For example,
550 // "_WX_VARARG_FIXED_EXPAND(3, (int, char*, int))" expands into
551 // "int f1, char* f2, int f3".
552 #define _WX_VARARG_FIXED_EXPAND(N, args) \
553 _WX_VARARG_FIXED_EXPAND_IMPL(N, args)
554 #define _WX_VARARG_FIXED_EXPAND_IMPL(N, args) \
555 _WX_VARARG_FIXED_EXPAND_##N args
556
557 // Ditto for unused arguments
558 #define _WX_VARARG_FIXED_UNUSED_EXPAND(N, args) \
559 _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args)
560 #define _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args) \
561 _WX_VARARG_FIXED_UNUSED_EXPAND_##N args
562
563
564 // This macro calls another macro 'm' passed as second argument 'N' times,
565 // with its only argument set to 1..N, and concatenates the results using
566 // comma as separator.
567 //
568 // An example:
569 // #define foo(i) x##i
570 // // this expands to "x1,x2,x3,x4"
571 // _WX_VARARG_JOIN(4, foo)
572 //
573 //
574 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
575 #define _WX_VARARG_JOIN(N, m) _WX_VARARG_JOIN_IMPL(N, m)
576 #define _WX_VARARG_JOIN_IMPL(N, m) _WX_VARARG_JOIN_##N(m)
577
578
579 // This macro calls another macro 'm' passed as second argument 'N' times, with
580 // its first argument set to 1..N and the remaining arguments set to 'a', 'b',
581 // 'c', 'd', 'e' and 'f'. The results are separated with whitespace in the
582 // expansion.
583 //
584 // An example:
585 // // this macro expands to:
586 // // foo(1,a,b,c,d,e,f)
587 // // foo(2,a,b,c,d,e,f)
588 // // foo(3,a,b,c,d,e,f)
589 // _WX_VARARG_ITER(3, foo, a, b, c, d, e, f)
590 //
591 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
592 #define _WX_VARARG_ITER(N,m,a,b,c,d,e,f) \
593 _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f)
594 #define _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f) \
595 _WX_VARARG_ITER_##N(m,a,b,c,d,e,f)
596
597 // Generates code snippet for i-th "variadic" argument in vararg function's
598 // prototype:
599 #define _WX_VARARG_ARG(i) T##i a##i
600
601 // Like _WX_VARARG_ARG_UNUSED, but outputs argument's type with WXUNUSED:
602 #define _WX_VARARG_ARG_UNUSED(i) T##i WXUNUSED(a##i)
603
604 // Generates code snippet for i-th type in vararg function's template<...>:
605 #define _WX_VARARG_TEMPL(i) typename T##i
606
607 // Generates code snippet for passing i-th argument of vararg function
608 // wrapper to its implementation, normalizing it in the process:
609 #define _WX_VARARG_PASS_WCHAR(i) wxArgNormalizerWchar<T##i>(a##i).get()
610 #define _WX_VARARG_PASS_UTF8(i) wxArgNormalizerUtf8<T##i>(a##i).get()
611
612
613 // And the same for fixed arguments, _not_ normalizing it:
614 // FIXME-UTF8: this works, but uses wxString's implicit conversion to wxChar*
615 // for the 'format' argument (which is const wxString&) _if_ the
616 // implementation function has C sting argument; we need to
617 // have wxFixedArgNormalizer<T> here that will pass everything
618 // as-is except for wxString (for which wx_str() would be used),
619 // but OTOH, we don't want to do that if the implementation takes
620 // wxString argument
621 #define _WX_VARARG_PASS_FIXED(i) f##i
622
623 #if wxUSE_UNICODE_UTF8
624 #define _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed) \
625 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
626 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_UTF8))
627 #define _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed) \
628 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
629 #endif // wxUSE_UNICODE_UTF8
630
631 #define _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed) \
632 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
633 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WCHAR))
634 #define _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed) \
635 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
636
637 #if wxUSE_UNICODE_UTF8
638 #if wxUSE_UTF8_LOCALE_ONLY
639 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_UTF8
640 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_UTF8
641 #else // possibly non-UTF8 locales
642 #define _WX_VARARG_DO_CALL(return_kw, impl, implUtf8, N, numfixed) \
643 if ( wxLocaleIsUtf8 ) \
644 _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed);\
645 else \
646 _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed)
647
648 #define _WX_VARARG_DO_CALL0(return_kw, impl, implUtf8, numfixed) \
649 if ( wxLocaleIsUtf8 ) \
650 _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed); \
651 else \
652 _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed)
653 #endif // wxUSE_UTF8_LOCALE_ONLY or not
654 #else // wxUSE_UNICODE_WCHAR or ANSI
655 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_WCHAR
656 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_WCHAR
657 #endif // wxUSE_UNICODE_UTF8 / wxUSE_UNICODE_WCHAR
658
659
660 // Macro to be used with _WX_VARARG_ITER in the implementation of
661 // WX_DEFINE_VARARG_FUNC (see its documentation for the meaning of arguments)
662 #define _WX_VARARG_DEFINE_FUNC(N, rettype, name, \
663 impl, implUtf8, numfixed, fixed) \
664 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
665 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
666 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
667 { \
668 _WX_VARARG_DO_CALL(return, impl, implUtf8, N, numfixed); \
669 }
670
671 #define _WX_VARARG_DEFINE_FUNC_N0(rettype, name, \
672 impl, implUtf8, numfixed, fixed) \
673 inline rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
674 { \
675 _WX_VARARG_DO_CALL0(return, impl, implUtf8, numfixed); \
676 }
677
678 // Macro to be used with _WX_VARARG_ITER in the implementation of
679 // WX_DEFINE_VARARG_FUNC_VOID (see its documentation for the meaning of
680 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
681 // requirements).
682 #define _WX_VARARG_DEFINE_FUNC_VOID(N, rettype, name, \
683 impl, implUtf8, numfixed, fixed) \
684 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
685 void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
686 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
687 { \
688 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
689 impl, implUtf8, N, numfixed); \
690 }
691
692 #define _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
693 inline void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
694 { \
695 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
696 impl, implUtf8, numfixed); \
697 }
698
699 // Macro to be used with _WX_VARARG_ITER in the implementation of
700 // WX_DEFINE_VARARG_FUNC_CTOR (see its documentation for the meaning of
701 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
702 // requirements).
703 #define _WX_VARARG_DEFINE_FUNC_CTOR(N, rettype, name, \
704 impl, implUtf8, numfixed, fixed) \
705 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
706 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
707 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
708 { \
709 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
710 impl, implUtf8, N, numfixed); \
711 }
712
713 #define _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
714 inline name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
715 { \
716 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
717 impl, implUtf8, numfixed); \
718 }
719
720 // Macro to be used with _WX_VARARG_ITER in the implementation of
721 // WX_DEFINE_VARARG_FUNC_NOP, i.e. empty stub for a disabled vararg function.
722 // The rettype and impl arguments are ignored.
723 #define _WX_VARARG_DEFINE_FUNC_NOP(N, rettype, name, \
724 impl, implUtf8, numfixed, fixed) \
725 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
726 void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed), \
727 _WX_VARARG_JOIN(N, _WX_VARARG_ARG_UNUSED)) \
728 {}
729
730 #define _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
731 inline void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed)) \
732 {}
733
734 #endif // _WX_STRVARARG_H_