fixed passing of strings to vararg templates in ANSI build
[wxWidgets.git] / src / common / strvararg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/strvararg.cpp
3 // Purpose: macros for implementing type-safe vararg passing of strings
4 // Author: Vaclav Slavik
5 // Created: 2007-02-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/strvararg.h"
27 #include "wx/string.h"
28
29 // ============================================================================
30 // implementation
31 // ============================================================================
32
33 // ----------------------------------------------------------------------------
34 // wxArgNormalizer<>
35 // ----------------------------------------------------------------------------
36
37 const wxStringCharType *wxArgNormalizerNative<const wxString&>::get() const
38 {
39 return m_value.wx_str();
40 }
41
42 const wxStringCharType *wxArgNormalizerNative<const wxCStrData&>::get() const
43 {
44 return m_value.AsInternal();
45 }
46
47 #if wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
48 wxArgNormalizerWchar<const wxString&>::wxArgNormalizerWchar(const wxString& s)
49 : wxArgNormalizerWithBuffer<wchar_t>(s.wc_str())
50 {
51 }
52
53 wxArgNormalizerWchar<const wxCStrData&>::wxArgNormalizerWchar(const wxCStrData& s)
54 : wxArgNormalizerWithBuffer<wchar_t>(s.AsWCharBuf())
55 {
56 }
57 #endif // wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
58
59 // ----------------------------------------------------------------------------
60 // wxArgNormalizedString
61 // ----------------------------------------------------------------------------
62
63 wxString wxArgNormalizedString::GetString() const
64 {
65 if ( !IsValid() )
66 return wxEmptyString;
67
68 #if wxUSE_UTF8_LOCALE_ONLY
69 return wxString(wx_reinterpret_cast(const char*, m_ptr));
70 #else
71 #if wxUSE_UNICODE_UTF8
72 if ( wxLocaleIsUtf8 )
73 return wxString(wx_reinterpret_cast(const char*, m_ptr));
74 else
75 #endif
76 return wxString(wx_reinterpret_cast(const wxChar*, m_ptr));
77 #endif // !wxUSE_UTF8_LOCALE_ONLY
78 }
79
80 wxArgNormalizedString::operator wxString() const
81 {
82 return GetString();
83 }
84
85 // ----------------------------------------------------------------------------
86 // wxFormatString
87 // ----------------------------------------------------------------------------
88
89 #if !wxUSE_UNICODE_WCHAR
90 const char* wxFormatString::AsChar()
91 {
92 if ( m_char )
93 return m_char.data();
94
95 // in ANSI build, wx_str() returns char*, in UTF-8 build, this function
96 // is only called under UTF-8 locales, so we should return UTF-8 string,
97 // which is, again, what wx_str() returns:
98 if ( m_str )
99 return m_str->wx_str();
100
101 // ditto wxCStrData:
102 if ( m_cstr )
103 return m_cstr->AsInternal();
104
105 // the last case is that wide string was passed in: in that case, we need
106 // to convert it:
107 wxASSERT( m_wchar );
108
109 m_char = wxConvLibc.cWC2MB(m_wchar.data());
110
111 return m_char.data();
112 }
113 #endif // !wxUSE_UNICODE_WCHAR
114
115 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
116 const wchar_t* wxFormatString::AsWChar()
117 {
118 if ( m_wchar )
119 return m_wchar.data();
120
121 #if wxUSE_UNICODE_WCHAR
122 if ( m_str )
123 return m_str->wc_str();
124 if ( m_cstr )
125 return m_cstr->AsInternal();
126 #else // wxUSE_UNICODE_UTF8
127 if ( m_str )
128 {
129 m_wchar = m_str->wc_str();
130 return m_wchar.data();
131 }
132 if ( m_cstr )
133 {
134 m_wchar = m_cstr->AsWCharBuf();
135 return m_wchar.data();
136 }
137 #endif // wxUSE_UNICODE_WCHAR/UTF8
138
139 // the last case is that narrow string was passed in: in that case, we need
140 // to convert it:
141 wxASSERT( m_char );
142
143 m_wchar = wxConvLibc.cMB2WC(m_char.data());
144
145 return m_wchar.data();
146 }
147 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY