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 wxUniChar(int c
) { m_value
= c
; }
41 wxUniChar(const wxUniCharRef
& c
);
43 // Returns Unicode code point value of the character
44 value_type
GetValue() const { return m_value
; }
46 // Casts to char and wchar_t types:
47 operator char() const { return To8bit(m_value
); }
48 operator unsigned char() const { return (unsigned char)To8bit(m_value
); }
49 operator wchar_t() const { return m_value
; }
50 operator int() const { return m_value
; }
51 operator unsigned int() const { return m_value
; }
53 // We need this operator for the "*p" part of expressions like "for (
54 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
55 // compilation would fail without it because the conversion to bool would
56 // be ambiguous (there are all these int types conversions...). (And adding
57 // operator unspecified_bool_type() would only makes the ambiguity worse.)
58 operator bool() const { return m_value
!= 0; }
59 bool operator!() const { return !((bool)*this); }
60 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
61 defined(__DIGITALMARS__) || defined(__BORLANDC__)
62 // We need this for VC++ < 8 or DigitalMars and expressions like
64 bool operator&&(bool v
) const { return (bool)*this && v
; }
67 // Assignment operators:
68 wxUniChar
& operator=(const wxUniChar
& c
) { m_value
= c
.m_value
; return *this; }
69 wxUniChar
& operator=(char c
) { m_value
= From8bit(c
); return *this; }
70 wxUniChar
& operator=(wchar_t c
) { m_value
= c
; return *this; }
72 // Comparison operators:
74 // define the given comparison operator for all the types
75 #define wxDEFINE_UNICHAR_OPERATOR(op) \
76 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
77 bool operator op(char c) const { return m_value op From8bit(c); } \
78 bool operator op(wchar_t c) const { return m_value op (value_type)c; }
80 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR
)
82 #undef wxDEFINE_UNICHAR_OPERATOR
84 // this is needed for expressions like 'Z'-c
85 int operator-(const wxUniChar
& c
) const { return m_value
- c
.m_value
; }
86 int operator-(char c
) const { return m_value
- From8bit(c
); }
87 int operator-(wchar_t c
) const { return m_value
- (value_type
)c
; }
90 static value_type
From8bit(char c
);
91 static char To8bit(value_type c
);
98 // Writeable reference to a character in wxString.
100 // This class can be used in the same way wxChar is used, except that changing
101 // its value updates the underlying string object.
102 class WXDLLIMPEXP_BASE wxUniCharRef
105 // create the reference
106 // FIXME-UTF8: the interface will need changes for UTF-8 build
107 wxUniCharRef(wxChar
*pos
) : m_pos(pos
) {}
110 // NB: we have to make this public, because we don't have wxString
111 // declaration available here and so can't declare wxString::iterator
112 // as friend; so at least don't use a ctor but a static function
113 // that must be used explicitly (this is more than using 'explicit'
114 // keyword on ctor!):
116 // FIXME-UTF8: the interface will need changes for UTF-8 build
117 static wxUniCharRef
CreateForString(wxChar
*pos
)
118 { return wxUniCharRef(pos
); }
120 wxUniChar::value_type
GetValue() const { return UniChar().GetValue(); }
122 // Assignment operators:
123 wxUniCharRef
& operator=(const wxUniCharRef
& c
)
129 wxUniCharRef
& operator=(const wxUniChar
& c
)
135 wxUniCharRef
& operator=(char c
) { return *this = wxUniChar(c
); }
136 wxUniCharRef
& operator=(wchar_t c
) { return *this = wxUniChar(c
); }
138 // Casts to wxUniChar type:
139 operator char() const { return UniChar(); }
140 operator unsigned char() const { return UniChar(); }
141 operator wchar_t() const { return UniChar(); }
142 operator int() const { return UniChar(); }
143 operator unsigned int() const { return UniChar(); }
145 // see wxUniChar::operator bool etc. for explanation
146 operator bool() const { return (bool)UniChar(); }
147 bool operator!() const { return !UniChar(); }
148 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
149 defined(__DIGITALMARS__) || defined(__BORLANDC__)
150 bool operator&&(bool v
) const { return UniChar() && v
; }
153 // Comparison operators:
154 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
155 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
156 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
157 bool operator op(char c) const { return UniChar() op c; } \
158 bool operator op(wchar_t c) const { return UniChar() op c; }
160 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR
)
162 #undef wxDEFINE_UNICHARREF_OPERATOR
164 // for expressions like c-'A':
165 int operator-(const wxUniCharRef
& c
) const { return UniChar() - c
.UniChar(); }
166 int operator-(const wxUniChar
& c
) const { return UniChar() - c
; }
167 int operator-(char c
) const { return UniChar() - c
; }
168 int operator-(wchar_t c
) const { return UniChar() - c
; }
171 wxUniChar
UniChar() const { return *m_pos
; }
172 friend class WXDLLIMPEXP_BASE wxUniChar
;
175 // pointer to the character in string
179 inline wxUniChar::wxUniChar(const wxUniCharRef
& c
)
181 m_value
= c
.UniChar().m_value
;
184 // Comparison operators for the case when wxUniChar(Ref) is the second operand
185 // implemented in terms of member comparison functions
187 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
189 wxDEFINE_COMPARISONS(char, const wxUniChar
&, wxCMP_REVERSE
)
190 wxDEFINE_COMPARISONS(char, const wxUniCharRef
&, wxCMP_REVERSE
)
192 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar
&, wxCMP_REVERSE
)
193 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef
&, wxCMP_REVERSE
)
195 wxDEFINE_COMPARISONS(const wxUniChar
&, const wxUniCharRef
&, wxCMP_REVERSE
)
199 // for expressions like c-'A':
200 inline int operator-(char c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
201 inline int operator-(wchar_t c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
202 inline int operator-(const wxUniChar
& c1
, const wxUniCharRef
& c2
) { return -(c2
- c1
); }
204 #endif /* _WX_UNICHAR_H_ */