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