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