]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unichar.h
Fix warnings about implicit float or double to int conversions in wxMSW.
[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 class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
19 class WXDLLIMPEXP_FWD_BASE wxString;
20
21 // This class represents single Unicode character. It can be converted to
22 // and from char or wchar_t and implements commonly used character operations.
23 class WXDLLIMPEXP_BASE wxUniChar
24 {
25 public:
26 // NB: this is not wchar_t on purpose, it needs to represent the entire
27 // Unicode code points range and wchar_t may be too small for that
28 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
29 typedef wxUint32 value_type;
30
31 wxUniChar() : m_value(0) {}
32
33 // Create the character from 8bit character value encoded in the current
34 // locale's charset.
35 wxUniChar(char c) { m_value = From8bit(c); }
36 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
37
38 #define wxUNICHAR_DEFINE_CTOR(type) \
39 wxUniChar(type c) { m_value = (value_type)c; }
40 wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_CTOR)
41 #undef wxUNICHAR_DEFINE_CTOR
42
43 wxUniChar(const wxUniCharRef& c);
44
45 // Returns Unicode code point value of the character
46 value_type GetValue() const { return m_value; }
47
48 #if wxUSE_UNICODE_UTF8
49 // buffer for single UTF-8 character
50 struct Utf8CharBuffer
51 {
52 char data[5];
53 operator const char*() const { return data; }
54 };
55
56 // returns the character encoded as UTF-8
57 // (NB: implemented in stringops.cpp)
58 Utf8CharBuffer AsUTF8() const;
59 #endif // wxUSE_UNICODE_UTF8
60
61 // Returns true if the character is an ASCII character:
62 bool IsAscii() const { return m_value < 0x80; }
63
64 // Returns true if the character is representable as a single byte in the
65 // current locale encoding and return this byte in output argument c (which
66 // must be non-NULL)
67 bool GetAsChar(char *c) const
68 {
69 #if wxUSE_UNICODE
70 if ( !IsAscii() )
71 {
72 #if !wxUSE_UTF8_LOCALE_ONLY
73 if ( GetAsHi8bit(m_value, c) )
74 return true;
75 #endif // !wxUSE_UTF8_LOCALE_ONLY
76
77 return false;
78 }
79 #endif // wxUSE_UNICODE
80
81 *c = wx_truncate_cast(char, m_value);
82 return true;
83 }
84
85 // Conversions to char and wchar_t types: all of those are needed to be
86 // able to pass wxUniChars to verious standard narrow and wide character
87 // functions
88 operator char() const { return To8bit(m_value); }
89 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
90
91 #define wxUNICHAR_DEFINE_OPERATOR_PAREN(type) \
92 operator type() const { return (type)m_value; }
93 wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_OPERATOR_PAREN)
94 #undef wxUNICHAR_DEFINE_OPERATOR_PAREN
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
104 // And this one is needed by some (not all, but not using ifdefs makes the
105 // code easier) compilers to parse "str[0] && *p" successfully
106 bool operator&&(bool v) const { return (bool)*this && v; }
107
108 // Assignment operators:
109 wxUniChar& operator=(const wxUniChar& c) { if (&c != this) m_value = c.m_value; return *this; }
110 wxUniChar& operator=(const wxUniCharRef& c);
111 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
112 wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
113
114 #define wxUNICHAR_DEFINE_OPERATOR_EQUAL(type) \
115 wxUniChar& operator=(type c) { m_value = (value_type)c; return *this; }
116 wxDO_FOR_INT_TYPES(wxUNICHAR_DEFINE_OPERATOR_EQUAL)
117 #undef wxUNICHAR_DEFINE_OPERATOR_EQUAL
118
119 // Comparison operators:
120 #define wxDEFINE_UNICHAR_CMP_WITH_INT(T, op) \
121 bool operator op(T c) const { return m_value op (value_type)c; }
122
123 // define the given comparison operator for all the types
124 #define wxDEFINE_UNICHAR_OPERATOR(op) \
125 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
126 bool operator op(char c) const { return m_value op From8bit(c); } \
127 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
128 wxDO_FOR_INT_TYPES_1(wxDEFINE_UNICHAR_CMP_WITH_INT, op)
129
130 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
131
132 #undef wxDEFINE_UNICHAR_OPERATOR
133 #undef wxDEFINE_UNCHAR_CMP_WITH_INT
134
135 // this is needed for expressions like 'Z'-c
136 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
137 int operator-(char c) const { return m_value - From8bit(c); }
138 int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
139 int operator-(wchar_t c) const { return m_value - (value_type)c; }
140
141
142 private:
143 // notice that we implement these functions inline for 7-bit ASCII
144 // characters purely for performance reasons
145 static value_type From8bit(char c)
146 {
147 #if wxUSE_UNICODE
148 if ( (unsigned char)c < 0x80 )
149 return c;
150
151 return FromHi8bit(c);
152 #else
153 return c;
154 #endif
155 }
156
157 static char To8bit(value_type c)
158 {
159 #if wxUSE_UNICODE
160 if ( c < 0x80 )
161 return wx_truncate_cast(char, c);
162
163 return ToHi8bit(c);
164 #else
165 return c;
166 #endif
167 }
168
169 // helpers of the functions above called to deal with non-ASCII chars
170 static value_type FromHi8bit(char c);
171 static char ToHi8bit(value_type v);
172 static bool GetAsHi8bit(value_type v, char *c);
173
174 private:
175 value_type m_value;
176 };
177
178
179 // Writeable reference to a character in wxString.
180 //
181 // This class can be used in the same way wxChar is used, except that changing
182 // its value updates the underlying string object.
183 class WXDLLIMPEXP_BASE wxUniCharRef
184 {
185 private:
186 typedef wxStringImpl::iterator iterator;
187
188 // create the reference
189 #if wxUSE_UNICODE_UTF8
190 wxUniCharRef(wxString& str, iterator pos) : m_str(str), m_pos(pos) {}
191 #else
192 wxUniCharRef(iterator pos) : m_pos(pos) {}
193 #endif
194
195 public:
196 // NB: we have to make this public, because we don't have wxString
197 // declaration available here and so can't declare wxString::iterator
198 // as friend; so at least don't use a ctor but a static function
199 // that must be used explicitly (this is more than using 'explicit'
200 // keyword on ctor!):
201 #if wxUSE_UNICODE_UTF8
202 static wxUniCharRef CreateForString(wxString& str, iterator pos)
203 { return wxUniCharRef(str, pos); }
204 #else
205 static wxUniCharRef CreateForString(iterator pos)
206 { return wxUniCharRef(pos); }
207 #endif
208
209 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
210
211 #if wxUSE_UNICODE_UTF8
212 wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
213 #endif // wxUSE_UNICODE_UTF8
214
215 bool IsAscii() const { return UniChar().IsAscii(); }
216 bool GetAsChar(char *c) const { return UniChar().GetAsChar(c); }
217
218 // Assignment operators:
219 #if wxUSE_UNICODE_UTF8
220 wxUniCharRef& operator=(const wxUniChar& c);
221 #else
222 wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
223 #endif
224
225 wxUniCharRef& operator=(const wxUniCharRef& c)
226 { if (&c != this) *this = c.UniChar(); return *this; }
227
228 #define wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL(type) \
229 wxUniCharRef& operator=(type c) { return *this = wxUniChar(c); }
230 wxDO_FOR_CHAR_INT_TYPES(wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL)
231 #undef wxUNICHAR_REF_DEFINE_OPERATOR_EQUAL
232
233 // Conversions to the same types as wxUniChar is convertible too:
234 #define wxUNICHAR_REF_DEFINE_OPERATOR_PAREN(type) \
235 operator type() const { return UniChar(); }
236 wxDO_FOR_CHAR_INT_TYPES(wxUNICHAR_REF_DEFINE_OPERATOR_PAREN)
237 #undef wxUNICHAR_REF_DEFINE_OPERATOR_PAREN
238
239 // see wxUniChar::operator bool etc. for explanation
240 operator bool() const { return (bool)UniChar(); }
241 bool operator!() const { return !UniChar(); }
242 bool operator&&(bool v) const { return UniChar() && v; }
243
244 #define wxDEFINE_UNICHARREF_CMP_WITH_INT(T, op) \
245 bool operator op(T c) const { return UniChar() op c; }
246
247 // Comparison operators:
248 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
249 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
250 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
251 wxDO_FOR_CHAR_INT_TYPES_1(wxDEFINE_UNICHARREF_CMP_WITH_INT, op)
252
253 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
254
255 #undef wxDEFINE_UNICHARREF_OPERATOR
256 #undef wxDEFINE_UNICHARREF_CMP_WITH_INT
257
258 // for expressions like c-'A':
259 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
260 int operator-(const wxUniChar& c) const { return UniChar() - c; }
261 int operator-(char c) const { return UniChar() - c; }
262 int operator-(unsigned char c) const { return UniChar() - c; }
263 int operator-(wchar_t c) const { return UniChar() - c; }
264
265 private:
266 #if wxUSE_UNICODE_UTF8
267 wxUniChar UniChar() const;
268 #else
269 wxUniChar UniChar() const { return *m_pos; }
270 #endif
271
272 friend class WXDLLIMPEXP_FWD_BASE wxUniChar;
273
274 private:
275 // reference to the string and pointer to the character in string
276 #if wxUSE_UNICODE_UTF8
277 wxString& m_str;
278 #endif
279 iterator m_pos;
280 };
281
282 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
283 {
284 m_value = c.UniChar().m_value;
285 }
286
287 inline wxUniChar& wxUniChar::operator=(const wxUniCharRef& c)
288 {
289 m_value = c.UniChar().m_value;
290 return *this;
291 }
292
293 // Comparison operators for the case when wxUniChar(Ref) is the second operand
294 // implemented in terms of member comparison functions
295
296 wxDEFINE_COMPARISONS_BY_REV(char, const wxUniChar&)
297 wxDEFINE_COMPARISONS_BY_REV(char, const wxUniCharRef&)
298
299 wxDEFINE_COMPARISONS_BY_REV(wchar_t, const wxUniChar&)
300 wxDEFINE_COMPARISONS_BY_REV(wchar_t, const wxUniCharRef&)
301
302 wxDEFINE_COMPARISONS_BY_REV(const wxUniChar&, const wxUniCharRef&)
303
304 // for expressions like c-'A':
305 inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
306 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
307 inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
308
309 #endif /* _WX_UNICHAR_H_ */