]> git.saurik.com Git - wxWidgets.git/blame - include/wx/buffer.h
A few tweaks and cleanups
[wxWidgets.git] / include / wx / buffer.h
CommitLineData
14971e5b
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: buffer.h
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>
9// Licence: wxWindows license
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
f93d01be 19#include <string.h> // strdup
14971e5b
VZ
20
21// ----------------------------------------------------------------------------
22// Special classes for (wide) character strings: they use malloc/free instead
23// of new/delete
24// ----------------------------------------------------------------------------
25
26class wxCharBuffer
27{
28public:
29 wxCharBuffer(const char *str)
30 {
223d09f6 31 wxASSERT_MSG( str, wxT("NULL string in wxCharBuffer") );
14971e5b 32
01357b50 33 m_str = str ? strdup(str) : (char *)NULL;
14971e5b 34 }
f93d01be
OK
35 wxCharBuffer(size_t len)
36 {
37 m_str = (char *)malloc(len+1);
e90c1d2a 38 m_str[len] = '\0';
f93d01be 39 }
14971e5b
VZ
40 // no need to check for NULL, free() does it
41 ~wxCharBuffer() { free(m_str); }
42
f93d01be
OK
43 wxCharBuffer(const wxCharBuffer& src)
44 {
45 m_str = src.m_str;
46 // no reference count yet...
d87c0ac7 47 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
f93d01be
OK
48 }
49 wxCharBuffer& operator=(const wxCharBuffer& src)
50 {
51 m_str = src.m_str;
52 // no reference count yet...
d87c0ac7 53 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
f93d01be
OK
54 return *this;
55 }
56
f6bcfd97 57 const char *data() const { return m_str; }
14971e5b 58 operator const char *() const { return m_str; }
b1fa8b4e 59 char operator[](size_t n) const { return m_str[n]; }
14971e5b
VZ
60
61private:
62 char *m_str;
63};
64
7a3e402c 65#if wxUSE_WCHAR_T
14971e5b
VZ
66class wxWCharBuffer
67{
68public:
69 wxWCharBuffer(const wchar_t *wcs)
70 {
223d09f6 71 wxASSERT_MSG( wcs, wxT("NULL string in wxWCharBuffer") );
14971e5b 72
853d7d3d 73 if (wcs) {
de85a884
VZ
74#if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
75 || ( defined(__MWERKS__) && defined(__WXMSW__) )
d834f22c
JS
76 size_t siz = (std::wcslen(wcs)+1)*sizeof(wchar_t);
77#else
c3b177ae 78 size_t siz = (::wcslen(wcs)+1)*sizeof(wchar_t);
d834f22c 79#endif
853d7d3d
OK
80 m_wcs = (wchar_t *)malloc(siz);
81 memcpy(m_wcs, wcs, siz);
82 }
83 else m_wcs = (wchar_t *)NULL;
f93d01be
OK
84 }
85 wxWCharBuffer(size_t len)
86 {
87 m_wcs = (wchar_t *)malloc((len+1)*sizeof(wchar_t));
e90c1d2a 88 m_wcs[len] = L'\0';
14971e5b
VZ
89 }
90
91 // no need to check for NULL, free() does it
92 ~wxWCharBuffer() { free(m_wcs); }
93
f93d01be
OK
94 wxWCharBuffer(const wxWCharBuffer& src)
95 {
96 m_wcs = src.m_wcs;
97 // no reference count yet...
20456250 98 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
f93d01be
OK
99 }
100 wxWCharBuffer& operator=(const wxWCharBuffer& src)
101 {
102 m_wcs = src.m_wcs;
103 // no reference count yet...
20456250 104 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
f93d01be
OK
105 return *this;
106 }
107
f6bcfd97 108 const wchar_t *data() const { return m_wcs; }
14971e5b 109 operator const wchar_t *() const { return m_wcs; }
b1fa8b4e 110 wchar_t operator[](size_t n) const { return m_wcs[n]; }
e90c1d2a 111
14971e5b
VZ
112private:
113 wchar_t *m_wcs;
114};
7a3e402c 115#endif
14971e5b 116
f93d01be 117#if wxUSE_UNICODE
e90c1d2a
VZ
118 #define wxMB2WXbuf wxWCharBuffer
119 #define wxWX2MBbuf wxCharBuffer
120 #define wxWC2WXbuf wxChar*
121 #define wxWX2WCbuf wxChar*
122#else // ANSI
123 #define wxMB2WXbuf wxChar*
124 #define wxWX2MBbuf wxChar*
125 #define wxWC2WXbuf wxCharBuffer
126 #define wxWX2WCbuf wxWCharBuffer
127#endif // Unicode/ANSI
f93d01be 128
b9fdb397
RD
129
130
131// ----------------------------------------------------------------------------
132// A class for holding growable data buffers (not necessarily strings)
133// ----------------------------------------------------------------------------
134
135class wxMemoryBuffer
136{
137public:
138 enum { BLOCK_SIZE = 1024 };
139 wxMemoryBuffer(size_t size=wxMemoryBuffer::BLOCK_SIZE)
140 {
141 wxASSERT(size > 0);
142 m_data = malloc(size);
143 wxASSERT(m_data != NULL);
144 m_size = size;
145 m_len = 0;
146 }
147
148 ~wxMemoryBuffer() { free(m_data); }
149
150 // Accessors
ef1cae87
RD
151 void* GetData() const { return m_data; }
152 size_t GetBufSize() const { return m_size; }
153 size_t GetDataLen() const { return m_len; }
b9fdb397
RD
154
155 void SetBufSize(size_t size) { ResizeIfNeeded(size); }
156 void SetDataLen(size_t len)
157 {
158 wxASSERT(len <= m_size);
159 m_len = len;
160 }
161
162 // Ensure the buffer is big enough and return a pointer to it
163 void* GetWriteBuf(size_t sizeNeeded)
164 {
165 ResizeIfNeeded(sizeNeeded);
166 return m_data;
167 }
168 // Update the length after the write
169 void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
170
171 // Like the above, but appends to the buffer
172 void* GetAppendBuf(size_t sizeNeeded)
173 {
174 ResizeIfNeeded(m_len + sizeNeeded);
175 return (char*)m_data + m_len;
176 }
177 void UngetAppendBuf(size_t sizeUsed) { SetDataLen(m_len + sizeUsed); }
178
179 // Other ways to append to the buffer
180 void AppendByte(char data) {
181 ResizeIfNeeded(m_len + 1);
ef1cae87 182 *(((char*)m_data)+m_len) = data;
b9fdb397
RD
183 m_len += 1;
184 }
185 void AppendData(void* data, size_t len)
186 {
187 memcpy(GetAppendBuf(len), data, len);
188 UngetAppendBuf(len);
189 }
190
191 operator const char *() const { return (const char*)m_data; }
192
ef1cae87
RD
193
194 // Copy and assignment
195 wxMemoryBuffer(const wxMemoryBuffer& src)
196 {
197 m_data = src.m_data;
198 m_size = src.m_size;
199 m_len = src.m_len;
200
201 // no reference count yet...
202 ((wxMemoryBuffer*)&src)->m_data = NULL;
203 ((wxMemoryBuffer*)&src)->m_size = 0;
204 ((wxMemoryBuffer*)&src)->m_len = 0;
205 }
206
207 wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
208 {
209 m_data = src.m_data;
210 m_size = src.m_size;
211 m_len = src.m_len;
212
213 // no reference count yet...
214 ((wxMemoryBuffer*)&src)->m_data = NULL;
215 ((wxMemoryBuffer*)&src)->m_size = 0;
216 ((wxMemoryBuffer*)&src)->m_len = 0;
217
218 return *this;
219 }
220
221
b9fdb397 222protected:
b9fdb397
RD
223
224 void ResizeIfNeeded(size_t newSize)
225 {
226 if (newSize > m_size)
227 {
228 m_data = realloc(m_data, newSize + wxMemoryBuffer::BLOCK_SIZE);
229 wxASSERT(m_data != NULL);
230 m_size = newSize + wxMemoryBuffer::BLOCK_SIZE;
231 }
232 }
233
234private:
235 void* m_data;
236 size_t m_size;
237 size_t m_len;
238};
239
240
241
14971e5b
VZ
242// ----------------------------------------------------------------------------
243// template class for any kind of data
244// ----------------------------------------------------------------------------
245
246// TODO
247
248#endif // _WX_BUFFER_H