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