]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unichar.h
added overloads of operators for wint_t for VC when the code is compiled with /Zc...
[wxWidgets.git] / include / wx / unichar.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unichar.h
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
5 // Created: 2007-03-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNICHAR_H_
12 #define _WX_UNICHAR_H_
13
14 #include "wx/defs.h"
15 #include "wx/chartype.h"
16 #include "wx/stringimpl.h"
17
18 // wint_t is just a typedef for wchar_t for many old compilers but for modern
19 // ones it's a separate type and we must provide a conversion to it to allow
20 // passing wxUniChar[Ref] to functions taking wint_t such as iswalnum() &c
21 #if (defined(__GNUC__) && !defined(__DARWIN__)) || \
22 (defined(__VISUALC__) && defined(_NATIVE_WCHAR_T_DEFINED))
23 #define wxWINT_T_IS_SEPARATE_TYPE
24 #endif
25
26 // helper macro for doing something dependent on whether wint_t is or isn't a
27 // typedef inside another macro
28 #ifdef wxWINT_T_IS_SEPARATE_TYPE
29 #define wxIF_WINT_T_TYPE(x) x
30 #else // !wxWINT_T_IS_SEPARATE_TYPE
31 #define wxIF_WINT_T_TYPE(x)
32 #endif // wxWINT_T_IS_SEPARATE_TYPE/!wxWINT_T_IS_SEPARATE_TYPE
33
34 class WXDLLIMPEXP_BASE wxUniCharRef;
35
36 // This class represents single Unicode character. It can be converted to
37 // and from char or wchar_t and implements commonly used character operations.
38 class WXDLLIMPEXP_BASE wxUniChar
39 {
40 public:
41 // NB: this is not wchar_t on purpose, it needs to represent the entire
42 // Unicode code points range and wchar_t may be too small for that
43 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
44 typedef wxUint32 value_type;
45
46 wxUniChar() : m_value(0) {}
47
48 // Create the character from 8bit character value encoded in the current
49 // locale's charset.
50 wxUniChar(char c) { m_value = From8bit(c); }
51 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
52
53 // Create the character from a wchar_t character value.
54 wxUniChar(wchar_t c) { m_value = c; }
55 #ifdef wxWINT_T_IS_SEPARATE_TYPE
56 wxUniChar(wint_t c) { m_value = c; }
57 #endif
58
59 wxUniChar(int c) { m_value = c; }
60
61 wxUniChar(const wxUniCharRef& c);
62
63 // Returns Unicode code point value of the character
64 value_type GetValue() const { return m_value; }
65
66 // Conversions to char and wchar_t types: all of those are needed to be
67 // able to pass wxUniChars to verious standard narrow and wide character
68 // functions
69 operator char() const { return To8bit(m_value); }
70 operator wchar_t() const { return m_value; }
71 operator int() const { return m_value; }
72
73 // More conversions needed for other standard functions: uchar is for VC++
74 // _mbxxx() ones (to which toxxx/isxxx() are mapped when _MBCS is defined)
75 // and some wide character functions take wint_t which happens to be the
76 // same as wchar_t for Windows compilers but not for g++ (except for the
77 // special Apple version)
78 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
79 #ifdef wxWINT_T_IS_SEPARATE_TYPE
80 operator wint_t() const { return m_value; }
81 #endif
82
83 // We need this operator for the "*p" part of expressions like "for (
84 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
85 // compilation would fail without it because the conversion to bool would
86 // be ambiguous (there are all these int types conversions...). (And adding
87 // operator unspecified_bool_type() would only makes the ambiguity worse.)
88 operator bool() const { return m_value != 0; }
89 bool operator!() const { return !((bool)*this); }
90 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
91 defined(__DIGITALMARS__) || defined(__BORLANDC__)
92 // We need this for VC++ < 8 or DigitalMars and expressions like
93 // "str[0] && *p":
94 bool operator&&(bool v) const { return (bool)*this && v; }
95 #endif
96
97 // Assignment operators:
98 wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; }
99 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
100 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
101 #ifdef wxWINT_T_IS_SEPARATE_TYPE
102 wxUniChar& operator=(wint_t c) { m_value = c; return *this; }
103 #endif
104
105 // Comparison operators:
106
107 // define the given comparison operator for all the types
108 #define wxDEFINE_UNICHAR_OPERATOR(op) \
109 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
110 bool operator op(char c) const { return m_value op From8bit(c); } \
111 bool operator op(wchar_t c) const { return m_value op (value_type)c; } \
112 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return m_value op (value_type)c; } )
113
114 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
115
116 #undef wxDEFINE_UNICHAR_OPERATOR
117
118 // this is needed for expressions like 'Z'-c
119 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
120 int operator-(char c) const { return m_value - From8bit(c); }
121 int operator-(wchar_t c) const { return m_value - (value_type)c; }
122 #ifdef wxWINT_T_IS_SEPARATE_TYPE
123 int operator-(wint_t c) const { return m_value - (value_type)c; }
124 #endif
125
126 private:
127 static value_type From8bit(char c);
128 static char To8bit(value_type c);
129
130 private:
131 value_type m_value;
132 };
133
134
135 // Writeable reference to a character in wxString.
136 //
137 // This class can be used in the same way wxChar is used, except that changing
138 // its value updates the underlying string object.
139 class WXDLLIMPEXP_BASE wxUniCharRef
140 {
141 private:
142 typedef wxStringImpl::iterator iterator;
143
144 // create the reference
145 wxUniCharRef(iterator pos) : m_pos(pos) {}
146
147 public:
148 // NB: we have to make this public, because we don't have wxString
149 // declaration available here and so can't declare wxString::iterator
150 // as friend; so at least don't use a ctor but a static function
151 // that must be used explicitly (this is more than using 'explicit'
152 // keyword on ctor!):
153 static wxUniCharRef CreateForString(iterator pos)
154 { return wxUniCharRef(pos); }
155
156 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
157
158 // Assignment operators:
159 wxUniCharRef& operator=(const wxUniCharRef& c)
160 {
161 *m_pos = *c.m_pos;
162 return *this;
163 };
164
165 wxUniCharRef& operator=(const wxUniChar& c)
166 {
167 *m_pos = c;
168 return *this;
169 };
170
171 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
172 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
173 #ifdef wxWINT_T_IS_SEPARATE_TYPE
174 wxUniCharRef& operator=(wint_t c) { return *this = wxUniChar(c); }
175 #endif
176
177 // Conversions to the same types as wxUniChar is convertible too:
178 operator char() const { return UniChar(); }
179 operator int() const { return UniChar(); }
180 operator unsigned char() const { return UniChar(); }
181 operator wchar_t() const { return UniChar(); }
182 #ifdef wxWINT_T_IS_SEPARATE_TYPE
183 operator wint_t() const { return UniChar(); }
184 #endif
185
186 // see wxUniChar::operator bool etc. for explanation
187 operator bool() const { return (bool)UniChar(); }
188 bool operator!() const { return !UniChar(); }
189 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
190 defined(__DIGITALMARS__) || defined(__BORLANDC__)
191 bool operator&&(bool v) const { return UniChar() && v; }
192 #endif
193
194 // Comparison operators:
195 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
196 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
197 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
198 bool operator op(char c) const { return UniChar() op c; } \
199 bool operator op(wchar_t c) const { return UniChar() op c; } \
200 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return UniChar() op c; } )
201
202 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
203
204 #undef wxDEFINE_UNICHARREF_OPERATOR
205
206 // for expressions like c-'A':
207 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
208 int operator-(const wxUniChar& c) const { return UniChar() - c; }
209 int operator-(char c) const { return UniChar() - c; }
210 int operator-(wchar_t c) const { return UniChar() - c; }
211 #ifdef wxWINT_T_IS_SEPARATE_TYPE
212 int operator-(wint_t c) const { return UniChar() - c; }
213 #endif
214
215 private:
216 wxUniChar UniChar() const { return *m_pos; }
217 friend class WXDLLIMPEXP_BASE wxUniChar;
218
219 private:
220 // pointer to the character in string
221 iterator m_pos;
222 };
223
224 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
225 {
226 m_value = c.UniChar().m_value;
227 }
228
229 // Comparison operators for the case when wxUniChar(Ref) is the second operand
230 // implemented in terms of member comparison functions
231
232 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
233
234 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
235 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
236
237 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
238 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
239
240 #ifdef wxWINT_T_IS_SEPARATE_TYPE
241 wxDEFINE_COMPARISONS(wint_t, const wxUniChar&, wxCMP_REVERSE)
242 wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef&, wxCMP_REVERSE)
243 #endif
244
245 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
246
247 #undef wxCMP_REVERSE
248
249 // for expressions like c-'A':
250 inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
251 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
252 inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
253 #ifdef wxWINT_T_IS_SEPARATE_TYPE
254 inline int operator-(wint_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
255 #endif
256
257 #endif /* _WX_UNICHAR_H_ */