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