1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxMemoryBuffer
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 wxScopedCharTypeBuffer<T> is a template class for storing characters.
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.
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
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.
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).
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).
38 The type of the characters stored in this class.
46 class wxScopedCharTypeBuffer
49 /// Stored characters type.
52 /// Default constructor, creates NULL buffer.
53 wxScopedCharTypeBuffer();
56 Creates non-owned buffer from string data @a str.
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.
61 @param str String data.
62 @param len If specified, length of the string, otherwise the string
63 is considered to be NUL-terminated.
65 static const wxScopedCharTypeBuffer
CreateNonOwned(const CharType
*str
, size_t len
= wxNO_LEN
);
68 Creates owned buffer from @a str and takes ownership of it.
70 The buffer's destructor will free @a str when its reference count
71 reaches zero (initial count is 1).
73 @param str String data.
74 @param len If specified, length of the string, otherwise the string
75 is considered to be NUL-terminated.
77 static const wxScopedCharTypeBuffer
CreateOwned(CharType
*str
, size_t len
= wxNO_LEN
);
82 Increases reference count on the data, does @em not make wxStrdup()
85 wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer
& src
);
87 /// Assignment operator behaves in the same way as the copy constructor.
88 wxScopedCharTypeBuffer
& operator=(const wxScopedCharTypeBuffer
& src
);
91 Destructor. Frees stored data if it is in "owned" mode and data's
92 reference count reaches zero.
94 ~wxScopedCharTypeBuffer();
96 /// Resets the buffer to NULL, freeing the data if necessary.
99 /// Returns pointer to the stored data.
102 /// Returns const pointer to the stored data.
103 const CharType
*data() const;
105 /// Returns length of the string stored.
106 size_t length() const;
108 /// Implicit conversion to C string.
109 operator const CharType
*() const;
111 /// Random access to the stored C string.
112 CharType
operator[](size_t n
) const;
115 /// Scoped char buffer.
116 typedef wxScopedCharTypeBuffer
<char> wxScopedCharBuffer
;
118 /// Scoped wchar_t buffer.
119 typedef wxScopedCharTypeBuffer
<wchar_t> wxScopedWCharBuffer
;
122 wxCharTypeBuffer<T> is a template class for storing characters.
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.
130 wxScopedCharTypeBuffer<T> buffers can be converted into wxCharTypeBuffer<T>.
133 The type of the characters stored in this class.
140 template <typename T
>
141 class wxCharTypeBuffer
: public wxScopedCharTypeBuffer
<T
>
145 Creates (owned) buffer from @a str and takes ownership of it.
147 @param str String data.
148 @param len If specified, length of the string, otherwise the string
149 is considered to be NUL-terminated.
151 @see wxScopedCharTypeBuffer<T>::CreateOwned()
153 wxCharTypeBuffer(const CharType
*str
= NULL
, size_t len
= wxNO_LEN
);
157 Creates (owned) buffer of size @a len.
159 @see wxScopedCharTypeBuffer<T>::CreateOwned()
161 wxCharTypeBuffer(size_t len
);
166 Increases reference count on the data, does @em not make wxStrdup()
169 wxCharTypeBuffer(const wxCharTypeBuffer
& src
);
172 Makes a copy of scoped buffer @a src.
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).
178 wxCharTypeBuffer(const wxScopedCharTypeBuffer
<T
>& src
);
181 Assigns @a str to this buffer and takes ownership of it (i.e. the
182 buffer becomes "owned").
184 wxCharTypeBuffer
& operator=(const CharType
*str
);
186 /// Assignment operator behaves in the same way as the copy constructor.
187 wxCharTypeBuffer
& operator=(const wxCharTypeBuffer
& src
);
190 Assigns a scoped buffer to this buffer.
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).
196 wxCharTypeBuffer
& operator=(const wxScopedCharTypeBuffer
<T
>& src
);
199 Extends the buffer to have size @a len.
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).
206 bool extend(size_t len
);
209 Shrinks the buffer to have size @a len and NUL-terminates the string
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).
215 @param len Length to shrink to. Must not be larger than current length.
217 @note The string is not reallocated to take less memory.
223 bool shrink(size_t len
);
227 This is a specialization of wxCharTypeBuffer<T> for @c char type.
232 class wxCharBuffer
: public wxCharTypeBuffer
<char>
235 typedef wxCharTypeBuffer
<char> wxCharTypeBufferBase
;
236 typedef wxScopedCharTypeBuffer
<char> wxScopedCharTypeBufferBase
;
238 wxCharBuffer(const wxCharTypeBufferBase
& buf
);
239 wxCharBuffer(const wxScopedCharTypeBufferBase
& buf
);
240 wxCharBuffer(const CharType
*str
= NULL
);
241 wxCharBuffer(size_t len
);
242 wxCharBuffer(const wxCStrData
& cstr
);
246 This is a specialization of wxCharTypeBuffer<T> for @c wchar_t type.
251 class wxWCharBuffer
: public wxCharTypeBuffer
<wchar_t>
254 typedef wxCharTypeBuffer
<wchar_t> wxCharTypeBufferBase
;
255 typedef wxScopedCharTypeBuffer
<wchar_t> wxScopedCharTypeBufferBase
;
257 wxWCharBuffer(const wxCharTypeBufferBase
& buf
);
258 wxWCharBuffer(const wxScopedCharTypeBufferBase
& buf
);
259 wxWCharBuffer(const CharType
*str
= NULL
);
260 wxWCharBuffer(size_t len
);
261 wxWCharBuffer(const wxCStrData
& cstr
);
265 @class wxMemoryBuffer
267 A @b wxMemoryBuffer is a useful data structure for storing arbitrary sized
268 blocks of memory. wxMemoryBuffer guarantees deletion of the memory block when
269 the object is destroyed.
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
282 @see @ref overview_refcount
284 wxMemoryBuffer(const wxMemoryBuffer
& src
);
290 size of the new buffer.
292 wxMemoryBuffer(size_t size
= DefBufSize
);
295 Append a single byte to the buffer.
298 New byte to append to the buffer.
300 void AppendByte(char data
);
303 Ensure that the buffer is big enough and return a pointer to the start
304 of the empty space in the buffer. This pointer can be used to directly
305 write data into the buffer, this new data will be appended to the
309 Amount of extra space required in the buffer for
312 void* GetAppendBuf(size_t sizeNeeded
);
315 Returns the size of the buffer.
317 size_t GetBufSize() const;
320 Return a pointer to the data in the buffer.
322 void* GetData() const;
325 Returns the length of the valid data in the buffer.
327 size_t GetDataLen() const;
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
332 up to @a sizeNeeded bytes.
334 void* GetWriteBuf(size_t sizeNeeded
);
337 Ensures the buffer has at least @a size bytes available.
339 void SetBufSize(size_t size
);
342 Sets the length of the data stored in the buffer.
343 Mainly useful for truncating existing data.
346 New length of the valid data in the buffer. This is
347 distinct from the allocated size
349 void SetDataLen(size_t size
);
352 Update the length after completing a direct append, which
353 you must have used GetAppendBuf() to initialise.
356 This is the amount of new data that has been
359 void UngetAppendBuf(size_t sizeUsed
);
362 Update the buffer after completing a direct write, which
363 you must have used GetWriteBuf() to initialise.
366 The amount of data written in to buffer
369 void UngetWriteBuf(size_t sizeUsed
);