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