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