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