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