]>
Commit | Line | Data |
---|---|---|
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 | 15 | #include "wx/chartype.h" |
a7ea63e2 | 16 | #include "wx/stringimpl.h" |
a962cdf4 | 17 | |
436bb9f5 VZ |
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 | |
9cea4218 | 21 | #if (defined(__GNUC__) && !defined(__DARWIN__) && !defined(__OS2__)) || \ |
436bb9f5 VZ |
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 | ||
153bc803 VZ |
34 | // wchar_t seems to be defined as unsigned short by all Windows compilers but |
35 | // unsigned int everywhere else | |
36 | #ifndef __WIN32__ | |
37 | #define wxWCHAR_T_IS_UINT | |
38 | #endif | |
39 | ||
e3f6cbd9 VS |
40 | class WXDLLIMPEXP_BASE wxUniCharRef; |
41 | ||
42 | // This class represents single Unicode character. It can be converted to | |
43 | // and from char or wchar_t and implements commonly used character operations. | |
44 | class WXDLLIMPEXP_BASE wxUniChar | |
45 | { | |
46 | public: | |
47 | // NB: this is not wchar_t on purpose, it needs to represent the entire | |
48 | // Unicode code points range and wchar_t may be too small for that | |
49 | // (e.g. on Win32 where wchar_t* is encoded in UTF-16) | |
dbecee02 | 50 | typedef wxUint32 value_type; |
e3f6cbd9 VS |
51 | |
52 | wxUniChar() : m_value(0) {} | |
53 | ||
54 | // Create the character from 8bit character value encoded in the current | |
55 | // locale's charset. | |
56 | wxUniChar(char c) { m_value = From8bit(c); } | |
9f1b1b78 | 57 | wxUniChar(unsigned char c) { m_value = From8bit((char)c); } |
e3f6cbd9 VS |
58 | |
59 | // Create the character from a wchar_t character value. | |
60 | wxUniChar(wchar_t c) { m_value = c; } | |
436bb9f5 VZ |
61 | #ifdef wxWINT_T_IS_SEPARATE_TYPE |
62 | wxUniChar(wint_t c) { m_value = c; } | |
63 | #endif | |
e3f6cbd9 | 64 | |
e3f6cbd9 VS |
65 | wxUniChar(int c) { m_value = c; } |
66 | ||
67 | wxUniChar(const wxUniCharRef& c); | |
68 | ||
69 | // Returns Unicode code point value of the character | |
d1b7ed67 | 70 | value_type GetValue() const { return m_value; } |
e3f6cbd9 | 71 | |
2a686bd3 VZ |
72 | // Conversions to char and wchar_t types: all of those are needed to be |
73 | // able to pass wxUniChars to verious standard narrow and wide character | |
74 | // functions | |
e3f6cbd9 VS |
75 | operator char() const { return To8bit(m_value); } |
76 | operator wchar_t() const { return m_value; } | |
e3f6cbd9 | 77 | operator int() const { return m_value; } |
153bc803 VZ |
78 | #ifndef wxWCHAR_T_IS_UINT |
79 | operator unsigned int() const { return m_value; } | |
80 | #endif | |
2a686bd3 VZ |
81 | |
82 | // More conversions needed for other standard functions: uchar is for VC++ | |
83 | // _mbxxx() ones (to which toxxx/isxxx() are mapped when _MBCS is defined) | |
c961c0cf VZ |
84 | // and some wide character functions take wint_t which happens to be the |
85 | // same as wchar_t for Windows compilers but not for g++ (except for the | |
86 | // special Apple version) | |
2a686bd3 | 87 | operator unsigned char() const { return (unsigned char)To8bit(m_value); } |
436bb9f5 | 88 | #ifdef wxWINT_T_IS_SEPARATE_TYPE |
c961c0cf VZ |
89 | operator wint_t() const { return m_value; } |
90 | #endif | |
e3f6cbd9 VS |
91 | |
92 | // We need this operator for the "*p" part of expressions like "for ( | |
93 | // const_iterator p = begin() + nStart; *p; ++p )". In this case, | |
94 | // compilation would fail without it because the conversion to bool would | |
95 | // be ambiguous (there are all these int types conversions...). (And adding | |
96 | // operator unspecified_bool_type() would only makes the ambiguity worse.) | |
97 | operator bool() const { return m_value != 0; } | |
98 | bool operator!() const { return !((bool)*this); } | |
99 | #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \ | |
100 | defined(__DIGITALMARS__) || defined(__BORLANDC__) | |
101 | // We need this for VC++ < 8 or DigitalMars and expressions like | |
102 | // "str[0] && *p": | |
103 | bool operator&&(bool v) const { return (bool)*this && v; } | |
104 | #endif | |
105 | ||
106 | // Assignment operators: | |
107 | wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; } | |
108 | wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; } | |
109 | wxUniChar& operator=(wchar_t c) { m_value = c; return *this; } | |
171d6132 | 110 | wxUniChar& operator=(int c) { m_value = c; return *this; } |
436bb9f5 VZ |
111 | #ifdef wxWINT_T_IS_SEPARATE_TYPE |
112 | wxUniChar& operator=(wint_t c) { m_value = c; return *this; } | |
113 | #endif | |
e3f6cbd9 | 114 | |
fb52c2b6 | 115 | // Comparison operators: |
e3f6cbd9 | 116 | |
fb52c2b6 | 117 | // define the given comparison operator for all the types |
98c4eb39 | 118 | #define wxDEFINE_UNICHAR_OPERATOR(op) \ |
fb52c2b6 VZ |
119 | bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\ |
120 | bool operator op(char c) const { return m_value op From8bit(c); } \ | |
436bb9f5 VZ |
121 | bool operator op(wchar_t c) const { return m_value op (value_type)c; } \ |
122 | wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return m_value op (value_type)c; } ) | |
e3f6cbd9 | 123 | |
fb52c2b6 | 124 | wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR) |
e3f6cbd9 | 125 | |
fb52c2b6 | 126 | #undef wxDEFINE_UNICHAR_OPERATOR |
e3f6cbd9 | 127 | |
fb52c2b6 | 128 | // this is needed for expressions like 'Z'-c |
e3f6cbd9 VS |
129 | int operator-(const wxUniChar& c) const { return m_value - c.m_value; } |
130 | int operator-(char c) const { return m_value - From8bit(c); } | |
d1b7ed67 | 131 | int operator-(wchar_t c) const { return m_value - (value_type)c; } |
436bb9f5 VZ |
132 | #ifdef wxWINT_T_IS_SEPARATE_TYPE |
133 | int operator-(wint_t c) const { return m_value - (value_type)c; } | |
134 | #endif | |
e3f6cbd9 VS |
135 | |
136 | private: | |
d1b7ed67 VS |
137 | static value_type From8bit(char c); |
138 | static char To8bit(value_type c); | |
e3f6cbd9 VS |
139 | |
140 | private: | |
d1b7ed67 | 141 | value_type m_value; |
e3f6cbd9 VS |
142 | }; |
143 | ||
144 | ||
145 | // Writeable reference to a character in wxString. | |
146 | // | |
147 | // This class can be used in the same way wxChar is used, except that changing | |
148 | // its value updates the underlying string object. | |
149 | class WXDLLIMPEXP_BASE wxUniCharRef | |
150 | { | |
151 | private: | |
a962cdf4 VS |
152 | typedef wxStringImpl::iterator iterator; |
153 | ||
e3f6cbd9 | 154 | // create the reference |
a962cdf4 | 155 | wxUniCharRef(iterator pos) : m_pos(pos) {} |
e3f6cbd9 VS |
156 | |
157 | public: | |
158 | // NB: we have to make this public, because we don't have wxString | |
159 | // declaration available here and so can't declare wxString::iterator | |
160 | // as friend; so at least don't use a ctor but a static function | |
161 | // that must be used explicitly (this is more than using 'explicit' | |
162 | // keyword on ctor!): | |
a962cdf4 | 163 | static wxUniCharRef CreateForString(iterator pos) |
e3f6cbd9 VS |
164 | { return wxUniCharRef(pos); } |
165 | ||
d1b7ed67 | 166 | wxUniChar::value_type GetValue() const { return UniChar().GetValue(); } |
e3f6cbd9 VS |
167 | |
168 | // Assignment operators: | |
169 | wxUniCharRef& operator=(const wxUniCharRef& c) | |
170 | { | |
171 | *m_pos = *c.m_pos; | |
172 | return *this; | |
173 | }; | |
174 | ||
175 | wxUniCharRef& operator=(const wxUniChar& c) | |
176 | { | |
177 | *m_pos = c; | |
178 | return *this; | |
179 | }; | |
180 | ||
181 | wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); } | |
182 | wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); } | |
171d6132 | 183 | wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); } |
436bb9f5 VZ |
184 | #ifdef wxWINT_T_IS_SEPARATE_TYPE |
185 | wxUniCharRef& operator=(wint_t c) { return *this = wxUniChar(c); } | |
186 | #endif | |
e3f6cbd9 | 187 | |
2a686bd3 | 188 | // Conversions to the same types as wxUniChar is convertible too: |
e3f6cbd9 | 189 | operator char() const { return UniChar(); } |
e3f6cbd9 | 190 | operator int() const { return UniChar(); } |
2a686bd3 | 191 | operator unsigned char() const { return UniChar(); } |
436bb9f5 VZ |
192 | operator wchar_t() const { return UniChar(); } |
193 | #ifdef wxWINT_T_IS_SEPARATE_TYPE | |
c961c0cf VZ |
194 | operator wint_t() const { return UniChar(); } |
195 | #endif | |
153bc803 VZ |
196 | #ifndef wxWCHAR_T_IS_UINT |
197 | operator unsigned int() const { return UniChar(); } | |
198 | #endif | |
e3f6cbd9 VS |
199 | |
200 | // see wxUniChar::operator bool etc. for explanation | |
201 | operator bool() const { return (bool)UniChar(); } | |
202 | bool operator!() const { return !UniChar(); } | |
203 | #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \ | |
204 | defined(__DIGITALMARS__) || defined(__BORLANDC__) | |
205 | bool operator&&(bool v) const { return UniChar() && v; } | |
206 | #endif | |
207 | ||
fb52c2b6 | 208 | // Comparison operators: |
98c4eb39 | 209 | #define wxDEFINE_UNICHARREF_OPERATOR(op) \ |
fb52c2b6 VZ |
210 | bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\ |
211 | bool operator op(const wxUniChar& c) const { return UniChar() op c; } \ | |
212 | bool operator op(char c) const { return UniChar() op c; } \ | |
436bb9f5 VZ |
213 | bool operator op(wchar_t c) const { return UniChar() op c; } \ |
214 | wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return UniChar() op c; } ) | |
e3f6cbd9 | 215 | |
fb52c2b6 | 216 | wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR) |
e3f6cbd9 | 217 | |
fb52c2b6 | 218 | #undef wxDEFINE_UNICHARREF_OPERATOR |
e3f6cbd9 VS |
219 | |
220 | // for expressions like c-'A': | |
221 | int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); } | |
222 | int operator-(const wxUniChar& c) const { return UniChar() - c; } | |
223 | int operator-(char c) const { return UniChar() - c; } | |
224 | int operator-(wchar_t c) const { return UniChar() - c; } | |
436bb9f5 VZ |
225 | #ifdef wxWINT_T_IS_SEPARATE_TYPE |
226 | int operator-(wint_t c) const { return UniChar() - c; } | |
227 | #endif | |
e3f6cbd9 VS |
228 | |
229 | private: | |
230 | wxUniChar UniChar() const { return *m_pos; } | |
231 | friend class WXDLLIMPEXP_BASE wxUniChar; | |
232 | ||
233 | private: | |
234 | // pointer to the character in string | |
a962cdf4 | 235 | iterator m_pos; |
e3f6cbd9 VS |
236 | }; |
237 | ||
238 | inline wxUniChar::wxUniChar(const wxUniCharRef& c) | |
239 | { | |
240 | m_value = c.UniChar().m_value; | |
241 | } | |
242 | ||
fb52c2b6 VZ |
243 | // Comparison operators for the case when wxUniChar(Ref) is the second operand |
244 | // implemented in terms of member comparison functions | |
e3f6cbd9 | 245 | |
fb52c2b6 | 246 | #define wxCMP_REVERSE(c1, c2, op) c2 op c1 |
e3f6cbd9 | 247 | |
fb52c2b6 VZ |
248 | wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE) |
249 | wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE) | |
e3f6cbd9 | 250 | |
fb52c2b6 VZ |
251 | wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE) |
252 | wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE) | |
e3f6cbd9 | 253 | |
436bb9f5 VZ |
254 | #ifdef wxWINT_T_IS_SEPARATE_TYPE |
255 | wxDEFINE_COMPARISONS(wint_t, const wxUniChar&, wxCMP_REVERSE) | |
256 | wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef&, wxCMP_REVERSE) | |
257 | #endif | |
258 | ||
fb52c2b6 | 259 | wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE) |
e3f6cbd9 | 260 | |
fb52c2b6 | 261 | #undef wxCMP_REVERSE |
e3f6cbd9 VS |
262 | |
263 | // for expressions like c-'A': | |
264 | inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); } | |
e3f6cbd9 | 265 | inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); } |
436bb9f5 VZ |
266 | inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); } |
267 | #ifdef wxWINT_T_IS_SEPARATE_TYPE | |
268 | inline int operator-(wint_t c1, const wxUniCharRef& c2) { return -(c2 - c1); } | |
269 | #endif | |
e3f6cbd9 VS |
270 | |
271 | #endif /* _WX_UNICHAR_H_ */ |