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 // ----------------------------------------------------------------------------
28 class WXDLLIMPEXP_BASE wxCharTypeBuffer
33 wxCharTypeBuffer(const CharType
*str
= NULL
)
34 : m_str(str
? wxStrdup(str
) : NULL
),
39 wxCharTypeBuffer(size_t len
)
40 : m_str((CharType
*)malloc((len
+ 1)*sizeof(CharType
))),
43 m_str
[len
] = (CharType
)0;
46 static const wxCharTypeBuffer
CreateNonOwned(const CharType
*str
)
49 buf
.m_str
= wx_const_cast(CharType
*, str
);
54 /* no need to check for NULL, free() does it */
64 the copy ctor and assignment operators change the passed in object
65 even although it is declared as "const", so:
67 a) it shouldn't be really const
68 b) you shouldn't use it afterwards (or know that it was reset)
70 This is very ugly but is unfortunately needed to make the normal use
71 of wxCharTypeBuffer buffer objects possible and is very similar to what
72 std::auto_ptr<> does (as if it were an excuse...)
76 because of the remark above, release() is declared const even if it
79 CharType
*release() const
81 wxASSERT_MSG( m_owned
, _T("can't release non-owned buffer") );
92 wxCharTypeBuffer(const wxCharTypeBuffer
& src
)
97 wxCharTypeBuffer
& operator=(const CharType
*str
)
101 m_str
= str
? wxStrdup(str
) : NULL
;
106 wxCharTypeBuffer
& operator=(const wxCharTypeBuffer
& src
)
117 bool extend(size_t len
)
119 wxASSERT_MSG( m_owned
, _T("cannot extend non-owned buffer") );
122 str
= (CharType
*)realloc(m_str
, (len
+ 1)*sizeof(CharType
));
131 CharType
*data() { return m_str
; }
132 const CharType
*data() const { return m_str
; }
133 operator const CharType
*() const { return m_str
; }
134 CharType
operator[](size_t n
) const { return m_str
[n
]; }
138 CharType
*DoRelease() const
141 ((wxCharTypeBuffer
*)this)->m_str
= NULL
;
145 void CopyFrom(const wxCharTypeBuffer
& src
)
147 m_owned
= src
.m_owned
;
148 m_str
= src
.DoRelease();
156 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer
<char> )
158 class WXDLLIMPEXP_BASE wxCharBuffer
: public wxCharTypeBuffer
<char>
161 typedef wxCharTypeBuffer
<char> wxCharTypeBufferBase
;
163 wxCharBuffer(const wxCharTypeBufferBase
& buf
)
164 : wxCharTypeBufferBase(buf
) {}
166 wxCharBuffer(const CharType
*str
= NULL
) : wxCharTypeBufferBase(str
) {}
167 wxCharBuffer(size_t len
) : wxCharTypeBufferBase(len
) {}
169 wxCharBuffer(const wxCStrData
& cstr
);
173 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer
<wchar_t> )
175 class WXDLLIMPEXP_BASE wxWCharBuffer
: public wxCharTypeBuffer
<wchar_t>
178 typedef wxCharTypeBuffer
<wchar_t> wxCharTypeBufferBase
;
180 wxWCharBuffer(const wxCharTypeBufferBase
& buf
)
181 : wxCharTypeBufferBase(buf
) {}
183 wxWCharBuffer(const CharType
*str
= NULL
) : wxCharTypeBufferBase(str
) {}
184 wxWCharBuffer(size_t len
) : wxCharTypeBufferBase(len
) {}
186 wxWCharBuffer(const wxCStrData
& cstr
);
188 #endif // wxUSE_WCHAR_T
190 // wxCharTypeBuffer<T> implicitly convertible to T*
191 template <typename T
>
192 class wxWritableCharTypeBuffer
: public wxCharTypeBuffer
<T
>
195 typedef typename wxCharTypeBuffer
<T
>::CharType CharType
;
197 wxWritableCharTypeBuffer(const wxCharTypeBuffer
<T
>& src
)
198 : wxCharTypeBuffer
<T
>(src
) {}
199 // FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to
200 // always return a buffer
201 wxWritableCharTypeBuffer(const CharType
*str
= NULL
)
202 : wxCharTypeBuffer
<T
>(str
) {}
204 operator CharType
*() { return this->data(); }
207 typedef wxWritableCharTypeBuffer
<char> wxWritableCharBuffer
;
208 typedef wxWritableCharTypeBuffer
<wchar_t> wxWritableWCharBuffer
;
212 #define wxWxCharBuffer wxWCharBuffer
214 #define wxMB2WXbuf wxWCharBuffer
215 #define wxWX2MBbuf wxCharBuffer
216 #if wxUSE_UNICODE_WCHAR
217 #define wxWC2WXbuf wxChar*
218 #define wxWX2WCbuf wxChar*
219 #elif wxUSE_UNICODE_UTF8
220 #define wxWC2WXbuf wxWCharBuffer
221 #define wxWX2WCbuf wxWCharBuffer
224 #define wxWxCharBuffer wxCharBuffer
226 #define wxMB2WXbuf wxChar*
227 #define wxWX2MBbuf wxChar*
228 #define wxWC2WXbuf wxCharBuffer
229 #define wxWX2WCbuf wxWCharBuffer
230 #endif // Unicode/ANSI
232 // type of the value returned by wxString::utf8_str()
233 #if wxUSE_UNICODE_UTF8
234 #define wxUTF8Buf char *
236 #define wxUTF8Buf wxCharBuffer
239 // ----------------------------------------------------------------------------
240 // A class for holding growable data buffers (not necessarily strings)
241 // ----------------------------------------------------------------------------
243 // This class manages the actual data buffer pointer and is ref-counted.
244 class wxMemoryBufferData
247 // the initial size and also the size added by ResizeIfNeeded()
248 enum { DefBufSize
= 1024 };
250 friend class wxMemoryBuffer
;
252 // everyting is private as it can only be used by wxMemoryBuffer
254 wxMemoryBufferData(size_t size
= wxMemoryBufferData::DefBufSize
)
255 : m_data(size
? malloc(size
) : NULL
), m_size(size
), m_len(0), m_ref(0)
258 ~wxMemoryBufferData() { free(m_data
); }
261 void ResizeIfNeeded(size_t newSize
)
263 if (newSize
> m_size
)
265 void *dataOld
= m_data
;
266 m_data
= realloc(m_data
, newSize
+ wxMemoryBufferData::DefBufSize
);
272 m_size
= newSize
+ wxMemoryBufferData::DefBufSize
;
276 void IncRef() { m_ref
+= 1; }
280 if (m_ref
== 0) // are there no more references?
285 // the buffer containing the data
288 // the size of the buffer
291 // the amount of data currently in the buffer
294 // the reference count
297 DECLARE_NO_COPY_CLASS(wxMemoryBufferData
)
301 class WXDLLIMPEXP_BASE wxMemoryBuffer
305 wxMemoryBuffer(size_t size
= wxMemoryBufferData::DefBufSize
)
307 m_bufdata
= new wxMemoryBufferData(size
);
311 ~wxMemoryBuffer() { m_bufdata
->DecRef(); }
314 // copy and assignment
315 wxMemoryBuffer(const wxMemoryBuffer
& src
)
316 : m_bufdata(src
.m_bufdata
)
321 wxMemoryBuffer
& operator=(const wxMemoryBuffer
& src
)
326 m_bufdata
= src
.m_bufdata
;
334 void *GetData() const { return m_bufdata
->m_data
; }
335 size_t GetBufSize() const { return m_bufdata
->m_size
; }
336 size_t GetDataLen() const { return m_bufdata
->m_len
; }
338 void SetBufSize(size_t size
) { m_bufdata
->ResizeIfNeeded(size
); }
339 void SetDataLen(size_t len
)
341 wxASSERT(len
<= m_bufdata
->m_size
);
342 m_bufdata
->m_len
= len
;
345 // Ensure the buffer is big enough and return a pointer to it
346 void *GetWriteBuf(size_t sizeNeeded
)
348 m_bufdata
->ResizeIfNeeded(sizeNeeded
);
349 return m_bufdata
->m_data
;
352 // Update the length after the write
353 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
355 // Like the above, but appends to the buffer
356 void *GetAppendBuf(size_t sizeNeeded
)
358 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ sizeNeeded
);
359 return (char*)m_bufdata
->m_data
+ m_bufdata
->m_len
;
362 // Update the length after the append
363 void UngetAppendBuf(size_t sizeUsed
)
365 SetDataLen(m_bufdata
->m_len
+ sizeUsed
);
368 // Other ways to append to the buffer
369 void AppendByte(char data
)
371 wxCHECK_RET( m_bufdata
->m_data
, _T("invalid wxMemoryBuffer") );
373 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ 1);
374 *(((char*)m_bufdata
->m_data
) + m_bufdata
->m_len
) = data
;
375 m_bufdata
->m_len
+= 1;
378 void AppendData(const void *data
, size_t len
)
380 memcpy(GetAppendBuf(len
), data
, len
);
384 operator const char *() const { return (const char*)GetData(); }
387 wxMemoryBufferData
* m_bufdata
;
390 // ----------------------------------------------------------------------------
391 // template class for any kind of data
392 // ----------------------------------------------------------------------------
396 #endif // _WX_BUFFER_H