#include "wx/cpp.h"
#include "wx/chartype.h"
-#include "wx/wxcrt.h"
#include "wx/strconv.h"
#include "wx/buffer.h"
+#include "wx/unichar.h"
+
+#if defined(HAVE_TYPE_TRAITS)
+ #include <type_traits>
+#elif defined(HAVE_TR1_TYPE_TRAITS)
+ #ifdef __VISUALC__
+ #include <type_traits>
+ #else
+ #include <tr1/type_traits>
+ #endif
+#endif
-class WXDLLIMPEXP_BASE wxCStrData;
-class WXDLLIMPEXP_BASE wxString;
+class WXDLLIMPEXP_FWD_BASE wxCStrData;
+class WXDLLIMPEXP_FWD_BASE wxString;
// ----------------------------------------------------------------------------
// WX_DEFINE_VARARG_FUNC* macros
// * 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
// if wxUSE_UNICODE_UTF8 and running under UTF-8 locale
// (ignored otherwise) [fprintf]
//
-#define WX_DEFINE_VARARG_FUNC2(rettype, name, numfixed, fixed, impl, implUtf8)\
+#define WX_DEFINE_VARARG_FUNC(rettype, name, numfixed, fixed, impl, implUtf8) \
_WX_VARARG_DEFINE_FUNC_N0(rettype, name, impl, implUtf8, numfixed, fixed) \
- WX_DEFINE_VARARG_FUNC2_SANS_N0(rettype, name, numfixed, fixed, impl, implUtf8)
+ WX_DEFINE_VARARG_FUNC_SANS_N0(rettype, name, numfixed, fixed, impl, implUtf8)
// ditto, but without the version with 0 template/vararg arguments
-#define WX_DEFINE_VARARG_FUNC2_SANS_N0(rettype, name, \
+#define WX_DEFINE_VARARG_FUNC_SANS_N0(rettype, name, \
numfixed, fixed, impl, implUtf8) \
_WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
_WX_VARARG_DEFINE_FUNC, \
rettype, name, impl, implUtf8, numfixed, fixed)
-// like WX_DEFINE_VARARG_FUNC2, but for impl=implUtf8:
-#define WX_DEFINE_VARARG_FUNC(rettype, name, numfixed, fixed, impl) \
- WX_DEFINE_VARARG_FUNC2(rettype, name, numfixed, fixed, impl, impl)
-
-// Like WX_DEFINE_VARARG_FUNC2, but for variadic functions that don't return
+// Like WX_DEFINE_VARARG_FUNC, but for variadic functions that don't return
// a value.
-#define WX_DEFINE_VARARG_FUNC_VOID2(name, numfixed, fixed, impl, implUtf8) \
+#define WX_DEFINE_VARARG_FUNC_VOID(name, numfixed, fixed, impl, implUtf8) \
_WX_VARARG_DEFINE_FUNC_VOID_N0(name, impl, implUtf8, numfixed, fixed) \
_WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
_WX_VARARG_DEFINE_FUNC_VOID, \
void, name, impl, implUtf8, numfixed, fixed)
-// like WX_DEFINE_VARARG_FUNC_VOID2, but for impl=implUtf8:
-#define WX_DEFINE_VARARG_FUNC_VOID(name, numfixed, fixed, impl) \
- WX_DEFINE_VARARG_FUNC_VOID2(name, numfixed, fixed, impl, impl)
-
// Like WX_DEFINE_VARARG_FUNC_VOID, but instead of wrapping an implementation
// function, does nothing in defined functions' bodies.
//
void, name, dummy, dummy, numfixed, fixed)
// Like WX_DEFINE_VARARG_FUNC_CTOR, but for defining template constructors
-#define WX_DEFINE_VARARG_FUNC_CTOR(name, numfixed, fixed, impl) \
- _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, impl, numfixed, fixed) \
+#define WX_DEFINE_VARARG_FUNC_CTOR(name, numfixed, fixed, impl, implUtf8) \
+ _WX_VARARG_DEFINE_FUNC_CTOR_N0(name, impl, implUtf8, numfixed, fixed) \
_WX_VARARG_ITER(_WX_VARARG_MAX_ARGS, \
_WX_VARARG_DEFINE_FUNC_CTOR, \
- void, name, impl, impl, numfixed, fixed)
+ void, name, impl, implUtf8, numfixed, fixed)
+
+
+// ----------------------------------------------------------------------------
+// wxFormatString
+// ----------------------------------------------------------------------------
+
+// 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()). 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
+{
+public:
+ wxFormatString(const char *str)
+ : m_char(wxScopedCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
+ wxFormatString(const wchar_t *str)
+ : m_wchar(wxScopedWCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
+ wxFormatString(const wxString& str)
+ : m_str(&str), m_cstr(NULL) {}
+ wxFormatString(const wxCStrData& str)
+ : m_str(NULL), m_cstr(&str) {}
+ wxFormatString(const wxScopedCharBuffer& str)
+ : m_char(str), m_str(NULL), m_cstr(NULL) {}
+ wxFormatString(const wxScopedWCharBuffer& str)
+ : m_wchar(str), m_str(NULL), m_cstr(NULL) {}
+
+ // Possible argument types. These are or-combinable for wxASSERT_ARG_TYPE
+ // convenience. Some of the values are or-combined with another value, this
+ // expresses "supertypes" for use with wxASSERT_ARG_TYPE masks. For example,
+ // a char* string is also a pointer and an integer is also a char.
+ enum ArgumentType
+ {
+ Arg_Char = 0x0001, // character as char %c
+ Arg_Pointer = 0x0002, // %p
+ Arg_String = 0x0004 | Arg_Pointer, // any form of string (%s and %p too)
+
+ Arg_Int = 0x0008 | Arg_Char, // (ints can be used with %c)
+#if SIZEOF_INT == SIZEOF_LONG
+ Arg_LongInt = Arg_Int,
+#else
+ Arg_LongInt = 0x0010,
+#endif
+#if defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG == SIZEOF_LONG
+ Arg_LongLongInt = Arg_LongInt,
+#elif defined(wxLongLong_t)
+ Arg_LongLongInt = 0x0020,
+#endif
+
+ Arg_Double = 0x0040,
+ Arg_LongDouble = 0x0080,
+
+#if defined(wxSIZE_T_IS_UINT)
+ Arg_Size_t = Arg_Int,
+#elif defined(wxSIZE_T_IS_ULONG)
+ Arg_Size_t = Arg_LongInt,
+#elif defined(SIZEOF_LONG_LONG) && SIZEOF_SIZE_T == SIZEOF_LONG_LONG
+ Arg_Size_t = Arg_LongLongInt,
+#else
+ Arg_Size_t = 0x0100,
+#endif
+
+ Arg_IntPtr = 0x0200, // %n -- store # of chars written
+ Arg_ShortIntPtr = 0x0400,
+ Arg_LongIntPtr = 0x0800,
+
+ Arg_Unknown = 0x8000 // unrecognized specifier (likely error)
+ };
+
+ // 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;
+
+ // returns the value passed to ctor, only converted to wxString, similarly
+ // to other InputAsXXX() methods
+ wxString InputAsString() const;
+
+#if !wxUSE_UNICODE_WCHAR
+ operator const char*() const
+ { return const_cast<wxFormatString*>(this)->AsChar(); }
+private:
+ // InputAsChar() returns the value passed to ctor, only converted
+ // to char, while AsChar() takes 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();
+ wxScopedCharBuffer m_convertedChar;
+#endif // !wxUSE_UNICODE_WCHAR
+
+#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
+public:
+ operator const wchar_t*() const
+ { return const_cast<wxFormatString*>(this)->AsWChar(); }
+private:
+ const wchar_t* InputAsWChar();
+ const wchar_t* AsWChar();
+ wxScopedWCharBuffer m_convertedWChar;
+#endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
+
+private:
+ wxScopedCharBuffer m_char;
+ wxScopedWCharBuffer 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;
+
+ wxDECLARE_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<typename T>
+struct wxFormatStringArgumentFinder
+{
+ static wxFormatStringArgument find(T)
+ {
+ // by default, arguments are not format strings, so return "not found"
+ return wxFormatStringArgument();
+ }
+};
+
+template<>
+struct wxFormatStringArgumentFinder<const wxFormatString&>
+{
+ static wxFormatStringArgument find(const wxFormatString& arg)
+ { return wxFormatStringArgument(&arg); }
+};
+
+template<>
+struct wxFormatStringArgumentFinder<wxFormatString>
+ : public wxFormatStringArgumentFinder<const wxFormatString&> {};
+
+// avoid passing big objects by value to wxFormatStringArgumentFinder::find()
+// (and especially wx[W]CharBuffer with its auto_ptr<> style semantics!):
+template<>
+struct wxFormatStringArgumentFinder<wxString>
+ : public wxFormatStringArgumentFinder<const wxString&> {};
+
+template<>
+struct wxFormatStringArgumentFinder<wxScopedCharBuffer>
+ : public wxFormatStringArgumentFinder<const wxScopedCharBuffer&> {};
+
+template<>
+struct wxFormatStringArgumentFinder<wxScopedWCharBuffer>
+ : public wxFormatStringArgumentFinder<const wxScopedWCharBuffer&> {};
+
+template<>
+struct wxFormatStringArgumentFinder<wxCharBuffer>
+ : public wxFormatStringArgumentFinder<const wxCharBuffer&> {};
+
+template<>
+struct wxFormatStringArgumentFinder<wxWCharBuffer>
+ : public wxFormatStringArgumentFinder<const wxWCharBuffer&> {};
+
// ----------------------------------------------------------------------------
// wxArgNormalizer*<T> converters
// ----------------------------------------------------------------------------
+#if wxDEBUG_LEVEL
+ // Check that the format specifier for index-th argument in 'fmt' has
+ // the correct type (one of wxFormatString::Arg_XXX or-combination in
+ // 'expected_mask').
+ #define wxASSERT_ARG_TYPE(fmt, index, expected_mask) \
+ do \
+ { \
+ if ( !fmt ) \
+ break; \
+ const int argtype = fmt->GetArgumentType(index); \
+ wxASSERT_MSG( (argtype & (expected_mask)) == argtype, \
+ "format specifier doesn't match argument type" ); \
+ } while ( wxFalse )
+#else
+ // Just define it to suppress "unused parameter" warnings for the
+ // parameters which we don't use otherwise
+ #define wxASSERT_ARG_TYPE(fmt, index, expected_mask) \
+ wxUnusedVar(fmt); \
+ wxUnusedVar(index)
+#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
+
+
+#if defined(HAVE_TYPE_TRAITS) || defined(HAVE_TR1_TYPE_TRAITS)
+
+// Note: this type is misnamed, so that the error message is easier to
+// understand (no error happens for enums, because the IsEnum=true case is
+// specialized).
+template<bool IsEnum>
+struct wxFormatStringSpecifierNonPodType {};
+
+template<>
+struct wxFormatStringSpecifierNonPodType<true>
+{
+ enum { value = wxFormatString::Arg_Int };
+};
+
+template<typename T>
+struct wxFormatStringSpecifier
+{
+#ifdef HAVE_TYPE_TRAITS
+ typedef std::is_enum<T> is_enum;
+#elif defined HAVE_TR1_TYPE_TRAITS
+ typedef std::tr1::is_enum<T> is_enum;
+#endif
+ enum { value = wxFormatStringSpecifierNonPodType<is_enum::value>::value };
+};
+
+#else // !HAVE_(TR1_)TYPE_TRAITS
+
+template<typename T>
+struct wxFormatStringSpecifier
+{
+ // We can't detect enums without is_enum, so the only thing we can
+ // do is to accept unknown types. However, the only acceptable unknown
+ // types still are enums, which are promoted to ints, so return Arg_Int
+ // here. This will at least catch passing of non-POD types through ... at
+ // runtime.
+ //
+ // Furthermore, if the compiler doesn't have partial template
+ // specialization, we didn't cover pointers either.
+#ifdef HAVE_PARTIAL_SPECIALIZATION
+ enum { value = wxFormatString::Arg_Int };
+#else
+ enum { value = wxFormatString::Arg_Int | wxFormatString::Arg_Pointer };
+#endif
+};
+
+#endif // HAVE_TR1_TYPE_TRAITS/!HAVE_TR1_TYPE_TRAITS
+
+
+#ifdef HAVE_PARTIAL_SPECIALIZATION
+template<typename T>
+struct wxFormatStringSpecifier<T*>
+{
+ enum { value = wxFormatString::Arg_Pointer };
+};
+
+template<typename T>
+struct wxFormatStringSpecifier<const T*>
+{
+ enum { value = wxFormatString::Arg_Pointer };
+};
+#endif // !HAVE_PARTIAL_SPECIALIZATION
+
+
+#define wxFORMAT_STRING_SPECIFIER(T, arg) \
+ template<> struct wxFormatStringSpecifier<T> \
+ { \
+ enum { value = arg }; \
+ };
+
+wxFORMAT_STRING_SPECIFIER(bool, wxFormatString::Arg_Int)
+wxFORMAT_STRING_SPECIFIER(int, wxFormatString::Arg_Int)
+wxFORMAT_STRING_SPECIFIER(unsigned int, wxFormatString::Arg_Int)
+wxFORMAT_STRING_SPECIFIER(short int, wxFormatString::Arg_Int)
+wxFORMAT_STRING_SPECIFIER(short unsigned int, wxFormatString::Arg_Int)
+wxFORMAT_STRING_SPECIFIER(long int, wxFormatString::Arg_LongInt)
+wxFORMAT_STRING_SPECIFIER(long unsigned int, wxFormatString::Arg_LongInt)
+#ifdef wxLongLong_t
+wxFORMAT_STRING_SPECIFIER(wxLongLong_t, wxFormatString::Arg_LongLongInt)
+wxFORMAT_STRING_SPECIFIER(wxULongLong_t, wxFormatString::Arg_LongLongInt)
+#endif
+wxFORMAT_STRING_SPECIFIER(float, wxFormatString::Arg_Double)
+wxFORMAT_STRING_SPECIFIER(double, wxFormatString::Arg_Double)
+wxFORMAT_STRING_SPECIFIER(long double, wxFormatString::Arg_LongDouble)
+
+#if wxWCHAR_T_IS_REAL_TYPE
+wxFORMAT_STRING_SPECIFIER(wchar_t, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
+#endif
+
+#if !wxUSE_UNICODE
+wxFORMAT_STRING_SPECIFIER(char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
+wxFORMAT_STRING_SPECIFIER(signed char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
+wxFORMAT_STRING_SPECIFIER(unsigned char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
+#endif
+
+wxFORMAT_STRING_SPECIFIER(char*, wxFormatString::Arg_String)
+wxFORMAT_STRING_SPECIFIER(unsigned char*, wxFormatString::Arg_String)
+wxFORMAT_STRING_SPECIFIER(signed char*, wxFormatString::Arg_String)
+wxFORMAT_STRING_SPECIFIER(const char*, wxFormatString::Arg_String)
+wxFORMAT_STRING_SPECIFIER(const unsigned char*, wxFormatString::Arg_String)
+wxFORMAT_STRING_SPECIFIER(const signed char*, wxFormatString::Arg_String)
+wxFORMAT_STRING_SPECIFIER(wchar_t*, wxFormatString::Arg_String)
+wxFORMAT_STRING_SPECIFIER(const wchar_t*, wxFormatString::Arg_String)
+
+wxFORMAT_STRING_SPECIFIER(int*, wxFormatString::Arg_IntPtr | wxFormatString::Arg_Pointer)
+wxFORMAT_STRING_SPECIFIER(short int*, wxFormatString::Arg_ShortIntPtr | wxFormatString::Arg_Pointer)
+wxFORMAT_STRING_SPECIFIER(long int*, wxFormatString::Arg_LongIntPtr | wxFormatString::Arg_Pointer)
+
+#undef wxFORMAT_STRING_SPECIFIER
+
+
// Converts an argument passed to wxPrint etc. into standard form expected,
// by wxXXX functions, e.g. all strings (wxString, char*, wchar_t*) are
// converted into wchar_t* or char* depending on the build.
template<typename T>
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 *fmt, unsigned index)
+ : m_value(value)
+ {
+ wxASSERT_ARG_TYPE( fmt, index, wxFormatStringSpecifier<T>::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*
// in Unicode build.
- const T& get() const { return m_value; }
+ T get() const { return m_value; }
- const T& m_value;
+ T m_value;
};
// normalizer for passing arguments to functions working with wchar_t* (and
template<typename T>
struct wxArgNormalizerWchar : public wxArgNormalizer<T>
{
- wxArgNormalizerWchar(const T& value) : wxArgNormalizer<T>(value) {}
+ wxArgNormalizerWchar(T value,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizer<T>(value, fmt, index) {}
};
#endif // !wxUSE_UTF8_LOCALE_ONLY
template<typename T>
struct wxArgNormalizerUtf8 : public wxArgNormalizer<T>
{
- wxArgNormalizerUtf8(const T& value) : wxArgNormalizer<T>(value) {}
+ wxArgNormalizerUtf8(T value,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizer<T>(value, fmt, index) {}
};
#define wxArgNormalizerNative wxArgNormalizerUtf8
template<typename CharType>
struct wxArgNormalizerWithBuffer
{
- typedef wxCharTypeBuffer<CharType> CharBuffer;
+ typedef wxScopedCharTypeBuffer<CharType> CharBuffer;
wxArgNormalizerWithBuffer() {}
- wxArgNormalizerWithBuffer(const CharBuffer& buf) : m_value(buf) {}
+ wxArgNormalizerWithBuffer(const CharBuffer& buf,
+ const wxFormatString *fmt,
+ unsigned index)
+ : m_value(buf)
+ {
+ wxASSERT_ARG_TYPE( fmt, index, wxFormatString::Arg_String );
+ }
const CharType *get() const { return m_value; }
template<>
struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxString&>
{
- wxArgNormalizerNative(const wxString& s) : m_value(s) {}
+ wxArgNormalizerNative(const wxString& s,
+ const wxFormatString *fmt,
+ unsigned index)
+ : m_value(s)
+ {
+ wxASSERT_ARG_TYPE( fmt, index, wxFormatString::Arg_String );
+ }
+
const wxStringCharType *get() const;
const wxString& m_value;
template<>
struct WXDLLIMPEXP_BASE wxArgNormalizerNative<const wxCStrData&>
{
- wxArgNormalizerNative(const wxCStrData& value) : m_value(value) {}
+ wxArgNormalizerNative(const wxCStrData& value,
+ const wxFormatString *fmt,
+ unsigned index)
+ : m_value(value)
+ {
+ wxASSERT_ARG_TYPE( fmt, index, wxFormatString::Arg_String );
+ }
+
const wxStringCharType *get() const;
const wxCStrData& m_value;
struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxString&>
: public wxArgNormalizerWithBuffer<wchar_t>
{
- wxArgNormalizerWchar(const wxString& s);
+ wxArgNormalizerWchar(const wxString& s,
+ const wxFormatString *fmt, unsigned index);
};
template<>
struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxCStrData&>
: public wxArgNormalizerWithBuffer<wchar_t>
{
- wxArgNormalizerWchar(const wxCStrData& s);
+ wxArgNormalizerWchar(const wxCStrData& s,
+ const wxFormatString *fmt, unsigned index);
};
#endif // wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
struct wxArgNormalizerWchar<const char*>
: public wxArgNormalizerWithBuffer<wchar_t>
{
- wxArgNormalizerWchar(const char* s)
- : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s)) {}
+ wxArgNormalizerWchar(const char* s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s), fmt, index) {}
};
#elif wxUSE_UNICODE_UTF8
struct wxArgNormalizerUtf8<const wchar_t*>
: public wxArgNormalizerWithBuffer<char>
{
- wxArgNormalizerUtf8(const wchar_t* s)
- : wxArgNormalizerWithBuffer<char>(wxConvUTF8.cWC2MB(s)) {}
+ wxArgNormalizerUtf8(const wchar_t* s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerWithBuffer<char>(wxConvUTF8.cWC2MB(s), fmt, index) {}
};
template<>
struct wxArgNormalizerUtf8<const char*>
: public wxArgNormalizerWithBuffer<char>
{
- wxArgNormalizerUtf8(const char* s)
+ wxArgNormalizerUtf8(const char* s,
+ const wxFormatString *fmt,
+ unsigned index)
{
- // FIXME-UTF8: optimize this if current locale is UTF-8 one
-
- // convert to widechar string first:
- wxWCharBuffer buf(wxConvLibc.cMB2WC(s));
-
- // then to UTF-8:
- if ( buf )
- m_value = wxConvUTF8.cWC2MB(buf);
+ wxASSERT_ARG_TYPE( fmt, index, wxFormatString::Arg_String );
+
+ if ( wxLocaleIsUtf8 )
+ {
+ m_value = wxScopedCharBuffer::CreateNonOwned(s);
+ }
+ else
+ {
+ // convert to widechar string first:
+ wxScopedWCharBuffer buf(wxConvLibc.cMB2WC(s));
+
+ // then to UTF-8:
+ if ( buf )
+ m_value = wxConvUTF8.cWC2MB(buf);
+ }
}
};
struct wxArgNormalizerWchar<const char*>
: public wxArgNormalizerWithBuffer<wchar_t>
{
- wxArgNormalizerWchar(const char* s)
- : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s)) {}
+ wxArgNormalizerWchar(const char* s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s), fmt, index) {}
};
#endif // !wxUSE_UTF8_LOCALE_ONLY
struct wxArgNormalizerWchar<const wchar_t*>
: public wxArgNormalizerWithBuffer<char>
{
- wxArgNormalizerWchar(const wchar_t* s)
- : wxArgNormalizerWithBuffer<char>(wxConvLibc.cWC2MB(s)) {}
+ wxArgNormalizerWchar(const wchar_t* s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerWithBuffer<char>(wxConvLibc.cWC2MB(s), fmt, index) {}
};
#endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8/ANSI
template<> \
struct Normalizer<T> : public Normalizer<BaseT> \
{ \
- Normalizer(BaseT value) : Normalizer<BaseT>(value) {} \
+ Normalizer(BaseT value, \
+ const wxFormatString *fmt, unsigned index) \
+ : Normalizer<BaseT>(value, fmt, index) {} \
}
// non-reference versions of specializations for string objects
WX_ARG_NORMALIZER_FORWARD(wchar_t*, const wchar_t*);
// versions for passing wx[W]CharBuffer:
+WX_ARG_NORMALIZER_FORWARD(wxScopedCharBuffer, const char*);
+WX_ARG_NORMALIZER_FORWARD(const wxScopedCharBuffer&, const char*);
+WX_ARG_NORMALIZER_FORWARD(wxScopedWCharBuffer, const wchar_t*);
+WX_ARG_NORMALIZER_FORWARD(const wxScopedWCharBuffer&, const wchar_t*);
WX_ARG_NORMALIZER_FORWARD(wxCharBuffer, const char*);
WX_ARG_NORMALIZER_FORWARD(const wxCharBuffer&, const char*);
WX_ARG_NORMALIZER_FORWARD(wxWCharBuffer, const wchar_t*);
struct wxArgNormalizerWchar<const std::string&>
: public wxArgNormalizerWchar<const char*>
{
- wxArgNormalizerWchar(const std::string& s)
- : wxArgNormalizerWchar<const char*>(s.c_str()) {}
+ wxArgNormalizerWchar(const std::string& s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerWchar<const char*>(s.c_str(), fmt, index) {}
};
template<>
struct wxArgNormalizerWchar<const wxStdWideString&>
: public wxArgNormalizerWchar<const wchar_t*>
{
- wxArgNormalizerWchar(const wxStdWideString& s)
- : wxArgNormalizerWchar<const wchar_t*>(s.c_str()) {}
+ wxArgNormalizerWchar(const wxStdWideString& s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerWchar<const wchar_t*>(s.c_str(), fmt, index) {}
};
#endif // !wxUSE_UTF8_LOCALE_ONLY
struct wxArgNormalizerUtf8<const std::string&>
: public wxArgNormalizerUtf8<const char*>
{
- wxArgNormalizerUtf8(const std::string& s)
- : wxArgNormalizerUtf8<const char*>(s.c_str()) {}
+ wxArgNormalizerUtf8(const std::string& s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerUtf8<const char*>(s.c_str(), fmt, index) {}
};
template<>
struct wxArgNormalizerUtf8<const wxStdWideString&>
: public wxArgNormalizerUtf8<const wchar_t*>
{
- wxArgNormalizerUtf8(const wxStdWideString& s)
- : wxArgNormalizerUtf8<const wchar_t*>(s.c_str()) {}
+ wxArgNormalizerUtf8(const wxStdWideString& s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerUtf8<const wchar_t*>(s.c_str(), fmt, index) {}
};
#endif // wxUSE_UNICODE_UTF8
#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<const wxUniChar&> : public wxArgNormalizer<wchar_t>
+{
+ wxArgNormalizer(const wxUniChar& s,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizer<wchar_t>(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<typename T>
+struct wxArgNormalizerNarrowChar
+{
+ wxArgNormalizerNarrowChar(T value,
+ const wxFormatString *fmt, unsigned index)
+ {
+ wxASSERT_ARG_TYPE( fmt, index,
+ wxFormatString::Arg_Char | wxFormatString::Arg_Int );
+
+ // 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 = wx_truncate_cast(T, wxUniChar(value).GetValue());
+ else
+ m_value = value;
+ }
+
+ int get() const { return m_value; }
+
+ T m_value;
+};
+
+template<>
+struct wxArgNormalizer<char> : public wxArgNormalizerNarrowChar<char>
+{
+ wxArgNormalizer(char value,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerNarrowChar<char>(value, fmt, index) {}
+};
+
+template<>
+struct wxArgNormalizer<unsigned char>
+ : public wxArgNormalizerNarrowChar<unsigned char>
+{
+ wxArgNormalizer(unsigned char value,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerNarrowChar<unsigned char>(value, fmt, index) {}
+};
+
+template<>
+struct wxArgNormalizer<signed char>
+ : public wxArgNormalizerNarrowChar<signed char>
+{
+ wxArgNormalizer(signed char value,
+ const wxFormatString *fmt, unsigned index)
+ : wxArgNormalizerNarrowChar<signed char>(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);
+WX_ARG_NORMALIZER_FORWARD(const signed char&, signed char);
+
+
#undef WX_ARG_NORMALIZER_FORWARD
#undef _WX_ARG_NORMALIZER_FORWARD_IMPL
+// NB: Don't #undef wxASSERT_ARG_TYPE here as it's also used in wx/longlong.h.
+
// ----------------------------------------------------------------------------
// WX_VA_ARG_STRING
// ----------------------------------------------------------------------------
#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,
#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
#define _WX_VARARG_ARG(i) T##i a##i
// Like _WX_VARARG_ARG_UNUSED, but outputs argument's type with WXUNUSED:
-#define _WX_VARARG_ARG_UNUSED(i) T##i WXUNUSED(a##i)
+#define _WX_VARARG_ARG_UNUSED(i) T##i WXUNUSED(a##i)
// Generates code snippet for i-th type in vararg function's template<...>:
#define _WX_VARARG_TEMPL(i) typename T##i
// 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<T##i>(a##i).get()
-#define _WX_VARARG_PASS_UTF8(i) wxArgNormalizerUtf8<T##i>(a##i).get()
+#define _WX_VARARG_PASS_WCHAR(i) \
+ wxArgNormalizerWchar<T##i>(a##i, fmt, i).get()
+#define _WX_VARARG_PASS_UTF8(i) \
+ wxArgNormalizerUtf8<T##i>(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<T> 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<TF##i>::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), \
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); \
}
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); \
}
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); \
}
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_