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