remove wxWINT_T_IS_TYPEDEF as it seems wint_t is always a typedef (although it can...
[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 // 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; }
52
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
63 // "str[0] && *p":
64 bool operator&&(bool v) const { return (bool)*this && v; }
65 #endif
66
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; }
71
72 // Comparison operators:
73
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; }
79
80 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
81
82 #undef wxDEFINE_UNICHAR_OPERATOR
83
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; }
88
89 private:
90 static value_type From8bit(char c);
91 static char To8bit(value_type c);
92
93 private:
94 value_type m_value;
95 };
96
97
98 // Writeable reference to a character in wxString.
99 //
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
103 {
104 private:
105 // create the reference
106 // FIXME-UTF8: the interface will need changes for UTF-8 build
107 wxUniCharRef(wxChar *pos) : m_pos(pos) {}
108
109 public:
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!):
115 //
116 // FIXME-UTF8: the interface will need changes for UTF-8 build
117 static wxUniCharRef CreateForString(wxChar *pos)
118 { return wxUniCharRef(pos); }
119
120 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
121
122 // Assignment operators:
123 wxUniCharRef& operator=(const wxUniCharRef& c)
124 {
125 *m_pos = *c.m_pos;
126 return *this;
127 };
128
129 wxUniCharRef& operator=(const wxUniChar& c)
130 {
131 *m_pos = c;
132 return *this;
133 };
134
135 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
136 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
137
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(); }
144
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; }
151 #endif
152
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; }
159
160 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
161
162 #undef wxDEFINE_UNICHARREF_OPERATOR
163
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; }
169
170 private:
171 wxUniChar UniChar() const { return *m_pos; }
172 friend class WXDLLIMPEXP_BASE wxUniChar;
173
174 private:
175 // pointer to the character in string
176 wxChar *m_pos;
177 };
178
179 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
180 {
181 m_value = c.UniChar().m_value;
182 }
183
184 // Comparison operators for the case when wxUniChar(Ref) is the second operand
185 // implemented in terms of member comparison functions
186
187 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
188
189 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
190 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
191
192 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
193 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
194
195 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
196
197 #undef wxCMP_REVERSE
198
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); }
203
204 #endif /* _WX_UNICHAR_H_ */