2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: buffer.h
|
2008-03-10 11:24:38 -04:00
|
|
|
// Purpose: interface of wxMemoryBuffer
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-12-01 17:09:47 -05:00
|
|
|
|
|
|
|
/**
|
2009-03-27 11:33:55 -04:00
|
|
|
wxScopedCharTypeBuffer<T> is a template class for storing characters.
|
|
|
|
|
|
|
|
Data are stored in reference-counted buffer. In other words, making a copy
|
|
|
|
of wxScopedCharTypeBuffer<T> will @em not make another copy of the stored
|
|
|
|
string data, it will still point to the same location in memory.
|
|
|
|
|
|
|
|
wxScopedCharTypeBuffer<T> supports two storage modes: owned and non-owned.
|
|
|
|
"Owned" data buffer (created with CreateOwned() or wxCharTypeBuffer<T>
|
|
|
|
derived class) owns the data and frees them when the last buffer pointing
|
|
|
|
to them is destroyed.
|
|
|
|
|
|
|
|
"Non-owned" buffer (created with CreateNonOwned()), on the other hand,
|
|
|
|
references data owned by somebody else -- typical use is by
|
|
|
|
wxString::mb_str() or wxString::wc_str(), which may return non-owned buffer
|
|
|
|
pointing to wxString's internal store.
|
|
|
|
|
|
|
|
Because of this, the validity of data stored in wxScopedCharTypeBuffer<T>
|
|
|
|
is limited by the lifetime of the "parent" object that created the
|
|
|
|
buffer (e.g. the wxString on which mb_str() was called).
|
2008-12-01 17:09:47 -05:00
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
If you need to preserve the data for longer, assign it to
|
|
|
|
wxCharTypeBuffer<T> instead of wxScopedCharTypeBuffer<T>. On the other
|
|
|
|
hand, use wxScopedCharTypeBuffer<T> if the buffer is to be destroyed before
|
|
|
|
the "parent" object -- typical use would be creating it on the stack and
|
|
|
|
destroying when it goes out of scope (hence the class' name).
|
2008-12-01 17:09:47 -05:00
|
|
|
|
2009-02-20 06:34:52 -05:00
|
|
|
@tparam T
|
|
|
|
The type of the characters stored in this class.
|
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
@since 2.9.0
|
|
|
|
|
2008-12-01 17:09:47 -05:00
|
|
|
@nolibrary
|
2009-02-20 06:34:52 -05:00
|
|
|
@category{data}
|
2008-12-01 17:09:47 -05:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2009-03-27 11:33:55 -04:00
|
|
|
class wxScopedCharTypeBuffer
|
2008-12-01 17:09:47 -05:00
|
|
|
{
|
|
|
|
public:
|
2009-03-27 11:33:55 -04:00
|
|
|
/// Stored characters type.
|
2008-12-01 17:09:47 -05:00
|
|
|
typedef T CharType;
|
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
/// Default constructor, creates NULL buffer.
|
|
|
|
wxScopedCharTypeBuffer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Creates non-owned buffer from string data @a str.
|
|
|
|
|
|
|
|
The buffer's destructor will not destroy @a str. The returned buffer's
|
|
|
|
data is valid only as long as @a str is valid.
|
2009-03-29 16:58:39 -04:00
|
|
|
|
|
|
|
@param str String data.
|
|
|
|
@param len If specified, length of the string, otherwise the string
|
|
|
|
is considered to be NUL-terminated.
|
2009-03-27 11:33:55 -04:00
|
|
|
*/
|
2009-03-29 16:58:39 -04:00
|
|
|
static const wxScopedCharTypeBuffer CreateNonOwned(const CharType *str, size_t len = wxNO_LEN);
|
2009-03-27 11:33:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Creates owned buffer from @a str and takes ownership of it.
|
|
|
|
|
|
|
|
The buffer's destructor will free @a str when its reference count
|
|
|
|
reaches zero (initial count is 1).
|
2009-03-29 16:58:39 -04:00
|
|
|
|
|
|
|
@param str String data.
|
|
|
|
@param len If specified, length of the string, otherwise the string
|
|
|
|
is considered to be NUL-terminated.
|
2009-03-27 11:33:55 -04:00
|
|
|
*/
|
2010-05-05 03:45:33 -04:00
|
|
|
static const wxScopedCharTypeBuffer CreateOwned(CharType *str, size_t len = wxNO_LEN);
|
2009-03-27 11:33:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Copy constructor.
|
|
|
|
|
|
|
|
Increases reference count on the data, does @em not make wxStrdup()
|
|
|
|
copy of the data.
|
|
|
|
*/
|
|
|
|
wxScopedCharTypeBuffer(const wxScopedCharTypeBuffer& src);
|
|
|
|
|
|
|
|
/// Assignment operator behaves in the same way as the copy constructor.
|
|
|
|
wxScopedCharTypeBuffer& operator=(const wxScopedCharTypeBuffer& src);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor. Frees stored data if it is in "owned" mode and data's
|
|
|
|
reference count reaches zero.
|
|
|
|
*/
|
|
|
|
~wxScopedCharTypeBuffer();
|
|
|
|
|
|
|
|
/// Resets the buffer to NULL, freeing the data if necessary.
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
/// Returns pointer to the stored data.
|
|
|
|
CharType *data();
|
|
|
|
|
|
|
|
/// Returns const pointer to the stored data.
|
|
|
|
const CharType *data() const;
|
|
|
|
|
2009-03-29 16:58:39 -04:00
|
|
|
/// Returns length of the string stored.
|
|
|
|
size_t length() const;
|
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
/// Implicit conversion to C string.
|
|
|
|
operator const CharType *() const;
|
|
|
|
|
|
|
|
/// Random access to the stored C string.
|
|
|
|
CharType operator[](size_t n) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Scoped char buffer.
|
|
|
|
typedef wxScopedCharTypeBuffer<char> wxScopedCharBuffer;
|
|
|
|
|
|
|
|
/// Scoped wchar_t buffer.
|
|
|
|
typedef wxScopedCharTypeBuffer<wchar_t> wxScopedWCharBuffer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
wxCharTypeBuffer<T> is a template class for storing characters.
|
|
|
|
|
|
|
|
The difference from wxScopedCharTypeBuffer<T> is that this class
|
|
|
|
doesn't have non-owned mode and the data stored in it are valid for
|
|
|
|
as long as the buffer instance exists. Other than that, this class'
|
|
|
|
behaviour is the same as wxScopedCharTypeBuffer<T>'s -- in particular,
|
|
|
|
the data are reference-counted and copying the buffer is cheap.
|
|
|
|
|
|
|
|
wxScopedCharTypeBuffer<T> buffers can be converted into wxCharTypeBuffer<T>.
|
|
|
|
|
|
|
|
@tparam T
|
|
|
|
The type of the characters stored in this class.
|
|
|
|
|
|
|
|
@since 2.9.0
|
|
|
|
|
|
|
|
@nolibrary
|
|
|
|
@category{data}
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class wxCharTypeBuffer : public wxScopedCharTypeBuffer<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Creates (owned) buffer from @a str and takes ownership of it.
|
|
|
|
|
2009-03-29 16:58:39 -04:00
|
|
|
@param str String data.
|
|
|
|
@param len If specified, length of the string, otherwise the string
|
|
|
|
is considered to be NUL-terminated.
|
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
@see wxScopedCharTypeBuffer<T>::CreateOwned()
|
|
|
|
*/
|
2009-03-29 16:58:39 -04:00
|
|
|
wxCharTypeBuffer(const CharType *str = NULL, size_t len = wxNO_LEN);
|
2009-03-27 11:33:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Creates (owned) buffer of size @a len.
|
|
|
|
|
|
|
|
@see wxScopedCharTypeBuffer<T>::CreateOwned()
|
|
|
|
*/
|
2008-12-01 17:09:47 -05:00
|
|
|
wxCharTypeBuffer(size_t len);
|
2009-03-27 11:33:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Copy constructor.
|
|
|
|
|
|
|
|
Increases reference count on the data, does @em not make wxStrdup()
|
|
|
|
copy of the data.
|
|
|
|
*/
|
2008-12-01 17:09:47 -05:00
|
|
|
wxCharTypeBuffer(const wxCharTypeBuffer& src);
|
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
/**
|
|
|
|
Makes a copy of scoped buffer @a src.
|
|
|
|
|
|
|
|
If @a src is a non-owned buffer, a copy of its data is made using
|
|
|
|
wxStrdup(). If @a src is an owned buffer, this constructor behaves
|
|
|
|
in the usual way (reference count on buffer data is incremented).
|
|
|
|
*/
|
|
|
|
wxCharTypeBuffer(const wxScopedCharTypeBuffer<T>& src);
|
2008-12-01 17:09:47 -05:00
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
/**
|
|
|
|
Assigns @a str to this buffer and takes ownership of it (i.e. the
|
|
|
|
buffer becomes "owned").
|
|
|
|
*/
|
2008-12-01 17:09:47 -05:00
|
|
|
wxCharTypeBuffer& operator=(const CharType *str);
|
2009-03-27 11:33:55 -04:00
|
|
|
|
|
|
|
/// Assignment operator behaves in the same way as the copy constructor.
|
2008-12-01 17:09:47 -05:00
|
|
|
wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src);
|
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
/**
|
|
|
|
Assigns a scoped buffer to this buffer.
|
2008-12-01 17:09:47 -05:00
|
|
|
|
2009-03-27 11:33:55 -04:00
|
|
|
If @a src is a non-owned buffer, a copy of its data is made using
|
|
|
|
wxStrdup(). If @a src is an owned buffer, the assignment behaves
|
|
|
|
in the usual way (reference count on buffer data is incremented).
|
|
|
|
*/
|
|
|
|
wxCharTypeBuffer& operator=(const wxScopedCharTypeBuffer<T>& src);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Extends the buffer to have size @a len.
|
|
|
|
|
|
|
|
Can only be called on buffers that don't share data with another
|
|
|
|
buffer (i.e. reference count of the data is 1).
|
2009-03-30 07:54:41 -04:00
|
|
|
|
|
|
|
@see shrink()
|
2009-03-27 11:33:55 -04:00
|
|
|
*/
|
|
|
|
bool extend(size_t len);
|
2009-03-30 07:54:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Shrinks the buffer to have size @a len and NUL-terminates the string
|
|
|
|
at this length.
|
|
|
|
|
|
|
|
Can only be called on buffers that don't share data with another
|
|
|
|
buffer (i.e. reference count of the data is 1).
|
|
|
|
|
|
|
|
@param len Length to shrink to. Must not be larger than current length.
|
|
|
|
|
|
|
|
@note The string is not reallocated to take less memory.
|
|
|
|
|
|
|
|
@since 2.9.0
|
|
|
|
|
|
|
|
@see extend()
|
|
|
|
*/
|
|
|
|
bool shrink(size_t len);
|
2008-12-01 17:09:47 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
This is a specialization of wxCharTypeBuffer<T> for @c char type.
|
|
|
|
|
|
|
|
@nolibrary
|
2009-02-20 06:34:52 -05:00
|
|
|
@category{data}
|
2008-12-01 17:09:47 -05:00
|
|
|
*/
|
|
|
|
class wxCharBuffer : public wxCharTypeBuffer<char>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
|
2009-03-27 11:33:55 -04:00
|
|
|
typedef wxScopedCharTypeBuffer<char> wxScopedCharTypeBufferBase;
|
2008-12-01 17:09:47 -05:00
|
|
|
|
|
|
|
wxCharBuffer(const wxCharTypeBufferBase& buf);
|
2009-03-27 11:33:55 -04:00
|
|
|
wxCharBuffer(const wxScopedCharTypeBufferBase& buf);
|
2008-12-01 17:09:47 -05:00
|
|
|
wxCharBuffer(const CharType *str = NULL);
|
|
|
|
wxCharBuffer(size_t len);
|
|
|
|
wxCharBuffer(const wxCStrData& cstr);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
This is a specialization of wxCharTypeBuffer<T> for @c wchar_t type.
|
|
|
|
|
|
|
|
@nolibrary
|
2009-02-20 06:34:52 -05:00
|
|
|
@category{data}
|
2008-12-01 17:09:47 -05:00
|
|
|
*/
|
|
|
|
class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
|
2009-03-27 11:33:55 -04:00
|
|
|
typedef wxScopedCharTypeBuffer<wchar_t> wxScopedCharTypeBufferBase;
|
2008-12-01 17:09:47 -05:00
|
|
|
|
|
|
|
wxWCharBuffer(const wxCharTypeBufferBase& buf);
|
2009-03-27 11:33:55 -04:00
|
|
|
wxWCharBuffer(const wxScopedCharTypeBufferBase& buf);
|
2008-12-01 17:09:47 -05:00
|
|
|
wxWCharBuffer(const CharType *str = NULL);
|
|
|
|
wxWCharBuffer(size_t len);
|
|
|
|
wxWCharBuffer(const wxCStrData& cstr);
|
|
|
|
};
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxMemoryBuffer
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
A @b wxMemoryBuffer is a useful data structure for storing arbitrary sized
|
2008-03-16 20:47:38 -04:00
|
|
|
blocks of memory. wxMemoryBuffer guarantees deletion of the memory block when
|
|
|
|
the object is destroyed.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
2008-03-16 20:47:38 -04:00
|
|
|
@category{data}
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxMemoryBuffer
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
2008-03-16 20:47:38 -04:00
|
|
|
/**
|
|
|
|
Copy constructor, refcounting is used for performance, but wxMemoryBuffer
|
|
|
|
is not a copy-on-write structure so changes made to one buffer effect all
|
|
|
|
copies made from it.
|
|
|
|
|
|
|
|
@see @ref overview_refcount
|
|
|
|
*/
|
|
|
|
wxMemoryBuffer(const wxMemoryBuffer& src);
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
Create a new buffer.
|
2008-03-16 20:47:38 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param size
|
2008-03-16 20:47:38 -04:00
|
|
|
size of the new buffer.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-11-10 16:33:25 -05:00
|
|
|
wxMemoryBuffer(size_t size = DefBufSize);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Append a single byte to the buffer.
|
2008-03-16 20:47:38 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param data
|
2008-03-09 08:33:59 -04:00
|
|
|
New byte to append to the buffer.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void AppendByte(char data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Ensure that the buffer is big enough and return a pointer to the start
|
2008-03-08 09:43:31 -05:00
|
|
|
of the empty space in the buffer. This pointer can be used to directly
|
2008-03-16 20:47:38 -04:00
|
|
|
write data into the buffer, this new data will be appended to the
|
|
|
|
existing data.
|
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param sizeNeeded
|
2008-03-09 08:33:59 -04:00
|
|
|
Amount of extra space required in the buffer for
|
|
|
|
the append operation
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
void* GetAppendBuf(size_t sizeNeeded);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the size of the buffer.
|
|
|
|
*/
|
2008-03-24 18:17:25 -04:00
|
|
|
size_t GetBufSize() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Return a pointer to the data in the buffer.
|
|
|
|
*/
|
2008-03-24 18:17:25 -04:00
|
|
|
void* GetData() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the length of the valid data in the buffer.
|
|
|
|
*/
|
2008-03-24 18:17:25 -04:00
|
|
|
size_t GetDataLen() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Ensure the buffer is big enough and return a pointer to the
|
|
|
|
buffer which can be used to directly write into the buffer
|
2008-03-09 08:33:59 -04:00
|
|
|
up to @a sizeNeeded bytes.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
void* GetWriteBuf(size_t sizeNeeded);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Ensures the buffer has at least @a size bytes available.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void SetBufSize(size_t size);
|
|
|
|
|
|
|
|
/**
|
2008-03-16 20:47:38 -04:00
|
|
|
Sets the length of the data stored in the buffer.
|
|
|
|
Mainly useful for truncating existing data.
|
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param size
|
2008-03-09 08:33:59 -04:00
|
|
|
New length of the valid data in the buffer. This is
|
|
|
|
distinct from the allocated size
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void SetDataLen(size_t size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Update the length after completing a direct append, which
|
|
|
|
you must have used GetAppendBuf() to initialise.
|
2008-03-16 20:47:38 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param sizeUsed
|
2008-03-09 08:33:59 -04:00
|
|
|
This is the amount of new data that has been
|
|
|
|
appended.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void UngetAppendBuf(size_t sizeUsed);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Update the buffer after completing a direct write, which
|
|
|
|
you must have used GetWriteBuf() to initialise.
|
2008-03-16 20:47:38 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param sizeUsed
|
2008-03-09 08:33:59 -04:00
|
|
|
The amount of data written in to buffer
|
|
|
|
by the direct write
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void UngetWriteBuf(size_t sizeUsed);
|
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|