strvararg.h 51 KB

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