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