/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/3rdparty/wxWidgets/include/wx/buffer.h
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/3rdparty/wxWidgets/include/wx/buffer.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download)
Tue Sep 7 03:29:01 2010 UTC (9 years, 5 months ago) by william
File MIME type: text/plain
File size: 13406 byte(s)
branching from upstream revision (http://pcsx2.googlecode.com/svn/trunk
): r3113 to
https://svn.netsolutions.dnsalias.com/websvn/ps2/pcsx2/pcsx2_0.9.7/branch/r3113_0.9.7_beta
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/buffer.h
3 // Purpose: auto buffer classes: buffers which automatically free memory
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 12.04.99
7 // RCS-ID: $Id: buffer.h 45761 2007-05-02 17:09:30Z VS $
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_BUFFER_H
13 #define _WX_BUFFER_H
14
15 #include "wx/wxchar.h"
16 #include "wx/msw/HeapAllocator.h"
17
18 // ----------------------------------------------------------------------------
19 // Special classes for (wide) character strings: they use malloc/free instead
20 // of new/delete
21 // ----------------------------------------------------------------------------
22
23 #define DEFINE_BUFFER(classname, chartype, strdupfunc) \
24 class WXDLLIMPEXP_BASE classname \
25 { \
26 public: \
27 classname(const chartype *str = NULL) \
28 : m_str(str ? strdupfunc(str) : NULL) \
29 { \
30 } \
31 \
32 classname(size_t len) \
33 : m_str((chartype *)_allocHeap_wxString((len + 1)*sizeof(chartype))) \
34 { \
35 m_str[len] = (chartype)0; \
36 } \
37 \
38 /* no need to check for NULL, free() does it */ \
39 ~classname() { _freeHeap_wxString(m_str); } \
40 \
41 /* \
42 WARNING: \
43 \
44 the copy ctor and assignment operators change the passed in object \
45 even although it is declared as "const", so: \
46 \
47 a) it shouldn't be really const \
48 b) you shouldn't use it afterwards (or know that it was reset) \
49 \
50 This is very ugly but is unfortunately needed to make the normal use\
51 of classname buffer objects possible and is very similar to what \
52 std::auto_ptr<> does (as if it were an excuse...) \
53 */ \
54 \
55 /* \
56 because of the remark above, release() is declared const even if it \
57 isn't really const \
58 */ \
59 chartype *release() const \
60 { \
61 chartype *p = m_str; \
62 ((classname *)this)->m_str = NULL; \
63 return p; \
64 } \
65 \
66 void reset() \
67 { \
68 _freeHeap_wxString(m_str); \
69 m_str = NULL; \
70 } \
71 \
72 classname(const classname& src) \
73 : m_str(src.release()) \
74 { \
75 } \
76 \
77 classname& operator=(const chartype *str) \
78 { \
79 _freeHeap_wxString(m_str); \
80 m_str = str ? strdupfunc(str) : NULL; \
81 return *this; \
82 } \
83 \
84 classname& operator=(const classname& src) \
85 { \
86 _freeHeap_wxString(m_str); \
87 m_str = src.release(); \
88 \
89 return *this; \
90 } \
91 \
92 bool extend(size_t len) \
93 { \
94 chartype * \
95 str = (chartype *)_reallocHeap_wxString(m_str, (len + 1)*sizeof(chartype)); \
96 if ( !str ) \
97 return false; \
98 \
99 m_str = str; \
100 \
101 return true; \
102 } \
103 \
104 chartype *data() { return m_str; } \
105 const chartype *data() const { return m_str; } \
106 operator const chartype *() const { return m_str; } \
107 chartype operator[](size_t n) const { return m_str[n]; } \
108 \
109 private: \
110 chartype *m_str; \
111 }
112
113 #if wxABI_VERSION >= 20804
114 // needed for wxString::char_str() and wchar_str()
115 #define DEFINE_WRITABLE_BUFFER(classname, baseclass, chartype) \
116 class WXDLLIMPEXP_BASE classname : public baseclass \
117 { \
118 public: \
119 classname(const baseclass& src) : baseclass(src) {} \
120 classname(const chartype *str = NULL) : baseclass(str) {} \
121 \
122 operator chartype*() { return this->data(); } \
123 }
124 #endif // wxABI_VERSION >= 20804
125
126 DEFINE_BUFFER(wxCharBuffer, char, _mswHeap_Strdup);
127 #if wxABI_VERSION >= 20804
128 DEFINE_WRITABLE_BUFFER(wxWritableCharBuffer, wxCharBuffer, char);
129 #endif
130
131 #if wxUSE_WCHAR_T
132
133 DEFINE_BUFFER(wxWCharBuffer, wchar_t, _mswHeap_Strdup);
134 #if wxABI_VERSION >= 20804
135 DEFINE_WRITABLE_BUFFER(wxWritableWCharBuffer, wxWCharBuffer, wchar_t);
136 #endif
137
138 #endif // wxUSE_WCHAR_T
139
140 #undef DEFINE_BUFFER
141 #undef DEFINE_WRITABLE_BUFFER
142
143 #if wxUSE_UNICODE
144 typedef wxWCharBuffer wxWxCharBuffer;
145
146 #define wxMB2WXbuf wxWCharBuffer
147 #define wxWX2MBbuf wxCharBuffer
148 #define wxWC2WXbuf wxChar*
149 #define wxWX2WCbuf wxChar*
150 #else // ANSI
151 typedef wxCharBuffer wxWxCharBuffer;
152
153 #define wxMB2WXbuf wxChar*
154 #define wxWX2MBbuf wxChar*
155 #define wxWC2WXbuf wxCharBuffer
156 #define wxWX2WCbuf wxWCharBuffer
157 #endif // Unicode/ANSI
158
159 // ----------------------------------------------------------------------------
160 // A class for holding growable data buffers (not necessarily strings)
161 // ----------------------------------------------------------------------------
162
163 // This class manages the actual data buffer pointer and is ref-counted.
164 class wxMemoryBufferData
165 {
166 public:
167 // the initial size and also the size added by ResizeIfNeeded()
168 enum { DefBufSize = 1024 };
169
170 friend class wxMemoryBuffer;
171
172 // everyting is private as it can only be used by wxMemoryBuffer
173 private:
174 wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize)
175 : m_data(size ? _allocHeap_wxObject(size) : NULL), m_size(size), m_len(0), m_ref(0)
176 {
177 }
178 ~wxMemoryBufferData() { _freeHeap_wxObject(m_data); }
179
180
181 void ResizeIfNeeded(size_t newSize)
182 {
183 if (newSize > m_size)
184 {
185 void *dataOld = m_data;
186 m_data = _reallocHeap_wxObject(m_data, newSize + wxMemoryBufferData::DefBufSize);
187 if ( !m_data )
188 {
189 _freeHeap_wxObject(dataOld);
190 }
191
192 m_size = newSize + wxMemoryBufferData::DefBufSize;
193 }
194 }
195
196 void IncRef() { m_ref += 1; }
197 void DecRef()
198 {
199 m_ref -= 1;
200 if (m_ref == 0) // are there no more references?
201 delete this;
202 }
203
204
205 // the buffer containing the data
206 void *m_data;
207
208 // the size of the buffer
209 size_t m_size;
210
211 // the amount of data currently in the buffer
212 size_t m_len;
213
214 // the reference count
215 size_t m_ref;
216
217 DECLARE_NO_COPY_CLASS(wxMemoryBufferData)
218 };
219
220
221 class wxMemoryBuffer
222 {
223 public:
224 // ctor and dtor
225 wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize)
226 {
227 m_bufdata = new wxMemoryBufferData(size);
228 m_bufdata->IncRef();
229 }
230
231 ~wxMemoryBuffer() { m_bufdata->DecRef(); }
232
233
234 // copy and assignment
235 wxMemoryBuffer(const wxMemoryBuffer& src)
236 : m_bufdata(src.m_bufdata)
237 {
238 m_bufdata->IncRef();
239 }
240
241 wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
242 {
243 m_bufdata->DecRef();
244 m_bufdata = src.m_bufdata;
245 m_bufdata->IncRef();
246 return *this;
247 }
248
249
250 // Accessors
251 void *GetData() const { return m_bufdata->m_data; }
252 size_t GetBufSize() const { return m_bufdata->m_size; }
253 size_t GetDataLen() const { return m_bufdata->m_len; }
254
255 void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); }
256 void SetDataLen(size_t len)
257 {
258 wxASSERT(len <= m_bufdata->m_size);
259 m_bufdata->m_len = len;
260 }
261
262 // Ensure the buffer is big enough and return a pointer to it
263 void *GetWriteBuf(size_t sizeNeeded)
264 {
265 m_bufdata->ResizeIfNeeded(sizeNeeded);
266 return m_bufdata->m_data;
267 }
268
269 // Update the length after the write
270 void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
271
272 // Like the above, but appends to the buffer
273 void *GetAppendBuf(size_t sizeNeeded)
274 {
275 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + sizeNeeded);
276 return (char*)m_bufdata->m_data + m_bufdata->m_len;
277 }
278
279 // Update the length after the append
280 void UngetAppendBuf(size_t sizeUsed)
281 {
282 SetDataLen(m_bufdata->m_len + sizeUsed);
283 }
284
285 // Other ways to append to the buffer
286 void AppendByte(char data)
287 {
288 wxCHECK_RET( m_bufdata->m_data, _T("invalid wxMemoryBuffer") );
289
290 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + 1);
291 *(((char*)m_bufdata->m_data) + m_bufdata->m_len) = data;
292 m_bufdata->m_len += 1;
293 }
294
295 void AppendData(const void *data, size_t len)
296 {
297 memcpy(GetAppendBuf(len), data, len);
298 UngetAppendBuf(len);
299 }
300
301 operator const char *() const { return (const char*)GetData(); }
302
303 private:
304 wxMemoryBufferData* m_bufdata;
305 };
306
307 // ----------------------------------------------------------------------------
308 // template class for any kind of data
309 // ----------------------------------------------------------------------------
310
311 // TODO
312
313 #endif // _WX_BUFFER_H

  ViewVC Help
Powered by ViewVC 1.1.22