]> git.saurik.com Git - wxWidgets.git/blob - include/wx/strvararg.h
21977e8e4007f1540837886ad167da2d4061f937
[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/strconv.h"
22 #include "wx/buffer.h"
23 #include "wx/unichar.h"
24
25 #if defined(HAVE_TYPE_TRAITS)
26 #include <type_traits>
27 #elif defined(HAVE_TR1_TYPE_TRAITS)
28 #ifdef __VISUALC__
29 #include <type_traits>
30 #else
31 #include <tr1/type_traits>
32 #endif
33 #endif
34
35 class WXDLLIMPEXP_FWD_BASE wxCStrData;
36 class WXDLLIMPEXP_FWD_BASE wxString;
37
38 // ----------------------------------------------------------------------------
39 // WX_DEFINE_VARARG_FUNC* macros
40 // ----------------------------------------------------------------------------
41
42 // This macro is used to implement type-safe wrappers for variadic functions
43 // that accept strings as arguments. This makes it possible to pass char*,
44 // wchar_t* or even wxString (as opposed to having to use wxString::c_str())
45 // to e.g. wxPrintf().
46 //
47 // This is done by defining a set of N template function taking 1..N arguments
48 // (currently, N is set to 30 in this header). These functions are just thin
49 // wrappers around another variadic function ('impl' or 'implUtf8' arguments,
50 // see below) and the only thing the wrapper does is that it normalizes the
51 // arguments passed in so that they are of the type expected by variadic
52 // functions taking string arguments, i.e., char* or wchar_t*, depending on the
53 // build:
54 // * char* in the current locale's charset in ANSI build
55 // * char* with UTF-8 encoding if wxUSE_UNICODE_UTF8 and the app is running
56 // under an UTF-8 locale
57 // * wchar_t* if wxUSE_UNICODE_WCHAR or if wxUSE_UNICODE_UTF8 and the current
58 // locale is not UTF-8
59 //
60 // Note that wxFormatString *must* be used for the format parameter of these
61 // functions, otherwise the implementation won't work correctly. Furthermore,
62 // it must be passed by value, not reference, because it's modified by the
63 // vararg templates internally.
64 //
65 // Parameters:
66 // [ there are examples in square brackets showing values of the parameters
67 // for the wxFprintf() wrapper for fprintf() function with the following
68 // prototype:
69 // int wxFprintf(FILE *stream, const wxString& format, ...); ]
70 //
71 // rettype Functions' return type [int]
72 // name Name of the function [fprintf]
73 // numfixed The number of leading "fixed" (i.e., not variadic)
74 // arguments of the function (e.g. "stream" and "format"
75 // arguments of fprintf()); their type is _not_ converted
76 // using wxArgNormalizer<T>, unlike the rest of
77 // the function's arguments [2]
78 // fixed List of types of the leading "fixed" arguments, in
79 // parenthesis [(FILE*,const wxString&)]
80 // impl Name of the variadic function that implements 'name' for
81 // the native strings representation (wchar_t* if
82 // wxUSE_UNICODE_WCHAR or wxUSE_UNICODE_UTF8 when running under
83 // non-UTF8 locale, char* in ANSI build) [wxCrt_Fprintf]
84 // implUtf8 Like 'impl', but for the UTF-8 char* version to be used
85 // if wxUSE_UNICODE_UTF8 and running under UTF-8 locale
86 // (ignored otherwise) [fprintf]
87 //
88 #define WX_DEFINE_VARARG_FUNC(rettype, name, numfixed, fixed, impl, implUtf8) \
89 _WX_VARARG_DEFINE_FUNC_N0(rettype, name, impl, implUtf8, numfixed, fixed) \
90 WX_DEFINE_VARARG_FUNC_SANS_N0(rettype, name, numfixed, fixed, impl, implUtf8)
91
92 // ditto, but without the version with 0 template/vararg arguments
93 #define WX_DEFINE_VARARG_FUNC_SANS_N0(rettype, name, \
94 numfixed, fixed, impl, implUtf8) \
95 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
96 _WX_VARARG_DEFINE_FUNC, \
97 rettype, name, impl, implUtf8, numfixed, fixed)
98
99 // Like WX_DEFINE_VARARG_FUNC, but for variadic functions that don't return
100 // a value.
101 #define WX_DEFINE_VARARG_FUNC_VOID(name, numfixed, fixed, impl, implUtf8) \
102 _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
103 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
104 _WX_VARARG_DEFINE_FUNC_VOID, \
105 void, name, impl, implUtf8, numfixed, fixed)
106
107 // Like WX_DEFINE_VARARG_FUNC_VOID, but instead of wrapping an implementation
108 // function, does nothing in defined functions' bodies.
109 //
110 // Used to implement wxLogXXX functions if wxUSE_LOG=0.
111 #define WX_DEFINE_VARARG_FUNC_NOP(name, numfixed, fixed) \
112 _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
113 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
114 _WX_VARARG_DEFINE_FUNC_NOP, \
115 void, name, dummy, dummy, numfixed, fixed)
116
117 // Like WX_DEFINE_VARARG_FUNC_CTOR, but for defining template constructors
118 #define WX_DEFINE_VARARG_FUNC_CTOR(name, numfixed, fixed, impl, implUtf8) \
119 _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
120 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
121 _WX_VARARG_DEFINE_FUNC_CTOR, \
122 void, name, impl, implUtf8, numfixed, fixed)
123
124
125 // ----------------------------------------------------------------------------
126 // wxFormatString
127 // ----------------------------------------------------------------------------
128
129 // This class must be used for format string argument of the functions
130 // defined using WX_DEFINE_VARARG_FUNC_* macros. It converts the string to
131 // char* or wchar_t* for passing to implementation function efficiently (i.e.
132 // without keeping the converted string in memory for longer than necessary,
133 // like c_str()). It also converts format string to the correct form that
134 // accounts for string changes done by wxArgNormalizer<>
135 //
136 // Note that this class can _only_ be used for function arguments!
137 class WXDLLIMPEXP_BASE wxFormatString
138 {
139 public:
140 wxFormatString(const char *str)
141 : m_char(wxScopedCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
142 wxFormatString(const wchar_t *str)
143 : m_wchar(wxScopedWCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
144 wxFormatString(const wxString& str)
145 : m_str(&str), m_cstr(NULL) {}
146 wxFormatString(const wxCStrData& str)
147 : m_str(NULL), m_cstr(&str) {}
148 wxFormatString(const wxScopedCharBuffer& str)
149 : m_char(str), m_str(NULL), m_cstr(NULL) {}
150 wxFormatString(const wxScopedWCharBuffer& str)
151 : m_wchar(str), m_str(NULL), m_cstr(NULL) {}
152
153 // Possible argument types. These are or-combinable for wxASSERT_ARG_TYPE
154 // convenience.
155 enum ArgumentType
156 {
157 Arg_Char = 0x0001, // character as char %c
158 Arg_Pointer = 0x0002, // %p
159 Arg_String = 0x0004, // any form of string
160
161 Arg_Int = 0x0008,
162 #if SIZEOF_INT == SIZEOF_LONG
163 Arg_LongInt = Arg_Int,
164 #else
165 Arg_LongInt = 0x0010,
166 #endif
167 #if defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG == SIZEOF_LONG
168 Arg_LongLongInt = Arg_LongInt,
169 #elif defined(wxLongLong_t)
170 Arg_LongLongInt = 0x0020,
171 #endif
172
173 Arg_Double = 0x0040,
174 Arg_LongDouble = 0x0080,
175
176 #if defined(wxSIZE_T_IS_UINT)
177 Arg_Size_t = Arg_Int,
178 #elif defined(wxSIZE_T_IS_ULONG)
179 Arg_Size_t = Arg_LongInt,
180 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_SIZE_T == SIZEOF_LONG_LONG
181 Arg_Size_t = Arg_LongLongInt,
182 #else
183 Arg_Size_t = 0x0100,
184 #endif
185
186 Arg_IntPtr = 0x0200, // %n -- store # of chars written
187 Arg_ShortIntPtr = 0x0400,
188 Arg_LongIntPtr = 0x0800,
189
190 Arg_Unknown = 0x8000 // unrecognized specifier (likely error)
191 };
192
193 // returns the type of format specifier for n-th variadic argument (this is
194 // not necessarily n-th format specifier if positional specifiers are used);
195 // called by wxArgNormalizer<> specializations to get information about
196 // n-th variadic argument desired representation
197 ArgumentType GetArgumentType(unsigned n) const;
198
199 // returns the value passed to ctor, only converted to wxString, similarly
200 // to other InputAsXXX() methods
201 wxString InputAsString() const;
202
203 #if !wxUSE_UNICODE_WCHAR
204 operator const char*() const
205 { return const_cast<wxFormatString*>(this)->AsChar(); }
206 private:
207 // InputAsChar() returns the value passed to ctor, only converted
208 // to char, while AsChar() takes the the string returned by InputAsChar()
209 // and does format string conversion on it as well (and similarly for
210 // ..AsWChar() below)
211 const char* InputAsChar();
212 const char* AsChar();
213 wxScopedCharBuffer m_convertedChar;
214 #endif // !wxUSE_UNICODE_WCHAR
215
216 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
217 public:
218 operator const wchar_t*() const
219 { return const_cast<wxFormatString*>(this)->AsWChar(); }
220 private:
221 const wchar_t* InputAsWChar();
222 const wchar_t* AsWChar();
223 wxScopedWCharBuffer m_convertedWChar;
224 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
225
226 private:
227 wxScopedCharBuffer m_char;
228 wxScopedWCharBuffer m_wchar;
229
230 // NB: we can use a pointer here, because wxFormatString is only used
231 // as function argument, so it has shorter life than the string
232 // passed to the ctor
233 const wxString * const m_str;
234 const wxCStrData * const m_cstr;
235
236 wxDECLARE_NO_ASSIGN_CLASS(wxFormatString);
237 };
238
239 // these two helper classes are used to find wxFormatString argument among fixed
240 // arguments passed to a vararg template
241 struct wxFormatStringArgument
242 {
243 wxFormatStringArgument(const wxFormatString *s = NULL) : m_str(s) {}
244 const wxFormatString *m_str;
245
246 // overriding this operator allows us to reuse _WX_VARARG_JOIN macro
247 wxFormatStringArgument operator,(const wxFormatStringArgument& a) const
248 {
249 wxASSERT_MSG( m_str == NULL || a.m_str == NULL,
250 "can't have two format strings in vararg function" );
251 return wxFormatStringArgument(m_str ? m_str : a.m_str);
252 }
253
254 operator const wxFormatString*() const { return m_str; }
255 };
256
257 template<typename T>
258 struct wxFormatStringArgumentFinder
259 {
260 static wxFormatStringArgument find(T)
261 {
262 // by default, arguments are not format strings, so return "not found"
263 return wxFormatStringArgument();
264 }
265 };
266
267 template<>
268 struct wxFormatStringArgumentFinder<const wxFormatString&>
269 {
270 static wxFormatStringArgument find(const wxFormatString& arg)
271 { return wxFormatStringArgument(&arg); }
272 };
273
274 template<>
275 struct wxFormatStringArgumentFinder<wxFormatString>
276 : public wxFormatStringArgumentFinder<const wxFormatString&> {};
277
278 // avoid passing big objects by value to wxFormatStringArgumentFinder::find()
279 // (and especially wx[W]CharBuffer with its auto_ptr<> style semantics!):
280 template<>
281 struct wxFormatStringArgumentFinder<wxString>
282 : public wxFormatStringArgumentFinder<const wxString&> {};
283
284 template<>
285 struct wxFormatStringArgumentFinder<wxScopedCharBuffer>
286 : public wxFormatStringArgumentFinder<const wxScopedCharBuffer&> {};
287
288 template<>
289 struct wxFormatStringArgumentFinder<wxScopedWCharBuffer>
290 : public wxFormatStringArgumentFinder<const wxScopedWCharBuffer&> {};
291
292 template<>
293 struct wxFormatStringArgumentFinder<wxCharBuffer>
294 : public wxFormatStringArgumentFinder<const wxCharBuffer&> {};
295
296 template<>
297 struct wxFormatStringArgumentFinder<wxWCharBuffer>
298 : public wxFormatStringArgumentFinder<const wxWCharBuffer&> {};
299
300
301 // ----------------------------------------------------------------------------
302 // wxArgNormalizer*<T> converters
303 // ----------------------------------------------------------------------------
304
305 #if wxDEBUG_LEVEL
306 // Check that the format specifier for index-th argument in 'fmt' has
307 // the correct type (one of wxFormatString::Arg_XXX or-combination in
308 // 'expected_mask').
309 #define wxASSERT_ARG_TYPE(fmt, index, expected_mask) \
310 do \
311 { \
312 if ( !fmt ) \
313 break; \
314 const int argtype = fmt->GetArgumentType(index); \
315 wxASSERT_MSG( (argtype & (expected_mask)) == argtype, \
316 "format specifier doesn't match argument type" ); \
317 } while ( wxFalse )
318 #else
319 #define wxASSERT_ARG_TYPE(fmt, index, expected_mask)
320 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
321
322
323 #if defined(HAVE_TYPE_TRAITS) || defined(HAVE_TR1_TYPE_TRAITS)
324
325 // Note: this type is misnamed, so that the error message is easier to
326 // understand (no error happens for enums, because the IsEnum=true case is
327 // specialized).
328 template<bool IsEnum>
329 struct wxFormatStringSpecifierNonPodType {};
330
331 template<>
332 struct wxFormatStringSpecifierNonPodType<true>
333 {
334 enum { value = wxFormatString::Arg_Int };
335 };
336
337 template<typename T>
338 struct wxFormatStringSpecifier
339 {
340 #ifdef HAVE_TYPE_TRAITS
341 typedef std::is_enum<T> is_enum;
342 #elif defined HAVE_TR1_TYPE_TRAITS
343 typedef std::tr1::is_enum<T> is_enum;
344 #endif
345 enum { value = wxFormatStringSpecifierNonPodType<is_enum::value>::value };
346 };
347
348 #else // !HAVE_(TR1_)TYPE_TRAITS
349
350 template<typename T>
351 struct wxFormatStringSpecifier
352 {
353 // We can't detect enums without is_enum, so the only thing we can
354 // do is to accept unknown types. However, the only acceptable unknown
355 // types still are enums, which are promoted to ints, so return Arg_Int
356 // here. This will at least catch passing of non-POD types through ... at
357 // runtime.
358 //
359 // Furthermore, if the compiler doesn't have partial template
360 // specialization, we didn't cover pointers either.
361 #ifdef HAVE_PARTIAL_SPECIALIZATION
362 enum { value = wxFormatString::Arg_Int };
363 #else
364 enum { value = wxFormatString::Arg_Int | wxFormatString::Arg_Pointer };
365 #endif
366 };
367
368 #endif // HAVE_TR1_TYPE_TRAITS/!HAVE_TR1_TYPE_TRAITS
369
370
371 #ifdef HAVE_PARTIAL_SPECIALIZATION
372 template<typename T>
373 struct wxFormatStringSpecifier<T*>
374 {
375 enum { value = wxFormatString::Arg_Pointer };
376 };
377
378 template<typename T>
379 struct wxFormatStringSpecifier<const T*>
380 {
381 enum { value = wxFormatString::Arg_Pointer };
382 };
383 #endif // !HAVE_PARTIAL_SPECIALIZATION
384
385
386 #define wxFORMAT_STRING_SPECIFIER(T, arg) \
387 template<> struct wxFormatStringSpecifier<T> \
388 { \
389 enum { value = arg }; \
390 };
391
392 wxFORMAT_STRING_SPECIFIER(bool, wxFormatString::Arg_Int)
393 wxFORMAT_STRING_SPECIFIER(int, wxFormatString::Arg_Int)
394 wxFORMAT_STRING_SPECIFIER(unsigned int, wxFormatString::Arg_Int)
395 wxFORMAT_STRING_SPECIFIER(short int, wxFormatString::Arg_Int)
396 wxFORMAT_STRING_SPECIFIER(short unsigned int, wxFormatString::Arg_Int)
397 wxFORMAT_STRING_SPECIFIER(long int, wxFormatString::Arg_LongInt)
398 wxFORMAT_STRING_SPECIFIER(long unsigned int, wxFormatString::Arg_LongInt)
399 #ifdef wxLongLong_t
400 wxFORMAT_STRING_SPECIFIER(wxLongLong_t, wxFormatString::Arg_LongLongInt)
401 wxFORMAT_STRING_SPECIFIER(wxULongLong_t, wxFormatString::Arg_LongLongInt)
402 #endif
403 wxFORMAT_STRING_SPECIFIER(float, wxFormatString::Arg_Double)
404 wxFORMAT_STRING_SPECIFIER(double, wxFormatString::Arg_Double)
405 wxFORMAT_STRING_SPECIFIER(long double, wxFormatString::Arg_LongDouble)
406
407 #if wxWCHAR_T_IS_REAL_TYPE
408 wxFORMAT_STRING_SPECIFIER(wchar_t, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
409 #endif
410
411 #if !wxUSE_UNICODE
412 wxFORMAT_STRING_SPECIFIER(char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
413 wxFORMAT_STRING_SPECIFIER(signed char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
414 wxFORMAT_STRING_SPECIFIER(unsigned char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
415 #endif
416
417 wxFORMAT_STRING_SPECIFIER(char*, wxFormatString::Arg_String | wxFormatString::Arg_Pointer)
418 wxFORMAT_STRING_SPECIFIER(unsigned char*, wxFormatString::Arg_String | wxFormatString::Arg_Pointer)
419 wxFORMAT_STRING_SPECIFIER(signed char*, wxFormatString::Arg_String | wxFormatString::Arg_Pointer)
420 wxFORMAT_STRING_SPECIFIER(const char*, wxFormatString::Arg_String | wxFormatString::Arg_Pointer)
421 wxFORMAT_STRING_SPECIFIER(const unsigned char*, wxFormatString::Arg_String | wxFormatString::Arg_Pointer)
422 wxFORMAT_STRING_SPECIFIER(const signed char*, wxFormatString::Arg_String | wxFormatString::Arg_Pointer)
423 wxFORMAT_STRING_SPECIFIER(wchar_t*, wxFormatString::Arg_String | wxFormatString::Arg_Pointer)
424 wxFORMAT_STRING_SPECIFIER(const wchar_t*, wxFormatString::Arg_String | wxFormatString::Arg_Pointer)
425
426 wxFORMAT_STRING_SPECIFIER(int*, wxFormatString::Arg_IntPtr | wxFormatString::Arg_Pointer)
427 wxFORMAT_STRING_SPECIFIER(short int*, wxFormatString::Arg_ShortIntPtr | wxFormatString::Arg_Pointer)
428 wxFORMAT_STRING_SPECIFIER(long int*, wxFormatString::Arg_LongIntPtr | wxFormatString::Arg_Pointer)
429
430 #undef wxFORMAT_STRING_SPECIFIER
431
432
433 // Converts an argument passed to wxPrint etc. into standard form expected,
434 // by wxXXX functions, e.g. all strings (wxString, char*, wchar_t*) are
435 // converted into wchar_t* or char* depending on the build.
436 template<typename T>
437 struct wxArgNormalizer
438 {
439 // Ctor. 'value' is the value passed as variadic argument, 'fmt' is pointer
440 // to printf-like format string or NULL if the variadic function doesn't
441 // use format string and 'index' is index of 'value' in variadic arguments
442 // list (starting at 1)
443 wxArgNormalizer(T value,
444 const wxFormatString *fmt, unsigned index)
445 : m_value(value)
446 {
447 wxASSERT_ARG_TYPE( fmt, index, wxFormatStringSpecifier<T>::value );
448 }
449
450 // Returns the value in a form that can be safely passed to real vararg
451 // functions. In case of strings, this is char* in ANSI build and wchar_t*
452 // in Unicode build.
453 T get() const { return m_value; }
454
455 T m_value;
456 };
457
458 // normalizer for passing arguments to functions working with wchar_t* (and
459 // until ANSI build is removed, char* in ANSI build as well - FIXME-UTF8)
460 // string representation
461 #if !wxUSE_UTF8_LOCALE_ONLY
462 template<typename T>
463 struct wxArgNormalizerWchar : public wxArgNormalizer<T>
464 {
465 wxArgNormalizerWchar(T value,
466 const wxFormatString *fmt, unsigned index)
467 : wxArgNormalizer<T>(value, fmt, index) {}
468 };
469 #endif // !wxUSE_UTF8_LOCALE_ONLY
470
471 // normalizer for passing arguments to functions working with UTF-8 encoded
472 // char* strings
473 #if wxUSE_UNICODE_UTF8
474 template<typename T>
475 struct wxArgNormalizerUtf8 : public wxArgNormalizer<T>
476 {
477 wxArgNormalizerUtf8(T value,
478 const wxFormatString *fmt, unsigned index)
479 : wxArgNormalizer<T>(value, fmt, index) {}
480 };
481
482 #define wxArgNormalizerNative wxArgNormalizerUtf8
483 #else // wxUSE_UNICODE_WCHAR
484 #define wxArgNormalizerNative wxArgNormalizerWchar
485 #endif // wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_UTF8
486
487
488
489 // special cases for converting strings:
490
491
492 // base class for wxArgNormalizer<T> specializations that need to do conversion;
493 // CharType is either wxStringCharType or wchar_t in UTF-8 build when wrapping
494 // widechar CRT function
495 template<typename CharType>
496 struct wxArgNormalizerWithBuffer
497 {
498 typedef wxScopedCharTypeBuffer<CharType> CharBuffer;
499
500 wxArgNormalizerWithBuffer() {}
501 wxArgNormalizerWithBuffer(const CharBuffer& buf,
502 const wxFormatString *fmt,
503 unsigned index)
504 : m_value(buf)
505 {
506 wxASSERT_ARG_TYPE( fmt, index,
507 wxFormatString::Arg_String | wxFormatString::Arg_Pointer );
508 }
509
510 const CharType *get() const { return m_value; }
511
512 CharBuffer m_value;
513 };
514
515 // string objects:
516 template<>
517 struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxString&>
518 {
519 wxArgNormalizerNative(const wxString& s,
520 const wxFormatString *fmt,
521 unsigned index)
522 : m_value(s)
523 {
524 wxASSERT_ARG_TYPE( fmt, index, wxFormatString::Arg_String );
525 }
526
527 const wxStringCharType *get() const;
528
529 const wxString& m_value;
530 };
531
532 // c_str() values:
533 template<>
534 struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxCStrData&>
535 {
536 wxArgNormalizerNative(const wxCStrData& value,
537 const wxFormatString *fmt,
538 unsigned index)
539 : m_value(value)
540 {
541 wxASSERT_ARG_TYPE( fmt, index,
542 wxFormatString::Arg_String | wxFormatString::Arg_Pointer );
543 }
544
545 const wxStringCharType *get() const;
546
547 const wxCStrData& m_value;
548 };
549
550 // wxString/wxCStrData conversion to wchar_t* value
551 #if wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
552 template<>
553 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxString&>
554 : public wxArgNormalizerWithBuffer<wchar_t>
555 {
556 wxArgNormalizerWchar(const wxString& s,
557 const wxFormatString *fmt, unsigned index);
558 };
559
560 template<>
561 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxCStrData&>
562 : public wxArgNormalizerWithBuffer<wchar_t>
563 {
564 wxArgNormalizerWchar(const wxCStrData& s,
565 const wxFormatString *fmt, unsigned index);
566 };
567 #endif // wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
568
569
570 // C string pointers of the wrong type (wchar_t* for ANSI or UTF8 build,
571 // char* for wchar_t Unicode build or UTF8):
572 #if wxUSE_UNICODE_WCHAR
573
574 template<>
575 struct wxArgNormalizerWchar<const char*>
576 : public wxArgNormalizerWithBuffer<wchar_t>
577 {
578 wxArgNormalizerWchar(const char* s,
579 const wxFormatString *fmt, unsigned index)
580 : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s), fmt, index) {}
581 };
582
583 #elif wxUSE_UNICODE_UTF8
584
585 template<>
586 struct wxArgNormalizerUtf8<const wchar_t*>
587 : public wxArgNormalizerWithBuffer<char>
588 {
589 wxArgNormalizerUtf8(const wchar_t* s,
590 const wxFormatString *fmt, unsigned index)
591 : wxArgNormalizerWithBuffer<char>(wxConvUTF8.cWC2MB(s), fmt, index) {}
592 };
593
594 template<>
595 struct wxArgNormalizerUtf8<const char*>
596 : public wxArgNormalizerWithBuffer<char>
597 {
598 wxArgNormalizerUtf8(const char* s,
599 const wxFormatString *fmt,
600 unsigned index)
601 {
602 wxASSERT_ARG_TYPE( fmt, index,
603 wxFormatString::Arg_String | wxFormatString::Arg_Pointer );
604
605 if ( wxLocaleIsUtf8 )
606 {
607 m_value = wxScopedCharBuffer::CreateNonOwned(s);
608 }
609 else
610 {
611 // convert to widechar string first:
612 wxScopedWCharBuffer buf(wxConvLibc.cMB2WC(s));
613
614 // then to UTF-8:
615 if ( buf )
616 m_value = wxConvUTF8.cWC2MB(buf);
617 }
618 }
619 };
620
621 // UTF-8 build needs conversion to wchar_t* too:
622 #if !wxUSE_UTF8_LOCALE_ONLY
623 template<>
624 struct wxArgNormalizerWchar<const char*>
625 : public wxArgNormalizerWithBuffer<wchar_t>
626 {
627 wxArgNormalizerWchar(const char* s,
628 const wxFormatString *fmt, unsigned index)
629 : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s), fmt, index) {}
630 };
631 #endif // !wxUSE_UTF8_LOCALE_ONLY
632
633 #else // ANSI - FIXME-UTF8
634
635 template<>
636 struct wxArgNormalizerWchar<const wchar_t*>
637 : public wxArgNormalizerWithBuffer<char>
638 {
639 wxArgNormalizerWchar(const wchar_t* s,
640 const wxFormatString *fmt, unsigned index)
641 : wxArgNormalizerWithBuffer<char>(wxConvLibc.cWC2MB(s), fmt, index) {}
642 };
643
644 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8/ANSI
645
646
647 // this macro is used to implement specialization that are exactly same as
648 // some other specialization, i.e. to "forward" the implementation (e.g. for
649 // T=wxString and T=const wxString&). Note that the ctor takes BaseT argument,
650 // not T!
651 #if wxUSE_UNICODE_UTF8
652 #if wxUSE_UTF8_LOCALE_ONLY
653 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
654 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
655 #else // possibly non-UTF8 locales
656 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
657 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT); \
658 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
659 #endif
660 #else // wxUSE_UNICODE_WCHAR
661 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
662 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT)
663 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
664
665 #define _WX_ARG_NORMALIZER_FORWARD_IMPL(Normalizer, T, BaseT) \
666 template<> \
667 struct Normalizer<T> : public Normalizer<BaseT> \
668 { \
669 Normalizer(BaseT value, \
670 const wxFormatString *fmt, unsigned index) \
671 : Normalizer<BaseT>(value, fmt, index) {} \
672 }
673
674 // non-reference versions of specializations for string objects
675 WX_ARG_NORMALIZER_FORWARD(wxString, const wxString&);
676 WX_ARG_NORMALIZER_FORWARD(wxCStrData, const wxCStrData&);
677
678 // versions for passing non-const pointers:
679 WX_ARG_NORMALIZER_FORWARD(char*, const char*);
680 WX_ARG_NORMALIZER_FORWARD(wchar_t*, const wchar_t*);
681
682 // versions for passing wx[W]CharBuffer:
683 WX_ARG_NORMALIZER_FORWARD(wxScopedCharBuffer, const char*);
684 WX_ARG_NORMALIZER_FORWARD(const wxScopedCharBuffer&, const char*);
685 WX_ARG_NORMALIZER_FORWARD(wxScopedWCharBuffer, const wchar_t*);
686 WX_ARG_NORMALIZER_FORWARD(const wxScopedWCharBuffer&, const wchar_t*);
687 WX_ARG_NORMALIZER_FORWARD(wxCharBuffer, const char*);
688 WX_ARG_NORMALIZER_FORWARD(const wxCharBuffer&, const char*);
689 WX_ARG_NORMALIZER_FORWARD(wxWCharBuffer, const wchar_t*);
690 WX_ARG_NORMALIZER_FORWARD(const wxWCharBuffer&, const wchar_t*);
691
692 // versions for std::[w]string:
693 #if wxUSE_STD_STRING
694
695 #include "wx/stringimpl.h"
696
697 #if !wxUSE_UTF8_LOCALE_ONLY
698 template<>
699 struct wxArgNormalizerWchar<const std::string&>
700 : public wxArgNormalizerWchar<const char*>
701 {
702 wxArgNormalizerWchar(const std::string& s,
703 const wxFormatString *fmt, unsigned index)
704 : wxArgNormalizerWchar<const char*>(s.c_str(), fmt, index) {}
705 };
706
707 template<>
708 struct wxArgNormalizerWchar<const wxStdWideString&>
709 : public wxArgNormalizerWchar<const wchar_t*>
710 {
711 wxArgNormalizerWchar(const wxStdWideString& s,
712 const wxFormatString *fmt, unsigned index)
713 : wxArgNormalizerWchar<const wchar_t*>(s.c_str(), fmt, index) {}
714 };
715 #endif // !wxUSE_UTF8_LOCALE_ONLY
716
717 #if wxUSE_UNICODE_UTF8
718 template<>
719 struct wxArgNormalizerUtf8<const std::string&>
720 : public wxArgNormalizerUtf8<const char*>
721 {
722 wxArgNormalizerUtf8(const std::string& s,
723 const wxFormatString *fmt, unsigned index)
724 : wxArgNormalizerUtf8<const char*>(s.c_str(), fmt, index) {}
725 };
726
727 template<>
728 struct wxArgNormalizerUtf8<const wxStdWideString&>
729 : public wxArgNormalizerUtf8<const wchar_t*>
730 {
731 wxArgNormalizerUtf8(const wxStdWideString& s,
732 const wxFormatString *fmt, unsigned index)
733 : wxArgNormalizerUtf8<const wchar_t*>(s.c_str(), fmt, index) {}
734 };
735 #endif // wxUSE_UNICODE_UTF8
736
737 WX_ARG_NORMALIZER_FORWARD(std::string, const std::string&);
738 WX_ARG_NORMALIZER_FORWARD(wxStdWideString, const wxStdWideString&);
739
740 #endif // wxUSE_STD_STRING
741
742
743 // versions for wxUniChar, wxUniCharRef:
744 // (this is same for UTF-8 and Wchar builds, we just convert to wchar_t)
745 template<>
746 struct wxArgNormalizer<const wxUniChar&> : public wxArgNormalizer<wchar_t>
747 {
748 wxArgNormalizer(const wxUniChar& s,
749 const wxFormatString *fmt, unsigned index)
750 : wxArgNormalizer<wchar_t>(s.GetValue(), fmt, index) {}
751 };
752
753 // for wchar_t, default handler does the right thing
754
755 // char has to be treated differently in Unicode builds: a char argument may
756 // be used either for a character value (which should be converted into
757 // wxUniChar) or as an integer value (which should be left as-is). We take
758 // advantage of the fact that both char and wchar_t are converted into int
759 // in variadic arguments here.
760 #if wxUSE_UNICODE
761 template<typename T>
762 struct wxArgNormalizerNarrowChar
763 {
764 wxArgNormalizerNarrowChar(T value,
765 const wxFormatString *fmt, unsigned index)
766 {
767 wxASSERT_ARG_TYPE( fmt, index,
768 wxFormatString::Arg_Char | wxFormatString::Arg_Int );
769
770 // FIXME-UTF8: which one is better default in absence of fmt string
771 // (i.e. when used like e.g. Foo("foo", "bar", 'c', NULL)?
772 if ( !fmt || fmt->GetArgumentType(index) == wxFormatString::Arg_Char )
773 m_value = wx_truncate_cast(T, wxUniChar(value).GetValue());
774 else
775 m_value = value;
776 }
777
778 int get() const { return m_value; }
779
780 T m_value;
781 };
782
783 template<>
784 struct wxArgNormalizer<char> : public wxArgNormalizerNarrowChar<char>
785 {
786 wxArgNormalizer(char value,
787 const wxFormatString *fmt, unsigned index)
788 : wxArgNormalizerNarrowChar<char>(value, fmt, index) {}
789 };
790
791 template<>
792 struct wxArgNormalizer<unsigned char>
793 : public wxArgNormalizerNarrowChar<unsigned char>
794 {
795 wxArgNormalizer(unsigned char value,
796 const wxFormatString *fmt, unsigned index)
797 : wxArgNormalizerNarrowChar<unsigned char>(value, fmt, index) {}
798 };
799
800 template<>
801 struct wxArgNormalizer<signed char>
802 : public wxArgNormalizerNarrowChar<signed char>
803 {
804 wxArgNormalizer(signed char value,
805 const wxFormatString *fmt, unsigned index)
806 : wxArgNormalizerNarrowChar<signed char>(value, fmt, index) {}
807 };
808
809 #endif // wxUSE_UNICODE
810
811 // convert references:
812 WX_ARG_NORMALIZER_FORWARD(wxUniChar, const wxUniChar&);
813 WX_ARG_NORMALIZER_FORWARD(const wxUniCharRef&, const wxUniChar&);
814 WX_ARG_NORMALIZER_FORWARD(wxUniCharRef, const wxUniChar&);
815 WX_ARG_NORMALIZER_FORWARD(const wchar_t&, wchar_t);
816
817 WX_ARG_NORMALIZER_FORWARD(const char&, char);
818 WX_ARG_NORMALIZER_FORWARD(const unsigned char&, unsigned char);
819 WX_ARG_NORMALIZER_FORWARD(const signed char&, signed char);
820
821
822 #undef WX_ARG_NORMALIZER_FORWARD
823 #undef _WX_ARG_NORMALIZER_FORWARD_IMPL
824
825 #undef wxASSERT_ARG_TYPE
826
827 // ----------------------------------------------------------------------------
828 // WX_VA_ARG_STRING
829 // ----------------------------------------------------------------------------
830
831 // Replacement for va_arg() for use with strings in functions that accept
832 // strings normalized by wxArgNormalizer<T>:
833
834 struct WXDLLIMPEXP_BASE wxArgNormalizedString
835 {
836 wxArgNormalizedString(const void* ptr) : m_ptr(ptr) {}
837
838 // returns true if non-NULL string was passed in
839 bool IsValid() const { return m_ptr != NULL; }
840 operator bool() const { return IsValid(); }
841
842 // extracts the string, returns empty string if NULL was passed in
843 wxString GetString() const;
844 operator wxString() const;
845
846 private:
847 const void *m_ptr;
848 };
849
850 #define WX_VA_ARG_STRING(ap) wxArgNormalizedString(va_arg(ap, const void*))
851
852 // ----------------------------------------------------------------------------
853 // implementation of the WX_DEFINE_VARARG_* macros
854 // ----------------------------------------------------------------------------
855
856 // NB: The vararg emulation code is limited to 30 variadic and 4 fixed
857 // arguments at the moment.
858 // If you need more variadic arguments, you need to
859 // 1) increase the value of _WX_VARARG_MAX_ARGS
860 // 2) add _WX_VARARG_JOIN_* and _WX_VARARG_ITER_* up to the new
861 // _WX_VARARG_MAX_ARGS value to the lists below
862 // If you need more fixed arguments, you need to
863 // 1) increase the value of _WX_VARARG_MAX_FIXED_ARGS
864 // 2) add _WX_VARARG_FIXED_EXPAND_* and _WX_VARARG_FIXED_UNUSED_EXPAND_*
865 // macros below
866 #define _WX_VARARG_MAX_ARGS 30
867 #define _WX_VARARG_MAX_FIXED_ARGS 4
868
869 #define _WX_VARARG_JOIN_1(m) m(1)
870 #define _WX_VARARG_JOIN_2(m) _WX_VARARG_JOIN_1(m), m(2)
871 #define _WX_VARARG_JOIN_3(m) _WX_VARARG_JOIN_2(m), m(3)
872 #define _WX_VARARG_JOIN_4(m) _WX_VARARG_JOIN_3(m), m(4)
873 #define _WX_VARARG_JOIN_5(m) _WX_VARARG_JOIN_4(m), m(5)
874 #define _WX_VARARG_JOIN_6(m) _WX_VARARG_JOIN_5(m), m(6)
875 #define _WX_VARARG_JOIN_7(m) _WX_VARARG_JOIN_6(m), m(7)
876 #define _WX_VARARG_JOIN_8(m) _WX_VARARG_JOIN_7(m), m(8)
877 #define _WX_VARARG_JOIN_9(m) _WX_VARARG_JOIN_8(m), m(9)
878 #define _WX_VARARG_JOIN_10(m) _WX_VARARG_JOIN_9(m), m(10)
879 #define _WX_VARARG_JOIN_11(m) _WX_VARARG_JOIN_10(m), m(11)
880 #define _WX_VARARG_JOIN_12(m) _WX_VARARG_JOIN_11(m), m(12)
881 #define _WX_VARARG_JOIN_13(m) _WX_VARARG_JOIN_12(m), m(13)
882 #define _WX_VARARG_JOIN_14(m) _WX_VARARG_JOIN_13(m), m(14)
883 #define _WX_VARARG_JOIN_15(m) _WX_VARARG_JOIN_14(m), m(15)
884 #define _WX_VARARG_JOIN_16(m) _WX_VARARG_JOIN_15(m), m(16)
885 #define _WX_VARARG_JOIN_17(m) _WX_VARARG_JOIN_16(m), m(17)
886 #define _WX_VARARG_JOIN_18(m) _WX_VARARG_JOIN_17(m), m(18)
887 #define _WX_VARARG_JOIN_19(m) _WX_VARARG_JOIN_18(m), m(19)
888 #define _WX_VARARG_JOIN_20(m) _WX_VARARG_JOIN_19(m), m(20)
889 #define _WX_VARARG_JOIN_21(m) _WX_VARARG_JOIN_20(m), m(21)
890 #define _WX_VARARG_JOIN_22(m) _WX_VARARG_JOIN_21(m), m(22)
891 #define _WX_VARARG_JOIN_23(m) _WX_VARARG_JOIN_22(m), m(23)
892 #define _WX_VARARG_JOIN_24(m) _WX_VARARG_JOIN_23(m), m(24)
893 #define _WX_VARARG_JOIN_25(m) _WX_VARARG_JOIN_24(m), m(25)
894 #define _WX_VARARG_JOIN_26(m) _WX_VARARG_JOIN_25(m), m(26)
895 #define _WX_VARARG_JOIN_27(m) _WX_VARARG_JOIN_26(m), m(27)
896 #define _WX_VARARG_JOIN_28(m) _WX_VARARG_JOIN_27(m), m(28)
897 #define _WX_VARARG_JOIN_29(m) _WX_VARARG_JOIN_28(m), m(29)
898 #define _WX_VARARG_JOIN_30(m) _WX_VARARG_JOIN_29(m), m(30)
899
900 #define _WX_VARARG_ITER_1(m,a,b,c,d,e,f) m(1,a,b,c,d,e,f)
901 #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)
902 #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)
903 #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)
904 #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)
905 #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)
906 #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)
907 #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)
908 #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)
909 #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)
910 #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)
911 #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)
912 #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)
913 #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)
914 #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)
915 #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)
916 #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)
917 #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)
918 #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)
919 #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)
920 #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)
921 #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)
922 #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)
923 #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)
924 #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)
925 #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)
926 #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)
927 #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)
928 #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)
929 #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)
930
931
932 #define _WX_VARARG_FIXED_EXPAND_1(t1) \
933 t1 f1
934 #define _WX_VARARG_FIXED_EXPAND_2(t1,t2) \
935 t1 f1, t2 f2
936 #define _WX_VARARG_FIXED_EXPAND_3(t1,t2,t3) \
937 t1 f1, t2 f2, t3 f3
938 #define _WX_VARARG_FIXED_EXPAND_4(t1,t2,t3,t4) \
939 t1 f1, t2 f2, t3 f3, t4 f4
940
941 #define _WX_VARARG_FIXED_UNUSED_EXPAND_1(t1) \
942 t1 WXUNUSED(f1)
943 #define _WX_VARARG_FIXED_UNUSED_EXPAND_2(t1,t2) \
944 t1 WXUNUSED(f1), t2 WXUNUSED(f2)
945 #define _WX_VARARG_FIXED_UNUSED_EXPAND_3(t1,t2,t3) \
946 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3)
947 #define _WX_VARARG_FIXED_UNUSED_EXPAND_4(t1,t2,t3,t4) \
948 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3), t4 WXUNUSED(f4)
949
950 #define _WX_VARARG_FIXED_TYPEDEFS_1(t1) \
951 typedef t1 TF1
952 #define _WX_VARARG_FIXED_TYPEDEFS_2(t1,t2) \
953 _WX_VARARG_FIXED_TYPEDEFS_1(t1); typedef t2 TF2
954 #define _WX_VARARG_FIXED_TYPEDEFS_3(t1,t2,t3) \
955 _WX_VARARG_FIXED_TYPEDEFS_2(t1,t2); typedef t3 TF3
956 #define _WX_VARARG_FIXED_TYPEDEFS_4(t1,t2,t3,t4) \
957 _WX_VARARG_FIXED_TYPEDEFS_3(t1,t2,t3); typedef t4 TF4
958
959 // This macro expands N-items tuple of fixed arguments types into part of
960 // function's declaration. For example,
961 // "_WX_VARARG_FIXED_EXPAND(3, (int, char*, int))" expands into
962 // "int f1, char* f2, int f3".
963 #define _WX_VARARG_FIXED_EXPAND(N, args) \
964 _WX_VARARG_FIXED_EXPAND_IMPL(N, args)
965 #define _WX_VARARG_FIXED_EXPAND_IMPL(N, args) \
966 _WX_VARARG_FIXED_EXPAND_##N args
967
968 // Ditto for unused arguments
969 #define _WX_VARARG_FIXED_UNUSED_EXPAND(N, args) \
970 _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args)
971 #define _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args) \
972 _WX_VARARG_FIXED_UNUSED_EXPAND_##N args
973
974 // Declarates typedefs for fixed arguments types; i-th fixed argument types
975 // will have TFi typedef.
976 #define _WX_VARARG_FIXED_TYPEDEFS(N, args) \
977 _WX_VARARG_FIXED_TYPEDEFS_IMPL(N, args)
978 #define _WX_VARARG_FIXED_TYPEDEFS_IMPL(N, args) \
979 _WX_VARARG_FIXED_TYPEDEFS_##N args
980
981
982 // This macro calls another macro 'm' passed as second argument 'N' times,
983 // with its only argument set to 1..N, and concatenates the results using
984 // comma as separator.
985 //
986 // An example:
987 // #define foo(i) x##i
988 // // this expands to "x1,x2,x3,x4"
989 // _WX_VARARG_JOIN(4, foo)
990 //
991 //
992 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
993 #define _WX_VARARG_JOIN(N, m) _WX_VARARG_JOIN_IMPL(N, m)
994 #define _WX_VARARG_JOIN_IMPL(N, m) _WX_VARARG_JOIN_##N(m)
995
996
997 // This macro calls another macro 'm' passed as second argument 'N' times, with
998 // its first argument set to 1..N and the remaining arguments set to 'a', 'b',
999 // 'c', 'd', 'e' and 'f'. The results are separated with whitespace in the
1000 // expansion.
1001 //
1002 // An example:
1003 // // this macro expands to:
1004 // // foo(1,a,b,c,d,e,f)
1005 // // foo(2,a,b,c,d,e,f)
1006 // // foo(3,a,b,c,d,e,f)
1007 // _WX_VARARG_ITER(3, foo, a, b, c, d, e, f)
1008 //
1009 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
1010 #define _WX_VARARG_ITER(N,m,a,b,c,d,e,f) \
1011 _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f)
1012 #define _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f) \
1013 _WX_VARARG_ITER_##N(m,a,b,c,d,e,f)
1014
1015 // Generates code snippet for i-th "variadic" argument in vararg function's
1016 // prototype:
1017 #define _WX_VARARG_ARG(i) T##i a##i
1018
1019 // Like _WX_VARARG_ARG_UNUSED, but outputs argument's type with WXUNUSED:
1020 #define _WX_VARARG_ARG_UNUSED(i) T##i WXUNUSED(a##i)
1021
1022 // Generates code snippet for i-th type in vararg function's template<...>:
1023 #define _WX_VARARG_TEMPL(i) typename T##i
1024
1025 // Generates code snippet for passing i-th argument of vararg function
1026 // wrapper to its implementation, normalizing it in the process:
1027 #define _WX_VARARG_PASS_WCHAR(i) \
1028 wxArgNormalizerWchar<T##i>(a##i, fmt, i).get()
1029 #define _WX_VARARG_PASS_UTF8(i) \
1030 wxArgNormalizerUtf8<T##i>(a##i, fmt, i).get()
1031
1032
1033 // And the same for fixed arguments, _not_ normalizing it:
1034 #define _WX_VARARG_PASS_FIXED(i) f##i
1035
1036 #define _WX_VARARG_FIND_FMT(i) \
1037 (wxFormatStringArgumentFinder<TF##i>::find(f##i))
1038
1039 #define _WX_VARARG_FORMAT_STRING(numfixed, fixed) \
1040 _WX_VARARG_FIXED_TYPEDEFS(numfixed, fixed); \
1041 const wxFormatString *fmt = \
1042 (_WX_VARARG_JOIN(numfixed, _WX_VARARG_FIND_FMT))
1043
1044 #if wxUSE_UNICODE_UTF8
1045 #define _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed) \
1046 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
1047 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_UTF8))
1048 #define _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed) \
1049 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
1050 #endif // wxUSE_UNICODE_UTF8
1051
1052 #define _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed) \
1053 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
1054 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WCHAR))
1055 #define _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed) \
1056 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
1057
1058 #if wxUSE_UNICODE_UTF8
1059 #if wxUSE_UTF8_LOCALE_ONLY
1060 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_UTF8
1061 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_UTF8
1062 #else // possibly non-UTF8 locales
1063 #define _WX_VARARG_DO_CALL(return_kw, impl, implUtf8, N, numfixed) \
1064 if ( wxLocaleIsUtf8 ) \
1065 _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed);\
1066 else \
1067 _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed)
1068
1069 #define _WX_VARARG_DO_CALL0(return_kw, impl, implUtf8, numfixed) \
1070 if ( wxLocaleIsUtf8 ) \
1071 _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed); \
1072 else \
1073 _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed)
1074 #endif // wxUSE_UTF8_LOCALE_ONLY or not
1075 #else // wxUSE_UNICODE_WCHAR or ANSI
1076 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_WCHAR
1077 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_WCHAR
1078 #endif // wxUSE_UNICODE_UTF8 / wxUSE_UNICODE_WCHAR
1079
1080
1081 // Macro to be used with _WX_VARARG_ITER in the implementation of
1082 // WX_DEFINE_VARARG_FUNC (see its documentation for the meaning of arguments)
1083 #define _WX_VARARG_DEFINE_FUNC(N, rettype, name, \
1084 impl, implUtf8, numfixed, fixed) \
1085 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1086 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1087 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1088 { \
1089 _WX_VARARG_FORMAT_STRING(numfixed, fixed); \
1090 _WX_VARARG_DO_CALL(return, impl, implUtf8, N, numfixed); \
1091 }
1092
1093 #define _WX_VARARG_DEFINE_FUNC_N0(rettype, name, \
1094 impl, implUtf8, numfixed, fixed) \
1095 inline rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
1096 { \
1097 _WX_VARARG_DO_CALL0(return, impl, implUtf8, numfixed); \
1098 }
1099
1100 // Macro to be used with _WX_VARARG_ITER in the implementation of
1101 // WX_DEFINE_VARARG_FUNC_VOID (see its documentation for the meaning of
1102 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
1103 // requirements).
1104 #define _WX_VARARG_DEFINE_FUNC_VOID(N, rettype, name, \
1105 impl, implUtf8, numfixed, fixed) \
1106 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1107 void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1108 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1109 { \
1110 _WX_VARARG_FORMAT_STRING(numfixed, fixed); \
1111 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
1112 impl, implUtf8, N, numfixed); \
1113 }
1114
1115 #define _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
1116 inline void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
1117 { \
1118 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
1119 impl, implUtf8, numfixed); \
1120 }
1121
1122 // Macro to be used with _WX_VARARG_ITER in the implementation of
1123 // WX_DEFINE_VARARG_FUNC_CTOR (see its documentation for the meaning of
1124 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
1125 // requirements).
1126 #define _WX_VARARG_DEFINE_FUNC_CTOR(N, rettype, name, \
1127 impl, implUtf8, numfixed, fixed) \
1128 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1129 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1130 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1131 { \
1132 _WX_VARARG_FORMAT_STRING(numfixed, fixed); \
1133 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
1134 impl, implUtf8, N, numfixed); \
1135 }
1136
1137 #define _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
1138 inline name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
1139 { \
1140 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
1141 impl, implUtf8, numfixed); \
1142 }
1143
1144 // Macro to be used with _WX_VARARG_ITER in the implementation of
1145 // WX_DEFINE_VARARG_FUNC_NOP, i.e. empty stub for a disabled vararg function.
1146 // The rettype and impl arguments are ignored.
1147 #define _WX_VARARG_DEFINE_FUNC_NOP(N, rettype, name, \
1148 impl, implUtf8, numfixed, fixed) \
1149 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1150 void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed), \
1151 _WX_VARARG_JOIN(N, _WX_VARARG_ARG_UNUSED)) \
1152 {}
1153
1154 #define _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
1155 inline void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed)) \
1156 {}
1157
1158
1159 // ----------------------------------------------------------------------------
1160 // workaround for OpenWatcom bug #351
1161 // ----------------------------------------------------------------------------
1162
1163 #ifdef __WATCOMC__
1164 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1165
1166 // This macro can be used to forward a 'vararg' template to another one with
1167 // different fixed arguments types. Parameters are same as for
1168 // WX_DEFINE_VARARG_FUNC (rettype=void can be used here), 'convfixed' is how
1169 // to convert fixed arguments. For example, this is typical code for dealing
1170 // with different forms of format string:
1171 //
1172 // WX_DEFINE_VARARG_FUNC_VOID(Printf, 1, (const wxFormatString&),
1173 // DoPrintfWchar, DoPrintfUtf8)
1174 // #ifdef __WATCOMC__
1175 // WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const wxString&),
1176 // (wxFormatString(f1)))
1177 // WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const char*),
1178 // (wxFormatString(f1)))
1179 // ...
1180 #define WX_VARARG_WATCOM_WORKAROUND(rettype, name, numfixed, fixed, convfixed)\
1181 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
1182 _WX_VARARG_WATCOM_WORKAROUND, \
1183 rettype, name, convfixed, dummy, numfixed, fixed)
1184
1185 #define WX_VARARG_WATCOM_WORKAROUND_CTOR(name, numfixed, fixed, convfixed) \
1186 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
1187 _WX_VARARG_WATCOM_WORKAROUND_CTOR, \
1188 dummy, name, convfixed, dummy, numfixed, fixed)
1189
1190 #define _WX_VARARG_WATCOM_UNPACK_1(a1) a1
1191 #define _WX_VARARG_WATCOM_UNPACK_2(a1, a2) a1, a2
1192 #define _WX_VARARG_WATCOM_UNPACK_3(a1, a2, a3) a1, a2, a3
1193 #define _WX_VARARG_WATCOM_UNPACK_4(a1, a2, a3, a4) a1, a2, a3, a4
1194 #define _WX_VARARG_WATCOM_UNPACK(N, convfixed) \
1195 _WX_VARARG_WATCOM_UNPACK_##N convfixed
1196
1197 #define _WX_VARARG_PASS_WATCOM(i) a##i
1198
1199 #define _WX_VARARG_WATCOM_WORKAROUND(N, rettype, name, \
1200 convfixed, dummy, numfixed, fixed) \
1201 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1202 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1203 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1204 { \
1205 return name(_WX_VARARG_WATCOM_UNPACK(numfixed, convfixed), \
1206 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WATCOM)); \
1207 }
1208
1209 #define _WX_VARARG_WATCOM_WORKAROUND_CTOR(N, dummy1, name, \
1210 convfixed, dummy2, numfixed, fixed) \
1211 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1212 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1213 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1214 { \
1215 name(_WX_VARARG_WATCOM_UNPACK(numfixed, convfixed), \
1216 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WATCOM)); \
1217 }
1218
1219 #endif // __WATCOMC__
1220
1221 #endif // _WX_STRVARARG_H_