+
+// ----------------------------------------------------------------------------
+// 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!
+#ifdef __VISUALC__
+ // "struct 'wx[W]CharBuffer<T>' needs to have dll-interface to be used by
+ // clients of class 'wxString'" - this is private, we don't care
+ #pragma warning (disable:4251)
+#endif
+class WXDLLIMPEXP_BASE wxFormatString
+{
+public:
+ wxFormatString(const char *str)
+ : m_char(wxCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
+ wxFormatString(const wchar_t *str)
+ : m_wchar(wxWCharBuffer::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 wxCharBuffer& str)
+ : m_char(str), m_str(NULL), m_cstr(NULL) {}
+ 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
+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;
+#ifdef __VISUALC__
+ #pragma warning (default:4251)
+#endif
+
+ // 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_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>
+{
+ static wxFormatStringArgument find(const wxFormatString& arg)
+ { return wxFormatStringArgument(&arg); }
+};
+
+