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