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