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"
18 #include <stdlib.h> // malloc() and free()
20 class WXDLLIMPEXP_FWD_BASE wxCStrData
;
22 // ----------------------------------------------------------------------------
23 // Special classes for (wide) character strings: they use malloc/free instead
25 // ----------------------------------------------------------------------------
27 // helpers used by wxCharTypeBuffer
31 struct UntypedBufferData
39 UntypedBufferData(void *str
, size_t len
, Kind kind
= Owned
)
40 : m_str(str
), m_length(len
), m_ref(1), m_owned(kind
== Owned
) {}
51 // "short" to have sizeof(Data)=12 on 32bit archs
57 // NB: this is defined in string.cpp and not the (non-existent) buffer.cpp
58 WXDLLIMPEXP_BASE UntypedBufferData
* GetUntypedNullData();
60 } // namespace wxPrivate
63 // Reference-counted character buffer for storing string data. The buffer
64 // is only valid for as long as the "parent" object that provided the data
65 // is valid; see wxCharTypeBuffer<T> for persistent variant.
67 class wxScopedCharTypeBuffer
72 wxScopedCharTypeBuffer()
74 m_data
= GetNullData();
77 // Creates "non-owned" buffer, i.e. 'str' is not owned by the buffer
78 // and doesn't get freed by dtor. Used e.g. to point to wxString's internal
81 const wxScopedCharTypeBuffer
CreateNonOwned(const CharType
*str
,
82 size_t len
= wxNO_LEN
)
84 if ( len
== wxNO_LEN
)
87 wxScopedCharTypeBuffer buf
;
89 buf
.m_data
= new Data(const_cast<CharType
*>(str
), len
, Data::NonOwned
);
93 // Creates "owned" buffer, i.e. takes over ownership of 'str' and frees it
94 // in dtor (if ref.count reaches 0).
96 const wxScopedCharTypeBuffer
CreateOwned(CharType
*str
,
97 size_t len
= wxNO_LEN
)
99 if ( len
== wxNO_LEN
)
102 wxScopedCharTypeBuffer buf
;
104 buf
.m_data
= new Data(str
, len
);
108 wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer
& src
)
114 wxScopedCharTypeBuffer
& operator=(const wxScopedCharTypeBuffer
& src
)
126 ~wxScopedCharTypeBuffer()
131 // NB: this method is only const for backward compatibility. It used to
132 // be needed for auto_ptr-like semantics of the copy ctor, but now
133 // that ref-counting is used, it's not really needed.
134 CharType
*release() const
136 if ( m_data
== GetNullData() )
139 wxASSERT_MSG( m_data
->m_owned
, wxT("can't release non-owned buffer") );
140 wxASSERT_MSG( m_data
->m_ref
== 1, wxT("can't release shared buffer") );
142 CharType
* const p
= m_data
->Get();
144 wxScopedCharTypeBuffer
*self
= const_cast<wxScopedCharTypeBuffer
*>(this);
145 self
->m_data
->Set(NULL
, 0);
156 CharType
*data() { return m_data
->Get(); }
157 const CharType
*data() const { return m_data
->Get(); }
158 operator const CharType
*() const { return data(); }
159 CharType
operator[](size_t n
) const { return data()[n
]; }
161 size_t length() const { return m_data
->m_length
; }
164 // reference-counted data
165 struct Data
: public wxPrivate::UntypedBufferData
167 Data(CharType
*str
, size_t len
, Kind kind
= Owned
)
168 : wxPrivate::UntypedBufferData(str
, len
, kind
)
172 CharType
*Get() const { return static_cast<CharType
*>(m_str
); }
173 void Set(CharType
*str
, size_t len
)
180 // placeholder for NULL string, to simplify this code
181 static Data
*GetNullData()
183 return static_cast<Data
*>(wxPrivate::GetUntypedNullData());
188 if ( m_data
== GetNullData() ) // exception, not ref-counted
195 if ( m_data
== GetNullData() ) // exception, not ref-counted
197 if ( --m_data
->m_ref
== 0 )
199 m_data
= GetNullData();
202 // sets this object to a be copy of 'other'; if 'src' is non-owned,
203 // a deep copy is made and 'this' will contain new instance of the data
204 void MakeOwnedCopyOf(const wxScopedCharTypeBuffer
& src
)
208 if ( src
.m_data
== this->GetNullData() )
210 this->m_data
= this->GetNullData();
212 else if ( src
.m_data
->m_owned
)
214 this->m_data
= src
.m_data
;
219 // if the scoped buffer had non-owned data, we have to make
220 // a copy here, because src.m_data->m_str is valid only for as long
222 this->m_data
= new Data
224 StrCopy(src
.data(), src
.length()),
230 static CharType
*StrCopy(const CharType
*src
, size_t len
)
232 CharType
*dst
= (CharType
*)malloc(sizeof(CharType
) * (len
+ 1));
233 memcpy(dst
, src
, sizeof(CharType
) * (len
+ 1));
241 typedef wxScopedCharTypeBuffer
<char> wxScopedCharBuffer
;
242 typedef wxScopedCharTypeBuffer
<wchar_t> wxScopedWCharBuffer
;
245 // this buffer class always stores data in "owned" (persistent) manner
246 template <typename T
>
247 class wxCharTypeBuffer
: public wxScopedCharTypeBuffer
<T
>
250 typedef typename wxScopedCharTypeBuffer
<T
>::Data Data
;
255 wxCharTypeBuffer(const CharType
*str
= NULL
, size_t len
= wxNO_LEN
)
259 if ( len
== wxNO_LEN
)
261 this->m_data
= new Data(this->StrCopy(str
, len
), len
);
265 this->m_data
= this->GetNullData();
269 wxCharTypeBuffer(size_t len
)
272 new Data((CharType
*)malloc((len
+ 1)*sizeof(CharType
)), len
);
273 this->m_data
->Get()[len
] = (CharType
)0;
276 wxCharTypeBuffer(const wxCharTypeBuffer
& src
)
277 : wxScopedCharTypeBuffer
<T
>(src
) {}
279 wxCharTypeBuffer
& operator=(const CharType
*str
)
284 this->m_data
= new Data(wxStrdup(str
), wxStrlen(str
));
288 wxCharTypeBuffer
& operator=(const wxCharTypeBuffer
& src
)
290 wxScopedCharTypeBuffer
<T
>::operator=(src
);
294 wxCharTypeBuffer(const wxScopedCharTypeBuffer
<T
>& src
)
296 this->MakeOwnedCopyOf(src
);
299 wxCharTypeBuffer
& operator=(const wxScopedCharTypeBuffer
<T
>& src
)
301 MakeOwnedCopyOf(src
);
305 bool extend(size_t len
)
307 wxASSERT_MSG( this->m_data
->m_owned
, "cannot extend non-owned buffer" );
308 wxASSERT_MSG( this->m_data
->m_ref
== 1, "can't extend shared buffer" );
311 (CharType
*)realloc(this->data(), (len
+ 1) * sizeof(CharType
));
315 if ( this->m_data
== this->GetNullData() )
317 this->m_data
= new Data(str
, len
);
321 this->m_data
->Set(str
, len
);
322 this->m_data
->m_owned
= true;
328 void shrink(size_t len
)
330 wxASSERT_MSG( this->m_data
->m_owned
, "cannot shrink non-owned buffer" );
331 wxASSERT_MSG( this->m_data
->m_ref
== 1, "can't shrink shared buffer" );
333 wxASSERT( len
<= this->length() );
335 this->m_data
->m_length
= len
;
336 this->data()[len
] = 0;
340 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxScopedCharTypeBuffer
<char> )
341 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer
<char> )
343 class wxCharBuffer
: public wxCharTypeBuffer
<char>
346 typedef wxCharTypeBuffer
<char> wxCharTypeBufferBase
;
347 typedef wxScopedCharTypeBuffer
<char> wxScopedCharTypeBufferBase
;
349 wxCharBuffer(const wxCharTypeBufferBase
& buf
)
350 : wxCharTypeBufferBase(buf
) {}
351 wxCharBuffer(const wxScopedCharTypeBufferBase
& buf
)
352 : wxCharTypeBufferBase(buf
) {}
354 wxCharBuffer(const CharType
*str
= NULL
) : wxCharTypeBufferBase(str
) {}
355 wxCharBuffer(size_t len
) : wxCharTypeBufferBase(len
) {}
357 wxCharBuffer(const wxCStrData
& cstr
);
360 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxScopedCharTypeBuffer
<wchar_t> )
361 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer
<wchar_t> )
363 class wxWCharBuffer
: public wxCharTypeBuffer
<wchar_t>
366 typedef wxCharTypeBuffer
<wchar_t> wxCharTypeBufferBase
;
367 typedef wxScopedCharTypeBuffer
<wchar_t> wxScopedCharTypeBufferBase
;
369 wxWCharBuffer(const wxCharTypeBufferBase
& buf
)
370 : wxCharTypeBufferBase(buf
) {}
371 wxWCharBuffer(const wxScopedCharTypeBufferBase
& buf
)
372 : wxCharTypeBufferBase(buf
) {}
374 wxWCharBuffer(const CharType
*str
= NULL
) : wxCharTypeBufferBase(str
) {}
375 wxWCharBuffer(size_t len
) : wxCharTypeBufferBase(len
) {}
377 wxWCharBuffer(const wxCStrData
& cstr
);
380 // wxCharTypeBuffer<T> implicitly convertible to T*
381 template <typename T
>
382 class wxWritableCharTypeBuffer
: public wxCharTypeBuffer
<T
>
385 typedef typename wxScopedCharTypeBuffer
<T
>::CharType CharType
;
387 wxWritableCharTypeBuffer(const wxScopedCharTypeBuffer
<T
>& src
)
388 : wxCharTypeBuffer
<T
>(src
) {}
389 // FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to
390 // always return a buffer
391 // + we should derive this class from wxScopedCharTypeBuffer
393 wxWritableCharTypeBuffer(const CharType
*str
= NULL
)
394 : wxCharTypeBuffer
<T
>(str
) {}
396 operator CharType
*() { return this->data(); }
399 typedef wxWritableCharTypeBuffer
<char> wxWritableCharBuffer
;
400 typedef wxWritableCharTypeBuffer
<wchar_t> wxWritableWCharBuffer
;
404 #define wxWxCharBuffer wxWCharBuffer
406 #define wxMB2WXbuf wxWCharBuffer
407 #define wxWX2MBbuf wxCharBuffer
408 #if wxUSE_UNICODE_WCHAR
409 #define wxWC2WXbuf wxChar*
410 #define wxWX2WCbuf wxChar*
411 #elif wxUSE_UNICODE_UTF8
412 #define wxWC2WXbuf wxWCharBuffer
413 #define wxWX2WCbuf wxWCharBuffer
416 #define wxWxCharBuffer wxCharBuffer
418 #define wxMB2WXbuf wxChar*
419 #define wxWX2MBbuf wxChar*
420 #define wxWC2WXbuf wxCharBuffer
421 #define wxWX2WCbuf wxWCharBuffer
422 #endif // Unicode/ANSI
424 // ----------------------------------------------------------------------------
425 // A class for holding growable data buffers (not necessarily strings)
426 // ----------------------------------------------------------------------------
428 // This class manages the actual data buffer pointer and is ref-counted.
429 class wxMemoryBufferData
432 // the initial size and also the size added by ResizeIfNeeded()
433 enum { DefBufSize
= 1024 };
435 friend class wxMemoryBuffer
;
437 // everyting is private as it can only be used by wxMemoryBuffer
439 wxMemoryBufferData(size_t size
= wxMemoryBufferData::DefBufSize
)
440 : m_data(size
? malloc(size
) : NULL
), m_size(size
), m_len(0), m_ref(0)
443 ~wxMemoryBufferData() { free(m_data
); }
446 void ResizeIfNeeded(size_t newSize
)
448 if (newSize
> m_size
)
450 void *dataOld
= m_data
;
451 m_data
= realloc(m_data
, newSize
+ wxMemoryBufferData::DefBufSize
);
457 m_size
= newSize
+ wxMemoryBufferData::DefBufSize
;
461 void IncRef() { m_ref
+= 1; }
465 if (m_ref
== 0) // are there no more references?
471 if ( m_data
== NULL
)
474 wxASSERT_MSG( m_ref
== 1, "can't release shared buffer" );
485 // the buffer containing the data
488 // the size of the buffer
491 // the amount of data currently in the buffer
494 // the reference count
497 wxDECLARE_NO_COPY_CLASS(wxMemoryBufferData
);
505 wxMemoryBuffer(size_t size
= wxMemoryBufferData::DefBufSize
)
507 m_bufdata
= new wxMemoryBufferData(size
);
511 ~wxMemoryBuffer() { m_bufdata
->DecRef(); }
514 // copy and assignment
515 wxMemoryBuffer(const wxMemoryBuffer
& src
)
516 : m_bufdata(src
.m_bufdata
)
521 wxMemoryBuffer
& operator=(const wxMemoryBuffer
& src
)
526 m_bufdata
= src
.m_bufdata
;
534 void *GetData() const { return m_bufdata
->m_data
; }
535 size_t GetBufSize() const { return m_bufdata
->m_size
; }
536 size_t GetDataLen() const { return m_bufdata
->m_len
; }
538 void SetBufSize(size_t size
) { m_bufdata
->ResizeIfNeeded(size
); }
539 void SetDataLen(size_t len
)
541 wxASSERT(len
<= m_bufdata
->m_size
);
542 m_bufdata
->m_len
= len
;
545 // Ensure the buffer is big enough and return a pointer to it
546 void *GetWriteBuf(size_t sizeNeeded
)
548 m_bufdata
->ResizeIfNeeded(sizeNeeded
);
549 return m_bufdata
->m_data
;
552 // Update the length after the write
553 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
555 // Like the above, but appends to the buffer
556 void *GetAppendBuf(size_t sizeNeeded
)
558 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ sizeNeeded
);
559 return (char*)m_bufdata
->m_data
+ m_bufdata
->m_len
;
562 // Update the length after the append
563 void UngetAppendBuf(size_t sizeUsed
)
565 SetDataLen(m_bufdata
->m_len
+ sizeUsed
);
568 // Other ways to append to the buffer
569 void AppendByte(char data
)
571 wxCHECK_RET( m_bufdata
->m_data
, wxT("invalid wxMemoryBuffer") );
573 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ 1);
574 *(((char*)m_bufdata
->m_data
) + m_bufdata
->m_len
) = data
;
575 m_bufdata
->m_len
+= 1;
578 void AppendData(const void *data
, size_t len
)
580 memcpy(GetAppendBuf(len
), data
, len
);
584 operator const char *() const { return (const char*)GetData(); }
586 // gives up ownership of data, returns the pointer; after this call,
587 // data isn't freed by the buffer and its content is resent to empty
590 return m_bufdata
->release();
594 wxMemoryBufferData
* m_bufdata
;
597 // ----------------------------------------------------------------------------
598 // template class for any kind of data
599 // ----------------------------------------------------------------------------
603 #endif // _WX_BUFFER_H