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