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