added macros to avoid code repetition when defining comparison operators; use them...
[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 #ifndef wxWINT_T_IS_TYPEDEF
40 // Create the character from a wint_t character value.
41 wxUniChar(wint_t c) { m_value = c; }
42 #endif
43
44 wxUniChar(int c) { m_value = c; }
45
46 wxUniChar(const wxUniCharRef& c);
47
48 // Returns Unicode code point value of the character
49 value_type GetValue() const { return m_value; }
50
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; }
57 #endif
58 operator int() 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 #ifndef wxWINT_T_IS_TYPEDEF
80 wxUniChar& operator=(wint_t c) { m_value = c; return *this; }
81 #endif
82
83 // Comparison operators:
84
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; }
90
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; }
97 #endif
98
99 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
100
101 #undef wxDEFINE_UNICHAR_OPERATOR_NO_WINT
102 #undef wxDEFINE_UNICHAR_OPERATOR
103
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; }
110 #endif
111
112 private:
113 static value_type From8bit(char c);
114 static char To8bit(value_type c);
115
116 private:
117 value_type m_value;
118 };
119
120
121 // Writeable reference to a character in wxString.
122 //
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
126 {
127 private:
128 // create the reference
129 // FIXME-UTF8: the interface will need changes for UTF-8 build
130 wxUniCharRef(wxChar *pos) : m_pos(pos) {}
131
132 public:
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!):
138 //
139 // FIXME-UTF8: the interface will need changes for UTF-8 build
140 static wxUniCharRef CreateForString(wxChar *pos)
141 { return wxUniCharRef(pos); }
142
143 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
144
145 // Assignment operators:
146 wxUniCharRef& operator=(const wxUniCharRef& c)
147 {
148 *m_pos = *c.m_pos;
149 return *this;
150 };
151
152 wxUniCharRef& operator=(const wxUniChar& c)
153 {
154 *m_pos = c;
155 return *this;
156 };
157
158 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
159 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
160
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(); }
167 #endif
168 operator int() const { return UniChar(); }
169 operator unsigned int() const { return UniChar(); }
170
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; }
177 #endif
178
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; }
185
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; }
192 #endif
193
194 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
195
196 #undef wxDEFINE_UNICHARREF_OPERATOR_NO_WINT
197 #undef wxDEFINE_UNICHARREF_OPERATOR
198
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; }
206 #endif
207
208 private:
209 wxUniChar UniChar() const { return *m_pos; }
210 friend class WXDLLIMPEXP_BASE wxUniChar;
211
212 private:
213 // pointer to the character in string
214 wxChar *m_pos;
215 };
216
217 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
218 {
219 m_value = c.UniChar().m_value;
220 }
221
222 // Comparison operators for the case when wxUniChar(Ref) is the second operand
223 // implemented in terms of member comparison functions
224
225 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
226
227 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
228 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
229
230 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
231 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
232
233 #ifndef wxWINT_T_IS_TYPEDEF
234 wxDEFINE_COMPARISONS(wint_t, const wxUniChar&, wxCMP_REVERSE)
235 wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef&, wxCMP_REVERSE)
236 #endif
237
238 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
239
240 #undef wxCMP_REVERSE
241
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); }
247 #endif
248 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
249
250 #endif /* _WX_UNICHAR_H_ */