]>
Commit | Line | Data |
---|---|---|
14971e5b | 1 | /////////////////////////////////////////////////////////////////////////////// |
c12ef8a2 | 2 | // Name: wx/buffer.h |
14971e5b VZ |
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$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
14971e5b VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
14971e5b VZ |
12 | #ifndef _WX_BUFFER_H |
13 | #define _WX_BUFFER_H | |
14 | ||
e713a90b | 15 | #include "wx/chartype.h" |
52de37c7 | 16 | #include "wx/wxcrtbase.h" |
e90c1d2a | 17 | |
e2473d4c VZ |
18 | #include <stdlib.h> // malloc() and free() |
19 | ||
b5dbe15d | 20 | class WXDLLIMPEXP_FWD_BASE wxCStrData; |
8db4a5d2 | 21 | |
14971e5b VZ |
22 | // ---------------------------------------------------------------------------- |
23 | // Special classes for (wide) character strings: they use malloc/free instead | |
24 | // of new/delete | |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
4e79262f VZ |
27 | // helpers used by wxCharTypeBuffer |
28 | namespace wxPrivate | |
29 | { | |
30 | ||
31 | struct UntypedBufferData | |
32 | { | |
33 | enum Kind | |
34 | { | |
35 | Owned, | |
36 | NonOwned | |
37 | }; | |
38 | ||
6df09f32 VS |
39 | UntypedBufferData(void *str, size_t len, Kind kind = Owned) |
40 | : m_str(str), m_length(len), m_ref(1), m_owned(kind == Owned) {} | |
4e79262f VZ |
41 | |
42 | ~UntypedBufferData() | |
43 | { | |
44 | if ( m_owned ) | |
45 | free(m_str); | |
46 | } | |
47 | ||
48 | void *m_str; | |
6df09f32 | 49 | size_t m_length; |
4e79262f | 50 | |
6df09f32 | 51 | // "short" to have sizeof(Data)=12 on 32bit archs |
4e79262f VZ |
52 | unsigned short m_ref; |
53 | ||
54 | bool m_owned; | |
55 | }; | |
56 | ||
4e79262f | 57 | // NB: this is defined in string.cpp and not the (non-existent) buffer.cpp |
b96a56e6 | 58 | WXDLLIMPEXP_BASE UntypedBufferData * GetUntypedNullData(); |
4e79262f VZ |
59 | |
60 | } // namespace wxPrivate | |
61 | ||
de4983f3 VS |
62 | |
63 | // Reference-counted character buffer for storing string data. The buffer | |
64 | // is only valid for as long as the "parent" object that provided the data | |
65 | // is valid; see wxCharTypeBuffer<T> for persistent variant. | |
d18c8d3d | 66 | template <typename T> |
de4983f3 | 67 | class wxScopedCharTypeBuffer |
d18c8d3d VS |
68 | { |
69 | public: | |
70 | typedef T CharType; | |
71 | ||
de4983f3 | 72 | wxScopedCharTypeBuffer() |
d18c8d3d | 73 | { |
de4983f3 | 74 | m_data = GetNullData(); |
d18c8d3d VS |
75 | } |
76 | ||
de4983f3 VS |
77 | // Creates "non-owned" buffer, i.e. 'str' is not owned by the buffer |
78 | // and doesn't get freed by dtor. Used e.g. to point to wxString's internal | |
79 | // storage. | |
6df09f32 VS |
80 | static |
81 | const wxScopedCharTypeBuffer CreateNonOwned(const CharType *str, | |
82 | size_t len = wxNO_LEN) | |
d18c8d3d | 83 | { |
6df09f32 VS |
84 | if ( len == wxNO_LEN ) |
85 | len = wxStrlen(str); | |
86 | ||
de4983f3 VS |
87 | wxScopedCharTypeBuffer buf; |
88 | if ( str ) | |
6df09f32 | 89 | buf.m_data = new Data(const_cast<CharType*>(str), len, Data::NonOwned); |
de4983f3 | 90 | return buf; |
d18c8d3d VS |
91 | } |
92 | ||
de4983f3 VS |
93 | // Creates "owned" buffer, i.e. takes over ownership of 'str' and frees it |
94 | // in dtor (if ref.count reaches 0). | |
6df09f32 | 95 | static |
33808494 | 96 | const wxScopedCharTypeBuffer CreateOwned(CharType *str, |
6df09f32 | 97 | size_t len = wxNO_LEN ) |
486a5941 | 98 | { |
6df09f32 VS |
99 | if ( len == wxNO_LEN ) |
100 | len = wxStrlen(str); | |
101 | ||
de4983f3 | 102 | wxScopedCharTypeBuffer buf; |
5c1de526 | 103 | if ( str ) |
33808494 | 104 | buf.m_data = new Data(str, len); |
486a5941 VS |
105 | return buf; |
106 | } | |
107 | ||
de4983f3 VS |
108 | wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer& src) |
109 | { | |
110 | m_data = src.m_data; | |
111 | IncRef(); | |
112 | } | |
113 | ||
114 | wxScopedCharTypeBuffer& operator=(const wxScopedCharTypeBuffer& src) | |
115 | { | |
116 | if ( &src == this ) | |
117 | return *this; | |
118 | ||
119 | DecRef(); | |
120 | m_data = src.m_data; | |
121 | IncRef(); | |
122 | ||
123 | return *this; | |
124 | } | |
125 | ||
126 | ~wxScopedCharTypeBuffer() | |
486a5941 | 127 | { |
5c1de526 | 128 | DecRef(); |
486a5941 | 129 | } |
d18c8d3d | 130 | |
03af437f VS |
131 | // NB: this method is only const for backward compatibility. It used to |
132 | // be needed for auto_ptr-like semantics of the copy ctor, but now | |
133 | // that ref-counting is used, it's not really needed. | |
134 | CharType *release() const | |
5c1de526 | 135 | { |
6b583d40 | 136 | if ( m_data == GetNullData() ) |
5c1de526 | 137 | return NULL; |
d18c8d3d | 138 | |
9a83f860 VZ |
139 | wxASSERT_MSG( m_data->m_owned, wxT("can't release non-owned buffer") ); |
140 | wxASSERT_MSG( m_data->m_ref == 1, wxT("can't release shared buffer") ); | |
14971e5b | 141 | |
4e79262f | 142 | CharType * const p = m_data->Get(); |
03af437f | 143 | |
de4983f3 | 144 | wxScopedCharTypeBuffer *self = const_cast<wxScopedCharTypeBuffer*>(this); |
6df09f32 | 145 | self->m_data->Set(NULL, 0); |
03af437f VS |
146 | self->DecRef(); |
147 | ||
5c1de526 | 148 | return p; |
d18c8d3d | 149 | } |
c12ef8a2 | 150 | |
d18c8d3d VS |
151 | void reset() |
152 | { | |
5c1de526 | 153 | DecRef(); |
d18c8d3d | 154 | } |
c12ef8a2 | 155 | |
4e79262f VZ |
156 | CharType *data() { return m_data->Get(); } |
157 | const CharType *data() const { return m_data->Get(); } | |
5c1de526 VS |
158 | operator const CharType *() const { return data(); } |
159 | CharType operator[](size_t n) const { return data()[n]; } | |
486a5941 | 160 | |
6df09f32 VS |
161 | size_t length() const { return m_data->m_length; } |
162 | ||
de4983f3 | 163 | protected: |
5c1de526 | 164 | // reference-counted data |
5c69ef61 | 165 | struct Data : public wxPrivate::UntypedBufferData |
486a5941 | 166 | { |
6df09f32 VS |
167 | Data(CharType *str, size_t len, Kind kind = Owned) |
168 | : wxPrivate::UntypedBufferData(str, len, kind) | |
5c1de526 | 169 | { |
5c1de526 VS |
170 | } |
171 | ||
4e79262f | 172 | CharType *Get() const { return static_cast<CharType *>(m_str); } |
6df09f32 VS |
173 | void Set(CharType *str, size_t len) |
174 | { | |
175 | m_str = str; | |
176 | m_length = len; | |
177 | } | |
5c1de526 VS |
178 | }; |
179 | ||
180 | // placeholder for NULL string, to simplify this code | |
6b583d40 VZ |
181 | static Data *GetNullData() |
182 | { | |
b96a56e6 | 183 | return static_cast<Data *>(wxPrivate::GetUntypedNullData()); |
6b583d40 | 184 | } |
5c1de526 VS |
185 | |
186 | void IncRef() | |
187 | { | |
6b583d40 | 188 | if ( m_data == GetNullData() ) // exception, not ref-counted |
5c1de526 VS |
189 | return; |
190 | m_data->m_ref++; | |
486a5941 VS |
191 | } |
192 | ||
5c1de526 | 193 | void DecRef() |
486a5941 | 194 | { |
6b583d40 | 195 | if ( m_data == GetNullData() ) // exception, not ref-counted |
5c1de526 VS |
196 | return; |
197 | if ( --m_data->m_ref == 0 ) | |
198 | delete m_data; | |
6b583d40 | 199 | m_data = GetNullData(); |
486a5941 VS |
200 | } |
201 | ||
de4983f3 VS |
202 | // sets this object to a be copy of 'other'; if 'src' is non-owned, |
203 | // a deep copy is made and 'this' will contain new instance of the data | |
204 | void MakeOwnedCopyOf(const wxScopedCharTypeBuffer& src) | |
205 | { | |
206 | this->DecRef(); | |
207 | ||
208 | if ( src.m_data == this->GetNullData() ) | |
209 | { | |
210 | this->m_data = this->GetNullData(); | |
211 | } | |
212 | else if ( src.m_data->m_owned ) | |
213 | { | |
214 | this->m_data = src.m_data; | |
215 | this->IncRef(); | |
216 | } | |
217 | else | |
218 | { | |
219 | // if the scoped buffer had non-owned data, we have to make | |
220 | // a copy here, because src.m_data->m_str is valid only for as long | |
221 | // as 'src' exists | |
6df09f32 VS |
222 | this->m_data = new Data |
223 | ( | |
224 | StrCopy(src.data(), src.length()), | |
225 | src.length() | |
226 | ); | |
de4983f3 VS |
227 | } |
228 | } | |
229 | ||
6df09f32 VS |
230 | static CharType *StrCopy(const CharType *src, size_t len) |
231 | { | |
232 | CharType *dst = (CharType*)malloc(sizeof(CharType) * (len + 1)); | |
233 | memcpy(dst, src, sizeof(CharType) * (len + 1)); | |
234 | return dst; | |
235 | } | |
236 | ||
de4983f3 | 237 | protected: |
5c1de526 | 238 | Data *m_data; |
d18c8d3d VS |
239 | }; |
240 | ||
de4983f3 VS |
241 | typedef wxScopedCharTypeBuffer<char> wxScopedCharBuffer; |
242 | typedef wxScopedCharTypeBuffer<wchar_t> wxScopedWCharBuffer; | |
243 | ||
244 | ||
245 | // this buffer class always stores data in "owned" (persistent) manner | |
246 | template <typename T> | |
247 | class wxCharTypeBuffer : public wxScopedCharTypeBuffer<T> | |
248 | { | |
249 | protected: | |
250 | typedef typename wxScopedCharTypeBuffer<T>::Data Data; | |
251 | ||
252 | public: | |
253 | typedef T CharType; | |
254 | ||
6df09f32 | 255 | wxCharTypeBuffer(const CharType *str = NULL, size_t len = wxNO_LEN) |
de4983f3 VS |
256 | { |
257 | if ( str ) | |
6df09f32 VS |
258 | { |
259 | if ( len == wxNO_LEN ) | |
260 | len = wxStrlen(str); | |
df04f800 | 261 | this->m_data = new Data(this->StrCopy(str, len), len); |
6df09f32 | 262 | } |
de4983f3 | 263 | else |
6df09f32 | 264 | { |
de4983f3 | 265 | this->m_data = this->GetNullData(); |
6df09f32 | 266 | } |
de4983f3 VS |
267 | } |
268 | ||
269 | wxCharTypeBuffer(size_t len) | |
270 | { | |
6df09f32 VS |
271 | this->m_data = |
272 | new Data((CharType *)malloc((len + 1)*sizeof(CharType)), len); | |
de4983f3 VS |
273 | this->m_data->Get()[len] = (CharType)0; |
274 | } | |
275 | ||
276 | wxCharTypeBuffer(const wxCharTypeBuffer& src) | |
3339414a | 277 | : wxScopedCharTypeBuffer<T>(src) {} |
de4983f3 VS |
278 | |
279 | wxCharTypeBuffer& operator=(const CharType *str) | |
280 | { | |
281 | this->DecRef(); | |
282 | ||
283 | if ( str ) | |
6df09f32 | 284 | this->m_data = new Data(wxStrdup(str), wxStrlen(str)); |
de4983f3 VS |
285 | return *this; |
286 | } | |
287 | ||
288 | wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src) | |
289 | { | |
3339414a | 290 | wxScopedCharTypeBuffer<T>::operator=(src); |
de4983f3 VS |
291 | return *this; |
292 | } | |
293 | ||
294 | wxCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src) | |
295 | { | |
df04f800 | 296 | this->MakeOwnedCopyOf(src); |
de4983f3 VS |
297 | } |
298 | ||
299 | wxCharTypeBuffer& operator=(const wxScopedCharTypeBuffer<T>& src) | |
300 | { | |
301 | MakeOwnedCopyOf(src); | |
302 | return *this; | |
303 | } | |
304 | ||
305 | bool extend(size_t len) | |
306 | { | |
307 | wxASSERT_MSG( this->m_data->m_owned, "cannot extend non-owned buffer" ); | |
308 | wxASSERT_MSG( this->m_data->m_ref == 1, "can't extend shared buffer" ); | |
309 | ||
310 | CharType *str = | |
311 | (CharType *)realloc(this->data(), (len + 1) * sizeof(CharType)); | |
312 | if ( !str ) | |
313 | return false; | |
314 | ||
315 | if ( this->m_data == this->GetNullData() ) | |
316 | { | |
6df09f32 | 317 | this->m_data = new Data(str, len); |
de4983f3 VS |
318 | } |
319 | else | |
320 | { | |
6df09f32 | 321 | this->m_data->Set(str, len); |
de4983f3 VS |
322 | this->m_data->m_owned = true; |
323 | } | |
324 | ||
325 | return true; | |
326 | } | |
a6bb7a28 VS |
327 | |
328 | void shrink(size_t len) | |
329 | { | |
330 | wxASSERT_MSG( this->m_data->m_owned, "cannot shrink non-owned buffer" ); | |
331 | wxASSERT_MSG( this->m_data->m_ref == 1, "can't shrink shared buffer" ); | |
332 | ||
333 | wxASSERT( len <= this->length() ); | |
334 | ||
335 | this->m_data->m_length = len; | |
336 | this->data()[len] = 0; | |
337 | } | |
de4983f3 VS |
338 | }; |
339 | ||
fb5ae4f6 | 340 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxScopedCharTypeBuffer<char> ) |
7c77f334 VZ |
341 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<char> ) |
342 | ||
6b583d40 | 343 | class wxCharBuffer : public wxCharTypeBuffer<char> |
d18c8d3d VS |
344 | { |
345 | public: | |
ccd4deab | 346 | typedef wxCharTypeBuffer<char> wxCharTypeBufferBase; |
de4983f3 | 347 | typedef wxScopedCharTypeBuffer<char> wxScopedCharTypeBufferBase; |
ccd4deab | 348 | |
681e4412 VS |
349 | wxCharBuffer(const wxCharTypeBufferBase& buf) |
350 | : wxCharTypeBufferBase(buf) {} | |
de4983f3 VS |
351 | wxCharBuffer(const wxScopedCharTypeBufferBase& buf) |
352 | : wxCharTypeBufferBase(buf) {} | |
681e4412 | 353 | |
ccd4deab VZ |
354 | wxCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {} |
355 | wxCharBuffer(size_t len) : wxCharTypeBufferBase(len) {} | |
356 | ||
ccd4deab | 357 | wxCharBuffer(const wxCStrData& cstr); |
d18c8d3d VS |
358 | }; |
359 | ||
fb5ae4f6 | 360 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxScopedCharTypeBuffer<wchar_t> ) |
7c77f334 VZ |
361 | WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<wchar_t> ) |
362 | ||
6b583d40 | 363 | class wxWCharBuffer : public wxCharTypeBuffer<wchar_t> |
d18c8d3d VS |
364 | { |
365 | public: | |
ccd4deab | 366 | typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase; |
de4983f3 | 367 | typedef wxScopedCharTypeBuffer<wchar_t> wxScopedCharTypeBufferBase; |
ccd4deab | 368 | |
681e4412 VS |
369 | wxWCharBuffer(const wxCharTypeBufferBase& buf) |
370 | : wxCharTypeBufferBase(buf) {} | |
de4983f3 VS |
371 | wxWCharBuffer(const wxScopedCharTypeBufferBase& buf) |
372 | : wxCharTypeBufferBase(buf) {} | |
681e4412 | 373 | |
ccd4deab VZ |
374 | wxWCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {} |
375 | wxWCharBuffer(size_t len) : wxCharTypeBufferBase(len) {} | |
376 | ||
ccd4deab | 377 | wxWCharBuffer(const wxCStrData& cstr); |
d18c8d3d | 378 | }; |
2b5f62a0 | 379 | |
ef0f1387 VS |
380 | // wxCharTypeBuffer<T> implicitly convertible to T* |
381 | template <typename T> | |
665e6a87 | 382 | class wxWritableCharTypeBuffer : public wxCharTypeBuffer<T> |
ef0f1387 VS |
383 | { |
384 | public: | |
de4983f3 | 385 | typedef typename wxScopedCharTypeBuffer<T>::CharType CharType; |
ef0f1387 | 386 | |
de4983f3 | 387 | wxWritableCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src) |
ef0f1387 VS |
388 | : wxCharTypeBuffer<T>(src) {} |
389 | // FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to | |
390 | // always return a buffer | |
de4983f3 VS |
391 | // + we should derive this class from wxScopedCharTypeBuffer |
392 | // then | |
ef0f1387 VS |
393 | wxWritableCharTypeBuffer(const CharType *str = NULL) |
394 | : wxCharTypeBuffer<T>(str) {} | |
395 | ||
396 | operator CharType*() { return this->data(); } | |
397 | }; | |
398 | ||
399 | typedef wxWritableCharTypeBuffer<char> wxWritableCharBuffer; | |
400 | typedef wxWritableCharTypeBuffer<wchar_t> wxWritableWCharBuffer; | |
401 | ||
402 | ||
f93d01be | 403 | #if wxUSE_UNICODE |
ccd4deab | 404 | #define wxWxCharBuffer wxWCharBuffer |
2f4f6de7 | 405 | |
e90c1d2a VZ |
406 | #define wxMB2WXbuf wxWCharBuffer |
407 | #define wxWX2MBbuf wxCharBuffer | |
81727065 VS |
408 | #if wxUSE_UNICODE_WCHAR |
409 | #define wxWC2WXbuf wxChar* | |
410 | #define wxWX2WCbuf wxChar* | |
411 | #elif wxUSE_UNICODE_UTF8 | |
412 | #define wxWC2WXbuf wxWCharBuffer | |
413 | #define wxWX2WCbuf wxWCharBuffer | |
414 | #endif | |
e90c1d2a | 415 | #else // ANSI |
ccd4deab | 416 | #define wxWxCharBuffer wxCharBuffer |
2f4f6de7 | 417 | |
e90c1d2a VZ |
418 | #define wxMB2WXbuf wxChar* |
419 | #define wxWX2MBbuf wxChar* | |
420 | #define wxWC2WXbuf wxCharBuffer | |
421 | #define wxWX2WCbuf wxWCharBuffer | |
422 | #endif // Unicode/ANSI | |
f93d01be | 423 | |
b9fdb397 RD |
424 | // ---------------------------------------------------------------------------- |
425 | // A class for holding growable data buffers (not necessarily strings) | |
426 | // ---------------------------------------------------------------------------- | |
427 | ||
2b5f62a0 VZ |
428 | // This class manages the actual data buffer pointer and is ref-counted. |
429 | class wxMemoryBufferData | |
b9fdb397 RD |
430 | { |
431 | public: | |
2b5f62a0 | 432 | // the initial size and also the size added by ResizeIfNeeded() |
ae0ca755 | 433 | enum { DefBufSize = 1024 }; |
2b5f62a0 VZ |
434 | |
435 | friend class wxMemoryBuffer; | |
436 | ||
437 | // everyting is private as it can only be used by wxMemoryBuffer | |
438 | private: | |
ae0ca755 | 439 | wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize) |
2b5f62a0 | 440 | : m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0) |
b9fdb397 | 441 | { |
b9fdb397 | 442 | } |
2b5f62a0 | 443 | ~wxMemoryBufferData() { free(m_data); } |
b9fdb397 | 444 | |
b9fdb397 | 445 | |
2b5f62a0 | 446 | void ResizeIfNeeded(size_t newSize) |
b9fdb397 | 447 | { |
2b5f62a0 VZ |
448 | if (newSize > m_size) |
449 | { | |
450 | void *dataOld = m_data; | |
ae0ca755 | 451 | m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize); |
2b5f62a0 VZ |
452 | if ( !m_data ) |
453 | { | |
454 | free(dataOld); | |
455 | } | |
456 | ||
ae0ca755 | 457 | m_size = newSize + wxMemoryBufferData::DefBufSize; |
2b5f62a0 | 458 | } |
b9fdb397 RD |
459 | } |
460 | ||
2b5f62a0 VZ |
461 | void IncRef() { m_ref += 1; } |
462 | void DecRef() | |
b9fdb397 | 463 | { |
2b5f62a0 VZ |
464 | m_ref -= 1; |
465 | if (m_ref == 0) // are there no more references? | |
466 | delete this; | |
b9fdb397 | 467 | } |
b9fdb397 | 468 | |
8dffb6b1 VS |
469 | void *release() |
470 | { | |
471 | if ( m_data == NULL ) | |
472 | return NULL; | |
473 | ||
474 | wxASSERT_MSG( m_ref == 1, "can't release shared buffer" ); | |
475 | ||
476 | void *p = m_data; | |
477 | m_data = NULL; | |
478 | m_len = | |
479 | m_size = 0; | |
480 | ||
481 | return p; | |
482 | } | |
483 | ||
2b5f62a0 VZ |
484 | |
485 | // the buffer containing the data | |
486 | void *m_data; | |
487 | ||
488 | // the size of the buffer | |
489 | size_t m_size; | |
490 | ||
491 | // the amount of data currently in the buffer | |
492 | size_t m_len; | |
493 | ||
494 | // the reference count | |
495 | size_t m_ref; | |
22f3361e | 496 | |
c0c133e1 | 497 | wxDECLARE_NO_COPY_CLASS(wxMemoryBufferData); |
2b5f62a0 VZ |
498 | }; |
499 | ||
500 | ||
6b583d40 | 501 | class wxMemoryBuffer |
2b5f62a0 VZ |
502 | { |
503 | public: | |
504 | // ctor and dtor | |
ae0ca755 | 505 | wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize) |
b9fdb397 | 506 | { |
2b5f62a0 VZ |
507 | m_bufdata = new wxMemoryBufferData(size); |
508 | m_bufdata->IncRef(); | |
b9fdb397 | 509 | } |
b9fdb397 | 510 | |
2b5f62a0 VZ |
511 | ~wxMemoryBuffer() { m_bufdata->DecRef(); } |
512 | ||
513 | ||
514 | // copy and assignment | |
515 | wxMemoryBuffer(const wxMemoryBuffer& src) | |
516 | : m_bufdata(src.m_bufdata) | |
517 | { | |
518 | m_bufdata->IncRef(); | |
b9fdb397 | 519 | } |
2b5f62a0 VZ |
520 | |
521 | wxMemoryBuffer& operator=(const wxMemoryBuffer& src) | |
b9fdb397 | 522 | { |
162e998c PC |
523 | if (&src != this) |
524 | { | |
525 | m_bufdata->DecRef(); | |
526 | m_bufdata = src.m_bufdata; | |
527 | m_bufdata->IncRef(); | |
528 | } | |
2b5f62a0 | 529 | return *this; |
b9fdb397 RD |
530 | } |
531 | ||
b9fdb397 | 532 | |
2b5f62a0 VZ |
533 | // Accessors |
534 | void *GetData() const { return m_bufdata->m_data; } | |
535 | size_t GetBufSize() const { return m_bufdata->m_size; } | |
536 | size_t GetDataLen() const { return m_bufdata->m_len; } | |
ef1cae87 | 537 | |
2b5f62a0 VZ |
538 | void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); } |
539 | void SetDataLen(size_t len) | |
ef1cae87 | 540 | { |
2b5f62a0 VZ |
541 | wxASSERT(len <= m_bufdata->m_size); |
542 | m_bufdata->m_len = len; | |
ef1cae87 RD |
543 | } |
544 | ||
2b5f62a0 VZ |
545 | // Ensure the buffer is big enough and return a pointer to it |
546 | void *GetWriteBuf(size_t sizeNeeded) | |
ef1cae87 | 547 | { |
2b5f62a0 VZ |
548 | m_bufdata->ResizeIfNeeded(sizeNeeded); |
549 | return m_bufdata->m_data; | |
550 | } | |
c12ef8a2 | 551 | |
2b5f62a0 VZ |
552 | // Update the length after the write |
553 | void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); } | |
ef1cae87 | 554 | |
2b5f62a0 VZ |
555 | // Like the above, but appends to the buffer |
556 | void *GetAppendBuf(size_t sizeNeeded) | |
557 | { | |
558 | m_bufdata->ResizeIfNeeded(m_bufdata->m_len + sizeNeeded); | |
559 | return (char*)m_bufdata->m_data + m_bufdata->m_len; | |
560 | } | |
ef1cae87 | 561 | |
2b5f62a0 VZ |
562 | // Update the length after the append |
563 | void UngetAppendBuf(size_t sizeUsed) | |
564 | { | |
565 | SetDataLen(m_bufdata->m_len + sizeUsed); | |
566 | } | |
ef1cae87 | 567 | |
2b5f62a0 VZ |
568 | // Other ways to append to the buffer |
569 | void AppendByte(char data) | |
b9fdb397 | 570 | { |
9a83f860 | 571 | wxCHECK_RET( m_bufdata->m_data, wxT("invalid wxMemoryBuffer") ); |
2b5f62a0 VZ |
572 | |
573 | m_bufdata->ResizeIfNeeded(m_bufdata->m_len + 1); | |
574 | *(((char*)m_bufdata->m_data) + m_bufdata->m_len) = data; | |
575 | m_bufdata->m_len += 1; | |
576 | } | |
577 | ||
68bc51a9 | 578 | void AppendData(const void *data, size_t len) |
2b5f62a0 VZ |
579 | { |
580 | memcpy(GetAppendBuf(len), data, len); | |
581 | UngetAppendBuf(len); | |
b9fdb397 RD |
582 | } |
583 | ||
2b5f62a0 VZ |
584 | operator const char *() const { return (const char*)GetData(); } |
585 | ||
8dffb6b1 VS |
586 | // gives up ownership of data, returns the pointer; after this call, |
587 | // data isn't freed by the buffer and its content is resent to empty | |
588 | void *release() | |
589 | { | |
590 | return m_bufdata->release(); | |
591 | } | |
592 | ||
b9fdb397 | 593 | private: |
2b5f62a0 | 594 | wxMemoryBufferData* m_bufdata; |
b9fdb397 RD |
595 | }; |
596 | ||
14971e5b VZ |
597 | // ---------------------------------------------------------------------------- |
598 | // template class for any kind of data | |
599 | // ---------------------------------------------------------------------------- | |
600 | ||
601 | // TODO | |
602 | ||
603 | #endif // _WX_BUFFER_H |