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