]> git.saurik.com Git - wxWidgets.git/blame - include/wx/unichar.h
added support for binary data to wxConfig (slightly modified patch 1736788)
[wxWidgets.git] / include / wx / unichar.h
CommitLineData
e3f6cbd9
VS
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
dbecee02 14#include "wx/defs.h"
e3f6cbd9 15#include "wx/chartype.h"
a7ea63e2 16#include "wx/stringimpl.h"
a962cdf4 17
b5dbe15d
VS
18class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
19class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
e3f6cbd9
VS
20
21// This class represents single Unicode character. It can be converted to
22// and from char or wchar_t and implements commonly used character operations.
23class WXDLLIMPEXP_BASE wxUniChar
24{
25public:
26 // NB: this is not wchar_t on purpose, it needs to represent the entire
27 // Unicode code points range and wchar_t may be too small for that
28 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
dbecee02 29 typedef wxUint32 value_type;
e3f6cbd9
VS
30
31 wxUniChar() : m_value(0) {}
32
33 // Create the character from 8bit character value encoded in the current
34 // locale's charset.
35 wxUniChar(char c) { m_value = From8bit(c); }
9f1b1b78 36 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
e3f6cbd9
VS
37
38 // Create the character from a wchar_t character value.
c5cf8663 39#if wxWCHAR_T_IS_REAL_TYPE
e3f6cbd9 40 wxUniChar(wchar_t c) { m_value = c; }
436bb9f5 41#endif
e3f6cbd9 42
e3f6cbd9 43 wxUniChar(int c) { m_value = c; }
21f0762a 44 wxUniChar(unsigned int c) { m_value = c; }
4e5b3a4e 45 wxUniChar(long int c) { m_value = c; }
21f0762a
VS
46 wxUniChar(unsigned long int c) { m_value = c; }
47 wxUniChar(short int c) { m_value = c; }
48 wxUniChar(unsigned short int c) { m_value = c; }
e3f6cbd9
VS
49
50 wxUniChar(const wxUniCharRef& c);
51
52 // Returns Unicode code point value of the character
d1b7ed67 53 value_type GetValue() const { return m_value; }
e3f6cbd9 54
1fc10687
VS
55#if wxUSE_UNICODE_UTF8
56 // buffer for single UTF-8 character
57 struct Utf8CharBuffer
58 {
59 char data[5];
60 operator const char*() const { return data; }
61 };
62
63 // returns the character encoded as UTF-8
64 // (NB: implemented in stringops.cpp)
65 Utf8CharBuffer AsUTF8() const;
66#endif // wxUSE_UNICODE_UTF8
67
81727065
VS
68 // Returns true if the character is an ASCII character:
69 bool IsAscii() const { return m_value < 0x80; }
70
2a686bd3
VZ
71 // Conversions to char and wchar_t types: all of those are needed to be
72 // able to pass wxUniChars to verious standard narrow and wide character
73 // functions
e3f6cbd9 74 operator char() const { return To8bit(m_value); }
21f0762a 75 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
c5cf8663 76#if wxWCHAR_T_IS_REAL_TYPE
d4e59879 77 operator wchar_t() const { return (wchar_t)m_value; }
21f0762a 78#endif
d4e59879
VS
79 operator int() const { return (int)m_value; }
80 operator unsigned int() const { return (unsigned int)m_value; }
81 operator long int() const { return (long int)m_value; }
82 operator unsigned long int() const { return (unsigned long)m_value; }
83 operator short int() const { return (short int)m_value; }
84 operator unsigned short int() const { return (unsigned short int)m_value; }
e3f6cbd9
VS
85
86 // We need this operator for the "*p" part of expressions like "for (
87 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
88 // compilation would fail without it because the conversion to bool would
89 // be ambiguous (there are all these int types conversions...). (And adding
90 // operator unspecified_bool_type() would only makes the ambiguity worse.)
91 operator bool() const { return m_value != 0; }
92 bool operator!() const { return !((bool)*this); }
8019a673
VZ
93
94 // And this one is needed by some (not all, but not using ifdefs makes the
95 // code easier) compilers to parse "str[0] && *p" successfully
e3f6cbd9 96 bool operator&&(bool v) const { return (bool)*this && v; }
e3f6cbd9
VS
97
98 // Assignment operators:
99 wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; }
c2d122aa 100 wxUniChar& operator=(const wxUniCharRef& c);
e3f6cbd9 101 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
db18be57 102 wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
c5cf8663 103#if wxWCHAR_T_IS_REAL_TYPE
e3f6cbd9 104 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
21f0762a 105#endif
171d6132 106 wxUniChar& operator=(int c) { m_value = c; return *this; }
21f0762a 107 wxUniChar& operator=(unsigned int c) { m_value = c; return *this; }
4e5b3a4e 108 wxUniChar& operator=(long int c) { m_value = c; return *this; }
21f0762a
VS
109 wxUniChar& operator=(unsigned long int c) { m_value = c; return *this; }
110 wxUniChar& operator=(short int c) { m_value = c; return *this; }
111 wxUniChar& operator=(unsigned short int c) { m_value = c; return *this; }
e3f6cbd9 112
fb52c2b6 113 // Comparison operators:
e3f6cbd9 114
fb52c2b6 115 // define the given comparison operator for all the types
98c4eb39 116#define wxDEFINE_UNICHAR_OPERATOR(op) \
fb52c2b6
VZ
117 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
118 bool operator op(char c) const { return m_value op From8bit(c); } \
db18be57 119 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
21f0762a 120 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
db18be57 121 bool operator op(int c) const { return m_value op (value_type)c; } \
21f0762a
VS
122 bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
123 bool operator op(short int c) const { return m_value op (value_type)c; } \
124 bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
4e5b3a4e 125 bool operator op(long int c) const { return m_value op (value_type)c; } \
21f0762a 126 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
e3f6cbd9 127
fb52c2b6 128 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
e3f6cbd9 129
fb52c2b6 130#undef wxDEFINE_UNICHAR_OPERATOR
e3f6cbd9 131
fb52c2b6 132 // this is needed for expressions like 'Z'-c
e3f6cbd9
VS
133 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
134 int operator-(char c) const { return m_value - From8bit(c); }
db18be57 135 int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
d1b7ed67 136 int operator-(wchar_t c) const { return m_value - (value_type)c; }
21f0762a 137
e3f6cbd9
VS
138
139private:
d1b7ed67
VS
140 static value_type From8bit(char c);
141 static char To8bit(value_type c);
e3f6cbd9
VS
142
143private:
d1b7ed67 144 value_type m_value;
e3f6cbd9
VS
145};
146
147
148// Writeable reference to a character in wxString.
149//
150// This class can be used in the same way wxChar is used, except that changing
151// its value updates the underlying string object.
152class WXDLLIMPEXP_BASE wxUniCharRef
153{
154private:
a962cdf4
VS
155 typedef wxStringImpl::iterator iterator;
156
e3f6cbd9 157 // create the reference
81727065 158#if wxUSE_UNICODE_UTF8
b0c4d5d7 159 wxUniCharRef(wxStringIteratorNode& node, iterator pos) : m_node(node), m_pos(pos) {}
81727065 160#else
a962cdf4 161 wxUniCharRef(iterator pos) : m_pos(pos) {}
81727065 162#endif
e3f6cbd9
VS
163
164public:
165 // NB: we have to make this public, because we don't have wxString
166 // declaration available here and so can't declare wxString::iterator
167 // as friend; so at least don't use a ctor but a static function
168 // that must be used explicitly (this is more than using 'explicit'
169 // keyword on ctor!):
81727065 170#if wxUSE_UNICODE_UTF8
b0c4d5d7
VS
171 static wxUniCharRef CreateForString(wxStringIteratorNode& node, iterator pos)
172 { return wxUniCharRef(node, pos); }
81727065 173#else
a962cdf4 174 static wxUniCharRef CreateForString(iterator pos)
e3f6cbd9 175 { return wxUniCharRef(pos); }
81727065 176#endif
e3f6cbd9 177
d1b7ed67 178 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
1fc10687
VS
179
180#if wxUSE_UNICODE_UTF8
181 wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
182#endif // wxUSE_UNICODE_UTF8
183
bc5443ba 184 bool IsAscii() const { return UniChar().IsAscii(); }
e3f6cbd9
VS
185
186 // Assignment operators:
81727065
VS
187#if wxUSE_UNICODE_UTF8
188 wxUniCharRef& operator=(const wxUniChar& c);
189#else
190 wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
191#endif
e3f6cbd9 192
81727065
VS
193 wxUniCharRef& operator=(const wxUniCharRef& c)
194 { return *this = c.UniChar(); }
e3f6cbd9
VS
195
196 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
db18be57 197 wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); }
c5cf8663 198#if wxWCHAR_T_IS_REAL_TYPE
e3f6cbd9 199 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
21f0762a 200#endif
171d6132 201 wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
21f0762a
VS
202 wxUniCharRef& operator=(unsigned int c) { return *this = wxUniChar(c); }
203 wxUniCharRef& operator=(short int c) { return *this = wxUniChar(c); }
204 wxUniCharRef& operator=(unsigned short int c) { return *this = wxUniChar(c); }
4e5b3a4e 205 wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); }
21f0762a 206 wxUniCharRef& operator=(unsigned long int c) { return *this = wxUniChar(c); }
e3f6cbd9 207
2a686bd3 208 // Conversions to the same types as wxUniChar is convertible too:
e3f6cbd9 209 operator char() const { return UniChar(); }
2a686bd3 210 operator unsigned char() const { return UniChar(); }
c5cf8663 211#if wxWCHAR_T_IS_REAL_TYPE
436bb9f5 212 operator wchar_t() const { return UniChar(); }
c961c0cf 213#endif
21f0762a 214 operator int() const { return UniChar(); }
153bc803 215 operator unsigned int() const { return UniChar(); }
21f0762a
VS
216 operator short int() const { return UniChar(); }
217 operator unsigned short int() const { return UniChar(); }
218 operator long int() const { return UniChar(); }
219 operator unsigned long int() const { return UniChar(); }
e3f6cbd9
VS
220
221 // see wxUniChar::operator bool etc. for explanation
222 operator bool() const { return (bool)UniChar(); }
223 bool operator!() const { return !UniChar(); }
e3f6cbd9 224 bool operator&&(bool v) const { return UniChar() && v; }
e3f6cbd9 225
fb52c2b6 226 // Comparison operators:
98c4eb39 227#define wxDEFINE_UNICHARREF_OPERATOR(op) \
fb52c2b6
VZ
228 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
229 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
230 bool operator op(char c) const { return UniChar() op c; } \
db18be57 231 bool operator op(unsigned char c) const { return UniChar() op c; } \
21f0762a 232 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
db18be57 233 bool operator op(int c) const { return UniChar() op c; } \
21f0762a
VS
234 bool operator op(unsigned int c) const { return UniChar() op c; } \
235 bool operator op(short int c) const { return UniChar() op c; } \
236 bool operator op(unsigned short int c) const { return UniChar() op c; } \
4e5b3a4e 237 bool operator op(long int c) const { return UniChar() op c; } \
21f0762a 238 bool operator op(unsigned long int c) const { return UniChar() op c; }
e3f6cbd9 239
fb52c2b6 240 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
e3f6cbd9 241
fb52c2b6 242#undef wxDEFINE_UNICHARREF_OPERATOR
e3f6cbd9
VS
243
244 // for expressions like c-'A':
245 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
246 int operator-(const wxUniChar& c) const { return UniChar() - c; }
247 int operator-(char c) const { return UniChar() - c; }
db18be57 248 int operator-(unsigned char c) const { return UniChar() - c; }
e3f6cbd9 249 int operator-(wchar_t c) const { return UniChar() - c; }
e3f6cbd9
VS
250
251private:
81727065 252#if wxUSE_UNICODE_UTF8
467175ab 253 wxUniChar UniChar() const;
81727065 254#else
467175ab 255 wxUniChar UniChar() const { return *m_pos; }
81727065
VS
256#endif
257
b5dbe15d 258 friend class WXDLLIMPEXP_FWD_BASE wxUniChar;
e3f6cbd9
VS
259
260private:
81727065
VS
261 // reference to the string and pointer to the character in string
262#if wxUSE_UNICODE_UTF8
b0c4d5d7 263 wxStringIteratorNode& m_node;
81727065 264#endif
a962cdf4 265 iterator m_pos;
e3f6cbd9
VS
266};
267
268inline wxUniChar::wxUniChar(const wxUniCharRef& c)
269{
270 m_value = c.UniChar().m_value;
271}
272
c2d122aa
VS
273inline wxUniChar& wxUniChar::operator=(const wxUniCharRef& c)
274{
275 m_value = c.UniChar().m_value;
276 return *this;
277}
278
fb52c2b6
VZ
279// Comparison operators for the case when wxUniChar(Ref) is the second operand
280// implemented in terms of member comparison functions
e3f6cbd9 281
fb52c2b6 282#define wxCMP_REVERSE(c1, c2, op) c2 op c1
e3f6cbd9 283
fb52c2b6
VZ
284wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
285wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 286
fb52c2b6
VZ
287wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
288wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 289
fb52c2b6 290wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 291
fb52c2b6 292#undef wxCMP_REVERSE
e3f6cbd9
VS
293
294// for expressions like c-'A':
295inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
e3f6cbd9 296inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
436bb9f5 297inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
e3f6cbd9
VS
298
299#endif /* _WX_UNICHAR_H_ */