1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_UNICHAR_H_
12 #define _WX_UNICHAR_H_
15 #include "wx/chartype.h"
16 #include "wx/stringimpl.h"
18 class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
19 class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
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
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
;
31 wxUniChar() : m_value(0) {}
33 // Create the character from 8bit character value encoded in the current
35 wxUniChar(char c
) { m_value
= From8bit(c
); }
36 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
38 // Create the character from a wchar_t character value.
39 #if wxWCHAR_T_IS_REAL_TYPE
40 wxUniChar(wchar_t c
) { m_value
= c
; }
43 wxUniChar(int c
) { m_value
= c
; }
44 wxUniChar(unsigned int c
) { m_value
= c
; }
45 wxUniChar(long int c
) { m_value
= c
; }
46 wxUniChar(unsigned long int c
) { m_value
= c
; }
47 wxUniChar(short int c
) { m_value
= c
; }
48 wxUniChar(unsigned short int c
) { m_value
= c
; }
50 wxUniChar(const wxUniCharRef
& c
);
52 // Returns Unicode code point value of the character
53 value_type
GetValue() const { return m_value
; }
55 #if wxUSE_UNICODE_UTF8
56 // buffer for single UTF-8 character
60 operator const char*() const { return data
; }
63 // returns the character encoded as UTF-8
64 // (NB: implemented in stringops.cpp)
65 Utf8CharBuffer
AsUTF8() const;
66 #endif // wxUSE_UNICODE_UTF8
68 // Returns true if the character is an ASCII character:
69 bool IsAscii() const { return m_value
< 0x80; }
71 // Conversions to char and wchar_t types: all of those are needed to be
72 // able to pass wxUniChars to verious standard narrow and wide character
74 operator char() const { return To8bit(m_value
); }
75 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
76 #if wxWCHAR_T_IS_REAL_TYPE
77 operator wchar_t() const { return (wchar_t)m_value
; }
79 operator int() const { return (int)m_value
; }
80 operator unsigned int() const { return (unsigned int)m_value
; }
81 operator long int() const { return (long int)m_value
; }
82 operator unsigned long int() const { return (unsigned long)m_value
; }
83 operator short int() const { return (short int)m_value
; }
84 operator unsigned short int() const { return (unsigned short int)m_value
; }
86 // We need this operator for the "*p" part of expressions like "for (
87 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
88 // compilation would fail without it because the conversion to bool would
89 // be ambiguous (there are all these int types conversions...). (And adding
90 // operator unspecified_bool_type() would only makes the ambiguity worse.)
91 operator bool() const { return m_value
!= 0; }
92 bool operator!() const { return !((bool)*this); }
94 // And this one is needed by some (not all, but not using ifdefs makes the
95 // code easier) compilers to parse "str[0] && *p" successfully
96 bool operator&&(bool v
) const { return (bool)*this && v
; }
98 // Assignment operators:
99 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
100 wxUniChar
& operator=(const wxUniCharRef
& c
);
101 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
102 wxUniChar
& operator=(unsigned char c
) { m_value
= From8bit((char)c
); return *this; }
103 #if wxWCHAR_T_IS_REAL_TYPE
104 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
106 wxUniChar
& operator=(int c
) { m_value
= c
; return *this; }
107 wxUniChar
& operator=(unsigned int c
) { m_value
= c
; return *this; }
108 wxUniChar
& operator=(long int c
) { m_value
= c
; return *this; }
109 wxUniChar
& operator=(unsigned long int c
) { m_value
= c
; return *this; }
110 wxUniChar
& operator=(short int c
) { m_value
= c
; return *this; }
111 wxUniChar
& operator=(unsigned short int c
) { m_value
= c
; return *this; }
113 // Comparison operators:
115 // define the given comparison operator for all the types
116 #define wxDEFINE_UNICHAR_OPERATOR(op) \
117 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
118 bool operator op(char c) const { return m_value op From8bit(c); } \
119 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
120 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
121 bool operator op(int c) const { return m_value op (value_type)c; } \
122 bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
123 bool operator op(short int c) const { return m_value op (value_type)c; } \
124 bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
125 bool operator op(long int c) const { return m_value op (value_type)c; } \
126 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
128 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
130 #undef wxDEFINE_UNICHAR_OPERATOR
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-(unsigned char c
) const { return m_value
- From8bit((char)c
); }
136 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
140 static value_type
From8bit(char c
);
141 static char To8bit(value_type c
);
148 // Writeable reference to a character in wxString.
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
155 typedef wxStringImpl::iterator iterator
;
157 // create the reference
158 #if wxUSE_UNICODE_UTF8
159 wxUniCharRef(wxStringIteratorNode
& node
, iterator pos
) : m_node(node
), m_pos(pos
) {}
161 wxUniCharRef(iterator pos
) : m_pos(pos
) {}
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(wxStringIteratorNode
& node
, iterator pos
)
172 { return wxUniCharRef(node
, pos
); }
174 static wxUniCharRef
CreateForString(iterator pos
)
175 { return wxUniCharRef(pos
); }
178 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
180 #if wxUSE_UNICODE_UTF8
181 wxUniChar::Utf8CharBuffer
AsUTF8() const { return UniChar().AsUTF8(); }
182 #endif // wxUSE_UNICODE_UTF8
184 bool IsAscii() const { return UniChar().IsAscii(); }
186 // Assignment operators:
187 #if wxUSE_UNICODE_UTF8
188 wxUniCharRef
& operator=(const wxUniChar
& c
);
190 wxUniCharRef
& operator=(const wxUniChar
& c
) { *m_pos
= c
; return *this; }
193 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
194 { return *this = c
.UniChar(); }
196 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
197 wxUniCharRef
& operator=(unsigned char c
) { return *this = wxUniChar(c
); }
198 #if wxWCHAR_T_IS_REAL_TYPE
199 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
201 wxUniCharRef
& operator=(int c
) { return *this = wxUniChar(c
); }
202 wxUniCharRef
& operator=(unsigned int c
) { return *this = wxUniChar(c
); }
203 wxUniCharRef
& operator=(short int c
) { return *this = wxUniChar(c
); }
204 wxUniCharRef
& operator=(unsigned short int c
) { return *this = wxUniChar(c
); }
205 wxUniCharRef
& operator=(long int c
) { return *this = wxUniChar(c
); }
206 wxUniCharRef
& operator=(unsigned long int c
) { return *this = wxUniChar(c
); }
208 // Conversions to the same types as wxUniChar is convertible too:
209 operator char() const { return UniChar(); }
210 operator unsigned char() const { return UniChar(); }
211 #if wxWCHAR_T_IS_REAL_TYPE
212 operator wchar_t() const { return UniChar(); }
214 operator int() const { return UniChar(); }
215 operator unsigned int() const { return UniChar(); }
216 operator short int() const { return UniChar(); }
217 operator unsigned short int() const { return UniChar(); }
218 operator long int() const { return UniChar(); }
219 operator unsigned long int() const { return UniChar(); }
221 // see wxUniChar::operator bool etc. for explanation
222 operator bool() const { return (bool)UniChar(); }
223 bool operator!() const { return !UniChar(); }
224 bool operator&&(bool v
) const { return UniChar() && v
; }
226 // Comparison operators:
227 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
228 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
229 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
230 bool operator op(char c) const { return UniChar() op c; } \
231 bool operator op(unsigned char c) const { return UniChar() op c; } \
232 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
233 bool operator op(int c) const { return UniChar() op c; } \
234 bool operator op(unsigned int c) const { return UniChar() op c; } \
235 bool operator op(short int c) const { return UniChar() op c; } \
236 bool operator op(unsigned short int c) const { return UniChar() op c; } \
237 bool operator op(long int c) const { return UniChar() op c; } \
238 bool operator op(unsigned long int c) const { return UniChar() op c; }
240 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
242 #undef wxDEFINE_UNICHARREF_OPERATOR
244 // for expressions like c-'A':
245 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
246 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
247 int operator-(char c
) const { return UniChar() - c
; }
248 int operator-(unsigned char c
) const { return UniChar() - c
; }
249 int operator-(wchar_t c
) const { return UniChar() - c
; }
252 #if wxUSE_UNICODE_UTF8
253 wxUniChar
UniChar() const;
255 wxUniChar
UniChar() const { return *m_pos
; }
258 friend class WXDLLIMPEXP_FWD_BASE wxUniChar
;
261 // reference to the string and pointer to the character in string
262 #if wxUSE_UNICODE_UTF8
263 wxStringIteratorNode
& m_node
;
268 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
270 m_value
= c
.UniChar().m_value
;
273 inline wxUniChar
& wxUniChar::operator=(const wxUniCharRef
& c
)
275 m_value
= c
.UniChar().m_value
;
279 // Comparison operators for the case when wxUniChar(Ref) is the second operand
280 // implemented in terms of member comparison functions
282 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
284 wxDEFINE_COMPARISONS(char, const wxUniChar
&, wxCMP_REVERSE
)
285 wxDEFINE_COMPARISONS(char, const wxUniCharRef
&, wxCMP_REVERSE
)
287 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar
&, wxCMP_REVERSE
)
288 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef
&, wxCMP_REVERSE
)
290 wxDEFINE_COMPARISONS(const wxUniChar
&, const wxUniCharRef
&, wxCMP_REVERSE
)
294 // for expressions like c-'A':
295 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
296 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
297 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
299 #endif /* _WX_UNICHAR_H_ */