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 // 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__) && !defined(__DARWIN__) && !defined(__OS2__)) || \
22 (defined(__VISUALC__) && defined(_NATIVE_WCHAR_T_DEFINED))
23 #define wxWINT_T_IS_SEPARATE_TYPE
26 // helper macro for doing something dependent on whether wint_t is or isn't a
27 // typedef inside another macro
28 #ifdef wxWINT_T_IS_SEPARATE_TYPE
29 #define wxIF_WINT_T_TYPE(x) x
30 #else // !wxWINT_T_IS_SEPARATE_TYPE
31 #define wxIF_WINT_T_TYPE(x)
32 #endif // wxWINT_T_IS_SEPARATE_TYPE/!wxWINT_T_IS_SEPARATE_TYPE
34 // wchar_t seems to be defined as unsigned short by all Windows compilers but
35 // unsigned int everywhere else
37 #define wxWCHAR_T_IS_UINT
40 class WXDLLIMPEXP_BASE wxUniCharRef
;
42 // This class represents single Unicode character. It can be converted to
43 // and from char or wchar_t and implements commonly used character operations.
44 class WXDLLIMPEXP_BASE wxUniChar
47 // NB: this is not wchar_t on purpose, it needs to represent the entire
48 // Unicode code points range and wchar_t may be too small for that
49 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
50 typedef wxUint32 value_type
;
52 wxUniChar() : m_value(0) {}
54 // Create the character from 8bit character value encoded in the current
56 wxUniChar(char c
) { m_value
= From8bit(c
); }
57 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
59 // Create the character from a wchar_t character value.
60 wxUniChar(wchar_t c
) { m_value
= c
; }
61 #ifdef wxWINT_T_IS_SEPARATE_TYPE
62 wxUniChar(wint_t c
) { m_value
= c
; }
65 wxUniChar(int c
) { m_value
= c
; }
67 wxUniChar(const wxUniCharRef
& c
);
69 // Returns Unicode code point value of the character
70 value_type
GetValue() const { return m_value
; }
72 // Conversions to char and wchar_t types: all of those are needed to be
73 // able to pass wxUniChars to verious standard narrow and wide character
75 operator char() const { return To8bit(m_value
); }
76 operator wchar_t() const { return m_value
; }
77 operator int() const { return m_value
; }
78 #ifndef wxWCHAR_T_IS_UINT
79 operator unsigned int() const { return m_value
; }
82 // More conversions needed for other standard functions: uchar is for VC++
83 // _mbxxx() ones (to which toxxx/isxxx() are mapped when _MBCS is defined)
84 // and some wide character functions take wint_t which happens to be the
85 // same as wchar_t for Windows compilers but not for g++ (except for the
86 // special Apple version)
87 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
88 #ifdef wxWINT_T_IS_SEPARATE_TYPE
89 operator wint_t() const { return m_value
; }
92 // We need this operator for the "*p" part of expressions like "for (
93 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
94 // compilation would fail without it because the conversion to bool would
95 // be ambiguous (there are all these int types conversions...). (And adding
96 // operator unspecified_bool_type() would only makes the ambiguity worse.)
97 operator bool() const { return m_value
!= 0; }
98 bool operator!() const { return !((bool)*this); }
99 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
100 defined(__DIGITALMARS__) || defined(__BORLANDC__)
101 // We need this for VC++ < 8 or DigitalMars and expressions like
103 bool operator&&(bool v
) const { return (bool)*this && v
; }
106 // Assignment operators:
107 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
108 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
109 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
110 wxUniChar
& operator=(int c
) { m_value
= c
; return *this; }
111 #ifdef wxWINT_T_IS_SEPARATE_TYPE
112 wxUniChar
& operator=(wint_t c
) { m_value
= c
; return *this; }
115 // Comparison operators:
117 // define the given comparison operator for all the types
118 #define wxDEFINE_UNICHAR_OPERATOR(op) \
119 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
120 bool operator op(char c) const { return m_value op From8bit(c); } \
121 bool operator op(wchar_t c) const { return m_value op (value_type)c; } \
122 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return m_value op (value_type)c; } )
124 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
126 #undef wxDEFINE_UNICHAR_OPERATOR
128 // this is needed for expressions like 'Z'-c
129 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
130 int operator-(char c
) const { return m_value
- From8bit(c
); }
131 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
132 #ifdef wxWINT_T_IS_SEPARATE_TYPE
133 int operator-(wint_t c
) const { return m_value
- (value_type
)c
; }
137 static value_type
From8bit(char c
);
138 static char To8bit(value_type c
);
145 // Writeable reference to a character in wxString.
147 // This class can be used in the same way wxChar is used, except that changing
148 // its value updates the underlying string object.
149 class WXDLLIMPEXP_BASE wxUniCharRef
152 typedef wxStringImpl::iterator iterator
;
154 // create the reference
155 wxUniCharRef(iterator pos
) : m_pos(pos
) {}
158 // NB: we have to make this public, because we don't have wxString
159 // declaration available here and so can't declare wxString::iterator
160 // as friend; so at least don't use a ctor but a static function
161 // that must be used explicitly (this is more than using 'explicit'
162 // keyword on ctor!):
163 static wxUniCharRef
CreateForString(iterator pos
)
164 { return wxUniCharRef(pos
); }
166 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
168 // Assignment operators:
169 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
175 wxUniCharRef
& operator=(const wxUniChar
& c
)
181 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
182 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
183 wxUniCharRef
& operator=(int c
) { return *this = wxUniChar(c
); }
184 #ifdef wxWINT_T_IS_SEPARATE_TYPE
185 wxUniCharRef
& operator=(wint_t c
) { return *this = wxUniChar(c
); }
188 // Conversions to the same types as wxUniChar is convertible too:
189 operator char() const { return UniChar(); }
190 operator int() const { return UniChar(); }
191 operator unsigned char() const { return UniChar(); }
192 operator wchar_t() const { return UniChar(); }
193 #ifdef wxWINT_T_IS_SEPARATE_TYPE
194 operator wint_t() const { return UniChar(); }
196 #ifndef wxWCHAR_T_IS_UINT
197 operator unsigned int() const { return UniChar(); }
200 // see wxUniChar::operator bool etc. for explanation
201 operator bool() const { return (bool)UniChar(); }
202 bool operator!() const { return !UniChar(); }
203 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
204 defined(__DIGITALMARS__) || defined(__BORLANDC__)
205 bool operator&&(bool v
) const { return UniChar() && v
; }
208 // Comparison operators:
209 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
210 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
211 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
212 bool operator op(char c) const { return UniChar() op c; } \
213 bool operator op(wchar_t c) const { return UniChar() op c; } \
214 wxIF_WINT_T_TYPE( bool operator op(wint_t c) const { return UniChar() op c; } )
216 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
218 #undef wxDEFINE_UNICHARREF_OPERATOR
220 // for expressions like c-'A':
221 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
222 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
223 int operator-(char c
) const { return UniChar() - c
; }
224 int operator-(wchar_t c
) const { return UniChar() - c
; }
225 #ifdef wxWINT_T_IS_SEPARATE_TYPE
226 int operator-(wint_t c
) const { return UniChar() - c
; }
230 wxUniChar
UniChar() const { return *m_pos
; }
231 friend class WXDLLIMPEXP_BASE wxUniChar
;
234 // pointer to the character in string
238 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
240 m_value
= c
.UniChar().m_value
;
243 // Comparison operators for the case when wxUniChar(Ref) is the second operand
244 // implemented in terms of member comparison functions
246 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
248 wxDEFINE_COMPARISONS(char, const wxUniChar
&, wxCMP_REVERSE
)
249 wxDEFINE_COMPARISONS(char, const wxUniCharRef
&, wxCMP_REVERSE
)
251 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar
&, wxCMP_REVERSE
)
252 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef
&, wxCMP_REVERSE
)
254 #ifdef wxWINT_T_IS_SEPARATE_TYPE
255 wxDEFINE_COMPARISONS(wint_t, const wxUniChar
&, wxCMP_REVERSE
)
256 wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef
&, wxCMP_REVERSE
)
259 wxDEFINE_COMPARISONS(const wxUniChar
&, const wxUniCharRef
&, wxCMP_REVERSE
)
263 // for expressions like c-'A':
264 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
265 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
266 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
267 #ifdef wxWINT_T_IS_SEPARATE_TYPE
268 inline int operator-(wint_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
271 #endif /* _WX_UNICHAR_H_ */