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/wxchar.h"
17 #include <stdlib.h> // malloc() and free()
19 // ----------------------------------------------------------------------------
20 // Special classes for (wide) character strings: they use malloc/free instead
22 // ----------------------------------------------------------------------------
24 #define DEFINE_BUFFER(classname, chartype, strdupfunc) \
25 class WXDLLIMPEXP_BASE classname \
28 classname(const chartype *str = NULL) \
29 : m_str(str ? strdupfunc(str) : NULL) \
33 classname(const wxCStrData& cstr); \
35 classname(size_t len) \
36 : m_str((chartype *)malloc((len + 1)*sizeof(chartype))) \
38 m_str[len] = (chartype)0; \
41 /* no need to check for NULL, free() does it */ \
42 ~classname() { free(m_str); } \
47 the copy ctor and assignment operators change the passed in object \
48 even although it is declared as "const", so: \
50 a) it shouldn't be really const \
51 b) you shouldn't use it afterwards (or know that it was reset) \
53 This is very ugly but is unfortunately needed to make the normal use\
54 of classname buffer objects possible and is very similar to what \
55 std::auto_ptr<> does (as if it were an excuse...) \
59 because of the remark above, release() is declared const even if it \
62 chartype *release() const \
64 chartype *p = m_str; \
65 ((classname *)this)->m_str = NULL; \
75 classname(const classname& src) \
76 : m_str(src.release()) \
80 classname& operator=(const chartype *str) \
83 m_str = str ? strdupfunc(str) : NULL; \
87 classname& operator=(const classname& src) \
90 m_str = src.release(); \
95 bool extend(size_t len) \
98 str = (chartype *)realloc(m_str, (len + 1)*sizeof(chartype)); \
107 chartype *data() { return m_str; } \
108 const chartype *data() const { return m_str; } \
109 operator const chartype *() const { return m_str; } \
110 chartype operator[](size_t n) const { return m_str[n]; } \
116 DEFINE_BUFFER(wxCharBuffer
, char, wxStrdupA
);
120 DEFINE_BUFFER(wxWCharBuffer
, wchar_t, wxStrdupW
);
122 #endif // wxUSE_WCHAR_T
127 typedef wxWCharBuffer wxWxCharBuffer
;
129 #define wxMB2WXbuf wxWCharBuffer
130 #define wxWX2MBbuf wxCharBuffer
131 #define wxWC2WXbuf wxChar*
132 #define wxWX2WCbuf wxChar*
134 typedef wxCharBuffer wxWxCharBuffer
;
136 #define wxMB2WXbuf wxChar*
137 #define wxWX2MBbuf wxChar*
138 #define wxWC2WXbuf wxCharBuffer
139 #define wxWX2WCbuf wxWCharBuffer
140 #endif // Unicode/ANSI
142 // ----------------------------------------------------------------------------
143 // A class for holding growable data buffers (not necessarily strings)
144 // ----------------------------------------------------------------------------
146 // This class manages the actual data buffer pointer and is ref-counted.
147 class wxMemoryBufferData
150 // the initial size and also the size added by ResizeIfNeeded()
151 enum { DefBufSize
= 1024 };
153 friend class wxMemoryBuffer
;
155 // everyting is private as it can only be used by wxMemoryBuffer
157 wxMemoryBufferData(size_t size
= wxMemoryBufferData::DefBufSize
)
158 : m_data(size
? malloc(size
) : NULL
), m_size(size
), m_len(0), m_ref(0)
161 ~wxMemoryBufferData() { free(m_data
); }
164 void ResizeIfNeeded(size_t newSize
)
166 if (newSize
> m_size
)
168 void *dataOld
= m_data
;
169 m_data
= realloc(m_data
, newSize
+ wxMemoryBufferData::DefBufSize
);
175 m_size
= newSize
+ wxMemoryBufferData::DefBufSize
;
179 void IncRef() { m_ref
+= 1; }
183 if (m_ref
== 0) // are there no more references?
188 // the buffer containing the data
191 // the size of the buffer
194 // the amount of data currently in the buffer
197 // the reference count
200 DECLARE_NO_COPY_CLASS(wxMemoryBufferData
)
208 wxMemoryBuffer(size_t size
= wxMemoryBufferData::DefBufSize
)
210 m_bufdata
= new wxMemoryBufferData(size
);
214 ~wxMemoryBuffer() { m_bufdata
->DecRef(); }
217 // copy and assignment
218 wxMemoryBuffer(const wxMemoryBuffer
& src
)
219 : m_bufdata(src
.m_bufdata
)
224 wxMemoryBuffer
& operator=(const wxMemoryBuffer
& src
)
227 m_bufdata
= src
.m_bufdata
;
234 void *GetData() const { return m_bufdata
->m_data
; }
235 size_t GetBufSize() const { return m_bufdata
->m_size
; }
236 size_t GetDataLen() const { return m_bufdata
->m_len
; }
238 void SetBufSize(size_t size
) { m_bufdata
->ResizeIfNeeded(size
); }
239 void SetDataLen(size_t len
)
241 wxASSERT(len
<= m_bufdata
->m_size
);
242 m_bufdata
->m_len
= len
;
245 // Ensure the buffer is big enough and return a pointer to it
246 void *GetWriteBuf(size_t sizeNeeded
)
248 m_bufdata
->ResizeIfNeeded(sizeNeeded
);
249 return m_bufdata
->m_data
;
252 // Update the length after the write
253 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
255 // Like the above, but appends to the buffer
256 void *GetAppendBuf(size_t sizeNeeded
)
258 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ sizeNeeded
);
259 return (char*)m_bufdata
->m_data
+ m_bufdata
->m_len
;
262 // Update the length after the append
263 void UngetAppendBuf(size_t sizeUsed
)
265 SetDataLen(m_bufdata
->m_len
+ sizeUsed
);
268 // Other ways to append to the buffer
269 void AppendByte(char data
)
271 wxCHECK_RET( m_bufdata
->m_data
, _T("invalid wxMemoryBuffer") );
273 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ 1);
274 *(((char*)m_bufdata
->m_data
) + m_bufdata
->m_len
) = data
;
275 m_bufdata
->m_len
+= 1;
278 void AppendData(const void *data
, size_t len
)
280 memcpy(GetAppendBuf(len
), data
, len
);
284 operator const char *() const { return (const char*)GetData(); }
287 wxMemoryBufferData
* m_bufdata
;
290 // ----------------------------------------------------------------------------
291 // template class for any kind of data
292 // ----------------------------------------------------------------------------
296 #endif // _WX_BUFFER_H