]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/buffer.h
Add support for gradient stops to wxGraphicsContext.
[wxWidgets.git] / interface / wx / buffer.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: buffer.h
e54c96f1 3// Purpose: interface of wxMemoryBuffer
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
8ef2a553
FM
9
10/**
de4983f3
VS
11 wxScopedCharTypeBuffer<T> is a template class for storing characters.
12
13 Data are stored in reference-counted buffer. In other words, making a copy
14 of wxScopedCharTypeBuffer<T> will @em not make another copy of the stored
15 string data, it will still point to the same location in memory.
16
17 wxScopedCharTypeBuffer<T> supports two storage modes: owned and non-owned.
18 "Owned" data buffer (created with CreateOwned() or wxCharTypeBuffer<T>
19 derived class) owns the data and frees them when the last buffer pointing
20 to them is destroyed.
21
22 "Non-owned" buffer (created with CreateNonOwned()), on the other hand,
23 references data owned by somebody else -- typical use is by
24 wxString::mb_str() or wxString::wc_str(), which may return non-owned buffer
25 pointing to wxString's internal store.
26
27 Because of this, the validity of data stored in wxScopedCharTypeBuffer<T>
28 is limited by the lifetime of the "parent" object that created the
29 buffer (e.g. the wxString on which mb_str() was called).
8ef2a553 30
de4983f3
VS
31 If you need to preserve the data for longer, assign it to
32 wxCharTypeBuffer<T> instead of wxScopedCharTypeBuffer<T>. On the other
33 hand, use wxScopedCharTypeBuffer<T> if the buffer is to be destroyed before
34 the "parent" object -- typical use would be creating it on the stack and
35 destroying when it goes out of scope (hence the class' name).
8ef2a553 36
3c99e2fd
FM
37 @tparam T
38 The type of the characters stored in this class.
39
de4983f3
VS
40 @since 2.9.0
41
8ef2a553 42 @nolibrary
3c99e2fd 43 @category{data}
8ef2a553
FM
44*/
45template <typename T>
de4983f3 46class wxScopedCharTypeBuffer
8ef2a553
FM
47{
48public:
de4983f3 49 /// Stored characters type.
8ef2a553
FM
50 typedef T CharType;
51
de4983f3
VS
52 /// Default constructor, creates NULL buffer.
53 wxScopedCharTypeBuffer();
54
55 /**
56 Creates non-owned buffer from string data @a str.
57
58 The buffer's destructor will not destroy @a str. The returned buffer's
59 data is valid only as long as @a str is valid.
6df09f32
VS
60
61 @param str String data.
62 @param len If specified, length of the string, otherwise the string
63 is considered to be NUL-terminated.
de4983f3 64 */
6df09f32 65 static const wxScopedCharTypeBuffer CreateNonOwned(const CharType *str, size_t len = wxNO_LEN);
de4983f3
VS
66
67 /**
68 Creates owned buffer from @a str and takes ownership of it.
69
70 The buffer's destructor will free @a str when its reference count
71 reaches zero (initial count is 1).
6df09f32
VS
72
73 @param str String data.
74 @param len If specified, length of the string, otherwise the string
75 is considered to be NUL-terminated.
de4983f3 76 */
6df09f32 77 static const wxScopedCharTypeBuffer CreateOwned(const CharType *str, size_t len = wxNO_LEN);
de4983f3
VS
78
79 /**
80 Copy constructor.
81
82 Increases reference count on the data, does @em not make wxStrdup()
83 copy of the data.
84 */
85 wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer& src);
86
87 /// Assignment operator behaves in the same way as the copy constructor.
88 wxScopedCharTypeBuffer& operator=(const wxScopedCharTypeBuffer& src);
89
90 /**
91 Destructor. Frees stored data if it is in "owned" mode and data's
92 reference count reaches zero.
93 */
94 ~wxScopedCharTypeBuffer();
95
96 /// Resets the buffer to NULL, freeing the data if necessary.
97 void reset();
98
99 /// Returns pointer to the stored data.
100 CharType *data();
101
102 /// Returns const pointer to the stored data.
103 const CharType *data() const;
104
6df09f32
VS
105 /// Returns length of the string stored.
106 size_t length() const;
107
de4983f3
VS
108 /// Implicit conversion to C string.
109 operator const CharType *() const;
110
111 /// Random access to the stored C string.
112 CharType operator[](size_t n) const;
113};
114
115/// Scoped char buffer.
116typedef wxScopedCharTypeBuffer<char> wxScopedCharBuffer;
117
118/// Scoped wchar_t buffer.
119typedef wxScopedCharTypeBuffer<wchar_t> wxScopedWCharBuffer;
120
121/**
122 wxCharTypeBuffer<T> is a template class for storing characters.
123
124 The difference from wxScopedCharTypeBuffer<T> is that this class
125 doesn't have non-owned mode and the data stored in it are valid for
126 as long as the buffer instance exists. Other than that, this class'
127 behaviour is the same as wxScopedCharTypeBuffer<T>'s -- in particular,
128 the data are reference-counted and copying the buffer is cheap.
129
130 wxScopedCharTypeBuffer<T> buffers can be converted into wxCharTypeBuffer<T>.
131
132 @tparam T
133 The type of the characters stored in this class.
134
135 @since 2.9.0
136
137 @nolibrary
138 @category{data}
139*/
140template <typename T>
141class wxCharTypeBuffer : public wxScopedCharTypeBuffer<T>
142{
143public:
144 /**
145 Creates (owned) buffer from @a str and takes ownership of it.
146
6df09f32
VS
147 @param str String data.
148 @param len If specified, length of the string, otherwise the string
149 is considered to be NUL-terminated.
150
de4983f3
VS
151 @see wxScopedCharTypeBuffer<T>::CreateOwned()
152 */
6df09f32 153 wxCharTypeBuffer(const CharType *str = NULL, size_t len = wxNO_LEN);
de4983f3
VS
154
155
156 /**
157 Creates (owned) buffer of size @a len.
158
159 @see wxScopedCharTypeBuffer<T>::CreateOwned()
160 */
8ef2a553 161 wxCharTypeBuffer(size_t len);
de4983f3
VS
162
163 /**
164 Copy constructor.
165
166 Increases reference count on the data, does @em not make wxStrdup()
167 copy of the data.
168 */
8ef2a553 169 wxCharTypeBuffer(const wxCharTypeBuffer& src);
8ef2a553 170
de4983f3
VS
171 /**
172 Makes a copy of scoped buffer @a src.
173
174 If @a src is a non-owned buffer, a copy of its data is made using
175 wxStrdup(). If @a src is an owned buffer, this constructor behaves
176 in the usual way (reference count on buffer data is incremented).
177 */
178 wxCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src);
8ef2a553 179
de4983f3
VS
180 /**
181 Assigns @a str to this buffer and takes ownership of it (i.e. the
182 buffer becomes "owned").
183 */
8ef2a553 184 wxCharTypeBuffer& operator=(const CharType *str);
de4983f3
VS
185
186 /// Assignment operator behaves in the same way as the copy constructor.
8ef2a553
FM
187 wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src);
188
de4983f3
VS
189 /**
190 Assigns a scoped buffer to this buffer.
8ef2a553 191
de4983f3
VS
192 If @a src is a non-owned buffer, a copy of its data is made using
193 wxStrdup(). If @a src is an owned buffer, the assignment behaves
194 in the usual way (reference count on buffer data is incremented).
195 */
196 wxCharTypeBuffer& operator=(const wxScopedCharTypeBuffer<T>& src);
197
198 /**
199 Extends the buffer to have size @a len.
200
201 Can only be called on buffers that don't share data with another
202 buffer (i.e. reference count of the data is 1).
a6bb7a28
VS
203
204 @see shrink()
de4983f3
VS
205 */
206 bool extend(size_t len);
a6bb7a28
VS
207
208 /**
209 Shrinks the buffer to have size @a len and NUL-terminates the string
210 at this length.
211
212 Can only be called on buffers that don't share data with another
213 buffer (i.e. reference count of the data is 1).
214
215 @param len Length to shrink to. Must not be larger than current length.
216
217 @note The string is not reallocated to take less memory.
218
219 @since 2.9.0
220
221 @see extend()
222 */
223 bool shrink(size_t len);
8ef2a553
FM
224};
225
226/**
227 This is a specialization of wxCharTypeBuffer<T> for @c char type.
228
8ef2a553 229 @nolibrary
3c99e2fd 230 @category{data}
8ef2a553
FM
231*/
232class wxCharBuffer : public wxCharTypeBuffer<char>
233{
234public:
235 typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
de4983f3 236 typedef wxScopedCharTypeBuffer<char> wxScopedCharTypeBufferBase;
8ef2a553
FM
237
238 wxCharBuffer(const wxCharTypeBufferBase& buf);
de4983f3 239 wxCharBuffer(const wxScopedCharTypeBufferBase& buf);
8ef2a553
FM
240 wxCharBuffer(const CharType *str = NULL);
241 wxCharBuffer(size_t len);
242 wxCharBuffer(const wxCStrData& cstr);
243};
244
245/**
246 This is a specialization of wxCharTypeBuffer<T> for @c wchar_t type.
8ef2a553
FM
247
248 @nolibrary
3c99e2fd 249 @category{data}
8ef2a553
FM
250*/
251class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
252{
253public:
254 typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
de4983f3 255 typedef wxScopedCharTypeBuffer<wchar_t> wxScopedCharTypeBufferBase;
8ef2a553
FM
256
257 wxWCharBuffer(const wxCharTypeBufferBase& buf);
de4983f3 258 wxWCharBuffer(const wxScopedCharTypeBufferBase& buf);
8ef2a553
FM
259 wxWCharBuffer(const CharType *str = NULL);
260 wxWCharBuffer(size_t len);
261 wxWCharBuffer(const wxCStrData& cstr);
262};
263
23324ae1
FM
264/**
265 @class wxMemoryBuffer
7c913512 266
23324ae1 267 A @b wxMemoryBuffer is a useful data structure for storing arbitrary sized
8024723d
FM
268 blocks of memory. wxMemoryBuffer guarantees deletion of the memory block when
269 the object is destroyed.
7c913512 270
23324ae1 271 @library{wxbase}
8024723d 272 @category{data}
23324ae1 273*/
7c913512 274class wxMemoryBuffer
23324ae1
FM
275{
276public:
8024723d
FM
277 /**
278 Copy constructor, refcounting is used for performance, but wxMemoryBuffer
279 is not a copy-on-write structure so changes made to one buffer effect all
280 copies made from it.
281
282 @see @ref overview_refcount
283 */
284 wxMemoryBuffer(const wxMemoryBuffer& src);
285
23324ae1
FM
286 /**
287 Create a new buffer.
8024723d 288
7c913512 289 @param size
8024723d 290 size of the new buffer.
23324ae1 291 */
0a98423e 292 wxMemoryBuffer(size_t size = DefBufSize);
23324ae1
FM
293
294 /**
295 Append a single byte to the buffer.
8024723d 296
7c913512 297 @param data
4cc4bfaf 298 New byte to append to the buffer.
23324ae1
FM
299 */
300 void AppendByte(char data);
301
302 /**
303 Ensure that the buffer is big enough and return a pointer to the start
7c913512 304 of the empty space in the buffer. This pointer can be used to directly
8024723d
FM
305 write data into the buffer, this new data will be appended to the
306 existing data.
307
7c913512 308 @param sizeNeeded
4cc4bfaf
FM
309 Amount of extra space required in the buffer for
310 the append operation
23324ae1 311 */
4cc4bfaf 312 void* GetAppendBuf(size_t sizeNeeded);
23324ae1
FM
313
314 /**
315 Returns the size of the buffer.
316 */
d2aa927a 317 size_t GetBufSize() const;
23324ae1
FM
318
319 /**
320 Return a pointer to the data in the buffer.
321 */
d2aa927a 322 void* GetData() const;
23324ae1
FM
323
324 /**
325 Returns the length of the valid data in the buffer.
326 */
d2aa927a 327 size_t GetDataLen() const;
23324ae1
FM
328
329 /**
330 Ensure the buffer is big enough and return a pointer to the
331 buffer which can be used to directly write into the buffer
4cc4bfaf 332 up to @a sizeNeeded bytes.
23324ae1 333 */
4cc4bfaf 334 void* GetWriteBuf(size_t sizeNeeded);
23324ae1
FM
335
336 /**
4cc4bfaf 337 Ensures the buffer has at least @a size bytes available.
23324ae1
FM
338 */
339 void SetBufSize(size_t size);
340
341 /**
8024723d
FM
342 Sets the length of the data stored in the buffer.
343 Mainly useful for truncating existing data.
344
7c913512 345 @param size
4cc4bfaf
FM
346 New length of the valid data in the buffer. This is
347 distinct from the allocated size
23324ae1
FM
348 */
349 void SetDataLen(size_t size);
350
351 /**
352 Update the length after completing a direct append, which
353 you must have used GetAppendBuf() to initialise.
8024723d 354
7c913512 355 @param sizeUsed
4cc4bfaf
FM
356 This is the amount of new data that has been
357 appended.
23324ae1
FM
358 */
359 void UngetAppendBuf(size_t sizeUsed);
360
361 /**
362 Update the buffer after completing a direct write, which
363 you must have used GetWriteBuf() to initialise.
8024723d 364
7c913512 365 @param sizeUsed
4cc4bfaf
FM
366 The amount of data written in to buffer
367 by the direct write
23324ae1
FM
368 */
369 void UngetWriteBuf(size_t sizeUsed);
370};
e54c96f1 371