1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxMemoryBuffer
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
10 wxScopedCharTypeBuffer<T> is a template class for storing characters.
12 Data are stored in reference-counted buffer. In other words, making a copy
13 of wxScopedCharTypeBuffer<T> will @em not make another copy of the stored
14 string data, it will still point to the same location in memory.
16 wxScopedCharTypeBuffer<T> supports two storage modes: owned and non-owned.
17 "Owned" data buffer (created with CreateOwned() or wxCharTypeBuffer<T>
18 derived class) owns the data and frees them when the last buffer pointing
21 "Non-owned" buffer (created with CreateNonOwned()), on the other hand,
22 references data owned by somebody else -- typical use is by
23 wxString::mb_str() or wxString::wc_str(), which may return non-owned buffer
24 pointing to wxString's internal store.
26 Because of this, the validity of data stored in wxScopedCharTypeBuffer<T>
27 is limited by the lifetime of the "parent" object that created the
28 buffer (e.g. the wxString on which mb_str() was called).
30 If you need to preserve the data for longer, assign it to
31 wxCharTypeBuffer<T> instead of wxScopedCharTypeBuffer<T>. On the other
32 hand, use wxScopedCharTypeBuffer<T> if the buffer is to be destroyed before
33 the "parent" object -- typical use would be creating it on the stack and
34 destroying when it goes out of scope (hence the class' name).
37 The type of the characters stored in this class.
45 class wxScopedCharTypeBuffer
48 /// Stored characters type.
51 /// Default constructor, creates NULL buffer.
52 wxScopedCharTypeBuffer();
55 Creates non-owned buffer from string data @a str.
57 The buffer's destructor will not destroy @a str. The returned buffer's
58 data is valid only as long as @a str is valid.
60 @param str String data.
61 @param len If specified, length of the string, otherwise the string
62 is considered to be NUL-terminated.
64 static const wxScopedCharTypeBuffer
CreateNonOwned(const CharType
*str
, size_t len
= wxNO_LEN
);
67 Creates owned buffer from @a str and takes ownership of it.
69 The buffer's destructor will free @a str when its reference count
70 reaches zero (initial count is 1).
72 @param str String data.
73 @param len If specified, length of the string, otherwise the string
74 is considered to be NUL-terminated.
76 static const wxScopedCharTypeBuffer
CreateOwned(CharType
*str
, size_t len
= wxNO_LEN
);
81 Increases reference count on the data, does @em not make wxStrdup()
84 wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer
& src
);
86 /// Assignment operator behaves in the same way as the copy constructor.
87 wxScopedCharTypeBuffer
& operator=(const wxScopedCharTypeBuffer
& src
);
90 Destructor. Frees stored data if it is in "owned" mode and data's
91 reference count reaches zero.
93 ~wxScopedCharTypeBuffer();
95 /// Resets the buffer to NULL, freeing the data if necessary.
98 /// Returns pointer to the stored data.
101 /// Returns const pointer to the stored data.
102 const CharType
*data() const;
104 /// Returns length of the string stored.
105 size_t length() const;
107 /// Implicit conversion to C string.
108 operator const CharType
*() const;
110 /// Random access to the stored C string.
111 CharType
operator[](size_t n
) const;
114 /// Scoped char buffer.
115 typedef wxScopedCharTypeBuffer
<char> wxScopedCharBuffer
;
117 /// Scoped wchar_t buffer.
118 typedef wxScopedCharTypeBuffer
<wchar_t> wxScopedWCharBuffer
;
121 wxCharTypeBuffer<T> is a template class for storing characters.
123 The difference from wxScopedCharTypeBuffer<T> is that this class
124 doesn't have non-owned mode and the data stored in it are valid for
125 as long as the buffer instance exists. Other than that, this class'
126 behaviour is the same as wxScopedCharTypeBuffer<T>'s -- in particular,
127 the data are reference-counted and copying the buffer is cheap.
129 wxScopedCharTypeBuffer<T> buffers can be converted into wxCharTypeBuffer<T>.
132 The type of the characters stored in this class.
139 template <typename T
>
140 class wxCharTypeBuffer
: public wxScopedCharTypeBuffer
<T
>
144 Creates (owned) buffer from @a str and takes ownership of it.
146 @param str String data.
147 @param len If specified, length of the string, otherwise the string
148 is considered to be NUL-terminated.
150 @see wxScopedCharTypeBuffer<T>::CreateOwned()
152 wxCharTypeBuffer(const CharType
*str
= NULL
, size_t len
= wxNO_LEN
);
156 Creates (owned) buffer of size @a len.
158 @see wxScopedCharTypeBuffer<T>::CreateOwned()
160 wxCharTypeBuffer(size_t len
);
165 Increases reference count on the data, does @em not make wxStrdup()
168 wxCharTypeBuffer(const wxCharTypeBuffer
& src
);
171 Makes a copy of scoped buffer @a src.
173 If @a src is a non-owned buffer, a copy of its data is made using
174 wxStrdup(). If @a src is an owned buffer, this constructor behaves
175 in the usual way (reference count on buffer data is incremented).
177 wxCharTypeBuffer(const wxScopedCharTypeBuffer
<T
>& src
);
180 Assigns @a str to this buffer and takes ownership of it (i.e.\ the
181 buffer becomes "owned").
183 wxCharTypeBuffer
& operator=(const CharType
*str
);
185 /// Assignment operator behaves in the same way as the copy constructor.
186 wxCharTypeBuffer
& operator=(const wxCharTypeBuffer
& src
);
189 Assigns a scoped buffer to this buffer.
191 If @a src is a non-owned buffer, a copy of its data is made using
192 wxStrdup(). If @a src is an owned buffer, the assignment behaves
193 in the usual way (reference count on buffer data is incremented).
195 wxCharTypeBuffer
& operator=(const wxScopedCharTypeBuffer
<T
>& src
);
198 Extends the buffer to have size @a len.
200 Can only be called on buffers that don't share data with another
201 buffer (i.e. reference count of the data is 1).
205 bool extend(size_t len
);
208 Shrinks the buffer to have size @a len and NUL-terminates the string
211 Can only be called on buffers that don't share data with another
212 buffer (i.e. reference count of the data is 1).
214 @param len Length to shrink to. Must not be larger than current length.
216 @note The string is not reallocated to take less memory.
222 bool shrink(size_t len
);
226 This is a specialization of wxCharTypeBuffer<T> for @c char type.
231 class wxCharBuffer
: public wxCharTypeBuffer
<char>
234 typedef wxCharTypeBuffer
<char> wxCharTypeBufferBase
;
235 typedef wxScopedCharTypeBuffer
<char> wxScopedCharTypeBufferBase
;
237 wxCharBuffer(const wxCharTypeBufferBase
& buf
);
238 wxCharBuffer(const wxScopedCharTypeBufferBase
& buf
);
239 wxCharBuffer(const CharType
*str
= NULL
);
240 wxCharBuffer(size_t len
);
241 wxCharBuffer(const wxCStrData
& cstr
);
245 This is a specialization of wxCharTypeBuffer<T> for @c wchar_t type.
250 class wxWCharBuffer
: public wxCharTypeBuffer
<wchar_t>
253 typedef wxCharTypeBuffer
<wchar_t> wxCharTypeBufferBase
;
254 typedef wxScopedCharTypeBuffer
<wchar_t> wxScopedCharTypeBufferBase
;
256 wxWCharBuffer(const wxCharTypeBufferBase
& buf
);
257 wxWCharBuffer(const wxScopedCharTypeBufferBase
& buf
);
258 wxWCharBuffer(const CharType
*str
= NULL
);
259 wxWCharBuffer(size_t len
);
260 wxWCharBuffer(const wxCStrData
& cstr
);
264 @class wxMemoryBuffer
266 A @b wxMemoryBuffer is a useful data structure for storing arbitrary sized
267 blocks of memory. wxMemoryBuffer guarantees deletion of the memory block when
268 the object is destroyed.
277 Copy constructor, refcounting is used for performance, but wxMemoryBuffer
278 is not a copy-on-write structure so changes made to one buffer effect all
281 @see @ref overview_refcount
283 wxMemoryBuffer(const wxMemoryBuffer
& src
);
289 size of the new buffer, 1KiB by default.
291 wxMemoryBuffer(size_t size
= 1024);
294 Append a single byte to the buffer.
297 New byte to append to the buffer.
299 void AppendByte(char data
);
302 Single call to append a data block to the buffer.
305 Pointer to block to append to the buffer.
307 Length of data to append.
309 void AppendData(const void *data
, size_t len
);
312 Clear the buffer contents.
314 The buffer won't contain any data after this method is called.
323 Ensure that the buffer is big enough and return a pointer to the start
324 of the empty space in the buffer. This pointer can be used to directly
325 write data into the buffer, this new data will be appended to the
329 Amount of extra space required in the buffer for
332 void* GetAppendBuf(size_t sizeNeeded
);
335 Returns the size of the buffer.
337 size_t GetBufSize() const;
340 Return a pointer to the data in the buffer.
342 void* GetData() const;
345 Returns the length of the valid data in the buffer.
347 size_t GetDataLen() const;
350 Ensure the buffer is big enough and return a pointer to the
351 buffer which can be used to directly write into the buffer
352 up to @a sizeNeeded bytes.
354 void* GetWriteBuf(size_t sizeNeeded
);
357 Returns true if the buffer contains no data.
363 bool IsEmpty() const;
366 Ensures the buffer has at least @a size bytes available.
368 void SetBufSize(size_t size
);
371 Sets the length of the data stored in the buffer.
372 Mainly useful for truncating existing data.
375 New length of the valid data in the buffer. This is
376 distinct from the allocated size
378 void SetDataLen(size_t size
);
381 Update the length after completing a direct append, which
382 you must have used GetAppendBuf() to initialise.
385 This is the amount of new data that has been
388 void UngetAppendBuf(size_t sizeUsed
);
391 Update the buffer after completing a direct write, which
392 you must have used GetWriteBuf() to initialise.
395 The amount of data written in to buffer
398 void UngetWriteBuf(size_t sizeUsed
);