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