removed outdated comment
[wxWidgets.git] / include / wx / strvararg.h
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
19 #include "wx/cpp.h"
20 #include "wx/chartype.h"
21 #include "wx/strconv.h"
22 #include "wx/buffer.h"
23 #include "wx/unichar.h"
24
25 class WXDLLIMPEXP_FWD_BASE wxCStrData;
26 class WXDLLIMPEXP_FWD_BASE wxString;
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
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:
44 // * char* in the current locale's charset in ANSI build
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
49 //
50 // Note that wxFormatString *must* be used for the format parameter of these
51 // functions, otherwise the implementation won't work correctly.
52 //
53 // Parameters:
54 // [ there are examples in square brackets showing values of the parameters
55 // for the wxFprintf() wrapper for fprintf() function with the following
56 // prototype:
57 // int wxFprintf(FILE *stream, const wxString& format, ...); ]
58 //
59 // rettype Functions' return type [int]
60 // name Name of the function [fprintf]
61 // numfixed The number of leading "fixed" (i.e., not variadic)
62 // arguments of the function (e.g. "stream" and "format"
63 // arguments of fprintf()); their type is _not_ converted
64 // using wxArgNormalizer<T>, unlike the rest of
65 // the function's arguments [2]
66 // fixed List of types of the leading "fixed" arguments, in
67 // parenthesis [(FILE*,const wxString&)]
68 // impl Name of the variadic function that implements 'name' for
69 // the native strings representation (wchar_t* if
70 // wxUSE_UNICODE_WCHAR or wxUSE_UNICODE_UTF8 when running under
71 // non-UTF8 locale, char* in ANSI build) [wxCrt_Fprintf]
72 // implUtf8 Like 'impl', but for the UTF-8 char* version to be used
73 // if wxUSE_UNICODE_UTF8 and running under UTF-8 locale
74 // (ignored otherwise) [fprintf]
75 //
76 #define WX_DEFINE_VARARG_FUNC(rettype, name, numfixed, fixed, impl, implUtf8) \
77 _WX_VARARG_DEFINE_FUNC_N0(rettype, name, impl, implUtf8, numfixed, fixed) \
78 WX_DEFINE_VARARG_FUNC_SANS_N0(rettype, name, numfixed, fixed, impl, implUtf8)
79
80 // ditto, but without the version with 0 template/vararg arguments
81 #define WX_DEFINE_VARARG_FUNC_SANS_N0(rettype, name, \
82 numfixed, fixed, impl, implUtf8) \
83 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
84 _WX_VARARG_DEFINE_FUNC, \
85 rettype, name, impl, implUtf8, numfixed, fixed)
86
87 // Like WX_DEFINE_VARARG_FUNC, but for variadic functions that don't return
88 // a value.
89 #define WX_DEFINE_VARARG_FUNC_VOID(name, numfixed, fixed, impl, implUtf8) \
90 _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
91 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
92 _WX_VARARG_DEFINE_FUNC_VOID, \
93 void, name, impl, implUtf8, numfixed, fixed)
94
95 // Like WX_DEFINE_VARARG_FUNC_VOID, but instead of wrapping an implementation
96 // function, does nothing in defined functions' bodies.
97 //
98 // Used to implement wxLogXXX functions if wxUSE_LOG=0.
99 #define WX_DEFINE_VARARG_FUNC_NOP(name, numfixed, fixed) \
100 _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
101 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
102 _WX_VARARG_DEFINE_FUNC_NOP, \
103 void, name, dummy, dummy, numfixed, fixed)
104
105 // Like WX_DEFINE_VARARG_FUNC_CTOR, but for defining template constructors
106 #define WX_DEFINE_VARARG_FUNC_CTOR(name, numfixed, fixed, impl, implUtf8) \
107 _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
108 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
109 _WX_VARARG_DEFINE_FUNC_CTOR, \
110 void, name, impl, implUtf8, numfixed, fixed)
111
112
113 // ----------------------------------------------------------------------------
114 // wxFormatString
115 // ----------------------------------------------------------------------------
116
117 // This class must be used for format string argument of the functions
118 // defined using WX_DEFINE_VARARG_FUNC_* macros. It converts the string to
119 // char* or wchar_t* for passing to implementation function efficiently (i.e.
120 // without keeping the converted string in memory for longer than necessary,
121 // like c_str()). It also converts format string to the correct form that
122 // accounts for string changes done by wxArgNormalizer<>
123 //
124 // Note that this class can _only_ be used for function arguments!
125 class WXDLLIMPEXP_BASE wxFormatString
126 {
127 public:
128 wxFormatString(const char *str)
129 : m_char(wxCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
130 wxFormatString(const wchar_t *str)
131 : m_wchar(wxWCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
132 wxFormatString(const wxString& str)
133 : m_str(&str), m_cstr(NULL) {}
134 wxFormatString(const wxCStrData& str)
135 : m_str(NULL), m_cstr(&str) {}
136 wxFormatString(const wxCharBuffer& str)
137 : m_char(str), m_str(NULL), m_cstr(NULL) {}
138 wxFormatString(const wxWCharBuffer& str)
139 : m_wchar(str), m_str(NULL), m_cstr(NULL) {}
140
141 #if !wxUSE_UNICODE_WCHAR
142 operator const char*() const
143 { return wx_const_cast(wxFormatString*, this)->AsChar(); }
144 private:
145 // InputAsChar() returns the value converted passed to ctor, only converted
146 // to char, while AsChar() takes the the string returned by InputAsChar()
147 // and does format string conversion on it as well (and similarly for
148 // ..AsWChar() below)
149 const char* InputAsChar();
150 const char* AsChar();
151 wxCharBuffer m_convertedChar;
152 #endif // !wxUSE_UNICODE_WCHAR
153
154 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
155 public:
156 operator const wchar_t*() const
157 { return wx_const_cast(wxFormatString*, this)->AsWChar(); }
158 private:
159 const wchar_t* InputAsWChar();
160 const wchar_t* AsWChar();
161 wxWCharBuffer m_convertedWChar;
162 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
163
164 private:
165 #ifdef __VISUALC__
166 // "struct 'ConvertedBuffer<T>' needs to have dll-interface to be used by
167 // clients of class 'wxString'" - this is private, we don't care
168 #pragma warning (disable:4251)
169 #endif
170 wxCharBuffer m_char;
171 wxWCharBuffer m_wchar;
172 #ifdef __VISUALC__
173 #pragma warning (default:4251)
174 #endif
175
176 // NB: we can use a pointer here, because wxFormatString is only used
177 // as function argument, so it has shorter life than the string
178 // passed to the ctor
179 const wxString * const m_str;
180 const wxCStrData * const m_cstr;
181
182 DECLARE_NO_COPY_CLASS(wxFormatString)
183 };
184
185 // ----------------------------------------------------------------------------
186 // wxArgNormalizer*<T> converters
187 // ----------------------------------------------------------------------------
188
189 // Converts an argument passed to wxPrint etc. into standard form expected,
190 // by wxXXX functions, e.g. all strings (wxString, char*, wchar_t*) are
191 // converted into wchar_t* or char* depending on the build.
192 template<typename T>
193 struct wxArgNormalizer
194 {
195 wxArgNormalizer(const T& value) : m_value(value) {}
196
197 // Returns the value in a form that can be safely passed to real vararg
198 // functions. In case of strings, this is char* in ANSI build and wchar_t*
199 // in Unicode build.
200 T get() const { return m_value; }
201
202 T m_value;
203 };
204
205 // normalizer for passing arguments to functions working with wchar_t* (and
206 // until ANSI build is removed, char* in ANSI build as well - FIXME-UTF8)
207 // string representation
208 #if !wxUSE_UTF8_LOCALE_ONLY
209 template<typename T>
210 struct wxArgNormalizerWchar : public wxArgNormalizer<T>
211 {
212 wxArgNormalizerWchar(const T& value) : wxArgNormalizer<T>(value) {}
213 };
214 #endif // !wxUSE_UTF8_LOCALE_ONLY
215
216 // normalizer for passing arguments to functions working with UTF-8 encoded
217 // char* strings
218 #if wxUSE_UNICODE_UTF8
219 template<typename T>
220 struct wxArgNormalizerUtf8 : public wxArgNormalizer<T>
221 {
222 wxArgNormalizerUtf8(const T& value) : wxArgNormalizer<T>(value) {}
223 };
224
225 #define wxArgNormalizerNative wxArgNormalizerUtf8
226 #else // wxUSE_UNICODE_WCHAR
227 #define wxArgNormalizerNative wxArgNormalizerWchar
228 #endif // wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_UTF8
229
230
231
232 // special cases for converting strings:
233
234
235 // base class for wxArgNormalizer<T> specializations that need to do conversion;
236 // CharType is either wxStringCharType or wchar_t in UTF-8 build when wrapping
237 // widechar CRT function
238 template<typename CharType>
239 struct wxArgNormalizerWithBuffer
240 {
241 typedef wxCharTypeBuffer<CharType> CharBuffer;
242
243 wxArgNormalizerWithBuffer() {}
244 wxArgNormalizerWithBuffer(const CharBuffer& buf) : m_value(buf) {}
245
246 const CharType *get() const { return m_value; }
247
248 CharBuffer m_value;
249 };
250
251 // string objects:
252 template<>
253 struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxString&>
254 {
255 wxArgNormalizerNative(const wxString& s) : m_value(s) {}
256 const wxStringCharType *get() const;
257
258 const wxString& m_value;
259 };
260
261 // c_str() values:
262 template<>
263 struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxCStrData&>
264 {
265 wxArgNormalizerNative(const wxCStrData& value) : m_value(value) {}
266 const wxStringCharType *get() const;
267
268 const wxCStrData& m_value;
269 };
270
271 // wxString/wxCStrData conversion to wchar_t* value
272 #if wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
273 template<>
274 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxString&>
275 : public wxArgNormalizerWithBuffer<wchar_t>
276 {
277 wxArgNormalizerWchar(const wxString& s);
278 };
279
280 template<>
281 struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxCStrData&>
282 : public wxArgNormalizerWithBuffer<wchar_t>
283 {
284 wxArgNormalizerWchar(const wxCStrData& s);
285 };
286 #endif // wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
287
288
289 // C string pointers of the wrong type (wchar_t* for ANSI or UTF8 build,
290 // char* for wchar_t Unicode build or UTF8):
291 #if wxUSE_UNICODE_WCHAR
292
293 template<>
294 struct wxArgNormalizerWchar<const char*>
295 : public wxArgNormalizerWithBuffer<wchar_t>
296 {
297 wxArgNormalizerWchar(const char* s)
298 : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s)) {}
299 };
300
301 #elif wxUSE_UNICODE_UTF8
302
303 template<>
304 struct wxArgNormalizerUtf8<const wchar_t*>
305 : public wxArgNormalizerWithBuffer<char>
306 {
307 wxArgNormalizerUtf8(const wchar_t* s)
308 : wxArgNormalizerWithBuffer<char>(wxConvUTF8.cWC2MB(s)) {}
309 };
310
311 template<>
312 struct wxArgNormalizerUtf8<const char*>
313 : public wxArgNormalizerWithBuffer<char>
314 {
315 wxArgNormalizerUtf8(const char* s)
316 {
317 if ( wxLocaleIsUtf8 )
318 {
319 m_value = wxCharBuffer::CreateNonOwned(s);
320 }
321 else
322 {
323 // convert to widechar string first:
324 wxWCharBuffer buf(wxConvLibc.cMB2WC(s));
325
326 // then to UTF-8:
327 if ( buf )
328 m_value = wxConvUTF8.cWC2MB(buf);
329 }
330 }
331 };
332
333 // UTF-8 build needs conversion to wchar_t* too:
334 #if !wxUSE_UTF8_LOCALE_ONLY
335 template<>
336 struct wxArgNormalizerWchar<const char*>
337 : public wxArgNormalizerWithBuffer<wchar_t>
338 {
339 wxArgNormalizerWchar(const char* s)
340 : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s)) {}
341 };
342 #endif // !wxUSE_UTF8_LOCALE_ONLY
343
344 #else // ANSI - FIXME-UTF8
345
346 template<>
347 struct wxArgNormalizerWchar<const wchar_t*>
348 : public wxArgNormalizerWithBuffer<char>
349 {
350 wxArgNormalizerWchar(const wchar_t* s)
351 : wxArgNormalizerWithBuffer<char>(wxConvLibc.cWC2MB(s)) {}
352 };
353
354 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8/ANSI
355
356
357 // this macro is used to implement specialization that are exactly same as
358 // some other specialization, i.e. to "forward" the implementation (e.g. for
359 // T=wxString and T=const wxString&). Note that the ctor takes BaseT argument,
360 // not T!
361 #if wxUSE_UNICODE_UTF8
362 #if wxUSE_UTF8_LOCALE_ONLY
363 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
364 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
365 #else // possibly non-UTF8 locales
366 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
367 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT); \
368 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerUtf8, T, BaseT)
369 #endif
370 #else // wxUSE_UNICODE_WCHAR
371 #define WX_ARG_NORMALIZER_FORWARD(T, BaseT) \
372 _WX_ARG_NORMALIZER_FORWARD_IMPL(wxArgNormalizerWchar, T, BaseT)
373 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
374
375 #define _WX_ARG_NORMALIZER_FORWARD_IMPL(Normalizer, T, BaseT) \
376 template<> \
377 struct Normalizer<T> : public Normalizer<BaseT> \
378 { \
379 Normalizer(BaseT value) : Normalizer<BaseT>(value) {} \
380 }
381
382 // non-reference versions of specializations for string objects
383 WX_ARG_NORMALIZER_FORWARD(wxString, const wxString&);
384 WX_ARG_NORMALIZER_FORWARD(wxCStrData, const wxCStrData&);
385
386 // versions for passing non-const pointers:
387 WX_ARG_NORMALIZER_FORWARD(char*, const char*);
388 WX_ARG_NORMALIZER_FORWARD(wchar_t*, const wchar_t*);
389
390 // versions for passing wx[W]CharBuffer:
391 WX_ARG_NORMALIZER_FORWARD(wxCharBuffer, const char*);
392 WX_ARG_NORMALIZER_FORWARD(const wxCharBuffer&, const char*);
393 WX_ARG_NORMALIZER_FORWARD(wxWCharBuffer, const wchar_t*);
394 WX_ARG_NORMALIZER_FORWARD(const wxWCharBuffer&, const wchar_t*);
395
396 // versions for std::[w]string:
397 #if wxUSE_STD_STRING
398
399 #include "wx/stringimpl.h"
400
401 #if !wxUSE_UTF8_LOCALE_ONLY
402 template<>
403 struct wxArgNormalizerWchar<const std::string&>
404 : public wxArgNormalizerWchar<const char*>
405 {
406 wxArgNormalizerWchar(const std::string& s)
407 : wxArgNormalizerWchar<const char*>(s.c_str()) {}
408 };
409
410 template<>
411 struct wxArgNormalizerWchar<const wxStdWideString&>
412 : public wxArgNormalizerWchar<const wchar_t*>
413 {
414 wxArgNormalizerWchar(const wxStdWideString& s)
415 : wxArgNormalizerWchar<const wchar_t*>(s.c_str()) {}
416 };
417 #endif // !wxUSE_UTF8_LOCALE_ONLY
418
419 #if wxUSE_UNICODE_UTF8
420 template<>
421 struct wxArgNormalizerUtf8<const std::string&>
422 : public wxArgNormalizerUtf8<const char*>
423 {
424 wxArgNormalizerUtf8(const std::string& s)
425 : wxArgNormalizerUtf8<const char*>(s.c_str()) {}
426 };
427
428 template<>
429 struct wxArgNormalizerUtf8<const wxStdWideString&>
430 : public wxArgNormalizerUtf8<const wchar_t*>
431 {
432 wxArgNormalizerUtf8(const wxStdWideString& s)
433 : wxArgNormalizerUtf8<const wchar_t*>(s.c_str()) {}
434 };
435 #endif // wxUSE_UNICODE_UTF8
436
437 WX_ARG_NORMALIZER_FORWARD(std::string, const std::string&);
438 WX_ARG_NORMALIZER_FORWARD(wxStdWideString, const wxStdWideString&);
439
440 #endif // wxUSE_STD_STRING
441
442
443 // versions for wxUniChar, wxUniCharRef:
444
445 #if !wxUSE_UTF8_LOCALE_ONLY
446 template<>
447 struct wxArgNormalizerWchar<const wxUniChar&>
448 {
449 wxArgNormalizerWchar(const wxUniChar& s) : m_value(s) {}
450
451 // FIXME-UTF8: use wchar_t once ANSI build is removed
452 wxChar get() const { return m_value; }
453
454 wxChar m_value;
455 };
456 #endif // !wxUSE_UTF8_LOCALE_ONLY
457
458 #if wxUSE_UNICODE_UTF8
459 template<>
460 struct wxArgNormalizerUtf8<const wxUniChar&>
461 {
462 wxArgNormalizerUtf8(const wxUniChar& s) : m_value(s.AsUTF8()) {}
463
464 const wxStringCharType *get() const { return m_value; }
465
466 wxUniChar::Utf8CharBuffer m_value;
467 };
468 #endif // wxUSE_UNICODE_UTF8
469
470 WX_ARG_NORMALIZER_FORWARD(wxUniChar, const wxUniChar&);
471 WX_ARG_NORMALIZER_FORWARD(const wxUniCharRef&, const wxUniChar&);
472 WX_ARG_NORMALIZER_FORWARD(wxUniCharRef, const wxUniChar&);
473 // convert char/wchar_t to wxUniChar to get output in the right encoding:
474 WX_ARG_NORMALIZER_FORWARD(char, const wxUniChar&);
475 WX_ARG_NORMALIZER_FORWARD(const char&, const wxUniChar&);
476 WX_ARG_NORMALIZER_FORWARD(unsigned char, const wxUniChar&);
477 WX_ARG_NORMALIZER_FORWARD(const unsigned char&, const wxUniChar&);
478 WX_ARG_NORMALIZER_FORWARD(wchar_t, const wxUniChar&);
479 WX_ARG_NORMALIZER_FORWARD(const wchar_t&, const wxUniChar&);
480
481
482 #undef WX_ARG_NORMALIZER_FORWARD
483 #undef _WX_ARG_NORMALIZER_FORWARD_IMPL
484
485 // ----------------------------------------------------------------------------
486 // WX_VA_ARG_STRING
487 // ----------------------------------------------------------------------------
488
489 // Replacement for va_arg() for use with strings in functions that accept
490 // strings normalized by wxArgNormalizer<T>:
491
492 struct WXDLLIMPEXP_BASE wxArgNormalizedString
493 {
494 wxArgNormalizedString(const void* ptr) : m_ptr(ptr) {}
495
496 // returns true if non-NULL string was passed in
497 bool IsValid() const { return m_ptr != NULL; }
498 operator bool() const { return IsValid(); }
499
500 // extracts the string, returns empty string if NULL was passed in
501 wxString GetString() const;
502 operator wxString() const;
503
504 private:
505 const void *m_ptr;
506 };
507
508 #define WX_VA_ARG_STRING(ap) wxArgNormalizedString(va_arg(ap, const void*))
509
510 // ----------------------------------------------------------------------------
511 // implementation of the WX_DEFINE_VARARG_* macros
512 // ----------------------------------------------------------------------------
513
514 // NB: The vararg emulation code is limited to 30 variadic and 4 fixed
515 // arguments at the moment.
516 // If you need more variadic arguments, you need to
517 // 1) increase the value of _WX_VARARG_MAX_ARGS
518 // 2) add _WX_VARARG_JOIN_* and _WX_VARARG_ITER_* up to the new
519 // _WX_VARARG_MAX_ARGS value to the lists below
520 // If you need more fixed arguments, you need to
521 // 1) increase the value of _WX_VARARG_MAX_FIXED_ARGS
522 // 2) add _WX_VARARG_FIXED_EXPAND_* and _WX_VARARG_FIXED_UNUSED_EXPAND_*
523 // macros below
524 #define _WX_VARARG_MAX_ARGS 30
525 #define _WX_VARARG_MAX_FIXED_ARGS 4
526
527 #define _WX_VARARG_JOIN_1(m) m(1)
528 #define _WX_VARARG_JOIN_2(m) _WX_VARARG_JOIN_1(m), m(2)
529 #define _WX_VARARG_JOIN_3(m) _WX_VARARG_JOIN_2(m), m(3)
530 #define _WX_VARARG_JOIN_4(m) _WX_VARARG_JOIN_3(m), m(4)
531 #define _WX_VARARG_JOIN_5(m) _WX_VARARG_JOIN_4(m), m(5)
532 #define _WX_VARARG_JOIN_6(m) _WX_VARARG_JOIN_5(m), m(6)
533 #define _WX_VARARG_JOIN_7(m) _WX_VARARG_JOIN_6(m), m(7)
534 #define _WX_VARARG_JOIN_8(m) _WX_VARARG_JOIN_7(m), m(8)
535 #define _WX_VARARG_JOIN_9(m) _WX_VARARG_JOIN_8(m), m(9)
536 #define _WX_VARARG_JOIN_10(m) _WX_VARARG_JOIN_9(m), m(10)
537 #define _WX_VARARG_JOIN_11(m) _WX_VARARG_JOIN_10(m), m(11)
538 #define _WX_VARARG_JOIN_12(m) _WX_VARARG_JOIN_11(m), m(12)
539 #define _WX_VARARG_JOIN_13(m) _WX_VARARG_JOIN_12(m), m(13)
540 #define _WX_VARARG_JOIN_14(m) _WX_VARARG_JOIN_13(m), m(14)
541 #define _WX_VARARG_JOIN_15(m) _WX_VARARG_JOIN_14(m), m(15)
542 #define _WX_VARARG_JOIN_16(m) _WX_VARARG_JOIN_15(m), m(16)
543 #define _WX_VARARG_JOIN_17(m) _WX_VARARG_JOIN_16(m), m(17)
544 #define _WX_VARARG_JOIN_18(m) _WX_VARARG_JOIN_17(m), m(18)
545 #define _WX_VARARG_JOIN_19(m) _WX_VARARG_JOIN_18(m), m(19)
546 #define _WX_VARARG_JOIN_20(m) _WX_VARARG_JOIN_19(m), m(20)
547 #define _WX_VARARG_JOIN_21(m) _WX_VARARG_JOIN_20(m), m(21)
548 #define _WX_VARARG_JOIN_22(m) _WX_VARARG_JOIN_21(m), m(22)
549 #define _WX_VARARG_JOIN_23(m) _WX_VARARG_JOIN_22(m), m(23)
550 #define _WX_VARARG_JOIN_24(m) _WX_VARARG_JOIN_23(m), m(24)
551 #define _WX_VARARG_JOIN_25(m) _WX_VARARG_JOIN_24(m), m(25)
552 #define _WX_VARARG_JOIN_26(m) _WX_VARARG_JOIN_25(m), m(26)
553 #define _WX_VARARG_JOIN_27(m) _WX_VARARG_JOIN_26(m), m(27)
554 #define _WX_VARARG_JOIN_28(m) _WX_VARARG_JOIN_27(m), m(28)
555 #define _WX_VARARG_JOIN_29(m) _WX_VARARG_JOIN_28(m), m(29)
556 #define _WX_VARARG_JOIN_30(m) _WX_VARARG_JOIN_29(m), m(30)
557
558 #define _WX_VARARG_ITER_1(m,a,b,c,d,e,f) m(1,a,b,c,d,e,f)
559 #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)
560 #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)
561 #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)
562 #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)
563 #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)
564 #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)
565 #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)
566 #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)
567 #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)
568 #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)
569 #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)
570 #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)
571 #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)
572 #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)
573 #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)
574 #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)
575 #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)
576 #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)
577 #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)
578 #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)
579 #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)
580 #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)
581 #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)
582 #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)
583 #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)
584 #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)
585 #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)
586 #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)
587 #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)
588
589
590 #define _WX_VARARG_FIXED_EXPAND_1(t1) \
591 t1 f1
592 #define _WX_VARARG_FIXED_EXPAND_2(t1,t2) \
593 t1 f1, t2 f2
594 #define _WX_VARARG_FIXED_EXPAND_3(t1,t2,t3) \
595 t1 f1, t2 f2, t3 f3
596 #define _WX_VARARG_FIXED_EXPAND_4(t1,t2,t3,t4) \
597 t1 f1, t2 f2, t3 f3, t4 f4
598
599 #define _WX_VARARG_FIXED_UNUSED_EXPAND_1(t1) \
600 t1 WXUNUSED(f1)
601 #define _WX_VARARG_FIXED_UNUSED_EXPAND_2(t1,t2) \
602 t1 WXUNUSED(f1), t2 WXUNUSED(f2)
603 #define _WX_VARARG_FIXED_UNUSED_EXPAND_3(t1,t2,t3) \
604 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3)
605 #define _WX_VARARG_FIXED_UNUSED_EXPAND_4(t1,t2,t3,t4) \
606 t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3), t4 WXUNUSED(f4)
607
608
609 // This macro expands N-items tuple of fixed arguments types into part of
610 // function's declaration. For example,
611 // "_WX_VARARG_FIXED_EXPAND(3, (int, char*, int))" expands into
612 // "int f1, char* f2, int f3".
613 #define _WX_VARARG_FIXED_EXPAND(N, args) \
614 _WX_VARARG_FIXED_EXPAND_IMPL(N, args)
615 #define _WX_VARARG_FIXED_EXPAND_IMPL(N, args) \
616 _WX_VARARG_FIXED_EXPAND_##N args
617
618 // Ditto for unused arguments
619 #define _WX_VARARG_FIXED_UNUSED_EXPAND(N, args) \
620 _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args)
621 #define _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args) \
622 _WX_VARARG_FIXED_UNUSED_EXPAND_##N args
623
624
625 // This macro calls another macro 'm' passed as second argument 'N' times,
626 // with its only argument set to 1..N, and concatenates the results using
627 // comma as separator.
628 //
629 // An example:
630 // #define foo(i) x##i
631 // // this expands to "x1,x2,x3,x4"
632 // _WX_VARARG_JOIN(4, foo)
633 //
634 //
635 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
636 #define _WX_VARARG_JOIN(N, m) _WX_VARARG_JOIN_IMPL(N, m)
637 #define _WX_VARARG_JOIN_IMPL(N, m) _WX_VARARG_JOIN_##N(m)
638
639
640 // This macro calls another macro 'm' passed as second argument 'N' times, with
641 // its first argument set to 1..N and the remaining arguments set to 'a', 'b',
642 // 'c', 'd', 'e' and 'f'. The results are separated with whitespace in the
643 // expansion.
644 //
645 // An example:
646 // // this macro expands to:
647 // // foo(1,a,b,c,d,e,f)
648 // // foo(2,a,b,c,d,e,f)
649 // // foo(3,a,b,c,d,e,f)
650 // _WX_VARARG_ITER(3, foo, a, b, c, d, e, f)
651 //
652 // N must not be greater than _WX_VARARG_MAX_ARGS (=30).
653 #define _WX_VARARG_ITER(N,m,a,b,c,d,e,f) \
654 _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f)
655 #define _WX_VARARG_ITER_IMPL(N,m,a,b,c,d,e,f) \
656 _WX_VARARG_ITER_##N(m,a,b,c,d,e,f)
657
658 // Generates code snippet for i-th "variadic" argument in vararg function's
659 // prototype:
660 #define _WX_VARARG_ARG(i) T##i a##i
661
662 // Like _WX_VARARG_ARG_UNUSED, but outputs argument's type with WXUNUSED:
663 #define _WX_VARARG_ARG_UNUSED(i) T##i WXUNUSED(a##i)
664
665 // Generates code snippet for i-th type in vararg function's template<...>:
666 #define _WX_VARARG_TEMPL(i) typename T##i
667
668 // Generates code snippet for passing i-th argument of vararg function
669 // wrapper to its implementation, normalizing it in the process:
670 #define _WX_VARARG_PASS_WCHAR(i) wxArgNormalizerWchar<T##i>(a##i).get()
671 #define _WX_VARARG_PASS_UTF8(i) wxArgNormalizerUtf8<T##i>(a##i).get()
672
673
674 // And the same for fixed arguments, _not_ normalizing it:
675 #define _WX_VARARG_PASS_FIXED(i) f##i
676
677 #if wxUSE_UNICODE_UTF8
678 #define _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed) \
679 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
680 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_UTF8))
681 #define _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed) \
682 return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
683 #endif // wxUSE_UNICODE_UTF8
684
685 #define _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed) \
686 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \
687 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WCHAR))
688 #define _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed) \
689 return_kw impl(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED))
690
691 #if wxUSE_UNICODE_UTF8
692 #if wxUSE_UTF8_LOCALE_ONLY
693 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_UTF8
694 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_UTF8
695 #else // possibly non-UTF8 locales
696 #define _WX_VARARG_DO_CALL(return_kw, impl, implUtf8, N, numfixed) \
697 if ( wxLocaleIsUtf8 ) \
698 _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed);\
699 else \
700 _WX_VARARG_DO_CALL_WCHAR(return_kw, impl, implUtf8, N, numfixed)
701
702 #define _WX_VARARG_DO_CALL0(return_kw, impl, implUtf8, numfixed) \
703 if ( wxLocaleIsUtf8 ) \
704 _WX_VARARG_DO_CALL0_UTF8(return_kw, impl, implUtf8, numfixed); \
705 else \
706 _WX_VARARG_DO_CALL0_WCHAR(return_kw, impl, implUtf8, numfixed)
707 #endif // wxUSE_UTF8_LOCALE_ONLY or not
708 #else // wxUSE_UNICODE_WCHAR or ANSI
709 #define _WX_VARARG_DO_CALL _WX_VARARG_DO_CALL_WCHAR
710 #define _WX_VARARG_DO_CALL0 _WX_VARARG_DO_CALL0_WCHAR
711 #endif // wxUSE_UNICODE_UTF8 / wxUSE_UNICODE_WCHAR
712
713
714 // Macro to be used with _WX_VARARG_ITER in the implementation of
715 // WX_DEFINE_VARARG_FUNC (see its documentation for the meaning of arguments)
716 #define _WX_VARARG_DEFINE_FUNC(N, rettype, name, \
717 impl, implUtf8, numfixed, fixed) \
718 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
719 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
720 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
721 { \
722 _WX_VARARG_DO_CALL(return, impl, implUtf8, N, numfixed); \
723 }
724
725 #define _WX_VARARG_DEFINE_FUNC_N0(rettype, name, \
726 impl, implUtf8, numfixed, fixed) \
727 inline rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
728 { \
729 _WX_VARARG_DO_CALL0(return, impl, implUtf8, numfixed); \
730 }
731
732 // Macro to be used with _WX_VARARG_ITER in the implementation of
733 // WX_DEFINE_VARARG_FUNC_VOID (see its documentation for the meaning of
734 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
735 // requirements).
736 #define _WX_VARARG_DEFINE_FUNC_VOID(N, rettype, name, \
737 impl, implUtf8, numfixed, fixed) \
738 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
739 void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
740 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
741 { \
742 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
743 impl, implUtf8, N, numfixed); \
744 }
745
746 #define _WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
747 inline void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
748 { \
749 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
750 impl, implUtf8, numfixed); \
751 }
752
753 // Macro to be used with _WX_VARARG_ITER in the implementation of
754 // WX_DEFINE_VARARG_FUNC_CTOR (see its documentation for the meaning of
755 // arguments; rettype is ignored and is used only to satisfy _WX_VARARG_ITER's
756 // requirements).
757 #define _WX_VARARG_DEFINE_FUNC_CTOR(N, rettype, name, \
758 impl, implUtf8, numfixed, fixed) \
759 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
760 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
761 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
762 { \
763 _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \
764 impl, implUtf8, N, numfixed); \
765 }
766
767 #define _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
768 inline name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed)) \
769 { \
770 _WX_VARARG_DO_CALL0(wxEMPTY_PARAMETER_VALUE, \
771 impl, implUtf8, numfixed); \
772 }
773
774 // Macro to be used with _WX_VARARG_ITER in the implementation of
775 // WX_DEFINE_VARARG_FUNC_NOP, i.e. empty stub for a disabled vararg function.
776 // The rettype and impl arguments are ignored.
777 #define _WX_VARARG_DEFINE_FUNC_NOP(N, rettype, name, \
778 impl, implUtf8, numfixed, fixed) \
779 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
780 void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed), \
781 _WX_VARARG_JOIN(N, _WX_VARARG_ARG_UNUSED)) \
782 {}
783
784 #define _WX_VARARG_DEFINE_FUNC_NOP_N0(name, numfixed, fixed) \
785 inline void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed)) \
786 {}
787
788
789 // ----------------------------------------------------------------------------
790 // workaround for OpenWatcom bug #351
791 // ----------------------------------------------------------------------------
792
793 #ifdef __WATCOMC__
794 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
795
796 // This macro can be used to forward a 'vararg' template to another one with
797 // different fixed arguments types. Parameters are same as for
798 // WX_DEFINE_VARARG_FUNC (rettype=void can be used here), 'convfixed' is how
799 // to convert fixed arguments. For example, this is typical code for dealing
800 // with different forms of format string:
801 //
802 // WX_DEFINE_VARARG_FUNC_VOID(Printf, 1, (const wxFormatString&),
803 // DoPrintfWchar, DoPrintfUtf8)
804 // #ifdef __WATCOMC__
805 // WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const wxString&),
806 // (wxFormatString(f1)))
807 // WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const char*),
808 // (wxFormatString(f1)))
809 // ...
810 #define WX_VARARG_WATCOM_WORKAROUND(rettype, name, numfixed, fixed, convfixed)\
811 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
812 _WX_VARARG_WATCOM_WORKAROUND, \
813 rettype, name, convfixed, dummy, numfixed, fixed)
814
815 #define WX_VARARG_WATCOM_WORKAROUND_CTOR(name, numfixed, fixed, convfixed) \
816 _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
817 _WX_VARARG_WATCOM_WORKAROUND_CTOR, \
818 dummy, name, convfixed, dummy, numfixed, fixed)
819
820 #define _WX_VARARG_WATCOM_UNPACK_1(a1) a1
821 #define _WX_VARARG_WATCOM_UNPACK_2(a1, a2) a1, a2
822 #define _WX_VARARG_WATCOM_UNPACK_3(a1, a2, a3) a1, a2, a3
823 #define _WX_VARARG_WATCOM_UNPACK_4(a1, a2, a3, a4) a1, a2, a3, a4
824 #define _WX_VARARG_WATCOM_UNPACK(N, convfixed) \
825 _WX_VARARG_WATCOM_UNPACK_##N convfixed
826
827 #define _WX_VARARG_WATCOM_WORKAROUND(N, rettype, name, \
828 convfixed, dummy, numfixed, fixed) \
829 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
830 rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
831 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
832 { \
833 return name(_WX_VARARG_WATCOM_UNPACK(numfixed, convfixed), \
834 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WCHAR)); \
835 }
836
837 #define _WX_VARARG_WATCOM_WORKAROUND_CTOR(N, dummy1, name, \
838 convfixed, dummy2, numfixed, fixed) \
839 template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \
840 name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \
841 _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \
842 { \
843 name(_WX_VARARG_WATCOM_UNPACK(numfixed, convfixed), \
844 _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WCHAR)); \
845 }
846
847 #endif // __WATCOMC__
848
849 #endif // _WX_STRVARARG_H_