removed extra semicolons (patch #1700459; fixes compilation with gcc's -pedantic...
[wxWidgets.git] / include / wx / unichar.h
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 // 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
21 #if (defined(__GNUC__) && !defined(__DARWIN__) && !defined(__OS2__)) || \
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
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
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)
50 typedef wxUint32 value_type;
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); }
57 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
58
59 // Create the character from a wchar_t character value.
60 wxUniChar(wchar_t c) { m_value = c; }
61 #ifdef wxWINT_T_IS_SEPARATE_TYPE
62 wxUniChar(wint_t c) { m_value = c; }
63 #endif
64
65 wxUniChar(int c) { m_value = c; }
66
67 wxUniChar(const wxUniCharRef& c);
68
69 // Returns Unicode code point value of the character
70 value_type GetValue() const { return m_value; }
71
72 // Returns true if the character is an ASCII character:
73 bool IsAscii() const { return m_value < 0x80; }
74
75 // Conversions to char and wchar_t types: all of those are needed to be
76 // able to pass wxUniChars to verious standard narrow and wide character
77 // functions
78 operator char() const { return To8bit(m_value); }
79 operator wchar_t() const { return m_value; }
80 operator int() const { return m_value; }
81 #ifndef wxWCHAR_T_IS_UINT
82 operator unsigned int() const { return m_value; }
83 #endif
84
85 // More conversions needed for other standard functions: uchar is for VC++
86 // _mbxxx() ones (to which toxxx/isxxx() are mapped when _MBCS is defined)
87 // and some wide character functions take wint_t which happens to be the
88 // same as wchar_t for Windows compilers but not for g++ (except for the
89 // special Apple version)
90 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
91 #ifdef wxWINT_T_IS_SEPARATE_TYPE
92 operator wint_t() const { return m_value; }
93 #endif
94
95 // We need this operator for the "*p" part of expressions like "for (
96 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
97 // compilation would fail without it because the conversion to bool would
98 // be ambiguous (there are all these int types conversions...). (And adding
99 // operator unspecified_bool_type() would only makes the ambiguity worse.)
100 operator bool() const { return m_value != 0; }
101 bool operator!() const { return !((bool)*this); }
102 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
103 defined(__DIGITALMARS__) || defined(__BORLANDC__)
104 // We need this for VC++ < 8 or DigitalMars and expressions like
105 // "str[0] && *p":
106 bool operator&&(bool v) const { return (bool)*this && v; }
107 #endif
108
109 // Assignment operators:
110 wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; }
111 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
112 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
113 wxUniChar& operator=(int c) { m_value = c; return *this; }
114 #ifdef wxWINT_T_IS_SEPARATE_TYPE
115 wxUniChar& operator=(wint_t c) { m_value = c; return *this; }
116 #endif
117
118 // Comparison operators:
119
120 // define the given comparison operator for all the types
121 #define wxDEFINE_UNICHAR_OPERATOR(op) \
122 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
123 bool operator op(char c) const { return m_value op From8bit(c); } \
124 bool operator op(wchar_t c) const { return m_value op (value_type)c; } \
125 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return m_value op (value_type)c; } )
126
127 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
128
129 #undef wxDEFINE_UNICHAR_OPERATOR
130
131 // this is needed for expressions like 'Z'-c
132 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
133 int operator-(char c) const { return m_value - From8bit(c); }
134 int operator-(wchar_t c) const { return m_value - (value_type)c; }
135 #ifdef wxWINT_T_IS_SEPARATE_TYPE
136 int operator-(wint_t c) const { return m_value - (value_type)c; }
137 #endif
138
139 private:
140 static value_type From8bit(char c);
141 static char To8bit(value_type c);
142
143 private:
144 value_type m_value;
145 };
146
147
148 // Writeable reference to a character in wxString.
149 //
150 // This class can be used in the same way wxChar is used, except that changing
151 // its value updates the underlying string object.
152 class WXDLLIMPEXP_BASE wxUniCharRef
153 {
154 private:
155 typedef wxStringImpl::iterator iterator;
156
157 // create the reference
158 #if wxUSE_UNICODE_UTF8
159 wxUniCharRef(wxStringImpl& str, iterator pos) : m_str(str), m_pos(pos) {}
160 #else
161 wxUniCharRef(iterator pos) : m_pos(pos) {}
162 #endif
163
164 public:
165 // NB: we have to make this public, because we don't have wxString
166 // declaration available here and so can't declare wxString::iterator
167 // as friend; so at least don't use a ctor but a static function
168 // that must be used explicitly (this is more than using 'explicit'
169 // keyword on ctor!):
170 #if wxUSE_UNICODE_UTF8
171 static wxUniCharRef CreateForString(wxStringImpl& str, iterator pos)
172 { return wxUniCharRef(str, pos); }
173 #else
174 static wxUniCharRef CreateForString(iterator pos)
175 { return wxUniCharRef(pos); }
176 #endif
177
178 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
179 bool IsAscii() const { return UniChar().GetValue(); }
180
181 // Assignment operators:
182 #if wxUSE_UNICODE_UTF8
183 wxUniCharRef& operator=(const wxUniChar& c);
184 #else
185 wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
186 #endif
187
188 wxUniCharRef& operator=(const wxUniCharRef& c)
189 { return *this = c.UniChar(); }
190
191 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
192 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
193 wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
194 #ifdef wxWINT_T_IS_SEPARATE_TYPE
195 wxUniCharRef& operator=(wint_t c) { return *this = wxUniChar(c); }
196 #endif
197
198 // Conversions to the same types as wxUniChar is convertible too:
199 operator char() const { return UniChar(); }
200 operator int() const { return UniChar(); }
201 operator unsigned char() const { return UniChar(); }
202 operator wchar_t() const { return UniChar(); }
203 #ifdef wxWINT_T_IS_SEPARATE_TYPE
204 operator wint_t() const { return UniChar(); }
205 #endif
206 #ifndef wxWCHAR_T_IS_UINT
207 operator unsigned int() const { return UniChar(); }
208 #endif
209
210 // see wxUniChar::operator bool etc. for explanation
211 operator bool() const { return (bool)UniChar(); }
212 bool operator!() const { return !UniChar(); }
213 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
214 defined(__DIGITALMARS__) || defined(__BORLANDC__)
215 bool operator&&(bool v) const { return UniChar() && v; }
216 #endif
217
218 // Comparison operators:
219 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
220 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
221 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
222 bool operator op(char c) const { return UniChar() op c; } \
223 bool operator op(wchar_t c) const { return UniChar() op c; } \
224 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return UniChar() op c; } )
225
226 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
227
228 #undef wxDEFINE_UNICHARREF_OPERATOR
229
230 // for expressions like c-'A':
231 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
232 int operator-(const wxUniChar& c) const { return UniChar() - c; }
233 int operator-(char c) const { return UniChar() - c; }
234 int operator-(wchar_t c) const { return UniChar() - c; }
235 #ifdef wxWINT_T_IS_SEPARATE_TYPE
236 int operator-(wint_t c) const { return UniChar() - c; }
237 #endif
238
239 private:
240 wxUniChar UniChar() const
241 {
242 #if wxUSE_UNICODE_UTF8
243 return DecodeChar(m_pos);
244 #else
245 return *m_pos;
246 #endif
247 }
248
249 #if wxUSE_UNICODE_UTF8
250 // FIXME-UTF8: move this to a separate 'string operations' class
251 static wxUniChar DecodeChar(wxStringImpl::const_iterator i);
252 friend class WXDLLIMPEXP_BASE wxString;
253 #endif
254
255 friend class WXDLLIMPEXP_BASE wxUniChar;
256
257 private:
258 // reference to the string and pointer to the character in string
259 #if wxUSE_UNICODE_UTF8
260 wxStringImpl& m_str;
261 #endif
262 iterator m_pos;
263 };
264
265 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
266 {
267 m_value = c.UniChar().m_value;
268 }
269
270 // Comparison operators for the case when wxUniChar(Ref) is the second operand
271 // implemented in terms of member comparison functions
272
273 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
274
275 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
276 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
277
278 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
279 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
280
281 #ifdef wxWINT_T_IS_SEPARATE_TYPE
282 //wxDEFINE_COMPARISONS(wint_t, const wxUniChar&, wxCMP_REVERSE)
283 wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef&, wxCMP_REVERSE)
284 #endif
285
286 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
287
288 #undef wxCMP_REVERSE
289
290 // for expressions like c-'A':
291 inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
292 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
293 inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
294 #ifdef wxWINT_T_IS_SEPARATE_TYPE
295 inline int operator-(wint_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
296 #endif
297
298 #endif /* _WX_UNICHAR_H_ */