]> git.saurik.com Git - wxWidgets.git/blob - include/wx/buffer.h
don't duplicate copy ctor and assignment operator code in wxCharTypeBuffer<T>, it...
[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 : wxScopedCharTypeBuffer<T>(src) {}
249
250 wxCharTypeBuffer& operator=(const CharType *str)
251 {
252 this->DecRef();
253
254 if ( str )
255 this->m_data = new Data(wxStrdup(str));
256 return *this;
257 }
258
259 wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src)
260 {
261 wxScopedCharTypeBuffer<T>::operator=(src);
262 return *this;
263 }
264
265 wxCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src)
266 {
267 MakeOwnedCopyOf(src);
268 }
269
270 wxCharTypeBuffer& operator=(const wxScopedCharTypeBuffer<T>& src)
271 {
272 MakeOwnedCopyOf(src);
273 return *this;
274 }
275
276 bool extend(size_t len)
277 {
278 wxASSERT_MSG( this->m_data->m_owned, "cannot extend non-owned buffer" );
279 wxASSERT_MSG( this->m_data->m_ref == 1, "can't extend shared buffer" );
280
281 CharType *str =
282 (CharType *)realloc(this->data(), (len + 1) * sizeof(CharType));
283 if ( !str )
284 return false;
285
286 if ( this->m_data == this->GetNullData() )
287 {
288 this->m_data = new Data(str);
289 }
290 else
291 {
292 this->m_data->Set(str);
293 this->m_data->m_owned = true;
294 }
295
296 return true;
297 }
298 };
299
300 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<char> )
301
302 class wxCharBuffer : public wxCharTypeBuffer<char>
303 {
304 public:
305 typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
306 typedef wxScopedCharTypeBuffer<char> wxScopedCharTypeBufferBase;
307
308 wxCharBuffer(const wxCharTypeBufferBase& buf)
309 : wxCharTypeBufferBase(buf) {}
310 wxCharBuffer(const wxScopedCharTypeBufferBase& buf)
311 : wxCharTypeBufferBase(buf) {}
312
313 wxCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
314 wxCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
315
316 wxCharBuffer(const wxCStrData& cstr);
317 };
318
319 #if wxUSE_WCHAR_T
320 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<wchar_t> )
321
322 class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
323 {
324 public:
325 typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
326 typedef wxScopedCharTypeBuffer<wchar_t> wxScopedCharTypeBufferBase;
327
328 wxWCharBuffer(const wxCharTypeBufferBase& buf)
329 : wxCharTypeBufferBase(buf) {}
330 wxWCharBuffer(const wxScopedCharTypeBufferBase& buf)
331 : wxCharTypeBufferBase(buf) {}
332
333 wxWCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
334 wxWCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
335
336 wxWCharBuffer(const wxCStrData& cstr);
337 };
338 #endif // wxUSE_WCHAR_T
339
340 // wxCharTypeBuffer<T> implicitly convertible to T*
341 template <typename T>
342 class wxWritableCharTypeBuffer : public wxCharTypeBuffer<T>
343 {
344 public:
345 typedef typename wxScopedCharTypeBuffer<T>::CharType CharType;
346
347 wxWritableCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src)
348 : wxCharTypeBuffer<T>(src) {}
349 // FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to
350 // always return a buffer
351 // + we should derive this class from wxScopedCharTypeBuffer
352 // then
353 wxWritableCharTypeBuffer(const CharType *str = NULL)
354 : wxCharTypeBuffer<T>(str) {}
355
356 operator CharType*() { return this->data(); }
357 };
358
359 typedef wxWritableCharTypeBuffer<char> wxWritableCharBuffer;
360 typedef wxWritableCharTypeBuffer<wchar_t> wxWritableWCharBuffer;
361
362
363 #if wxUSE_UNICODE
364 #define wxWxCharBuffer wxWCharBuffer
365
366 #define wxMB2WXbuf wxWCharBuffer
367 #define wxWX2MBbuf wxCharBuffer
368 #if wxUSE_UNICODE_WCHAR
369 #define wxWC2WXbuf wxChar*
370 #define wxWX2WCbuf wxChar*
371 #elif wxUSE_UNICODE_UTF8
372 #define wxWC2WXbuf wxWCharBuffer
373 #define wxWX2WCbuf wxWCharBuffer
374 #endif
375 #else // ANSI
376 #define wxWxCharBuffer wxCharBuffer
377
378 #define wxMB2WXbuf wxChar*
379 #define wxWX2MBbuf wxChar*
380 #define wxWC2WXbuf wxCharBuffer
381 #define wxWX2WCbuf wxWCharBuffer
382 #endif // Unicode/ANSI
383
384 // type of the value returned by wxString::utf8_str()
385 #if wxUSE_UNICODE_UTF8
386 #define wxUTF8Buf char *
387 #else
388 #define wxUTF8Buf wxCharBuffer
389 #endif
390
391 // ----------------------------------------------------------------------------
392 // A class for holding growable data buffers (not necessarily strings)
393 // ----------------------------------------------------------------------------
394
395 // This class manages the actual data buffer pointer and is ref-counted.
396 class wxMemoryBufferData
397 {
398 public:
399 // the initial size and also the size added by ResizeIfNeeded()
400 enum { DefBufSize = 1024 };
401
402 friend class wxMemoryBuffer;
403
404 // everyting is private as it can only be used by wxMemoryBuffer
405 private:
406 wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize)
407 : m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0)
408 {
409 }
410 ~wxMemoryBufferData() { free(m_data); }
411
412
413 void ResizeIfNeeded(size_t newSize)
414 {
415 if (newSize > m_size)
416 {
417 void *dataOld = m_data;
418 m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize);
419 if ( !m_data )
420 {
421 free(dataOld);
422 }
423
424 m_size = newSize + wxMemoryBufferData::DefBufSize;
425 }
426 }
427
428 void IncRef() { m_ref += 1; }
429 void DecRef()
430 {
431 m_ref -= 1;
432 if (m_ref == 0) // are there no more references?
433 delete this;
434 }
435
436
437 // the buffer containing the data
438 void *m_data;
439
440 // the size of the buffer
441 size_t m_size;
442
443 // the amount of data currently in the buffer
444 size_t m_len;
445
446 // the reference count
447 size_t m_ref;
448
449 wxDECLARE_NO_COPY_CLASS(wxMemoryBufferData);
450 };
451
452
453 class wxMemoryBuffer
454 {
455 public:
456 // ctor and dtor
457 wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize)
458 {
459 m_bufdata = new wxMemoryBufferData(size);
460 m_bufdata->IncRef();
461 }
462
463 ~wxMemoryBuffer() { m_bufdata->DecRef(); }
464
465
466 // copy and assignment
467 wxMemoryBuffer(const wxMemoryBuffer& src)
468 : m_bufdata(src.m_bufdata)
469 {
470 m_bufdata->IncRef();
471 }
472
473 wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
474 {
475 if (&src != this)
476 {
477 m_bufdata->DecRef();
478 m_bufdata = src.m_bufdata;
479 m_bufdata->IncRef();
480 }
481 return *this;
482 }
483
484
485 // Accessors
486 void *GetData() const { return m_bufdata->m_data; }
487 size_t GetBufSize() const { return m_bufdata->m_size; }
488 size_t GetDataLen() const { return m_bufdata->m_len; }
489
490 void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); }
491 void SetDataLen(size_t len)
492 {
493 wxASSERT(len <= m_bufdata->m_size);
494 m_bufdata->m_len = len;
495 }
496
497 // Ensure the buffer is big enough and return a pointer to it
498 void *GetWriteBuf(size_t sizeNeeded)
499 {
500 m_bufdata->ResizeIfNeeded(sizeNeeded);
501 return m_bufdata->m_data;
502 }
503
504 // Update the length after the write
505 void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
506
507 // Like the above, but appends to the buffer
508 void *GetAppendBuf(size_t sizeNeeded)
509 {
510 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + sizeNeeded);
511 return (char*)m_bufdata->m_data + m_bufdata->m_len;
512 }
513
514 // Update the length after the append
515 void UngetAppendBuf(size_t sizeUsed)
516 {
517 SetDataLen(m_bufdata->m_len + sizeUsed);
518 }
519
520 // Other ways to append to the buffer
521 void AppendByte(char data)
522 {
523 wxCHECK_RET( m_bufdata->m_data, _T("invalid wxMemoryBuffer") );
524
525 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + 1);
526 *(((char*)m_bufdata->m_data) + m_bufdata->m_len) = data;
527 m_bufdata->m_len += 1;
528 }
529
530 void AppendData(const void *data, size_t len)
531 {
532 memcpy(GetAppendBuf(len), data, len);
533 UngetAppendBuf(len);
534 }
535
536 operator const char *() const { return (const char*)GetData(); }
537
538 private:
539 wxMemoryBufferData* m_bufdata;
540 };
541
542 // ----------------------------------------------------------------------------
543 // template class for any kind of data
544 // ----------------------------------------------------------------------------
545
546 // TODO
547
548 #endif // _WX_BUFFER_H