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