]> git.saurik.com Git - wxWidgets.git/blame - include/wx/buffer.h
use proper dllexport declaration with _WX_LIST_HELPER_
[wxWidgets.git] / include / wx / buffer.h
CommitLineData
14971e5b 1///////////////////////////////////////////////////////////////////////////////
c12ef8a2 2// Name: wx/buffer.h
14971e5b
VZ
3// Purpose: auto buffer classes: buffers which automatically free memory
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 12.04.99
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
14971e5b
VZ
10///////////////////////////////////////////////////////////////////////////////
11
14971e5b
VZ
12#ifndef _WX_BUFFER_H
13#define _WX_BUFFER_H
14
853d7d3d 15#include "wx/wxchar.h"
e90c1d2a 16
e2473d4c
VZ
17#include <stdlib.h> // malloc() and free()
18
14971e5b
VZ
19// ----------------------------------------------------------------------------
20// Special classes for (wide) character strings: they use malloc/free instead
21// of new/delete
22// ----------------------------------------------------------------------------
23
2b5f62a0 24#define DEFINE_BUFFER(classname, chartype, strdupfunc) \
b08a2401 25class WXDLLIMPEXP_BASE classname \
2b5f62a0
VZ
26{ \
27public: \
fedde9b9 28 classname(const chartype *str = NULL) \
2b5f62a0
VZ
29 : m_str(str ? strdupfunc(str) : NULL) \
30 { \
31 } \
32 \
fedde9b9 33 classname(size_t len) \
2b5f62a0
VZ
34 : m_str((chartype *)malloc((len + 1)*sizeof(chartype))) \
35 { \
36 m_str[len] = (chartype)0; \
37 } \
38 \
39 /* no need to check for NULL, free() does it */ \
40 ~classname() { free(m_str); } \
41 \
42 /* \
43 WARNING: \
44 \
45 the copy ctor and assignment operators change the passed in object \
46 even although it is declared as "const", so: \
47 \
48 a) it shouldn't be really const \
49 b) you shouldn't use it afterwards (or know that it was reset) \
50 \
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...) \
54 */ \
55 \
56 /* \
57 because of the remark above, release() is declared const even if it \
58 isn't really const \
59 */ \
60 chartype *release() const \
61 { \
62 chartype *p = m_str; \
63 ((classname *)this)->m_str = NULL; \
64 return p; \
65 } \
66 \
67 classname(const classname& src) \
68 : m_str(src.release()) \
69 { \
70 } \
71 \
72 classname& operator=(const chartype *str) \
73 { \
74 free(m_str); \
75 m_str = str ? strdupfunc(str) : NULL; \
76 return *this; \
77 } \
78 \
79 classname& operator=(const classname& src) \
80 { \
81 free(m_str); \
82 m_str = src.release(); \
83 \
84 return *this; \
85 } \
86 \
6901ac37
VZ
87 bool extend(size_t len) \
88 { \
89 chartype * \
90 str = (chartype *)realloc(m_str, (len + 1)*sizeof(chartype)); \
91 if ( !str ) \
92 return false; \
93 \
94 m_str = str; \
95 \
96 return true; \
97 } \
98 \
2b5f62a0
VZ
99 chartype *data() { return m_str; } \
100 const chartype *data() const { return m_str; } \
101 operator const chartype *() const { return m_str; } \
102 chartype operator[](size_t n) const { return m_str[n]; } \
103 \
104private: \
105 chartype *m_str; \
106}
107
07243717 108DEFINE_BUFFER(wxCharBuffer, char, wxStrdupA);
14971e5b 109
7a3e402c 110#if wxUSE_WCHAR_T
c12ef8a2 111
07243717 112DEFINE_BUFFER(wxWCharBuffer, wchar_t, wxStrdupW);
c12ef8a2
VZ
113
114#endif // wxUSE_WCHAR_T
14971e5b 115
2b5f62a0
VZ
116#undef DEFINE_BUFFER
117
f93d01be 118#if wxUSE_UNICODE
e90c1d2a
VZ
119 #define wxMB2WXbuf wxWCharBuffer
120 #define wxWX2MBbuf wxCharBuffer
121 #define wxWC2WXbuf wxChar*
122 #define wxWX2WCbuf wxChar*
123#else // ANSI
124 #define wxMB2WXbuf wxChar*
125 #define wxWX2MBbuf wxChar*
126 #define wxWC2WXbuf wxCharBuffer
127 #define wxWX2WCbuf wxWCharBuffer
128#endif // Unicode/ANSI
f93d01be 129
b9fdb397
RD
130// ----------------------------------------------------------------------------
131// A class for holding growable data buffers (not necessarily strings)
132// ----------------------------------------------------------------------------
133
2b5f62a0
VZ
134// This class manages the actual data buffer pointer and is ref-counted.
135class wxMemoryBufferData
b9fdb397
RD
136{
137public:
2b5f62a0 138 // the initial size and also the size added by ResizeIfNeeded()
ae0ca755 139 enum { DefBufSize = 1024 };
2b5f62a0
VZ
140
141 friend class wxMemoryBuffer;
142
143 // everyting is private as it can only be used by wxMemoryBuffer
144private:
ae0ca755 145 wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize)
2b5f62a0 146 : m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0)
b9fdb397 147 {
b9fdb397 148 }
2b5f62a0 149 ~wxMemoryBufferData() { free(m_data); }
b9fdb397 150
b9fdb397 151
2b5f62a0 152 void ResizeIfNeeded(size_t newSize)
b9fdb397 153 {
2b5f62a0
VZ
154 if (newSize > m_size)
155 {
156 void *dataOld = m_data;
ae0ca755 157 m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize);
2b5f62a0
VZ
158 if ( !m_data )
159 {
160 free(dataOld);
161 }
162
ae0ca755 163 m_size = newSize + wxMemoryBufferData::DefBufSize;
2b5f62a0 164 }
b9fdb397
RD
165 }
166
2b5f62a0
VZ
167 void IncRef() { m_ref += 1; }
168 void DecRef()
b9fdb397 169 {
2b5f62a0
VZ
170 m_ref -= 1;
171 if (m_ref == 0) // are there no more references?
172 delete this;
b9fdb397 173 }
b9fdb397 174
2b5f62a0
VZ
175
176 // the buffer containing the data
177 void *m_data;
178
179 // the size of the buffer
180 size_t m_size;
181
182 // the amount of data currently in the buffer
183 size_t m_len;
184
185 // the reference count
186 size_t m_ref;
22f3361e
VZ
187
188 DECLARE_NO_COPY_CLASS(wxMemoryBufferData)
2b5f62a0
VZ
189};
190
191
192class wxMemoryBuffer
193{
194public:
195 // ctor and dtor
ae0ca755 196 wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize)
b9fdb397 197 {
2b5f62a0
VZ
198 m_bufdata = new wxMemoryBufferData(size);
199 m_bufdata->IncRef();
b9fdb397 200 }
b9fdb397 201
2b5f62a0
VZ
202 ~wxMemoryBuffer() { m_bufdata->DecRef(); }
203
204
205 // copy and assignment
206 wxMemoryBuffer(const wxMemoryBuffer& src)
207 : m_bufdata(src.m_bufdata)
208 {
209 m_bufdata->IncRef();
b9fdb397 210 }
2b5f62a0
VZ
211
212 wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
b9fdb397 213 {
2b5f62a0
VZ
214 m_bufdata->DecRef();
215 m_bufdata = src.m_bufdata;
216 m_bufdata->IncRef();
217 return *this;
b9fdb397
RD
218 }
219
b9fdb397 220
2b5f62a0
VZ
221 // Accessors
222 void *GetData() const { return m_bufdata->m_data; }
223 size_t GetBufSize() const { return m_bufdata->m_size; }
224 size_t GetDataLen() const { return m_bufdata->m_len; }
ef1cae87 225
2b5f62a0
VZ
226 void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); }
227 void SetDataLen(size_t len)
ef1cae87 228 {
2b5f62a0
VZ
229 wxASSERT(len <= m_bufdata->m_size);
230 m_bufdata->m_len = len;
ef1cae87
RD
231 }
232
2b5f62a0
VZ
233 // Ensure the buffer is big enough and return a pointer to it
234 void *GetWriteBuf(size_t sizeNeeded)
ef1cae87 235 {
2b5f62a0
VZ
236 m_bufdata->ResizeIfNeeded(sizeNeeded);
237 return m_bufdata->m_data;
238 }
c12ef8a2 239
2b5f62a0
VZ
240 // Update the length after the write
241 void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
ef1cae87 242
2b5f62a0
VZ
243 // Like the above, but appends to the buffer
244 void *GetAppendBuf(size_t sizeNeeded)
245 {
246 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + sizeNeeded);
247 return (char*)m_bufdata->m_data + m_bufdata->m_len;
248 }
ef1cae87 249
2b5f62a0
VZ
250 // Update the length after the append
251 void UngetAppendBuf(size_t sizeUsed)
252 {
253 SetDataLen(m_bufdata->m_len + sizeUsed);
254 }
ef1cae87 255
2b5f62a0
VZ
256 // Other ways to append to the buffer
257 void AppendByte(char data)
b9fdb397 258 {
2b5f62a0
VZ
259 wxCHECK_RET( m_bufdata->m_data, _T("invalid wxMemoryBuffer") );
260
261 m_bufdata->ResizeIfNeeded(m_bufdata->m_len + 1);
262 *(((char*)m_bufdata->m_data) + m_bufdata->m_len) = data;
263 m_bufdata->m_len += 1;
264 }
265
68bc51a9 266 void AppendData(const void *data, size_t len)
2b5f62a0
VZ
267 {
268 memcpy(GetAppendBuf(len), data, len);
269 UngetAppendBuf(len);
b9fdb397
RD
270 }
271
2b5f62a0
VZ
272 operator const char *() const { return (const char*)GetData(); }
273
b9fdb397 274private:
2b5f62a0 275 wxMemoryBufferData* m_bufdata;
b9fdb397
RD
276};
277
14971e5b
VZ
278// ----------------------------------------------------------------------------
279// template class for any kind of data
280// ----------------------------------------------------------------------------
281
282// TODO
283
284#endif // _WX_BUFFER_H