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