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