1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/strvararg.h
3 // Purpose: macros for implementing type-safe vararg passing of strings
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_STRVARARG_H_
12 #define _WX_STRVARARG_H_
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"
20 #include "wx/chartype.h"
21 #include "wx/strconv.h"
22 #include "wx/buffer.h"
23 #include "wx/unichar.h"
25 #if defined(HAVE_TYPE_TRAITS)
26 #include <type_traits>
27 #elif defined(HAVE_TR1_TYPE_TRAITS)
29 #include <type_traits>
31 #include <tr1/type_traits>
35 class WXDLLIMPEXP_FWD_BASE wxCStrData
;
36 class WXDLLIMPEXP_FWD_BASE wxString
;
38 // ----------------------------------------------------------------------------
39 // WX_DEFINE_VARARG_FUNC* macros
40 // ----------------------------------------------------------------------------
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().
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
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
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.
66 // [ there are examples in square brackets showing values of the parameters
67 // for the wxFprintf() wrapper for fprintf() function with the following
69 // int wxFprintf(FILE *stream, const wxString& format, ...); ]
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]
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)
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)
99 // Like WX_DEFINE_VARARG_FUNC, but for variadic functions that don't return
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)
107 // Like WX_DEFINE_VARARG_FUNC_VOID, but instead of wrapping an implementation
108 // function, does nothing in defined functions' bodies.
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)
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)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
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<>
136 // Note that this class can _only_ be used for function arguments!
137 class WXDLLIMPEXP_BASE wxFormatString
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
) {}
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.
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)
163 Arg_Int
= 0x0008 | Arg_Char
, // (ints can be used with %c)
164 #if SIZEOF_INT == SIZEOF_LONG
165 Arg_LongInt
= Arg_Int
,
167 Arg_LongInt
= 0x0010,
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,
176 Arg_LongDouble
= 0x0080,
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
,
188 Arg_IntPtr
= 0x0200, // %n -- store # of chars written
189 Arg_ShortIntPtr
= 0x0400,
190 Arg_LongIntPtr
= 0x0800,
192 Arg_Unknown
= 0x8000 // unrecognized specifier (likely error)
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;
201 // returns the value passed to ctor, only converted to wxString, similarly
202 // to other InputAsXXX() methods
203 wxString
InputAsString() const;
205 #if !wxUSE_UNICODE_WCHAR
206 operator const char*() const
207 { return const_cast<wxFormatString
*>(this)->AsChar(); }
209 // InputAsChar() returns the value passed to ctor, only converted
210 // to char, while AsChar() takes 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
218 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
220 operator const wchar_t*() const
221 { return const_cast<wxFormatString
*>(this)->AsWChar(); }
223 const wchar_t* InputAsWChar();
224 const wchar_t* AsWChar();
225 wxScopedWCharBuffer m_convertedWChar
;
226 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
229 wxScopedCharBuffer m_char
;
230 wxScopedWCharBuffer m_wchar
;
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
;
238 wxDECLARE_NO_ASSIGN_CLASS(wxFormatString
);
241 // these two helper classes are used to find wxFormatString argument among fixed
242 // arguments passed to a vararg template
243 struct wxFormatStringArgument
245 wxFormatStringArgument(const wxFormatString
*s
= NULL
) : m_str(s
) {}
246 const wxFormatString
*m_str
;
248 // overriding this operator allows us to reuse _WX_VARARG_JOIN macro
249 wxFormatStringArgument
operator,(const wxFormatStringArgument
& a
) const
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
);
256 operator const wxFormatString
*() const { return m_str
; }
260 struct wxFormatStringArgumentFinder
262 static wxFormatStringArgument
find(T
)
264 // by default, arguments are not format strings, so return "not found"
265 return wxFormatStringArgument();
270 struct wxFormatStringArgumentFinder
<const wxFormatString
&>
272 static wxFormatStringArgument
find(const wxFormatString
& arg
)
273 { return wxFormatStringArgument(&arg
); }
277 struct wxFormatStringArgumentFinder
<wxFormatString
>
278 : public wxFormatStringArgumentFinder
<const wxFormatString
&> {};
280 // avoid passing big objects by value to wxFormatStringArgumentFinder::find()
281 // (and especially wx[W]CharBuffer with its auto_ptr<> style semantics!):
283 struct wxFormatStringArgumentFinder
<wxString
>
284 : public wxFormatStringArgumentFinder
<const wxString
&> {};
287 struct wxFormatStringArgumentFinder
<wxScopedCharBuffer
>
288 : public wxFormatStringArgumentFinder
<const wxScopedCharBuffer
&> {};
291 struct wxFormatStringArgumentFinder
<wxScopedWCharBuffer
>
292 : public wxFormatStringArgumentFinder
<const wxScopedWCharBuffer
&> {};
295 struct wxFormatStringArgumentFinder
<wxCharBuffer
>
296 : public wxFormatStringArgumentFinder
<const wxCharBuffer
&> {};
299 struct wxFormatStringArgumentFinder
<wxWCharBuffer
>
300 : public wxFormatStringArgumentFinder
<const wxWCharBuffer
&> {};
303 // ----------------------------------------------------------------------------
304 // wxArgNormalizer*<T> converters
305 // ----------------------------------------------------------------------------
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
311 #define wxASSERT_ARG_TYPE(fmt, index, expected_mask) \
316 const int argtype = fmt->GetArgumentType(index); \
317 wxASSERT_MSG( (argtype & (expected_mask)) == argtype, \
318 "format specifier doesn't match argument type" ); \
321 // Just define it to suppress "unused parameter" warnings for the
322 // parameters which we don't use otherwise
323 #define wxASSERT_ARG_TYPE(fmt, index, expected_mask) \
326 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
329 #if defined(HAVE_TYPE_TRAITS) || defined(HAVE_TR1_TYPE_TRAITS)
331 // Note: this type is misnamed, so that the error message is easier to
332 // understand (no error happens for enums, because the IsEnum=true case is
334 template<bool IsEnum
>
335 struct wxFormatStringSpecifierNonPodType
{};
338 struct wxFormatStringSpecifierNonPodType
<true>
340 enum { value
= wxFormatString::Arg_Int
};
344 struct wxFormatStringSpecifier
346 #ifdef HAVE_TYPE_TRAITS
347 typedef std::is_enum
<T
> is_enum
;
348 #elif defined HAVE_TR1_TYPE_TRAITS
349 typedef std::tr1::is_enum
<T
> is_enum
;
351 enum { value
= wxFormatStringSpecifierNonPodType
<is_enum::value
>::value
};
354 #else // !HAVE_(TR1_)TYPE_TRAITS
357 struct wxFormatStringSpecifier
359 // We can't detect enums without is_enum, so the only thing we can
360 // do is to accept unknown types. However, the only acceptable unknown
361 // types still are enums, which are promoted to ints, so return Arg_Int
362 // here. This will at least catch passing of non-POD types through ... at
365 // Furthermore, if the compiler doesn't have partial template
366 // specialization, we didn't cover pointers either.
367 #ifdef HAVE_PARTIAL_SPECIALIZATION
368 enum { value
= wxFormatString::Arg_Int
};
370 enum { value
= wxFormatString::Arg_Int
| wxFormatString::Arg_Pointer
};
374 #endif // HAVE_TR1_TYPE_TRAITS/!HAVE_TR1_TYPE_TRAITS
377 #ifdef HAVE_PARTIAL_SPECIALIZATION
379 struct wxFormatStringSpecifier
<T
*>
381 enum { value
= wxFormatString::Arg_Pointer
};
385 struct wxFormatStringSpecifier
<const T
*>
387 enum { value
= wxFormatString::Arg_Pointer
};
389 #endif // !HAVE_PARTIAL_SPECIALIZATION
392 #define wxFORMAT_STRING_SPECIFIER(T, arg) \
393 template<> struct wxFormatStringSpecifier<T> \
395 enum { value = arg }; \
398 wxFORMAT_STRING_SPECIFIER(bool, wxFormatString::Arg_Int
)
399 wxFORMAT_STRING_SPECIFIER(int, wxFormatString::Arg_Int
)
400 wxFORMAT_STRING_SPECIFIER(unsigned int, wxFormatString::Arg_Int
)
401 wxFORMAT_STRING_SPECIFIER(short int, wxFormatString::Arg_Int
)
402 wxFORMAT_STRING_SPECIFIER(short unsigned int, wxFormatString::Arg_Int
)
403 wxFORMAT_STRING_SPECIFIER(long int, wxFormatString::Arg_LongInt
)
404 wxFORMAT_STRING_SPECIFIER(long unsigned int, wxFormatString::Arg_LongInt
)
406 wxFORMAT_STRING_SPECIFIER(wxLongLong_t
, wxFormatString::Arg_LongLongInt
)
407 wxFORMAT_STRING_SPECIFIER(wxULongLong_t
, wxFormatString::Arg_LongLongInt
)
409 wxFORMAT_STRING_SPECIFIER(float, wxFormatString::Arg_Double
)
410 wxFORMAT_STRING_SPECIFIER(double, wxFormatString::Arg_Double
)
411 wxFORMAT_STRING_SPECIFIER(long double, wxFormatString::Arg_LongDouble
)
413 #if wxWCHAR_T_IS_REAL_TYPE
414 wxFORMAT_STRING_SPECIFIER(wchar_t, wxFormatString::Arg_Char
| wxFormatString::Arg_Int
)
418 wxFORMAT_STRING_SPECIFIER(char, wxFormatString::Arg_Char
| wxFormatString::Arg_Int
)
419 wxFORMAT_STRING_SPECIFIER(signed char, wxFormatString::Arg_Char
| wxFormatString::Arg_Int
)
420 wxFORMAT_STRING_SPECIFIER(unsigned char, wxFormatString::Arg_Char
| wxFormatString::Arg_Int
)
423 wxFORMAT_STRING_SPECIFIER(char*, wxFormatString::Arg_String
)
424 wxFORMAT_STRING_SPECIFIER(unsigned char*, wxFormatString::Arg_String
)
425 wxFORMAT_STRING_SPECIFIER(signed char*, wxFormatString::Arg_String
)
426 wxFORMAT_STRING_SPECIFIER(const char*, wxFormatString::Arg_String
)
427 wxFORMAT_STRING_SPECIFIER(const unsigned char*, wxFormatString::Arg_String
)
428 wxFORMAT_STRING_SPECIFIER(const signed char*, wxFormatString::Arg_String
)
429 wxFORMAT_STRING_SPECIFIER(wchar_t*, wxFormatString::Arg_String
)
430 wxFORMAT_STRING_SPECIFIER(const wchar_t*, wxFormatString::Arg_String
)
432 wxFORMAT_STRING_SPECIFIER(int*, wxFormatString::Arg_IntPtr
| wxFormatString::Arg_Pointer
)
433 wxFORMAT_STRING_SPECIFIER(short int*, wxFormatString::Arg_ShortIntPtr
| wxFormatString::Arg_Pointer
)
434 wxFORMAT_STRING_SPECIFIER(long int*, wxFormatString::Arg_LongIntPtr
| wxFormatString::Arg_Pointer
)
436 #undef wxFORMAT_STRING_SPECIFIER
439 // Converts an argument passed to wxPrint etc. into standard form expected,
440 // by wxXXX functions, e.g. all strings (wxString, char*, wchar_t*) are
441 // converted into wchar_t* or char* depending on the build.
443 struct wxArgNormalizer
445 // Ctor. 'value' is the value passed as variadic argument, 'fmt' is pointer
446 // to printf-like format string or NULL if the variadic function doesn't
447 // use format string and 'index' is index of 'value' in variadic arguments
448 // list (starting at 1)
449 wxArgNormalizer(T value
,
450 const wxFormatString
*fmt
, unsigned index
)
453 wxASSERT_ARG_TYPE( fmt
, index
, wxFormatStringSpecifier
<T
>::value
);
456 // Returns the value in a form that can be safely passed to real vararg
457 // functions. In case of strings, this is char* in ANSI build and wchar_t*
459 T
get() const { return m_value
; }
464 // normalizer for passing arguments to functions working with wchar_t* (and
465 // until ANSI build is removed, char* in ANSI build as well - FIXME-UTF8)
466 // string representation
467 #if !wxUSE_UTF8_LOCALE_ONLY
469 struct wxArgNormalizerWchar
: public wxArgNormalizer
<T
>
471 wxArgNormalizerWchar(T value
,
472 const wxFormatString
*fmt
, unsigned index
)
473 : wxArgNormalizer
<T
>(value
, fmt
, index
) {}
475 #endif // !wxUSE_UTF8_LOCALE_ONLY
477 // normalizer for passing arguments to functions working with UTF-8 encoded
479 #if wxUSE_UNICODE_UTF8
481 struct wxArgNormalizerUtf8
: public wxArgNormalizer
<T
>
483 wxArgNormalizerUtf8(T value
,
484 const wxFormatString
*fmt
, unsigned index
)
485 : wxArgNormalizer
<T
>(value
, fmt
, index
) {}
488 #define wxArgNormalizerNative wxArgNormalizerUtf8
489 #else // wxUSE_UNICODE_WCHAR
490 #define wxArgNormalizerNative wxArgNormalizerWchar
491 #endif // wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_UTF8
495 // special cases for converting strings:
498 // base class for wxArgNormalizer<T> specializations that need to do conversion;
499 // CharType is either wxStringCharType or wchar_t in UTF-8 build when wrapping
500 // widechar CRT function
501 template<typename CharType
>
502 struct wxArgNormalizerWithBuffer
504 typedef wxScopedCharTypeBuffer
<CharType
> CharBuffer
;
506 wxArgNormalizerWithBuffer() {}
507 wxArgNormalizerWithBuffer(const CharBuffer
& buf
,
508 const wxFormatString
*fmt
,
512 wxASSERT_ARG_TYPE( fmt
, index
, wxFormatString::Arg_String
);
515 const CharType
*get() const { return m_value
; }
522 struct WXDLLIMPEXP_BASE wxArgNormalizerNative
<const wxString
&>
524 wxArgNormalizerNative(const wxString
& s
,
525 const wxFormatString
*fmt
,
529 wxASSERT_ARG_TYPE( fmt
, index
, wxFormatString::Arg_String
);
532 const wxStringCharType
*get() const;
534 const wxString
& m_value
;
539 struct WXDLLIMPEXP_BASE wxArgNormalizerNative
<const wxCStrData
&>
541 wxArgNormalizerNative(const wxCStrData
& value
,
542 const wxFormatString
*fmt
,
546 wxASSERT_ARG_TYPE( fmt
, index
, wxFormatString::Arg_String
);
549 const wxStringCharType
*get() const;
551 const wxCStrData
& m_value
;
554 // wxString/wxCStrData conversion to wchar_t* value
555 #if wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
557 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar
<const wxString
&>
558 : public wxArgNormalizerWithBuffer
<wchar_t>
560 wxArgNormalizerWchar(const wxString
& s
,
561 const wxFormatString
*fmt
, unsigned index
);
565 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar
<const wxCStrData
&>
566 : public wxArgNormalizerWithBuffer
<wchar_t>
568 wxArgNormalizerWchar(const wxCStrData
& s
,
569 const wxFormatString
*fmt
, unsigned index
);
571 #endif // wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
574 // C string pointers of the wrong type (wchar_t* for ANSI or UTF8 build,
575 // char* for wchar_t Unicode build or UTF8):
576 #if wxUSE_UNICODE_WCHAR
579 struct wxArgNormalizerWchar
<const char*>
580 : public wxArgNormalizerWithBuffer
<wchar_t>
582 wxArgNormalizerWchar(const char* s
,
583 const wxFormatString
*fmt
, unsigned index
)
584 : wxArgNormalizerWithBuffer
<wchar_t>(wxConvLibc
.cMB2WC(s
), fmt
, index
) {}
587 #elif wxUSE_UNICODE_UTF8
590 struct wxArgNormalizerUtf8
<const wchar_t*>
591 : public wxArgNormalizerWithBuffer
<char>
593 wxArgNormalizerUtf8(const wchar_t* s
,
594 const wxFormatString
*fmt
, unsigned index
)
595 : wxArgNormalizerWithBuffer
<char>(wxConvUTF8
.cWC2MB(s
), fmt
, index
) {}
599 struct wxArgNormalizerUtf8
<const char*>
600 : public wxArgNormalizerWithBuffer
<char>
602 wxArgNormalizerUtf8(const char* s
,
603 const wxFormatString
*fmt
,
606 wxASSERT_ARG_TYPE( fmt
, index
, wxFormatString::Arg_String
);
608 if ( wxLocaleIsUtf8
)
610 m_value
= wxScopedCharBuffer::CreateNonOwned(s
);
614 // convert to widechar string first:
615 wxScopedWCharBuffer
buf(wxConvLibc
.cMB2WC(s
));
619 m_value
= wxConvUTF8
.cWC2MB(buf
);
624 // UTF-8 build needs conversion to wchar_t* too:
625 #if !wxUSE_UTF8_LOCALE_ONLY
627 struct wxArgNormalizerWchar
<const char*>
628 : public wxArgNormalizerWithBuffer
<wchar_t>
630 wxArgNormalizerWchar(const char* s
,
631 const wxFormatString
*fmt
, unsigned index
)
632 : wxArgNormalizerWithBuffer
<wchar_t>(wxConvLibc
.cMB2WC(s
), fmt
, index
) {}
634 #endif // !wxUSE_UTF8_LOCALE_ONLY
636 #else // ANSI - FIXME-UTF8
639 struct wxArgNormalizerWchar
<const wchar_t*>
640 : public wxArgNormalizerWithBuffer
<char>
642 wxArgNormalizerWchar(const wchar_t* s
,
643 const wxFormatString
*fmt
, unsigned index
)
644 : wxArgNormalizerWithBuffer
<char>(wxConvLibc
.cWC2MB(s
), fmt
, index
) {}
647 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8/ANSI
650 // this macro is used to implement specialization that are exactly same as
651 // some other specialization, i.e. to "forward" the implementation (e.g. for
652 // T=wxString and T=const wxString&). Note that the ctor takes BaseT argument,
654 #if wxUSE_UNICODE_UTF8
655 #if wxUSE_UTF8_LOCALE_ONLY
656 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
657 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
658 #else // possibly non-UTF8 locales
659 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
660 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT); \
661 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
663 #else // wxUSE_UNICODE_WCHAR
664 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
665 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT)
666 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
668 #define _WX_ARG_NORMALIZER_FORWARD_IMPL(Normalizer, T, BaseT) \
670 struct Normalizer<T> : public Normalizer<BaseT> \
672 Normalizer(BaseT value, \
673 const wxFormatString *fmt, unsigned index) \
674 : Normalizer<BaseT>(value, fmt, index) {} \
677 // non-reference versions of specializations for string objects
678 WX_ARG_NORMALIZER_FORWARD(wxString
, const wxString
&);
679 WX_ARG_NORMALIZER_FORWARD(wxCStrData
, const wxCStrData
&);
681 // versions for passing non-const pointers:
682 WX_ARG_NORMALIZER_FORWARD(char*, const char*);
683 WX_ARG_NORMALIZER_FORWARD(wchar_t*, const wchar_t*);
685 // versions for passing wx[W]CharBuffer:
686 WX_ARG_NORMALIZER_FORWARD(wxScopedCharBuffer
, const char*);
687 WX_ARG_NORMALIZER_FORWARD(const wxScopedCharBuffer
&, const char*);
688 WX_ARG_NORMALIZER_FORWARD(wxScopedWCharBuffer
, const wchar_t*);
689 WX_ARG_NORMALIZER_FORWARD(const wxScopedWCharBuffer
&, const wchar_t*);
690 WX_ARG_NORMALIZER_FORWARD(wxCharBuffer
, const char*);
691 WX_ARG_NORMALIZER_FORWARD(const wxCharBuffer
&, const char*);
692 WX_ARG_NORMALIZER_FORWARD(wxWCharBuffer
, const wchar_t*);
693 WX_ARG_NORMALIZER_FORWARD(const wxWCharBuffer
&, const wchar_t*);
695 // versions for std::[w]string:
698 #include "wx/stringimpl.h"
700 #if !wxUSE_UTF8_LOCALE_ONLY
702 struct wxArgNormalizerWchar
<const std::string
&>
703 : public wxArgNormalizerWchar
<const char*>
705 wxArgNormalizerWchar(const std::string
& s
,
706 const wxFormatString
*fmt
, unsigned index
)
707 : wxArgNormalizerWchar
<const char*>(s
.c_str(), fmt
, index
) {}
711 struct wxArgNormalizerWchar
<const wxStdWideString
&>
712 : public wxArgNormalizerWchar
<const wchar_t*>
714 wxArgNormalizerWchar(const wxStdWideString
& s
,
715 const wxFormatString
*fmt
, unsigned index
)
716 : wxArgNormalizerWchar
<const wchar_t*>(s
.c_str(), fmt
, index
) {}
718 #endif // !wxUSE_UTF8_LOCALE_ONLY
720 #if wxUSE_UNICODE_UTF8
722 struct wxArgNormalizerUtf8
<const std::string
&>
723 : public wxArgNormalizerUtf8
<const char*>
725 wxArgNormalizerUtf8(const std::string
& s
,
726 const wxFormatString
*fmt
, unsigned index
)
727 : wxArgNormalizerUtf8
<const char*>(s
.c_str(), fmt
, index
) {}
731 struct wxArgNormalizerUtf8
<const wxStdWideString
&>
732 : public wxArgNormalizerUtf8
<const wchar_t*>
734 wxArgNormalizerUtf8(const wxStdWideString
& s
,
735 const wxFormatString
*fmt
, unsigned index
)
736 : wxArgNormalizerUtf8
<const wchar_t*>(s
.c_str(), fmt
, index
) {}
738 #endif // wxUSE_UNICODE_UTF8
740 WX_ARG_NORMALIZER_FORWARD(std::string
, const std::string
&);
741 WX_ARG_NORMALIZER_FORWARD(wxStdWideString
, const wxStdWideString
&);
743 #endif // wxUSE_STD_STRING
746 // versions for wxUniChar, wxUniCharRef:
747 // (this is same for UTF-8 and Wchar builds, we just convert to wchar_t)
749 struct wxArgNormalizer
<const wxUniChar
&> : public wxArgNormalizer
<wchar_t>
751 wxArgNormalizer(const wxUniChar
& s
,
752 const wxFormatString
*fmt
, unsigned index
)
753 : wxArgNormalizer
<wchar_t>(s
.GetValue(), fmt
, index
) {}
756 // for wchar_t, default handler does the right thing
758 // char has to be treated differently in Unicode builds: a char argument may
759 // be used either for a character value (which should be converted into
760 // wxUniChar) or as an integer value (which should be left as-is). We take
761 // advantage of the fact that both char and wchar_t are converted into int
762 // in variadic arguments here.
765 struct wxArgNormalizerNarrowChar
767 wxArgNormalizerNarrowChar(T value
,
768 const wxFormatString
*fmt
, unsigned index
)
770 wxASSERT_ARG_TYPE( fmt
, index
,
771 wxFormatString::Arg_Char
| wxFormatString::Arg_Int
);
773 // FIXME-UTF8: which one is better default in absence of fmt string
774 // (i.e. when used like e.g. Foo("foo", "bar", 'c', NULL)?
775 if ( !fmt
|| fmt
->GetArgumentType(index
) == wxFormatString::Arg_Char
)
776 m_value
= wx_truncate_cast(T
, wxUniChar(value
).GetValue());
781 int get() const { return m_value
; }
787 struct wxArgNormalizer
<char> : public wxArgNormalizerNarrowChar
<char>
789 wxArgNormalizer(char value
,
790 const wxFormatString
*fmt
, unsigned index
)
791 : wxArgNormalizerNarrowChar
<char>(value
, fmt
, index
) {}
795 struct wxArgNormalizer
<unsigned char>
796 : public wxArgNormalizerNarrowChar
<unsigned char>
798 wxArgNormalizer(unsigned char value
,
799 const wxFormatString
*fmt
, unsigned index
)
800 : wxArgNormalizerNarrowChar
<unsigned char>(value
, fmt
, index
) {}
804 struct wxArgNormalizer
<signed char>
805 : public wxArgNormalizerNarrowChar
<signed char>
807 wxArgNormalizer(signed char value
,
808 const wxFormatString
*fmt
, unsigned index
)
809 : wxArgNormalizerNarrowChar
<signed char>(value
, fmt
, index
) {}
812 #endif // wxUSE_UNICODE
814 // convert references:
815 WX_ARG_NORMALIZER_FORWARD(wxUniChar
, const wxUniChar
&);
816 WX_ARG_NORMALIZER_FORWARD(const wxUniCharRef
&, const wxUniChar
&);
817 WX_ARG_NORMALIZER_FORWARD(wxUniCharRef
, const wxUniChar
&);
818 WX_ARG_NORMALIZER_FORWARD(const wchar_t&, wchar_t);
820 WX_ARG_NORMALIZER_FORWARD(const char&, char);
821 WX_ARG_NORMALIZER_FORWARD(const unsigned char&, unsigned char);
822 WX_ARG_NORMALIZER_FORWARD(const signed char&, signed char);
825 #undef WX_ARG_NORMALIZER_FORWARD
826 #undef _WX_ARG_NORMALIZER_FORWARD_IMPL
828 // NB: Don't #undef wxASSERT_ARG_TYPE here as it's also used in wx/longlong.h.
830 // ----------------------------------------------------------------------------
832 // ----------------------------------------------------------------------------
834 // Replacement for va_arg() for use with strings in functions that accept
835 // strings normalized by wxArgNormalizer<T>:
837 struct WXDLLIMPEXP_BASE wxArgNormalizedString
839 wxArgNormalizedString(const void* ptr
) : m_ptr(ptr
) {}
841 // returns true if non-NULL string was passed in
842 bool IsValid() const { return m_ptr
!= NULL
; }
843 operator bool() const { return IsValid(); }
845 // extracts the string, returns empty string if NULL was passed in
846 wxString
GetString() const;
847 operator wxString() const;
853 #define WX_VA_ARG_STRING(ap) wxArgNormalizedString(va_arg(ap, const void*))
855 // ----------------------------------------------------------------------------
856 // implementation of the WX_DEFINE_VARARG_* macros
857 // ----------------------------------------------------------------------------
859 // NB: The vararg emulation code is limited to 30 variadic and 4 fixed
860 // arguments at the moment.
861 // If you need more variadic arguments, you need to
862 // 1) increase the value of _WX_VARARG_MAX_ARGS
863 // 2) add _WX_VARARG_JOIN_* and _WX_VARARG_ITER_* up to the new
864 // _WX_VARARG_MAX_ARGS value to the lists below
865 // If you need more fixed arguments, you need to
866 // 1) increase the value of _WX_VARARG_MAX_FIXED_ARGS
867 // 2) add _WX_VARARG_FIXED_EXPAND_* and _WX_VARARG_FIXED_UNUSED_EXPAND_*
869 #define _WX_VARARG_MAX_ARGS 30
870 #define _WX_VARARG_MAX_FIXED_ARGS 4
872 #define _WX_VARARG_JOIN_1(m) m(1)
873 #define _WX_VARARG_JOIN_2(m) _WX_VARARG_JOIN_1(m), m(2)
874 #define _WX_VARARG_JOIN_3(m) _WX_VARARG_JOIN_2(m), m(3)
875 #define _WX_VARARG_JOIN_4(m) _WX_VARARG_JOIN_3(m), m(4)
876 #define _WX_VARARG_JOIN_5(m) _WX_VARARG_JOIN_4(m), m(5)
877 #define _WX_VARARG_JOIN_6(m) _WX_VARARG_JOIN_5(m), m(6)
878 #define _WX_VARARG_JOIN_7(m) _WX_VARARG_JOIN_6(m), m(7)
879 #define _WX_VARARG_JOIN_8(m) _WX_VARARG_JOIN_7(m), m(8)
880 #define _WX_VARARG_JOIN_9(m) _WX_VARARG_JOIN_8(m), m(9)
881 #define _WX_VARARG_JOIN_10(m) _WX_VARARG_JOIN_9(m), m(10)
882 #define _WX_VARARG_JOIN_11(m) _WX_VARARG_JOIN_10(m), m(11)
883 #define _WX_VARARG_JOIN_12(m) _WX_VARARG_JOIN_11(m), m(12)
884 #define _WX_VARARG_JOIN_13(m) _WX_VARARG_JOIN_12(m), m(13)
885 #define _WX_VARARG_JOIN_14(m) _WX_VARARG_JOIN_13(m), m(14)
886 #define _WX_VARARG_JOIN_15(m) _WX_VARARG_JOIN_14(m), m(15)
887 #define _WX_VARARG_JOIN_16(m) _WX_VARARG_JOIN_15(m), m(16)
888 #define _WX_VARARG_JOIN_17(m) _WX_VARARG_JOIN_16(m), m(17)
889 #define _WX_VARARG_JOIN_18(m) _WX_VARARG_JOIN_17(m), m(18)
890 #define _WX_VARARG_JOIN_19(m) _WX_VARARG_JOIN_18(m), m(19)
891 #define _WX_VARARG_JOIN_20(m) _WX_VARARG_JOIN_19(m), m(20)
892 #define _WX_VARARG_JOIN_21(m) _WX_VARARG_JOIN_20(m), m(21)
893 #define _WX_VARARG_JOIN_22(m) _WX_VARARG_JOIN_21(m), m(22)
894 #define _WX_VARARG_JOIN_23(m) _WX_VARARG_JOIN_22(m), m(23)
895 #define _WX_VARARG_JOIN_24(m) _WX_VARARG_JOIN_23(m), m(24)
896 #define _WX_VARARG_JOIN_25(m) _WX_VARARG_JOIN_24(m), m(25)
897 #define _WX_VARARG_JOIN_26(m) _WX_VARARG_JOIN_25(m), m(26)
898 #define _WX_VARARG_JOIN_27(m) _WX_VARARG_JOIN_26(m), m(27)
899 #define _WX_VARARG_JOIN_28(m) _WX_VARARG_JOIN_27(m), m(28)
900 #define _WX_VARARG_JOIN_29(m) _WX_VARARG_JOIN_28(m), m(29)
901 #define _WX_VARARG_JOIN_30(m) _WX_VARARG_JOIN_29(m), m(30)
903 #define _WX_VARARG_ITER_1(m,a,b,c,d,e,f) m(1,a,b,c,d,e,f)
904 #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)
905 #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)
906 #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)
907 #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)
908 #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)
909 #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)
910 #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)
911 #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)
912 #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)
913 #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)
914 #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)
915 #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)
916 #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)
917 #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)
918 #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)
919 #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)
920 #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)
921 #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)
922 #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)
923 #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)
924 #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)
925 #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)
926 #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)
927 #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)
928 #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)
929 #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)
930 #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)
931 #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)
932 #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)
935 #define _WX_VARARG_FIXED_EXPAND_1(t1) \
937 #define _WX_VARARG_FIXED_EXPAND_2(t1,t2) \
939 #define _WX_VARARG_FIXED_EXPAND_3(t1,t2,t3) \
941 #define _WX_VARARG_FIXED_EXPAND_4(t1,t2,t3,t4) \
942 t1 f1, t2 f2, t3 f3, t4 f4
944 #define _WX_VARARG_FIXED_UNUSED_EXPAND_1(t1) \
946 #define _WX_VARARG_FIXED_UNUSED_EXPAND_2(t1,t2) \
947 t1 WXUNUSED(f1), t2 WXUNUSED(f2)
948 #define _WX_VARARG_FIXED_UNUSED_EXPAND_3(t1,t2,t3) \
949 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3)
950 #define _WX_VARARG_FIXED_UNUSED_EXPAND_4(t1,t2,t3,t4) \
951 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3), t4 WXUNUSED(f4)
953 #define _WX_VARARG_FIXED_TYPEDEFS_1(t1) \
955 #define _WX_VARARG_FIXED_TYPEDEFS_2(t1,t2) \
956 _WX_VARARG_FIXED_TYPEDEFS_1(t1); typedef t2 TF2
957 #define _WX_VARARG_FIXED_TYPEDEFS_3(t1,t2,t3) \
958 _WX_VARARG_FIXED_TYPEDEFS_2(t1,t2); typedef t3 TF3
959 #define _WX_VARARG_FIXED_TYPEDEFS_4(t1,t2,t3,t4) \
960 _WX_VARARG_FIXED_TYPEDEFS_3(t1,t2,t3); typedef t4 TF4
962 // This macro expands N-items tuple of fixed arguments types into part of
963 // function's declaration. For example,
964 // "_WX_VARARG_FIXED_EXPAND(3, (int, char*, int))" expands into
965 // "int f1, char* f2, int f3".
966 #define _WX_VARARG_FIXED_EXPAND(N, args) \
967 _WX_VARARG_FIXED_EXPAND_IMPL(N, args)
968 #define _WX_VARARG_FIXED_EXPAND_IMPL(N, args) \
969 _WX_VARARG_FIXED_EXPAND_##N args
971 // Ditto for unused arguments
972 #define _WX_VARARG_FIXED_UNUSED_EXPAND(N, args) \
973 _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args)
974 #define _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args) \
975 _WX_VARARG_FIXED_UNUSED_EXPAND_##N args
977 // Declarates typedefs for fixed arguments types; i-th fixed argument types
978 // will have TFi typedef.
979 #define _WX_VARARG_FIXED_TYPEDEFS(N, args) \
980 _WX_VARARG_FIXED_TYPEDEFS_IMPL(N, args)
981 #define _WX_VARARG_FIXED_TYPEDEFS_IMPL(N, args) \
982 _WX_VARARG_FIXED_TYPEDEFS_##N args
985 // This macro calls another macro 'm' passed as second argument 'N' times,
986 // with its only argument set to 1..N, and concatenates the results using
987 // comma as separator.
990 // #define foo(i) x##i
991 // // this expands to "x1,x2,x3,x4"
992 // _WX_VARARG_JOIN(4, foo)
995 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
996 #define _WX_VARARG_JOIN(N, m) _WX_VARARG_JOIN_IMPL(N, m)
997 #define _WX_VARARG_JOIN_IMPL(N, m) _WX_VARARG_JOIN_##N(m)
1000 // This macro calls another macro 'm' passed as second argument 'N' times, with
1001 // its first argument set to 1..N and the remaining arguments set to 'a', 'b',
1002 // 'c', 'd', 'e' and 'f'. The results are separated with whitespace in the
1006 // // this macro expands to:
1007 // // foo(1,a,b,c,d,e,f)
1008 // // foo(2,a,b,c,d,e,f)
1009 // // foo(3,a,b,c,d,e,f)
1010 // _WX_VARARG_ITER(3, foo, a, b, c, d, e, f)
1012 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
1013 #define _WX_VARARG_ITER(N,m,a,b,c,d,e,f) \
1014 _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f)
1015 #define _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f) \
1016 _WX_VARARG_ITER_##N(m,a,b,c,d,e,f)
1018 // Generates code snippet for i-th "variadic" argument in vararg function's
1020 #define _WX_VARARG_ARG(i) T##i a##i
1022 // Like _WX_VARARG_ARG_UNUSED, but outputs argument's type with WXUNUSED:
1023 #define _WX_VARARG_ARG_UNUSED(i) T##i WXUNUSED(a##i)
1025 // Generates code snippet for i-th type in vararg function's template<...>:
1026 #define _WX_VARARG_TEMPL(i) typename T##i
1028 // Generates code snippet for passing i-th argument of vararg function
1029 // wrapper to its implementation, normalizing it in the process:
1030 #define _WX_VARARG_PASS_WCHAR(i) \
1031 wxArgNormalizerWchar<T##i>(a##i, fmt, i).get()
1032 #define _WX_VARARG_PASS_UTF8(i) \
1033 wxArgNormalizerUtf8<T##i>(a##i, fmt, i).get()
1036 // And the same for fixed arguments, _not_ normalizing it:
1037 #define _WX_VARARG_PASS_FIXED(i) f##i
1039 #define _WX_VARARG_FIND_FMT(i) \
1040 (wxFormatStringArgumentFinder<TF##i>::find(f##i))
1042 #define _WX_VARARG_FORMAT_STRING(numfixed, fixed) \
1043 _WX_VARARG_FIXED_TYPEDEFS(numfixed, fixed); \
1044 const wxFormatString *fmt = \
1045 (_WX_VARARG_JOIN(numfixed, _WX_VARARG_FIND_FMT))
1047 #if wxUSE_UNICODE_UTF8
1048 #define _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed) \
1049 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
1050 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_UTF8))
1051 #define _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed) \
1052 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
1053 #endif // wxUSE_UNICODE_UTF8
1055 #define _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed) \
1056 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
1057 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WCHAR))
1058 #define _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed) \
1059 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
1061 #if wxUSE_UNICODE_UTF8
1062 #if wxUSE_UTF8_LOCALE_ONLY
1063 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_UTF8
1064 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_UTF8
1065 #else // possibly non-UTF8 locales
1066 #define _WX_VARARG_DO_CALL(return_kw, impl, implUtf8, N, numfixed) \
1067 if ( wxLocaleIsUtf8 ) \
1068 _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed);\
1070 _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed)
1072 #define _WX_VARARG_DO_CALL0(return_kw, impl, implUtf8, numfixed) \
1073 if ( wxLocaleIsUtf8 ) \
1074 _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed); \
1076 _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed)
1077 #endif // wxUSE_UTF8_LOCALE_ONLY or not
1078 #else // wxUSE_UNICODE_WCHAR or ANSI
1079 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_WCHAR
1080 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_WCHAR
1081 #endif // wxUSE_UNICODE_UTF8 / wxUSE_UNICODE_WCHAR
1084 // Macro to be used with _WX_VARARG_ITER in the implementation of
1085 // WX_DEFINE_VARARG_FUNC (see its documentation for the meaning of arguments)
1086 #define _WX_VARARG_DEFINE_FUNC(N, rettype, name, \
1087 impl, implUtf8, numfixed, fixed) \
1088 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1089 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1090 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1092 _WX_VARARG_FORMAT_STRING(numfixed, fixed); \
1093 _WX_VARARG_DO_CALL(return, impl, implUtf8, N, numfixed); \
1096 #define _WX_VARARG_DEFINE_FUNC_N0(rettype, name, \
1097 impl, implUtf8, numfixed, fixed) \
1098 inline rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
1100 _WX_VARARG_DO_CALL0(return, impl, implUtf8, numfixed); \
1103 // Macro to be used with _WX_VARARG_ITER in the implementation of
1104 // WX_DEFINE_VARARG_FUNC_VOID (see its documentation for the meaning of
1105 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
1107 #define _WX_VARARG_DEFINE_FUNC_VOID(N, rettype, name, \
1108 impl, implUtf8, numfixed, fixed) \
1109 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1110 void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1111 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1113 _WX_VARARG_FORMAT_STRING(numfixed, fixed); \
1114 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
1115 impl, implUtf8, N, numfixed); \
1118 #define _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
1119 inline void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
1121 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
1122 impl, implUtf8, numfixed); \
1125 // Macro to be used with _WX_VARARG_ITER in the implementation of
1126 // WX_DEFINE_VARARG_FUNC_CTOR (see its documentation for the meaning of
1127 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
1129 #define _WX_VARARG_DEFINE_FUNC_CTOR(N, rettype, name, \
1130 impl, implUtf8, numfixed, fixed) \
1131 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1132 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1133 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1135 _WX_VARARG_FORMAT_STRING(numfixed, fixed); \
1136 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
1137 impl, implUtf8, N, numfixed); \
1140 #define _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
1141 inline name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
1143 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
1144 impl, implUtf8, numfixed); \
1147 // Macro to be used with _WX_VARARG_ITER in the implementation of
1148 // WX_DEFINE_VARARG_FUNC_NOP, i.e. empty stub for a disabled vararg function.
1149 // The rettype and impl arguments are ignored.
1150 #define _WX_VARARG_DEFINE_FUNC_NOP(N, rettype, name, \
1151 impl, implUtf8, numfixed, fixed) \
1152 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1153 void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed), \
1154 _WX_VARARG_JOIN(N, _WX_VARARG_ARG_UNUSED)) \
1157 #define _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
1158 inline void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed)) \
1162 // ----------------------------------------------------------------------------
1163 // workaround for OpenWatcom bug #351
1164 // ----------------------------------------------------------------------------
1167 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1169 // This macro can be used to forward a 'vararg' template to another one with
1170 // different fixed arguments types. Parameters are same as for
1171 // WX_DEFINE_VARARG_FUNC (rettype=void can be used here), 'convfixed' is how
1172 // to convert fixed arguments. For example, this is typical code for dealing
1173 // with different forms of format string:
1175 // WX_DEFINE_VARARG_FUNC_VOID(Printf, 1, (const wxFormatString&),
1176 // DoPrintfWchar, DoPrintfUtf8)
1177 // #ifdef __WATCOMC__
1178 // WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const wxString&),
1179 // (wxFormatString(f1)))
1180 // WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const char*),
1181 // (wxFormatString(f1)))
1183 #define WX_VARARG_WATCOM_WORKAROUND(rettype, name, numfixed, fixed, convfixed)\
1184 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
1185 _WX_VARARG_WATCOM_WORKAROUND, \
1186 rettype, name, convfixed, dummy, numfixed, fixed)
1188 #define WX_VARARG_WATCOM_WORKAROUND_CTOR(name, numfixed, fixed, convfixed) \
1189 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
1190 _WX_VARARG_WATCOM_WORKAROUND_CTOR, \
1191 dummy, name, convfixed, dummy, numfixed, fixed)
1193 #define _WX_VARARG_WATCOM_UNPACK_1(a1) a1
1194 #define _WX_VARARG_WATCOM_UNPACK_2(a1, a2) a1, a2
1195 #define _WX_VARARG_WATCOM_UNPACK_3(a1, a2, a3) a1, a2, a3
1196 #define _WX_VARARG_WATCOM_UNPACK_4(a1, a2, a3, a4) a1, a2, a3, a4
1197 #define _WX_VARARG_WATCOM_UNPACK(N, convfixed) \
1198 _WX_VARARG_WATCOM_UNPACK_##N convfixed
1200 #define _WX_VARARG_PASS_WATCOM(i) a##i
1202 #define _WX_VARARG_WATCOM_WORKAROUND(N, rettype, name, \
1203 convfixed, dummy, numfixed, fixed) \
1204 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1205 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1206 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1208 return name(_WX_VARARG_WATCOM_UNPACK(numfixed, convfixed), \
1209 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WATCOM)); \
1212 #define _WX_VARARG_WATCOM_WORKAROUND_CTOR(N, dummy1, name, \
1213 convfixed, dummy2, numfixed, fixed) \
1214 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
1215 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
1216 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
1218 name(_WX_VARARG_WATCOM_UNPACK(numfixed, convfixed), \
1219 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WATCOM)); \
1222 #endif // __WATCOMC__
1224 #endif // _WX_STRVARARG_H_