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