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