]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/buffer.h
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(size_t len) \
34 : m_str((chartype *)malloc((len + 1)*sizeof(chartype))) \
36 m_str[len] = (chartype)0; \
39 /* no need to check for NULL, free() does it */ \
40 ~classname() { free(m_str); } \
45 the copy ctor and assignment operators change the passed in object \
46 even although it is declared as "const", so: \
48 a) it shouldn't be really const \
49 b) you shouldn't use it afterwards (or know that it was reset) \
51 This is very ugly but is unfortunately needed to make the normal use\
52 of classname buffer objects possible and is very similar to what \
53 std::auto_ptr<> does (as if it were an excuse...) \
57 because of the remark above, release() is declared const even if it \
60 chartype *release() const \
62 chartype *p = m_str; \
63 ((classname *)this)->m_str = NULL; \
73 classname(const classname& src) \
74 : m_str(src.release()) \
78 classname& operator=(const chartype *str) \
81 m_str = str ? strdupfunc(str) : NULL; \
85 classname& operator=(const classname& src) \
88 m_str = src.release(); \
93 bool extend(size_t len) \
96 str = (chartype *)realloc(m_str, (len + 1)*sizeof(chartype)); \
105 chartype *data() { return m_str; } \
106 const chartype *data() const { return m_str; } \
107 operator const chartype *() const { return m_str; } \
108 chartype operator[](size_t n) const { return m_str[n]; } \
114 DEFINE_BUFFER(wxCharBuffer
, char, wxStrdupA
);
118 DEFINE_BUFFER(wxWCharBuffer
, wchar_t, wxStrdupW
);
120 #endif // wxUSE_WCHAR_T
125 #define wxMB2WXbuf wxWCharBuffer
126 #define wxWX2MBbuf wxCharBuffer
127 #define wxWC2WXbuf wxChar*
128 #define wxWX2WCbuf wxChar*
130 #define wxMB2WXbuf wxChar*
131 #define wxWX2MBbuf wxChar*
132 #define wxWC2WXbuf wxCharBuffer
133 #define wxWX2WCbuf wxWCharBuffer
134 #endif // Unicode/ANSI
136 // ----------------------------------------------------------------------------
137 // A class for holding growable data buffers (not necessarily strings)
138 // ----------------------------------------------------------------------------
140 // This class manages the actual data buffer pointer and is ref-counted.
141 class wxMemoryBufferData
144 // the initial size and also the size added by ResizeIfNeeded()
145 enum { DefBufSize
= 1024 };
147 friend class wxMemoryBuffer
;
149 // everyting is private as it can only be used by wxMemoryBuffer
151 wxMemoryBufferData(size_t size
= wxMemoryBufferData::DefBufSize
)
152 : m_data(size
? malloc(size
) : NULL
), m_size(size
), m_len(0), m_ref(0)
155 ~wxMemoryBufferData() { free(m_data
); }
158 void ResizeIfNeeded(size_t newSize
)
160 if (newSize
> m_size
)
162 void *dataOld
= m_data
;
163 m_data
= realloc(m_data
, newSize
+ wxMemoryBufferData::DefBufSize
);
169 m_size
= newSize
+ wxMemoryBufferData::DefBufSize
;
173 void IncRef() { m_ref
+= 1; }
177 if (m_ref
== 0) // are there no more references?
182 // the buffer containing the data
185 // the size of the buffer
188 // the amount of data currently in the buffer
191 // the reference count
194 DECLARE_NO_COPY_CLASS(wxMemoryBufferData
)
202 wxMemoryBuffer(size_t size
= wxMemoryBufferData::DefBufSize
)
204 m_bufdata
= new wxMemoryBufferData(size
);
208 ~wxMemoryBuffer() { m_bufdata
->DecRef(); }
211 // copy and assignment
212 wxMemoryBuffer(const wxMemoryBuffer
& src
)
213 : m_bufdata(src
.m_bufdata
)
218 wxMemoryBuffer
& operator=(const wxMemoryBuffer
& src
)
221 m_bufdata
= src
.m_bufdata
;
228 void *GetData() const { return m_bufdata
->m_data
; }
229 size_t GetBufSize() const { return m_bufdata
->m_size
; }
230 size_t GetDataLen() const { return m_bufdata
->m_len
; }
232 void SetBufSize(size_t size
) { m_bufdata
->ResizeIfNeeded(size
); }
233 void SetDataLen(size_t len
)
235 wxASSERT(len
<= m_bufdata
->m_size
);
236 m_bufdata
->m_len
= len
;
239 // Ensure the buffer is big enough and return a pointer to it
240 void *GetWriteBuf(size_t sizeNeeded
)
242 m_bufdata
->ResizeIfNeeded(sizeNeeded
);
243 return m_bufdata
->m_data
;
246 // Update the length after the write
247 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
249 // Like the above, but appends to the buffer
250 void *GetAppendBuf(size_t sizeNeeded
)
252 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ sizeNeeded
);
253 return (char*)m_bufdata
->m_data
+ m_bufdata
->m_len
;
256 // Update the length after the append
257 void UngetAppendBuf(size_t sizeUsed
)
259 SetDataLen(m_bufdata
->m_len
+ sizeUsed
);
262 // Other ways to append to the buffer
263 void AppendByte(char data
)
265 wxCHECK_RET( m_bufdata
->m_data
, _T("invalid wxMemoryBuffer") );
267 m_bufdata
->ResizeIfNeeded(m_bufdata
->m_len
+ 1);
268 *(((char*)m_bufdata
->m_data
) + m_bufdata
->m_len
) = data
;
269 m_bufdata
->m_len
+= 1;
272 void AppendData(const void *data
, size_t len
)
274 memcpy(GetAppendBuf(len
), data
, len
);
278 operator const char *() const { return (const char*)GetData(); }
281 wxMemoryBufferData
* m_bufdata
;
284 // ----------------------------------------------------------------------------
285 // template class for any kind of data
286 // ----------------------------------------------------------------------------
290 #endif // _WX_BUFFER_H