X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4960f9fc9aab2b2205197813c041ce0681c4df9b..6dfbea27b9d581dbb6c033e2b336d6035f5e4fc8:/include/wx/strvararg.h diff --git a/include/wx/strvararg.h b/include/wx/strvararg.h index 05460711d7..4cbe39b3aa 100644 --- a/include/wx/strvararg.h +++ b/include/wx/strvararg.h @@ -18,12 +18,12 @@ #include "wx/cpp.h" #include "wx/chartype.h" -#include "wx/wxcrt.h" #include "wx/strconv.h" #include "wx/buffer.h" +#include "wx/unichar.h" -class WXDLLIMPEXP_BASE wxCStrData; -class WXDLLIMPEXP_BASE wxString; +class WXDLLIMPEXP_FWD_BASE wxCStrData; +class WXDLLIMPEXP_FWD_BASE wxString; // ---------------------------------------------------------------------------- // WX_DEFINE_VARARG_FUNC* macros @@ -47,6 +47,11 @@ class WXDLLIMPEXP_BASE wxString; // * wchar_t* if wxUSE_UNICODE_WCHAR or if wxUSE_UNICODE_UTF8 and the current // locale is not UTF-8 // +// Note that wxFormatString *must* be used for the format parameter of these +// functions, otherwise the implementation won't work correctly. Furthermore, +// it must be passed by value, not reference, because it's modified by the +// vararg templates internally. +// // Parameters: // [ there are examples in square brackets showing values of the parameters // for the wxFprintf() wrapper for fprintf() function with the following @@ -111,11 +116,12 @@ class WXDLLIMPEXP_BASE wxString; // wxFormatString // ---------------------------------------------------------------------------- -// This class should be used for format string argument of the functions +// This class must be used for format string argument of the functions // defined using WX_DEFINE_VARARG_FUNC_* macros. It converts the string to // char* or wchar_t* for passing to implementation function efficiently (i.e. // without keeping the converted string in memory for longer than necessary, -// like c_str()) +// like c_str()). It also converts format string to the correct form that +// accounts for string changes done by wxArgNormalizer<> // // Note that this class can _only_ be used for function arguments! class WXDLLIMPEXP_BASE wxFormatString @@ -134,11 +140,31 @@ public: wxFormatString(const wxWCharBuffer& str) : m_wchar(str), m_str(NULL), m_cstr(NULL) {} + + enum ArgumentType + { + Arg_Char, // character as char + + Arg_Other // something else, for example int for %d + }; + + // returns the type of format specifier for n-th variadic argument (this is + // not necessarily n-th format specifier if positional specifiers are used); + // called by wxArgNormalizer<> specializations to get information about + // n-th variadic argument desired representation + ArgumentType GetArgumentType(unsigned n) const; + #if !wxUSE_UNICODE_WCHAR operator const char*() const { return wx_const_cast(wxFormatString*, this)->AsChar(); } private: + // InputAsChar() returns the value converted passed to ctor, only converted + // to char, while AsChar() takes the the string returned by InputAsChar() + // and does format string conversion on it as well (and similarly for + // ..AsWChar() below) + const char* InputAsChar(); const char* AsChar(); + wxCharBuffer m_convertedChar; #endif // !wxUSE_UNICODE_WCHAR #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY @@ -146,21 +172,67 @@ public: operator const wchar_t*() const { return wx_const_cast(wxFormatString*, this)->AsWChar(); } private: + const wchar_t* InputAsWChar(); const wchar_t* AsWChar(); + wxWCharBuffer m_convertedWChar; #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY private: wxCharBuffer m_char; wxWCharBuffer m_wchar; + // NB: we can use a pointer here, because wxFormatString is only used // as function argument, so it has shorter life than the string // passed to the ctor const wxString * const m_str; const wxCStrData * const m_cstr; - DECLARE_NO_COPY_CLASS(wxFormatString) + DECLARE_NO_ASSIGN_CLASS(wxFormatString) +}; + +// these two helper classes are used to find wxFormatString argument among fixed +// arguments passed to a vararg template +struct wxFormatStringArgument +{ + wxFormatStringArgument(const wxFormatString *s = NULL) : m_str(s) {} + const wxFormatString *m_str; + + // overriding this operator allows us to reuse _WX_VARARG_JOIN macro + wxFormatStringArgument operator,(const wxFormatStringArgument& a) const + { + wxASSERT_MSG( m_str == NULL || a.m_str == NULL, + "can't have two format strings in vararg function" ); + return wxFormatStringArgument(m_str ? m_str : a.m_str); + } + + operator const wxFormatString*() const { return m_str; } +}; + +template +struct wxFormatStringArgumentFinder +{ + static wxFormatStringArgument find(T) + { + // by default, arguments are not format strings, so return "not found" + return wxFormatStringArgument(); + } +}; + +template<> +struct wxFormatStringArgumentFinder +{ + static wxFormatStringArgument find(const wxFormatString& arg) + { return wxFormatStringArgument(&arg); } }; +template<> +struct wxFormatStringArgumentFinder +{ + static wxFormatStringArgument find(const wxFormatString& arg) + { return wxFormatStringArgument(&arg); } +}; + + // ---------------------------------------------------------------------------- // wxArgNormalizer* converters // ---------------------------------------------------------------------------- @@ -171,7 +243,13 @@ private: template struct wxArgNormalizer { - wxArgNormalizer(const T& value) : m_value(value) {} + // Ctor. 'value' is the value passed as variadic argument, 'fmt' is pointer + // to printf-like format string or NULL if the variadic function doesn't + // use format string and 'index' is index of 'value' in variadic arguments + // list (starting at 1) + wxArgNormalizer(T value, + const wxFormatString *WXUNUSED(fmt), unsigned WXUNUSED(index)) + : m_value(value) {} // Returns the value in a form that can be safely passed to real vararg // functions. In case of strings, this is char* in ANSI build and wchar_t* @@ -188,7 +266,9 @@ struct wxArgNormalizer template struct wxArgNormalizerWchar : public wxArgNormalizer { - wxArgNormalizerWchar(const T& value) : wxArgNormalizer(value) {} + wxArgNormalizerWchar(T value, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizer(value, fmt, index) {} }; #endif // !wxUSE_UTF8_LOCALE_ONLY @@ -198,7 +278,9 @@ struct wxArgNormalizerWchar : public wxArgNormalizer template struct wxArgNormalizerUtf8 : public wxArgNormalizer { - wxArgNormalizerUtf8(const T& value) : wxArgNormalizer(value) {} + wxArgNormalizerUtf8(T value, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizer(value, fmt, index) {} }; #define wxArgNormalizerNative wxArgNormalizerUtf8 @@ -220,7 +302,10 @@ struct wxArgNormalizerWithBuffer typedef wxCharTypeBuffer CharBuffer; wxArgNormalizerWithBuffer() {} - wxArgNormalizerWithBuffer(const CharBuffer& buf) : m_value(buf) {} + wxArgNormalizerWithBuffer(const CharBuffer& buf, + const wxFormatString *WXUNUSED(fmt), + unsigned WXUNUSED(index)) + : m_value(buf) {} const CharType *get() const { return m_value; } @@ -231,7 +316,11 @@ struct wxArgNormalizerWithBuffer template<> struct WXDLLIMPEXP_BASE wxArgNormalizerNative { - wxArgNormalizerNative(const wxString& s) : m_value(s) {} + wxArgNormalizerNative(const wxString& s, + const wxFormatString *WXUNUSED(fmt), + unsigned WXUNUSED(index)) + : m_value(s) {} + const wxStringCharType *get() const; const wxString& m_value; @@ -241,7 +330,11 @@ struct WXDLLIMPEXP_BASE wxArgNormalizerNative template<> struct WXDLLIMPEXP_BASE wxArgNormalizerNative { - wxArgNormalizerNative(const wxCStrData& value) : m_value(value) {} + wxArgNormalizerNative(const wxCStrData& value, + const wxFormatString *WXUNUSED(fmt), + unsigned WXUNUSED(index)) + : m_value(value) {} + const wxStringCharType *get() const; const wxCStrData& m_value; @@ -253,14 +346,16 @@ template<> struct WXDLLIMPEXP_BASE wxArgNormalizerWchar : public wxArgNormalizerWithBuffer { - wxArgNormalizerWchar(const wxString& s); + wxArgNormalizerWchar(const wxString& s, + const wxFormatString *fmt, unsigned index); }; template<> struct WXDLLIMPEXP_BASE wxArgNormalizerWchar : public wxArgNormalizerWithBuffer { - wxArgNormalizerWchar(const wxCStrData& s); + wxArgNormalizerWchar(const wxCStrData& s, + const wxFormatString *fmt, unsigned index); }; #endif // wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY @@ -273,8 +368,9 @@ template<> struct wxArgNormalizerWchar : public wxArgNormalizerWithBuffer { - wxArgNormalizerWchar(const char* s) - : wxArgNormalizerWithBuffer(wxConvLibc.cMB2WC(s)) {} + wxArgNormalizerWchar(const char* s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerWithBuffer(wxConvLibc.cMB2WC(s), fmt, index) {} }; #elif wxUSE_UNICODE_UTF8 @@ -283,15 +379,18 @@ template<> struct wxArgNormalizerUtf8 : public wxArgNormalizerWithBuffer { - wxArgNormalizerUtf8(const wchar_t* s) - : wxArgNormalizerWithBuffer(wxConvUTF8.cWC2MB(s)) {} + wxArgNormalizerUtf8(const wchar_t* s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerWithBuffer(wxConvUTF8.cWC2MB(s), fmt, index) {} }; template<> struct wxArgNormalizerUtf8 : public wxArgNormalizerWithBuffer { - wxArgNormalizerUtf8(const char* s) + wxArgNormalizerUtf8(const char* s, + const wxFormatString *WXUNUSED(fmt), + unsigned WXUNUSED(index)) { if ( wxLocaleIsUtf8 ) { @@ -315,8 +414,9 @@ template<> struct wxArgNormalizerWchar : public wxArgNormalizerWithBuffer { - wxArgNormalizerWchar(const char* s) - : wxArgNormalizerWithBuffer(wxConvLibc.cMB2WC(s)) {} + wxArgNormalizerWchar(const char* s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerWithBuffer(wxConvLibc.cMB2WC(s), fmt, index) {} }; #endif // !wxUSE_UTF8_LOCALE_ONLY @@ -326,8 +426,9 @@ template<> struct wxArgNormalizerWchar : public wxArgNormalizerWithBuffer { - wxArgNormalizerWchar(const wchar_t* s) - : wxArgNormalizerWithBuffer(wxConvLibc.cWC2MB(s)) {} + wxArgNormalizerWchar(const wchar_t* s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerWithBuffer(wxConvLibc.cWC2MB(s), fmt, index) {} }; #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8/ANSI @@ -355,7 +456,9 @@ struct wxArgNormalizerWchar template<> \ struct Normalizer : public Normalizer \ { \ - Normalizer(BaseT value) : Normalizer(value) {} \ + Normalizer(BaseT value, \ + const wxFormatString *fmt, unsigned index) \ + : Normalizer(value, fmt, index) {} \ } // non-reference versions of specializations for string objects @@ -382,16 +485,18 @@ template<> struct wxArgNormalizerWchar : public wxArgNormalizerWchar { - wxArgNormalizerWchar(const std::string& s) - : wxArgNormalizerWchar(s.c_str()) {} + wxArgNormalizerWchar(const std::string& s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerWchar(s.c_str(), fmt, index) {} }; template<> struct wxArgNormalizerWchar : public wxArgNormalizerWchar { - wxArgNormalizerWchar(const wxStdWideString& s) - : wxArgNormalizerWchar(s.c_str()) {} + wxArgNormalizerWchar(const wxStdWideString& s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerWchar(s.c_str(), fmt, index) {} }; #endif // !wxUSE_UTF8_LOCALE_ONLY @@ -400,16 +505,18 @@ template<> struct wxArgNormalizerUtf8 : public wxArgNormalizerUtf8 { - wxArgNormalizerUtf8(const std::string& s) - : wxArgNormalizerUtf8(s.c_str()) {} + wxArgNormalizerUtf8(const std::string& s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerUtf8(s.c_str(), fmt, index) {} }; template<> struct wxArgNormalizerUtf8 : public wxArgNormalizerUtf8 { - wxArgNormalizerUtf8(const wxStdWideString& s) - : wxArgNormalizerUtf8(s.c_str()) {} + wxArgNormalizerUtf8(const wxStdWideString& s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerUtf8(s.c_str(), fmt, index) {} }; #endif // wxUSE_UNICODE_UTF8 @@ -419,6 +526,72 @@ WX_ARG_NORMALIZER_FORWARD(wxStdWideString, const wxStdWideString&); #endif // wxUSE_STD_STRING +// versions for wxUniChar, wxUniCharRef: +// (this is same for UTF-8 and Wchar builds, we just convert to wchar_t) +template<> +struct wxArgNormalizer : public wxArgNormalizer +{ + wxArgNormalizer(const wxUniChar& s, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizer(s.GetValue(), fmt, index) {} +}; + +// for wchar_t, default handler does the right thing + +// char has to be treated differently in Unicode builds: a char argument may +// be used either for a character value (which should be converted into +// wxUniChar) or as an integer value (which should be left as-is). We take +// advantage of the fact that both char and wchar_t are converted into int +// in variadic arguments here. +#if wxUSE_UNICODE +template +struct wxArgNormalizerNarrowChar +{ + wxArgNormalizerNarrowChar(T value, + const wxFormatString *fmt, unsigned index) + { + // FIXME-UTF8: which one is better default in absence of fmt string + // (i.e. when used like e.g. Foo("foo", "bar", 'c', NULL)? + if ( !fmt || fmt->GetArgumentType(index) == wxFormatString::Arg_Char ) + m_value = wxUniChar(value).GetValue(); + else + m_value = value; + } + + int get() const { return m_value; } + + T m_value; +}; + +template<> +struct wxArgNormalizer : public wxArgNormalizerNarrowChar +{ + wxArgNormalizer(char value, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerNarrowChar(value, fmt, index) {} +}; + +template<> +struct wxArgNormalizer + : public wxArgNormalizerNarrowChar +{ + wxArgNormalizer(unsigned char value, + const wxFormatString *fmt, unsigned index) + : wxArgNormalizerNarrowChar(value, fmt, index) {} +}; + +#endif // wxUSE_UNICODE + +// convert references: +WX_ARG_NORMALIZER_FORWARD(wxUniChar, const wxUniChar&); +WX_ARG_NORMALIZER_FORWARD(const wxUniCharRef&, const wxUniChar&); +WX_ARG_NORMALIZER_FORWARD(wxUniCharRef, const wxUniChar&); +WX_ARG_NORMALIZER_FORWARD(const wchar_t&, wchar_t); + +WX_ARG_NORMALIZER_FORWARD(const char&, char); +WX_ARG_NORMALIZER_FORWARD(const unsigned char&, unsigned char); + + #undef WX_ARG_NORMALIZER_FORWARD #undef _WX_ARG_NORMALIZER_FORWARD_IMPL @@ -545,6 +718,14 @@ private: #define _WX_VARARG_FIXED_UNUSED_EXPAND_4(t1,t2,t3,t4) \ t1 WXUNUSED(f1), t2 WXUNUSED(f2), t3 WXUNUSED(f3), t4 WXUNUSED(f4) +#define _WX_VARARG_FIXED_TYPEDEFS_1(t1) \ + typedef t1 TF1 +#define _WX_VARARG_FIXED_TYPEDEFS_2(t1,t2) \ + _WX_VARARG_FIXED_TYPEDEFS_1(t1); typedef t2 TF2 +#define _WX_VARARG_FIXED_TYPEDEFS_3(t1,t2,t3) \ + _WX_VARARG_FIXED_TYPEDEFS_2(t1,t2); typedef t3 TF3 +#define _WX_VARARG_FIXED_TYPEDEFS_4(t1,t2,t3,t4) \ + _WX_VARARG_FIXED_TYPEDEFS_3(t1,t2,t3); typedef t4 TF4 // This macro expands N-items tuple of fixed arguments types into part of // function's declaration. For example, @@ -561,6 +742,13 @@ private: #define _WX_VARARG_FIXED_UNUSED_EXPAND_IMPL(N, args) \ _WX_VARARG_FIXED_UNUSED_EXPAND_##N args +// Declarates typedefs for fixed arguments types; i-th fixed argument types +// will have TFi typedef. +#define _WX_VARARG_FIXED_TYPEDEFS(N, args) \ + _WX_VARARG_FIXED_TYPEDEFS_IMPL(N, args) +#define _WX_VARARG_FIXED_TYPEDEFS_IMPL(N, args) \ + _WX_VARARG_FIXED_TYPEDEFS_##N args + // This macro calls another macro 'm' passed as second argument 'N' times, // with its only argument set to 1..N, and concatenates the results using @@ -607,20 +795,23 @@ private: // Generates code snippet for passing i-th argument of vararg function // wrapper to its implementation, normalizing it in the process: -#define _WX_VARARG_PASS_WCHAR(i) wxArgNormalizerWchar(a##i).get() -#define _WX_VARARG_PASS_UTF8(i) wxArgNormalizerUtf8(a##i).get() +#define _WX_VARARG_PASS_WCHAR(i) \ + wxArgNormalizerWchar(a##i, fmt, i).get() +#define _WX_VARARG_PASS_UTF8(i) \ + wxArgNormalizerUtf8(a##i, fmt, i).get() // And the same for fixed arguments, _not_ normalizing it: -// FIXME-UTF8: this works, but uses wxString's implicit conversion to wxChar* -// for the 'format' argument (which is const wxString&) _if_ the -// implementation function has C sting argument; we need to -// have wxFixedArgNormalizer here that will pass everything -// as-is except for wxString (for which wx_str() would be used), -// but OTOH, we don't want to do that if the implementation takes -// wxString argument #define _WX_VARARG_PASS_FIXED(i) f##i +#define _WX_VARARG_FIND_FMT(i) \ + (wxFormatStringArgumentFinder::find(f##i)) + +#define _WX_VARARG_FORMAT_STRING(numfixed, fixed) \ + _WX_VARARG_FIXED_TYPEDEFS(numfixed, fixed); \ + const wxFormatString *fmt = \ + (_WX_VARARG_JOIN(numfixed, _WX_VARARG_FIND_FMT)) + #if wxUSE_UNICODE_UTF8 #define _WX_VARARG_DO_CALL_UTF8(return_kw, impl, implUtf8, N, numfixed) \ return_kw implUtf8(_WX_VARARG_JOIN(numfixed, _WX_VARARG_PASS_FIXED), \ @@ -666,6 +857,7 @@ private: rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \ _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \ { \ + _WX_VARARG_FORMAT_STRING(numfixed, fixed); \ _WX_VARARG_DO_CALL(return, impl, implUtf8, N, numfixed); \ } @@ -686,6 +878,7 @@ private: void name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \ _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \ { \ + _WX_VARARG_FORMAT_STRING(numfixed, fixed); \ _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \ impl, implUtf8, N, numfixed); \ } @@ -707,6 +900,7 @@ private: name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \ _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \ { \ + _WX_VARARG_FORMAT_STRING(numfixed, fixed); \ _WX_VARARG_DO_CALL(wxEMPTY_PARAMETER_VALUE, \ impl, implUtf8, N, numfixed); \ } @@ -732,4 +926,67 @@ private: inline void name(_WX_VARARG_FIXED_UNUSED_EXPAND(numfixed, fixed)) \ {} + +// ---------------------------------------------------------------------------- +// workaround for OpenWatcom bug #351 +// ---------------------------------------------------------------------------- + +#ifdef __WATCOMC__ +// workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 + +// This macro can be used to forward a 'vararg' template to another one with +// different fixed arguments types. Parameters are same as for +// WX_DEFINE_VARARG_FUNC (rettype=void can be used here), 'convfixed' is how +// to convert fixed arguments. For example, this is typical code for dealing +// with different forms of format string: +// +// WX_DEFINE_VARARG_FUNC_VOID(Printf, 1, (const wxFormatString&), +// DoPrintfWchar, DoPrintfUtf8) +// #ifdef __WATCOMC__ +// WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const wxString&), +// (wxFormatString(f1))) +// WX_VARARG_WATCOM_WORKAROUND(void, Printf, 1, (const char*), +// (wxFormatString(f1))) +// ... +#define WX_VARARG_WATCOM_WORKAROUND(rettype, name, numfixed, fixed, convfixed)\ + _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \ + _WX_VARARG_WATCOM_WORKAROUND, \ + rettype, name, convfixed, dummy, numfixed, fixed) + +#define WX_VARARG_WATCOM_WORKAROUND_CTOR(name, numfixed, fixed, convfixed) \ + _WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \ + _WX_VARARG_WATCOM_WORKAROUND_CTOR, \ + dummy, name, convfixed, dummy, numfixed, fixed) + +#define _WX_VARARG_WATCOM_UNPACK_1(a1) a1 +#define _WX_VARARG_WATCOM_UNPACK_2(a1, a2) a1, a2 +#define _WX_VARARG_WATCOM_UNPACK_3(a1, a2, a3) a1, a2, a3 +#define _WX_VARARG_WATCOM_UNPACK_4(a1, a2, a3, a4) a1, a2, a3, a4 +#define _WX_VARARG_WATCOM_UNPACK(N, convfixed) \ + _WX_VARARG_WATCOM_UNPACK_##N convfixed + +#define _WX_VARARG_PASS_WATCOM(i) a##i + +#define _WX_VARARG_WATCOM_WORKAROUND(N, rettype, name, \ + convfixed, dummy, numfixed, fixed) \ + template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \ + rettype name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \ + _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \ + { \ + return name(_WX_VARARG_WATCOM_UNPACK(numfixed, convfixed), \ + _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WATCOM)); \ + } + +#define _WX_VARARG_WATCOM_WORKAROUND_CTOR(N, dummy1, name, \ + convfixed, dummy2, numfixed, fixed) \ + template<_WX_VARARG_JOIN(N, _WX_VARARG_TEMPL)> \ + name(_WX_VARARG_FIXED_EXPAND(numfixed, fixed), \ + _WX_VARARG_JOIN(N, _WX_VARARG_ARG)) \ + { \ + name(_WX_VARARG_WATCOM_UNPACK(numfixed, convfixed), \ + _WX_VARARG_JOIN(N, _WX_VARARG_PASS_WATCOM)); \ + } + +#endif // __WATCOMC__ + #endif // _WX_STRVARARG_H_