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"
17 class WXDLLIMPEXP_BASE wxUniCharRef
;
19 // This class represents single Unicode character. It can be converted to
20 // and from char or wchar_t and implements commonly used character operations.
21 class WXDLLIMPEXP_BASE wxUniChar
24 // NB: this is not wchar_t on purpose, it needs to represent the entire
25 // Unicode code points range and wchar_t may be too small for that
26 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
27 typedef wxUint32 value_type
;
29 wxUniChar() : m_value(0) {}
31 // Create the character from 8bit character value encoded in the current
33 wxUniChar(char c
) { m_value
= From8bit(c
); }
34 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
36 // Create the character from a wchar_t character value.
37 wxUniChar(wchar_t c
) { m_value
= c
; }
39 #ifndef wxWINT_T_IS_TYPEDEF
40 // Create the character from a wint_t character value.
41 wxUniChar(wint_t c
) { m_value
= c
; }
44 wxUniChar(int c
) { m_value
= c
; }
46 wxUniChar(const wxUniCharRef
& c
);
48 // Returns Unicode code point value of the character
49 value_type
GetValue() const { return m_value
; }
51 // Casts to char and wchar_t types:
52 operator char() const { return To8bit(m_value
); }
53 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
54 operator wchar_t() const { return m_value
; }
55 #ifndef wxWINT_T_IS_TYPEDEF
56 operator wint_t() const { return m_value
; }
58 operator int() const { return m_value
; }
59 operator unsigned int() const { return m_value
; }
61 // We need this operator for the "*p" part of expressions like "for (
62 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
63 // compilation would fail without it because the conversion to bool would
64 // be ambiguous (there are all these int types conversions...). (And adding
65 // operator unspecified_bool_type() would only makes the ambiguity worse.)
66 operator bool() const { return m_value
!= 0; }
67 bool operator!() const { return !((bool)*this); }
68 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
69 defined(__DIGITALMARS__) || defined(__BORLANDC__)
70 // We need this for VC++ < 8 or DigitalMars and expressions like
72 bool operator&&(bool v
) const { return (bool)*this && v
; }
75 // Assignment operators:
76 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
77 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
78 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
79 #ifndef wxWINT_T_IS_TYPEDEF
80 wxUniChar
& operator=(wint_t c
) { m_value
= c
; return *this; }
83 // Comparison operators:
85 // define the given comparison operator for all the types
86 #define wxDEFINE_UNICHAR_OPERATOR_NO_WINT(op) \
87 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
88 bool operator op(char c) const { return m_value op From8bit(c); } \
89 bool operator op(wchar_t c) const { return m_value op (value_type)c; }
91 #ifdef wxWINT_T_IS_TYPEDEF
92 #define wxDEFINE_UNICHAR_OPERATOR wxDEFINE_UNICHAR_OPERATOR_NO_WINT
93 #else // wint_t is a separate type, need to overload for it too
94 #define wxDEFINE_UNICHAR_OPERATOR(op) \
95 wxDEFINE_UNICHAR_OPERATOR_NO_WINT(op) \
96 bool operator op(wint_t c) const { return m_value op (value_type)c; }
99 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
101 #undef wxDEFINE_UNICHAR_OPERATOR_NO_WINT
102 #undef wxDEFINE_UNICHAR_OPERATOR
104 // this is needed for expressions like 'Z'-c
105 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
106 int operator-(char c
) const { return m_value
- From8bit(c
); }
107 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
108 #ifndef wxWINT_T_IS_TYPEDEF
109 int operator-(wint_t c
) const { return m_value
- (value_type
)c
; }
113 static value_type
From8bit(char c
);
114 static char To8bit(value_type c
);
121 // Writeable reference to a character in wxString.
123 // This class can be used in the same way wxChar is used, except that changing
124 // its value updates the underlying string object.
125 class WXDLLIMPEXP_BASE wxUniCharRef
128 // create the reference
129 // FIXME-UTF8: the interface will need changes for UTF-8 build
130 wxUniCharRef(wxChar
*pos
) : m_pos(pos
) {}
133 // NB: we have to make this public, because we don't have wxString
134 // declaration available here and so can't declare wxString::iterator
135 // as friend; so at least don't use a ctor but a static function
136 // that must be used explicitly (this is more than using 'explicit'
137 // keyword on ctor!):
139 // FIXME-UTF8: the interface will need changes for UTF-8 build
140 static wxUniCharRef
CreateForString(wxChar
*pos
)
141 { return wxUniCharRef(pos
); }
143 wxUniChar
::value_type
GetValue() const { return UniChar().GetValue(); }
145 // Assignment operators:
146 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
152 wxUniCharRef
& operator=(const wxUniChar
& c
)
158 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
159 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
161 // Casts to wxUniChar type:
162 operator char() const { return UniChar(); }
163 operator unsigned char() const { return UniChar(); }
164 operator wchar_t() const { return UniChar(); }
165 #ifndef wxWINT_T_IS_TYPEDEF
166 operator wint_t() const { return UniChar(); }
168 operator int() const { return UniChar(); }
169 operator unsigned int() const { return UniChar(); }
171 // see wxUniChar::operator bool etc. for explanation
172 operator bool() const { return (bool)UniChar(); }
173 bool operator!() const { return !UniChar(); }
174 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
175 defined(__DIGITALMARS__) || defined(__BORLANDC__)
176 bool operator&&(bool v
) const { return UniChar() && v
; }
179 // Comparison operators:
180 #define wxDEFINE_UNICHARREF_OPERATOR_NO_WINT(op) \
181 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
182 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
183 bool operator op(char c) const { return UniChar() op c; } \
184 bool operator op(wchar_t c) const { return UniChar() op c; }
186 #ifdef wxWINT_T_IS_TYPEDEF
187 #define wxDEFINE_UNICHARREF_OPERATOR wxDEFINE_UNICHARREF_OPERATOR_NO_WINT
188 #else // wint_t is a separate type, need to overload for it too
189 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
190 wxDEFINE_UNICHARREF_OPERATOR_NO_WINT(op) \
191 bool operator op(wint_t c) const { return UniChar() op c; }
194 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
196 #undef wxDEFINE_UNICHARREF_OPERATOR_NO_WINT
197 #undef wxDEFINE_UNICHARREF_OPERATOR
199 // for expressions like c-'A':
200 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
201 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
202 int operator-(char c
) const { return UniChar() - c
; }
203 int operator-(wchar_t c
) const { return UniChar() - c
; }
204 #ifndef wxWINT_T_IS_TYPEDEF
205 int operator-(wint_t c
) const { return UniChar() - c
; }
209 wxUniChar
UniChar() const { return *m_pos
; }
210 friend class WXDLLIMPEXP_BASE wxUniChar
;
213 // pointer to the character in string
217 inline wxUniChar
::wxUniChar(const wxUniCharRef
& c
)
219 m_value
= c
.UniChar().m_value
;
222 // Comparison operators for the case when wxUniChar(Ref) is the second operand
223 // implemented in terms of member comparison functions
225 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
227 wxDEFINE_COMPARISONS(char, const wxUniChar
&, wxCMP_REVERSE
)
228 wxDEFINE_COMPARISONS(char, const wxUniCharRef
&, wxCMP_REVERSE
)
230 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar
&, wxCMP_REVERSE
)
231 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef
&, wxCMP_REVERSE
)
233 #ifndef wxWINT_T_IS_TYPEDEF
234 wxDEFINE_COMPARISONS(wint_t, const wxUniChar
&, wxCMP_REVERSE
)
235 wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef
&, wxCMP_REVERSE
)
238 wxDEFINE_COMPARISONS(const wxUniChar
&, const wxUniCharRef
&, wxCMP_REVERSE
)
242 // for expressions like c-'A':
243 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
244 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
245 #ifndef wxWINT_T_IS_TYPEDEF
246 inline int operator-(wint_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
248 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
250 #endif /* _WX_UNICHAR_H_ */