added conversions to unsigned short: this fixes compilation for some platforms where...
[wxWidgets.git] / include / wx / unichar.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unichar.h
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
5 // Created: 2007-03-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNICHAR_H_
12 #define _WX_UNICHAR_H_
13
14 #include "wx/defs.h"
15 #include "wx/chartype.h"
16
17 class WXDLLIMPEXP_BASE wxUniCharRef;
18
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
22 {
23 public:
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;
28
29 wxUniChar() : m_value(0) {}
30
31 // Create the character from 8bit character value encoded in the current
32 // locale's charset.
33 wxUniChar(char c) { m_value = From8bit(c); }
34 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
35
36 // Create the character from a wchar_t character value.
37 wxUniChar(wchar_t c) { m_value = c; }
38
39 wxUniChar(int c) { m_value = c; }
40
41 wxUniChar(const wxUniCharRef& c);
42
43 // Returns Unicode code point value of the character
44 value_type GetValue() const { return m_value; }
45
46 // Conversions to char and wchar_t types: all of those are needed to be
47 // able to pass wxUniChars to verious standard narrow and wide character
48 // functions
49 operator char() const { return To8bit(m_value); }
50 operator wchar_t() const { return m_value; }
51 operator int() const { return m_value; }
52
53 // More conversions needed for other standard functions: uchar is for VC++
54 // _mbxxx() ones (to which toxxx/isxxx() are mapped when _MBCS is defined)
55 // and wint_t is either uint or ushort and it's easier for us to just
56 // define both at this stage than have a separate test for what wint_t is
57 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
58 operator unsigned short() const { return m_value; }
59 operator unsigned int() const { return m_value; }
60
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
71 // "str[0] && *p":
72 bool operator&&(bool v) const { return (bool)*this && v; }
73 #endif
74
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
80 // Comparison operators:
81
82 // define the given comparison operator for all the types
83 #define wxDEFINE_UNICHAR_OPERATOR(op) \
84 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
85 bool operator op(char c) const { return m_value op From8bit(c); } \
86 bool operator op(wchar_t c) const { return m_value op (value_type)c; }
87
88 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
89
90 #undef wxDEFINE_UNICHAR_OPERATOR
91
92 // this is needed for expressions like 'Z'-c
93 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
94 int operator-(char c) const { return m_value - From8bit(c); }
95 int operator-(wchar_t c) const { return m_value - (value_type)c; }
96
97 private:
98 static value_type From8bit(char c);
99 static char To8bit(value_type c);
100
101 private:
102 value_type m_value;
103 };
104
105
106 // Writeable reference to a character in wxString.
107 //
108 // This class can be used in the same way wxChar is used, except that changing
109 // its value updates the underlying string object.
110 class WXDLLIMPEXP_BASE wxUniCharRef
111 {
112 private:
113 // create the reference
114 // FIXME-UTF8: the interface will need changes for UTF-8 build
115 wxUniCharRef(wxChar *pos) : m_pos(pos) {}
116
117 public:
118 // NB: we have to make this public, because we don't have wxString
119 // declaration available here and so can't declare wxString::iterator
120 // as friend; so at least don't use a ctor but a static function
121 // that must be used explicitly (this is more than using 'explicit'
122 // keyword on ctor!):
123 //
124 // FIXME-UTF8: the interface will need changes for UTF-8 build
125 static wxUniCharRef CreateForString(wxChar *pos)
126 { return wxUniCharRef(pos); }
127
128 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
129
130 // Assignment operators:
131 wxUniCharRef& operator=(const wxUniCharRef& c)
132 {
133 *m_pos = *c.m_pos;
134 return *this;
135 };
136
137 wxUniCharRef& operator=(const wxUniChar& c)
138 {
139 *m_pos = c;
140 return *this;
141 };
142
143 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
144 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
145
146 // Conversions to the same types as wxUniChar is convertible too:
147 operator char() const { return UniChar(); }
148 operator wchar_t() const { return UniChar(); }
149 operator int() const { return UniChar(); }
150 operator unsigned char() const { return UniChar(); }
151 operator unsigned short() const { return UniChar(); }
152 operator unsigned int() const { return UniChar(); }
153
154 // see wxUniChar::operator bool etc. for explanation
155 operator bool() const { return (bool)UniChar(); }
156 bool operator!() const { return !UniChar(); }
157 #if (defined(__VISUALC__) && __VISUALC__ < 1400) || \
158 defined(__DIGITALMARS__) || defined(__BORLANDC__)
159 bool operator&&(bool v) const { return UniChar() && v; }
160 #endif
161
162 // Comparison operators:
163 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
164 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
165 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
166 bool operator op(char c) const { return UniChar() op c; } \
167 bool operator op(wchar_t c) const { return UniChar() op c; }
168
169 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
170
171 #undef wxDEFINE_UNICHARREF_OPERATOR
172
173 // for expressions like c-'A':
174 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
175 int operator-(const wxUniChar& c) const { return UniChar() - c; }
176 int operator-(char c) const { return UniChar() - c; }
177 int operator-(wchar_t c) const { return UniChar() - c; }
178
179 private:
180 wxUniChar UniChar() const { return *m_pos; }
181 friend class WXDLLIMPEXP_BASE wxUniChar;
182
183 private:
184 // pointer to the character in string
185 wxChar *m_pos;
186 };
187
188 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
189 {
190 m_value = c.UniChar().m_value;
191 }
192
193 // Comparison operators for the case when wxUniChar(Ref) is the second operand
194 // implemented in terms of member comparison functions
195
196 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
197
198 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
199 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
200
201 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
202 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
203
204 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
205
206 #undef wxCMP_REVERSE
207
208 // for expressions like c-'A':
209 inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
210 inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
211 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
212
213 #endif /* _WX_UNICHAR_H_ */