1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: auto buffer classes: buffers which automatically free memory
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
15 #include "wx/chartype.h"
16 #include "wx/wxcrtbase.h"
19 #include <stdlib.h> // malloc() and free()
20 #endif // ! __WXPALMOS5__
22 class WXDLLIMPEXP_FWD_BASE wxCStrData
;
24 // ----------------------------------------------------------------------------
25 // Special classes for (wide) character strings: they use malloc/free instead
27 // ----------------------------------------------------------------------------
29 // helpers used by wxCharTypeBuffer
33 struct UntypedBufferData
41 UntypedBufferData(void *str
, size_t len
, Kind kind
= Owned
)
42 : m_str(str
), m_length(len
), m_ref(1), m_owned(kind
== Owned
) {}
53 // "short" to have sizeof(Data)=12 on 32bit archs
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)
63 // NB: this is defined in string.cpp and not the (non-existent) buffer.cpp
64 extern WXDLLIMPEXP_DATA_BASE(UntypedBufferData
* const) untypedNullDataPtr
;
66 } // namespace wxPrivate
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.
73 class wxScopedCharTypeBuffer
78 wxScopedCharTypeBuffer()
80 m_data
= GetNullData();
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
87 const wxScopedCharTypeBuffer
CreateNonOwned(const CharType
*str
,
88 size_t len
= wxNO_LEN
)
90 if ( len
== wxNO_LEN
)
93 wxScopedCharTypeBuffer buf
;
95 buf
.m_data
= new Data(const_cast<CharType
*>(str
), len
, Data::NonOwned
);
99 // Creates "owned" buffer, i.e. takes over ownership of 'str' and frees it
100 // in dtor (if ref.count reaches 0).
102 const wxScopedCharTypeBuffer
CreateOwned(const CharType
*str
,
103 size_t len
= wxNO_LEN
)
105 if ( len
== wxNO_LEN
)
108 wxScopedCharTypeBuffer buf
;
110 buf
.m_data
= new Data(StrCopy(str
, len
), len
);
114 wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer
& src
)
120 wxScopedCharTypeBuffer
& operator=(const wxScopedCharTypeBuffer
& src
)
132 ~wxScopedCharTypeBuffer()
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
142 if ( m_data
== GetNullData() )
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") );
148 CharType
* const p
= m_data
->Get();
150 wxScopedCharTypeBuffer
*self
= const_cast<wxScopedCharTypeBuffer
*>(this);
151 self
->m_data
->Set(NULL
, 0);
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
]; }
167 size_t length() const { return m_data
->m_length
; }
170 // reference-counted data
171 struct Data
: public wxPrivate::UntypedBufferData
173 Data(CharType
*str
, size_t len
, Kind kind
= Owned
)
174 : wxPrivate::UntypedBufferData(str
, len
, kind
)
178 CharType
*Get() const { return static_cast<CharType
*>(m_str
); }
179 void Set(CharType
*str
, size_t len
)
186 // placeholder for NULL string, to simplify this code
187 static Data
*GetNullData()
189 return static_cast<Data
*>(wxPrivate::untypedNullDataPtr
);
194 if ( m_data
== GetNullData() ) // exception, not ref-counted
201 if ( m_data
== GetNullData() ) // exception, not ref-counted
203 if ( --m_data
->m_ref
== 0 )
205 m_data
= GetNullData();
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
)
214 if ( src
.m_data
== this->GetNullData() )
216 this->m_data
= this->GetNullData();
218 else if ( src
.m_data
->m_owned
)
220 this->m_data
= src
.m_data
;
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
228 this->m_data
= new Data
230 StrCopy(src
.data(), src
.length()),
236 static CharType
*StrCopy(const CharType
*src
, size_t len
)
238 CharType
*dst
= (CharType
*)malloc(sizeof(CharType
) * (len
+ 1));
239 memcpy(dst
, src
, sizeof(CharType
) * (len
+ 1));
247 typedef wxScopedCharTypeBuffer
<char> wxScopedCharBuffer
;
248 typedef wxScopedCharTypeBuffer
<wchar_t> wxScopedWCharBuffer
;
251 // this buffer class always stores data in "owned" (persistent) manner
252 template <typename T
>
253 class wxCharTypeBuffer
: public wxScopedCharTypeBuffer
<T
>
256 typedef typename wxScopedCharTypeBuffer
<T
>::Data Data
;
261 wxCharTypeBuffer(const CharType
*str
= NULL
, size_t len
= wxNO_LEN
)
265 if ( len
== wxNO_LEN
)
267 this->m_data
= new Data(StrCopy(str
, len
), len
);
271 this->m_data
= this->GetNullData();
275 wxCharTypeBuffer(size_t len
)
278 new Data((CharType
*)malloc((len
+ 1)*sizeof(CharType
)), len
);
279 this->m_data
->Get()[len
] = (CharType
)0;
282 wxCharTypeBuffer(const wxCharTypeBuffer
& src
)
283 : wxScopedCharTypeBuffer
<T
>(src
) {}
285 wxCharTypeBuffer
& operator=(const CharType
*str
)
290 this->m_data
= new Data(wxStrdup(str
), wxStrlen(str
));
294 wxCharTypeBuffer
& operator=(const wxCharTypeBuffer
& src
)
296 wxScopedCharTypeBuffer
<T
>::operator=(src
);
300 wxCharTypeBuffer(const wxScopedCharTypeBuffer
<T
>& src
)
302 MakeOwnedCopyOf(src
);
305 wxCharTypeBuffer
& operator=(const wxScopedCharTypeBuffer
<T
>& src
)
307 MakeOwnedCopyOf(src
);
311 bool extend(size_t len
)
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" );
317 (CharType
*)realloc(this->data(), (len
+ 1) * sizeof(CharType
));
321 if ( this->m_data
== this->GetNullData() )
323 this->m_data
= new Data(str
, len
);
327 this->m_data
->Set(str
, len
);
328 this->m_data
->m_owned
= true;
334 void shrink(size_t len
)
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" );
339 wxASSERT( len
<= this->length() );
341 this->m_data
->m_length
= len
;
342 this->data()[len
] = 0;
346 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer
<char> )
348 class wxCharBuffer
: public wxCharTypeBuffer
<char>
351 typedef wxCharTypeBuffer
<char> wxCharTypeBufferBase
;
352 typedef wxScopedCharTypeBuffer
<char> wxScopedCharTypeBufferBase
;
354 wxCharBuffer(const wxCharTypeBufferBase
& buf
)
355 : wxCharTypeBufferBase(buf
) {}
356 wxCharBuffer(const wxScopedCharTypeBufferBase
& buf
)
357 : wxCharTypeBufferBase(buf
) {}
359 wxCharBuffer(const CharType
*str
= NULL
) : wxCharTypeBufferBase(str
) {}
360 wxCharBuffer(size_t len
) : wxCharTypeBufferBase(len
) {}
362 wxCharBuffer(const wxCStrData
& cstr
);
366 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer
<wchar_t> )
368 class wxWCharBuffer
: public wxCharTypeBuffer
<wchar_t>
371 typedef wxCharTypeBuffer
<wchar_t> wxCharTypeBufferBase
;
372 typedef wxScopedCharTypeBuffer
<wchar_t> wxScopedCharTypeBufferBase
;
374 wxWCharBuffer(const wxCharTypeBufferBase
& buf
)
375 : wxCharTypeBufferBase(buf
) {}
376 wxWCharBuffer(const wxScopedCharTypeBufferBase
& buf
)
377 : wxCharTypeBufferBase(buf
) {}
379 wxWCharBuffer(const CharType
*str
= NULL
) : wxCharTypeBufferBase(str
) {}
380 wxWCharBuffer(size_t len
) : wxCharTypeBufferBase(len
) {}
382 wxWCharBuffer(const wxCStrData
& cstr
);
384 #endif // wxUSE_WCHAR_T
386 // wxCharTypeBuffer<T> implicitly convertible to T*
387 template <typename T
>
388 class wxWritableCharTypeBuffer
: public wxCharTypeBuffer
<T
>
391 typedef typename wxScopedCharTypeBuffer
<T
>::CharType CharType
;
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
399 wxWritableCharTypeBuffer(const CharType
*str
= NULL
)
400 : wxCharTypeBuffer
<T
>(str
) {}
402 operator CharType
*() { return this->data(); }
405 typedef wxWritableCharTypeBuffer
<char> wxWritableCharBuffer
;
406 typedef wxWritableCharTypeBuffer
<wchar_t> wxWritableWCharBuffer
;
410 #define wxWxCharBuffer wxWCharBuffer
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
422 #define wxWxCharBuffer wxCharBuffer
424 #define wxMB2WXbuf wxChar*
425 #define wxWX2MBbuf wxChar*
426 #define wxWC2WXbuf wxCharBuffer
427 #define wxWX2WCbuf wxWCharBuffer
428 #endif // Unicode/ANSI
430 // type of the value returned by wxString::utf8_str()
431 #if wxUSE_UNICODE_UTF8
432 #define wxUTF8Buf char *
434 #define wxUTF8Buf wxCharBuffer
437 // ----------------------------------------------------------------------------
438 // A class for holding growable data buffers (not necessarily strings)
439 // ----------------------------------------------------------------------------
441 // This class manages the actual data buffer pointer and is ref-counted.
442 class wxMemoryBufferData
445 // the initial size and also the size added by ResizeIfNeeded()
446 enum { DefBufSize
= 1024 };
448 friend class wxMemoryBuffer
;
450 // everyting is private as it can only be used by wxMemoryBuffer
452 wxMemoryBufferData(size_t size
= wxMemoryBufferData::DefBufSize
)
453 : m_data(size
? malloc(size
) : NULL
), m_size(size
), m_len(0), m_ref(0)
456 ~wxMemoryBufferData() { free(m_data
); }
459 void ResizeIfNeeded(size_t newSize
)
461 if (newSize
> m_size
)
463 void *dataOld
= m_data
;
464 m_data
= realloc(m_data
, newSize
+ wxMemoryBufferData::DefBufSize
);
470 m_size
= newSize
+ wxMemoryBufferData::DefBufSize
;
474 void IncRef() { m_ref
+= 1; }
478 if (m_ref
== 0) // are there no more references?
483 // the buffer containing the data
486 // the size of the buffer
489 // the amount of data currently in the buffer
492 // the reference count
495 wxDECLARE_NO_COPY_CLASS(wxMemoryBufferData
);
503 wxMemoryBuffer(size_t size
= wxMemoryBufferData::DefBufSize
)
505 m_bufdata
= new wxMemoryBufferData(size
);
509 ~wxMemoryBuffer() { m_bufdata
->DecRef(); }
512 // copy and assignment
513 wxMemoryBuffer(const wxMemoryBuffer
& src
)
514 : m_bufdata(src
.m_bufdata
)
519 wxMemoryBuffer
& operator=(const wxMemoryBuffer
& src
)
524 m_bufdata
= src
.m_bufdata
;
532 void *GetData() const { return m_bufdata
->m_data
; }
533 size_t GetBufSize() const { return m_bufdata
->m_size
; }
534 size_t GetDataLen() const { return m_bufdata
->m_len
; }
536 void SetBufSize(size_t size
) { m_bufdata
->ResizeIfNeeded(size
); }
537 void SetDataLen(size_t len
)
539 wxASSERT(len
<= m_bufdata
->m_size
);
540 m_bufdata
->m_len
= len
;
543 // Ensure the buffer is big enough and return a pointer to it
544 void *GetWriteBuf(size_t sizeNeeded
)
546 m_bufdata
->ResizeIfNeeded(sizeNeeded
);
547 return m_bufdata
->m_data
;
550 // Update the length after the write
551 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
553 // Like the above, but appends to the buffer
554 void *GetAppendBuf(size_t sizeNeeded
)
556 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ sizeNeeded
);
557 return (char*)m_bufdata
->m_data
+ m_bufdata
->m_len
;
560 // Update the length after the append
561 void UngetAppendBuf(size_t sizeUsed
)
563 SetDataLen(m_bufdata
->m_len
+ sizeUsed
);
566 // Other ways to append to the buffer
567 void AppendByte(char data
)
569 wxCHECK_RET( m_bufdata
->m_data
, _T("invalid wxMemoryBuffer") );
571 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ 1);
572 *(((char*)m_bufdata
->m_data
) + m_bufdata
->m_len
) = data
;
573 m_bufdata
->m_len
+= 1;
576 void AppendData(const void *data
, size_t len
)
578 memcpy(GetAppendBuf(len
), data
, len
);
582 operator const char *() const { return (const char*)GetData(); }
585 wxMemoryBufferData
* m_bufdata
;
588 // ----------------------------------------------------------------------------
589 // template class for any kind of data
590 // ----------------------------------------------------------------------------
594 #endif // _WX_BUFFER_H