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