re-enabled using std::string for wxStringImpl if wxUSE_STL again
[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 // 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"
20
21 class WXDLLIMPEXP_BASE wxUniCharRef;
22
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
26 {
27 public:
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;
32
33 wxUniChar() : m_value(0) {}
34
35 // Create the character from 8bit character value encoded in the current
36 // locale's charset.
37 wxUniChar(char c) { m_value = From8bit(c); }
38 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
39
40 // Create the character from a wchar_t character value.
41 wxUniChar(wchar_t c) { m_value = c; }
42
43 wxUniChar(int c) { m_value = c; }
44
45 wxUniChar(const wxUniCharRef& c);
46
47 // Returns Unicode code point value of the character
48 value_type GetValue() const { return m_value; }
49
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
52 // functions
53 operator char() const { return To8bit(m_value); }
54 operator wchar_t() const { return m_value; }
55 operator int() const { return m_value; }
56
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; }
65 #endif
66
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
77 // "str[0] && *p":
78 bool operator&&(bool v) const { return (bool)*this && v; }
79 #endif
80
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; }
85
86 // Comparison operators:
87
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; }
93
94 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
95
96 #undef wxDEFINE_UNICHAR_OPERATOR
97
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; }
102
103 private:
104 static value_type From8bit(char c);
105 static char To8bit(value_type c);
106
107 private:
108 value_type m_value;
109 };
110
111
112 // Writeable reference to a character in wxString.
113 //
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
117 {
118 private:
119 typedef wxStringImpl::iterator iterator;
120
121 // create the reference
122 wxUniCharRef(iterator pos) : m_pos(pos) {}
123
124 public:
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); }
132
133 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
134
135 // Assignment operators:
136 wxUniCharRef& operator=(const wxUniCharRef& c)
137 {
138 *m_pos = *c.m_pos;
139 return *this;
140 };
141
142 wxUniCharRef& operator=(const wxUniChar& c)
143 {
144 *m_pos = c;
145 return *this;
146 };
147
148 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
149 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
150
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(); }
158 #endif
159
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; }
166 #endif
167
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; }
174
175 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
176
177 #undef wxDEFINE_UNICHARREF_OPERATOR
178
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; }
184
185 private:
186 wxUniChar UniChar() const { return *m_pos; }
187 friend class WXDLLIMPEXP_BASE wxUniChar;
188
189 private:
190 // pointer to the character in string
191 iterator m_pos;
192 };
193
194 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
195 {
196 m_value = c.UniChar().m_value;
197 }
198
199 // Comparison operators for the case when wxUniChar(Ref) is the second operand
200 // implemented in terms of member comparison functions
201
202 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
203
204 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
205 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
206
207 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
208 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
209
210 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
211
212 #undef wxCMP_REVERSE
213
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); }
218
219 #endif /* _WX_UNICHAR_H_ */