include wxUniCharRef::operator&& unconditionally too, not just wxUniChar one
[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__) && \
22 !defined(__DARWIN__) && !defined(__OS2__) && !defined(__DOS__)) || \
23 (defined(__VISUALC__) && defined(_NATIVE_WCHAR_T_DEFINED)) || \
24 wxCHECK_SUNCC_VERSION(5, 9)
25 #define wxWINT_T_IS_SEPARATE_TYPE
26 #endif
27
28 // helper macro for doing something dependent on whether wint_t is or isn't a
29 // typedef inside another macro
30 #ifdef wxWINT_T_IS_SEPARATE_TYPE
31 #define wxIF_WINT_T_TYPE(x) x
32 #else // !wxWINT_T_IS_SEPARATE_TYPE
33 #define wxIF_WINT_T_TYPE(x)
34 #endif // wxWINT_T_IS_SEPARATE_TYPE/!wxWINT_T_IS_SEPARATE_TYPE
35
36 // wchar_t seems to be defined as unsigned short by all Windows compilers but
37 // unsigned int everywhere else
38 #ifndef __WIN32__
39 #define wxWCHAR_T_IS_UINT
40 #endif
41
42 class WXDLLIMPEXP_BASE wxUniCharRef;
43 class WXDLLIMPEXP_BASE wxStringIteratorNode;
44
45 // This class represents single Unicode character. It can be converted to
46 // and from char or wchar_t and implements commonly used character operations.
47 class WXDLLIMPEXP_BASE wxUniChar
48 {
49 public:
50 // NB: this is not wchar_t on purpose, it needs to represent the entire
51 // Unicode code points range and wchar_t may be too small for that
52 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
53 typedef wxUint32 value_type;
54
55 wxUniChar() : m_value(0) {}
56
57 // Create the character from 8bit character value encoded in the current
58 // locale's charset.
59 wxUniChar(char c) { m_value = From8bit(c); }
60 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
61
62 // Create the character from a wchar_t character value.
63 wxUniChar(wchar_t c) { m_value = c; }
64 #ifdef wxWINT_T_IS_SEPARATE_TYPE
65 wxUniChar(wint_t c) { m_value = c; }
66 #endif
67
68 wxUniChar(int c) { m_value = c; }
69 wxUniChar(long int c) { m_value = c; }
70
71 wxUniChar(const wxUniCharRef& c);
72
73 // Returns Unicode code point value of the character
74 value_type GetValue() const { return m_value; }
75
76 #if wxUSE_UNICODE_UTF8
77 // buffer for single UTF-8 character
78 struct Utf8CharBuffer
79 {
80 char data[5];
81 operator const char*() const { return data; }
82 };
83
84 // returns the character encoded as UTF-8
85 // (NB: implemented in stringops.cpp)
86 Utf8CharBuffer AsUTF8() const;
87 #endif // wxUSE_UNICODE_UTF8
88
89 // Returns true if the character is an ASCII character:
90 bool IsAscii() const { return m_value < 0x80; }
91
92 // Conversions to char and wchar_t types: all of those are needed to be
93 // able to pass wxUniChars to verious standard narrow and wide character
94 // functions
95 operator char() const { return To8bit(m_value); }
96 operator wchar_t() const { return m_value; }
97 operator int() const { return m_value; }
98 operator long int() const { return m_value; }
99 #ifndef wxWCHAR_T_IS_UINT
100 operator unsigned int() const { return m_value; }
101 #endif
102
103 // More conversions needed for other standard functions: uchar is for VC++
104 // _mbxxx() ones (to which toxxx/isxxx() are mapped when _MBCS is defined)
105 // and some wide character functions take wint_t which happens to be the
106 // same as wchar_t for Windows compilers but not for g++ (except for the
107 // special Apple version)
108 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
109 #ifdef wxWINT_T_IS_SEPARATE_TYPE
110 operator wint_t() const { return m_value; }
111 #endif
112
113 // We need this operator for the "*p" part of expressions like "for (
114 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
115 // compilation would fail without it because the conversion to bool would
116 // be ambiguous (there are all these int types conversions...). (And adding
117 // operator unspecified_bool_type() would only makes the ambiguity worse.)
118 operator bool() const { return m_value != 0; }
119 bool operator!() const { return !((bool)*this); }
120
121 // And this one is needed by some (not all, but not using ifdefs makes the
122 // code easier) compilers to parse "str[0] && *p" successfully
123 bool operator&&(bool v) const { return (bool)*this && v; }
124
125 // Assignment operators:
126 wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; }
127 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
128 wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
129 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
130 wxUniChar& operator=(int c) { m_value = c; return *this; }
131 wxUniChar& operator=(long int c) { m_value = c; return *this; }
132 #ifdef wxWINT_T_IS_SEPARATE_TYPE
133 wxUniChar& operator=(wint_t c) { m_value = c; return *this; }
134 #endif
135
136 // Comparison operators:
137
138 // define the given comparison operator for all the types
139 #define wxDEFINE_UNICHAR_OPERATOR(op) \
140 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
141 bool operator op(char c) const { return m_value op From8bit(c); } \
142 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
143 bool operator op(wchar_t c) const { return m_value op (value_type)c; } \
144 bool operator op(int c) const { return m_value op (value_type)c; } \
145 bool operator op(long int c) const { return m_value op (value_type)c; } \
146 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return m_value op (value_type)c; } )
147
148 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
149
150 #undef wxDEFINE_UNICHAR_OPERATOR
151
152 // this is needed for expressions like 'Z'-c
153 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
154 int operator-(char c) const { return m_value - From8bit(c); }
155 int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
156 int operator-(wchar_t c) const { return m_value - (value_type)c; }
157 #ifdef wxWINT_T_IS_SEPARATE_TYPE
158 int operator-(wint_t c) const { return m_value - (value_type)c; }
159 #endif
160
161 private:
162 static value_type From8bit(char c);
163 static char To8bit(value_type c);
164
165 private:
166 value_type m_value;
167 };
168
169
170 // Writeable reference to a character in wxString.
171 //
172 // This class can be used in the same way wxChar is used, except that changing
173 // its value updates the underlying string object.
174 class WXDLLIMPEXP_BASE wxUniCharRef
175 {
176 private:
177 typedef wxStringImpl::iterator iterator;
178
179 // create the reference
180 #if wxUSE_UNICODE_UTF8
181 wxUniCharRef(wxStringIteratorNode& node, iterator pos) : m_node(node), m_pos(pos) {}
182 #else
183 wxUniCharRef(iterator pos) : m_pos(pos) {}
184 #endif
185
186 public:
187 // NB: we have to make this public, because we don't have wxString
188 // declaration available here and so can't declare wxString::iterator
189 // as friend; so at least don't use a ctor but a static function
190 // that must be used explicitly (this is more than using 'explicit'
191 // keyword on ctor!):
192 #if wxUSE_UNICODE_UTF8
193 static wxUniCharRef CreateForString(wxStringIteratorNode& node, iterator pos)
194 { return wxUniCharRef(node, pos); }
195 #else
196 static wxUniCharRef CreateForString(iterator pos)
197 { return wxUniCharRef(pos); }
198 #endif
199
200 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
201
202 #if wxUSE_UNICODE_UTF8
203 wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
204 #endif // wxUSE_UNICODE_UTF8
205
206 bool IsAscii() const { return UniChar().IsAscii(); }
207
208 // Assignment operators:
209 #if wxUSE_UNICODE_UTF8
210 wxUniCharRef& operator=(const wxUniChar& c);
211 #else
212 wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
213 #endif
214
215 wxUniCharRef& operator=(const wxUniCharRef& c)
216 { return *this = c.UniChar(); }
217
218 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
219 wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); }
220 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
221 wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
222 wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); }
223 #ifdef wxWINT_T_IS_SEPARATE_TYPE
224 wxUniCharRef& operator=(wint_t c) { return *this = wxUniChar(c); }
225 #endif
226
227 // Conversions to the same types as wxUniChar is convertible too:
228 operator char() const { return UniChar(); }
229 operator int() const { return UniChar(); }
230 operator long int() const { return UniChar(); }
231 operator unsigned char() const { return UniChar(); }
232 operator wchar_t() const { return UniChar(); }
233 #ifdef wxWINT_T_IS_SEPARATE_TYPE
234 operator wint_t() const { return UniChar(); }
235 #endif
236 #ifndef wxWCHAR_T_IS_UINT
237 operator unsigned int() const { return UniChar(); }
238 #endif
239
240 // see wxUniChar::operator bool etc. for explanation
241 operator bool() const { return (bool)UniChar(); }
242 bool operator!() const { return !UniChar(); }
243 bool operator&&(bool v) const { return UniChar() && v; }
244
245 // Comparison operators:
246 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
247 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
248 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
249 bool operator op(char c) const { return UniChar() op c; } \
250 bool operator op(unsigned char c) const { return UniChar() op c; } \
251 bool operator op(wchar_t c) const { return UniChar() op c; } \
252 bool operator op(int c) const { return UniChar() op c; } \
253 bool operator op(long int c) const { return UniChar() op c; } \
254 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return UniChar() op c; } )
255
256 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
257
258 #undef wxDEFINE_UNICHARREF_OPERATOR
259
260 // for expressions like c-'A':
261 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
262 int operator-(const wxUniChar& c) const { return UniChar() - c; }
263 int operator-(char c) const { return UniChar() - c; }
264 int operator-(unsigned char c) const { return UniChar() - c; }
265 int operator-(wchar_t c) const { return UniChar() - c; }
266 #ifdef wxWINT_T_IS_SEPARATE_TYPE
267 int operator-(wint_t c) const { return UniChar() - c; }
268 #endif
269
270 private:
271 #if wxUSE_UNICODE_UTF8
272 wxUniChar UniChar() const;
273 #else
274 wxUniChar UniChar() const { return *m_pos; }
275 #endif
276
277 friend class WXDLLIMPEXP_BASE wxUniChar;
278
279 private:
280 // reference to the string and pointer to the character in string
281 #if wxUSE_UNICODE_UTF8
282 wxStringIteratorNode& m_node;
283 #endif
284 iterator m_pos;
285 };
286
287 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
288 {
289 m_value = c.UniChar().m_value;
290 }
291
292 // Comparison operators for the case when wxUniChar(Ref) is the second operand
293 // implemented in terms of member comparison functions
294
295 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
296
297 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
298 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
299
300 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
301 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
302
303 #ifdef wxWINT_T_IS_SEPARATE_TYPE
304 wxDEFINE_COMPARISONS(wint_t, const wxUniChar&, wxCMP_REVERSE)
305 wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef&, wxCMP_REVERSE)
306 #endif
307
308 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
309
310 #undef wxCMP_REVERSE
311
312 // for expressions like c-'A':
313 inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
314 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
315 inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
316 #ifdef wxWINT_T_IS_SEPARATE_TYPE
317 inline int operator-(wint_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
318 #endif
319
320 #endif /* _WX_UNICHAR_H_ */