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 // NB: this header is included from string.h as well, but from the place
18 // where wxStringImpl is already declared and that's all we need
19 #include "wx/string.h"
21 class WXDLLIMPEXP_BASE wxUniCharRef
;
23 // This class represents single Unicode character. It can be converted to
24 // and from char or wchar_t and implements commonly used character operations.
25 class WXDLLIMPEXP_BASE wxUniChar
28 // NB: this is not wchar_t on purpose, it needs to represent the entire
29 // Unicode code points range and wchar_t may be too small for that
30 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
31 typedef wxUint32 value_type
;
33 wxUniChar() : m_value(0) {}
35 // Create the character from 8bit character value encoded in the current
37 wxUniChar(char c
) { m_value
= From8bit(c
); }
38 wxUniChar(unsigned char c
) { m_value
= From8bit((char)c
); }
40 // Create the character from a wchar_t character value.
41 wxUniChar(wchar_t c
) { m_value
= c
; }
43 wxUniChar(int c
) { m_value
= c
; }
45 wxUniChar(const wxUniCharRef
& c
);
47 // Returns Unicode code point value of the character
48 value_type
GetValue() const { return m_value
; }
50 // Conversions to char and wchar_t types: all of those are needed to be
51 // able to pass wxUniChars to verious standard narrow and wide character
53 operator char() const { return To8bit(m_value
); }
54 operator wchar_t() const { return m_value
; }
55 operator int() const { return m_value
; }
57 // More conversions needed for other standard functions: uchar is for VC++
58 // _mbxxx() ones (to which toxxx/isxxx() are mapped when _MBCS is defined)
59 // and some wide character functions take wint_t which happens to be the
60 // same as wchar_t for Windows compilers but not for g++ (except for the
61 // special Apple version)
62 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
63 #if defined(__GNUC__) && !defined(__DARWIN__)
64 operator wint_t() const { return m_value
; }
67 // We need this operator for the "*p" part of expressions like "for (
68 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
69 // compilation would fail without it because the conversion to bool would
70 // be ambiguous (there are all these int types conversions...). (And adding
71 // operator unspecified_bool_type() would only makes the ambiguity worse.)
72 operator bool() const { return m_value
!= 0; }
73 bool operator!() const { return !((bool)*this); }
74 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
75 defined(__DIGITALMARS__) || defined(__BORLANDC__)
76 // We need this for VC++ < 8 or DigitalMars and expressions like
78 bool operator&&(bool v
) const { return (bool)*this && v
; }
81 // Assignment operators:
82 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
83 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
84 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
86 // Comparison operators:
88 // define the given comparison operator for all the types
89 #define wxDEFINE_UNICHAR_OPERATOR(op) \
90 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
91 bool operator op(char c) const { return m_value op From8bit(c); } \
92 bool operator op(wchar_t c) const { return m_value op (value_type)c; }
94 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
96 #undef wxDEFINE_UNICHAR_OPERATOR
98 // this is needed for expressions like 'Z'-c
99 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
100 int operator-(char c
) const { return m_value
- From8bit(c
); }
101 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
104 static value_type
From8bit(char c
);
105 static char To8bit(value_type c
);
112 // Writeable reference to a character in wxString.
114 // This class can be used in the same way wxChar is used, except that changing
115 // its value updates the underlying string object.
116 class WXDLLIMPEXP_BASE wxUniCharRef
119 typedef wxStringImpl::iterator iterator
;
121 // create the reference
122 wxUniCharRef(iterator pos
) : m_pos(pos
) {}
125 // NB: we have to make this public, because we don't have wxString
126 // declaration available here and so can't declare wxString::iterator
127 // as friend; so at least don't use a ctor but a static function
128 // that must be used explicitly (this is more than using 'explicit'
129 // keyword on ctor!):
130 static wxUniCharRef
CreateForString(iterator pos
)
131 { return wxUniCharRef(pos
); }
133 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
135 // Assignment operators:
136 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
142 wxUniCharRef
& operator=(const wxUniChar
& c
)
148 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
149 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
151 // Conversions to the same types as wxUniChar is convertible too:
152 operator char() const { return UniChar(); }
153 operator wchar_t() const { return UniChar(); }
154 operator int() const { return UniChar(); }
155 operator unsigned char() const { return UniChar(); }
156 #if defined(__GNUC__) && !defined(__DARWIN__)
157 operator wint_t() const { return UniChar(); }
160 // see wxUniChar::operator bool etc. for explanation
161 operator bool() const { return (bool)UniChar(); }
162 bool operator!() const { return !UniChar(); }
163 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
164 defined(__DIGITALMARS__) || defined(__BORLANDC__)
165 bool operator&&(bool v
) const { return UniChar() && v
; }
168 // Comparison operators:
169 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
170 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
171 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
172 bool operator op(char c) const { return UniChar() op c; } \
173 bool operator op(wchar_t c) const { return UniChar() op c; }
175 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
177 #undef wxDEFINE_UNICHARREF_OPERATOR
179 // for expressions like c-'A':
180 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
181 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
182 int operator-(char c
) const { return UniChar() - c
; }
183 int operator-(wchar_t c
) const { return UniChar() - c
; }
186 wxUniChar
UniChar() const { return *m_pos
; }
187 friend class WXDLLIMPEXP_BASE wxUniChar
;
190 // pointer to the character in string
194 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
196 m_value
= c
.UniChar().m_value
;
199 // Comparison operators for the case when wxUniChar(Ref) is the second operand
200 // implemented in terms of member comparison functions
202 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
204 wxDEFINE_COMPARISONS(char, const wxUniChar
&, wxCMP_REVERSE
)
205 wxDEFINE_COMPARISONS(char, const wxUniCharRef
&, wxCMP_REVERSE
)
207 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar
&, wxCMP_REVERSE
)
208 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef
&, wxCMP_REVERSE
)
210 wxDEFINE_COMPARISONS(const wxUniChar
&, const wxUniCharRef
&, wxCMP_REVERSE
)
214 // for expressions like c-'A':
215 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
216 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
217 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
219 #endif /* _WX_UNICHAR_H_ */