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