]> git.saurik.com Git - wxWidgets.git/blame - include/wx/unichar.h
fixed compilation if wxUSE_STL=1
[wxWidgets.git] / include / wx / unichar.h
CommitLineData
e3f6cbd9
VS
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
dbecee02 14#include "wx/defs.h"
e3f6cbd9
VS
15#include "wx/chartype.h"
16
17class WXDLLIMPEXP_BASE wxUniCharRef;
18
19// This class represents single Unicode character. It can be converted to
20// and from char or wchar_t and implements commonly used character operations.
21class WXDLLIMPEXP_BASE wxUniChar
22{
23public:
24 // NB: this is not wchar_t on purpose, it needs to represent the entire
25 // Unicode code points range and wchar_t may be too small for that
26 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
dbecee02 27 typedef wxUint32 value_type;
e3f6cbd9
VS
28
29 wxUniChar() : m_value(0) {}
30
31 // Create the character from 8bit character value encoded in the current
32 // locale's charset.
33 wxUniChar(char c) { m_value = From8bit(c); }
34
35 // Create the character from a wchar_t character value.
36 wxUniChar(wchar_t c) { m_value = c; }
37
38#ifndef wxWINT_T_IS_TYPEDEF
39 // Create the character from a wint_t character value.
40 wxUniChar(wint_t c) { m_value = c; }
41#endif
42
43 wxUniChar(int c) { m_value = c; }
44
45 wxUniChar(const wxUniCharRef& c);
46
47 // Returns Unicode code point value of the character
d1b7ed67 48 value_type GetValue() const { return m_value; }
e3f6cbd9
VS
49
50 // Casts to char and wchar_t types:
51 operator char() const { return To8bit(m_value); }
52 operator wchar_t() const { return m_value; }
53#ifndef wxWINT_T_IS_TYPEDEF
54 operator wint_t() const { return m_value; }
55#endif
56 operator int() const { return m_value; }
57
58 // We need this operator for the "*p" part of expressions like "for (
59 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
60 // compilation would fail without it because the conversion to bool would
61 // be ambiguous (there are all these int types conversions...). (And adding
62 // operator unspecified_bool_type() would only makes the ambiguity worse.)
63 operator bool() const { return m_value != 0; }
64 bool operator!() const { return !((bool)*this); }
65#if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
66 defined(__DIGITALMARS__) || defined(__BORLANDC__)
67 // We need this for VC++ < 8 or DigitalMars and expressions like
68 // "str[0] && *p":
69 bool operator&&(bool v) const { return (bool)*this && v; }
70#endif
71
72 // Assignment operators:
73 wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; }
74 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
75 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
76#ifndef wxWINT_T_IS_TYPEDEF
77 wxUniChar& operator=(wint_t c) { m_value = c; return *this; }
78#endif
79
80 // Comparision operators:
81 bool operator==(const wxUniChar& c) const { return m_value == c.m_value; }
82 bool operator==(char c) const { return m_value == From8bit(c); }
d1b7ed67 83 bool operator==(wchar_t c) const { return m_value == (value_type)c; }
e3f6cbd9 84#ifndef wxWINT_T_IS_TYPEDEF
d1b7ed67 85 bool operator==(wint_t c) const { return m_value == (value_type)c; }
e3f6cbd9
VS
86#endif
87
88 bool operator!=(const wxUniChar& c) const { return m_value != c.m_value; }
89 bool operator!=(char c) const { return m_value != From8bit(c); }
d1b7ed67 90 bool operator!=(wchar_t c) const { return m_value != (value_type)c; }
e3f6cbd9 91#ifndef wxWINT_T_IS_TYPEDEF
d1b7ed67 92 bool operator!=(wint_t c) const { return m_value != (value_type)c; }
e3f6cbd9
VS
93#endif
94
95 bool operator>(const wxUniChar& c) const { return m_value > c.m_value; }
d1b7ed67
VS
96 bool operator>(char c) const { return m_value > (value_type)c; }
97 bool operator>(wchar_t c) const { return m_value > (value_type)c; }
e3f6cbd9 98#ifndef wxWINT_T_IS_TYPEDEF
d1b7ed67 99 bool operator>(wint_t c) const { return m_value > (value_type)c; }
e3f6cbd9
VS
100#endif
101
102 bool operator<(const wxUniChar& c) const { return m_value < c.m_value; }
103 bool operator<(char c) const { return m_value < From8bit(c); }
d1b7ed67 104 bool operator<(wchar_t c) const { return m_value < (value_type)c; }
e3f6cbd9 105#ifndef wxWINT_T_IS_TYPEDEF
d1b7ed67 106 bool operator<(wint_t c) const { return m_value < (value_type)c; }
e3f6cbd9
VS
107#endif
108
109 bool operator>=(const wxUniChar& c) const { return m_value >= c.m_value; }
110 bool operator>=(char c) const { return m_value >= From8bit(c); }
d1b7ed67 111 bool operator>=(wchar_t c) const { return m_value >= (value_type)c; }
e3f6cbd9 112#ifndef wxWINT_T_IS_TYPEDEF
d1b7ed67 113 bool operator>=(wint_t c) const { return m_value >= (value_type)c; }
e3f6cbd9
VS
114#endif
115
116 bool operator<=(const wxUniChar& c) const { return m_value <= c.m_value; }
117 bool operator<=(char c) const { return m_value <= From8bit(c); }
d1b7ed67 118 bool operator<=(wchar_t c) const { return m_value <= (value_type)c; }
e3f6cbd9 119#ifndef wxWINT_T_IS_TYPEDEF
d1b7ed67 120 bool operator<=(wint_t c) const { return m_value <= (value_type)c; }
e3f6cbd9
VS
121#endif
122
123 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
124 int operator-(char c) const { return m_value - From8bit(c); }
d1b7ed67 125 int operator-(wchar_t c) const { return m_value - (value_type)c; }
e3f6cbd9 126#ifndef wxWINT_T_IS_TYPEDEF
d1b7ed67 127 int operator-(wint_t c) const { return m_value - (value_type)c; }
e3f6cbd9
VS
128#endif
129
130private:
d1b7ed67
VS
131 static value_type From8bit(char c);
132 static char To8bit(value_type c);
e3f6cbd9
VS
133
134private:
d1b7ed67 135 value_type m_value;
e3f6cbd9
VS
136};
137
138
139// Writeable reference to a character in wxString.
140//
141// This class can be used in the same way wxChar is used, except that changing
142// its value updates the underlying string object.
143class WXDLLIMPEXP_BASE wxUniCharRef
144{
145private:
146 // create the reference
147 // FIXME-UTF8: the interface will need changes for UTF-8 build
148 wxUniCharRef(wxChar *pos) : m_pos(pos) {}
149
150public:
151 // NB: we have to make this public, because we don't have wxString
152 // declaration available here and so can't declare wxString::iterator
153 // as friend; so at least don't use a ctor but a static function
154 // that must be used explicitly (this is more than using 'explicit'
155 // keyword on ctor!):
156 //
157 // FIXME-UTF8: the interface will need changes for UTF-8 build
158 static wxUniCharRef CreateForString(wxChar *pos)
159 { return wxUniCharRef(pos); }
160
d1b7ed67 161 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
e3f6cbd9
VS
162
163 // Assignment operators:
164 wxUniCharRef& operator=(const wxUniCharRef& c)
165 {
166 *m_pos = *c.m_pos;
167 return *this;
168 };
169
170 wxUniCharRef& operator=(const wxUniChar& c)
171 {
172 *m_pos = c;
173 return *this;
174 };
175
176 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
177 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
178
179 // Casts to wxUniChar type:
180 operator char() const { return UniChar(); }
181 operator wchar_t() const { return UniChar(); }
182#ifndef wxWINT_T_IS_TYPEDEF
183 operator wint_t() const { return UniChar(); }
184#endif
185 operator int() const { return UniChar(); }
186
187 // see wxUniChar::operator bool etc. for explanation
188 operator bool() const { return (bool)UniChar(); }
189 bool operator!() const { return !UniChar(); }
190#if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
191 defined(__DIGITALMARS__) || defined(__BORLANDC__)
192 bool operator&&(bool v) const { return UniChar() && v; }
193#endif
194
195 // Comparision operators:
196 bool operator==(const wxUniCharRef& c) const { return m_pos == c.m_pos; }
197 bool operator==(const wxUniChar& c) const { return UniChar() == c; }
198 bool operator==(char c) const { return UniChar() == c; }
199 bool operator==(wchar_t c) const { return UniChar() == c; }
200#ifndef wxWINT_T_IS_TYPEDEF
201 bool operator==(wint_t c) const { return UniChar() == c; }
202#endif
203
204 bool operator!=(const wxUniCharRef& c) const { return m_pos != c.m_pos; }
205 bool operator!=(const wxUniChar& c) const { return UniChar() != c; }
206 bool operator!=(char c) const { return UniChar() != c; }
207 bool operator!=(wchar_t c) const { return UniChar() != c; }
208#ifndef wxWINT_T_IS_TYPEDEF
209 bool operator!=(wint_t c) const { return UniChar() != c; }
210#endif
211
212 bool operator>(const wxUniCharRef& c) const { return UniChar() > c.UniChar(); }
213 bool operator>(const wxUniChar& c) const { return UniChar() > c; }
214 bool operator>(char c) const { return UniChar() > c; }
215 bool operator>(wchar_t c) const { return UniChar() > c; }
216#ifndef wxWINT_T_IS_TYPEDEF
217 bool operator>(wint_t c) const { return UniChar() > c; }
218#endif
219
220 bool operator<(const wxUniCharRef& c) const { return UniChar() < c.UniChar(); }
221 bool operator<(const wxUniChar& c) const { return UniChar() < c; }
222 bool operator<(char c) const { return UniChar() < c; }
223 bool operator<(wchar_t c) const { return UniChar() < c; }
224#ifndef wxWINT_T_IS_TYPEDEF
225 bool operator<(wint_t c) const { return UniChar() < c; }
226#endif
227
228 bool operator>=(const wxUniCharRef& c) const { return UniChar() >= c.UniChar(); }
229 bool operator>=(const wxUniChar& c) const { return UniChar() >= c; }
230 bool operator>=(char c) const { return UniChar() >= c; }
231 bool operator>=(wchar_t c) const { return UniChar() >= c; }
232#ifndef wxWINT_T_IS_TYPEDEF
233 bool operator>=(wint_t c) const { return UniChar() >= c; }
234#endif
235
236 bool operator<=(const wxUniCharRef& c) const { return UniChar() <= c.UniChar(); }
237 bool operator<=(const wxUniChar& c) const { return UniChar() <= c; }
238 bool operator<=(char c) const { return UniChar() <= c; }
239 bool operator<=(wchar_t c) const { return UniChar() <= c; }
240#ifndef wxWINT_T_IS_TYPEDEF
241 bool operator<=(wint_t c) const { return UniChar() <= c; }
242#endif
243
244 // for expressions like c-'A':
245 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
246 int operator-(const wxUniChar& c) const { return UniChar() - c; }
247 int operator-(char c) const { return UniChar() - c; }
248 int operator-(wchar_t c) const { return UniChar() - c; }
249#ifndef wxWINT_T_IS_TYPEDEF
250 int operator-(wint_t c) const { return UniChar() - c; }
251#endif
252
253private:
254 wxUniChar UniChar() const { return *m_pos; }
255 friend class WXDLLIMPEXP_BASE wxUniChar;
256
257private:
258 // pointer to the character in string
259 wxChar *m_pos;
260};
261
262inline wxUniChar::wxUniChar(const wxUniCharRef& c)
263{
264 m_value = c.UniChar().m_value;
265}
266
267// Comparision operators for the case when wxUniChar(Ref) is the second operand:
268inline bool operator==(char c1, const wxUniChar& c2) { return c2 == c1; }
269inline bool operator==(wchar_t c1, const wxUniChar& c2) { return c2 == c1; }
270#ifndef wxWINT_T_IS_TYPEDEF
271inline bool operator==(wint_t c1, const wxUniChar& c2) { return c2 == c1; }
272#endif
273
274inline bool operator!=(char c1, const wxUniChar& c2) { return c2 != c1; }
275inline bool operator!=(wchar_t c1, const wxUniChar& c2) { return c2 != c1; }
276#ifndef wxWINT_T_IS_TYPEDEF
277inline bool operator!=(wint_t c1, const wxUniChar& c2) { return c2 != c1; }
278#endif
279
280inline bool operator>(char c1, const wxUniChar& c2) { return c2 < c1; }
281inline bool operator>(wchar_t c1, const wxUniChar& c2) { return c2 < c1; }
282#ifndef wxWINT_T_IS_TYPEDEF
283inline bool operator>(wint_t c1, const wxUniChar& c2) { return c2 < c1; }
284#endif
285
286inline bool operator<(char c1, const wxUniChar& c2) { return c2 > c1; }
287inline bool operator<(wchar_t c1, const wxUniChar& c2) { return c2 > c1; }
288#ifndef wxWINT_T_IS_TYPEDEF
289inline bool operator<(wint_t c1, const wxUniChar& c2) { return c2 > c1; }
290#endif
291
292inline bool operator>=(char c1, const wxUniChar& c2) { return c2 <= c1; }
293inline bool operator>=(wchar_t c1, const wxUniChar& c2) { return c2 <= c1; }
294#ifndef wxWINT_T_IS_TYPEDEF
295inline bool operator>=(wint_t c1, const wxUniChar& c2) { return c2 <= c1; }
296#endif
297
298inline bool operator<=(char c1, const wxUniChar& c2) { return c2 >= c1; }
299inline bool operator<=(wchar_t c1, const wxUniChar& c2) { return c2 >= c1; }
300#ifndef wxWINT_T_IS_TYPEDEF
301inline bool operator<=(wint_t c1, const wxUniChar& c2) { return c2 >= c1; }
302#endif
303
304
305inline bool operator==(char c1, const wxUniCharRef& c2) { return c2 == c1; }
306inline bool operator==(wchar_t c1, const wxUniCharRef& c2) { return c2 == c1; }
307#ifndef wxWINT_T_IS_TYPEDEF
308inline bool operator==(wint_t c1, const wxUniCharRef& c2) { return c2 == c1; }
309#endif
310inline bool operator==(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 == c1; }
311
312inline bool operator!=(char c1, const wxUniCharRef& c2) { return c2 != c1; }
313inline bool operator!=(wchar_t c1, const wxUniCharRef& c2) { return c2 != c1; }
314#ifndef wxWINT_T_IS_TYPEDEF
315inline bool operator!=(wint_t c1, const wxUniCharRef& c2) { return c2 != c1; }
316#endif
317inline bool operator!=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 != c1; }
318
319inline bool operator>(char c1, const wxUniCharRef& c2) { return c2 < c1; }
320inline bool operator>(wchar_t c1, const wxUniCharRef& c2) { return c2 < c1; }
321#ifndef wxWINT_T_IS_TYPEDEF
322inline bool operator>(wint_t c1, const wxUniCharRef& c2) { return c2 < c1; }
323#endif
324inline bool operator>(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 < c1; }
325
326inline bool operator<(char c1, const wxUniCharRef& c2) { return c2 > c1; }
327inline bool operator<(wchar_t c1, const wxUniCharRef& c2) { return c2 > c1; }
328#ifndef wxWINT_T_IS_TYPEDEF
329inline bool operator<(wint_t c1, const wxUniCharRef& c2) { return c2 > c1; }
330#endif
331inline bool operator<(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 > c1; }
332
333inline bool operator>=(char c1, const wxUniCharRef& c2) { return c2 <= c1; }
334inline bool operator>=(wchar_t c1, const wxUniCharRef& c2) { return c2 <= c1; }
335#ifndef wxWINT_T_IS_TYPEDEF
336inline bool operator>=(wint_t c1, const wxUniCharRef& c2) { return c2 <= c1; }
337#endif
338inline bool operator>=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 <= c1; }
339
340inline bool operator<=(char c1, const wxUniCharRef& c2) { return c2 >= c1; }
341inline bool operator<=(wchar_t c1, const wxUniCharRef& c2) { return c2 >= c1; }
342#ifndef wxWINT_T_IS_TYPEDEF
343inline bool operator<=(wint_t c1, const wxUniCharRef& c2) { return c2 >= c1; }
344#endif
345inline bool operator<=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 >= c1; }
346
347// for expressions like c-'A':
348inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
349inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
350#ifndef wxWINT_T_IS_TYPEDEF
351inline int operator-(wint_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
352#endif
353inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
354
355#endif /* _WX_UNICHAR_H_ */