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