added non const accessors to wx(W)CharBuffer; minor changes to wxMemoryBuffer; made copy ctor and assignment operator argument non const

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@17492 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-10-11 15:26:43 +00:00
parent 1e4d96486a
commit da266dd05c

View File

@@ -40,20 +40,22 @@ public:
// no need to check for NULL, free() does it
~wxCharBuffer() { free(m_str); }
wxCharBuffer(const wxCharBuffer& src)
wxCharBuffer(/* non const! */ wxCharBuffer& src)
: m_str(src.m_str)
{
// no reference count yet...
((wxCharBuffer*)&src)->m_str = (char *)NULL;
src.m_str = (char *)NULL;
}
wxCharBuffer& operator=(const wxCharBuffer& src)
wxCharBuffer& operator=(/* non const! */ wxCharBuffer& src)
{
m_str = src.m_str;
// no reference count yet...
((wxCharBuffer*)&src)->m_str = (char *)NULL;
src.m_str = (char *)NULL;
return *this;
}
char *data() { return m_str; }
const char *data() const { return m_str; }
operator const char *() const { return m_str; }
char operator[](size_t n) const { return m_str[n]; }
@@ -85,20 +87,21 @@ public:
// no need to check for NULL, free() does it
~wxWCharBuffer() { free(m_wcs); }
wxWCharBuffer(const wxWCharBuffer& src)
wxWCharBuffer(/* non const! */ wxWCharBuffer& src)
: m_wcs(src.m_wcs)
{
// no reference count yet...
((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
src.m_wcs = (wchar_t *)NULL;
}
wxWCharBuffer& operator=(const wxWCharBuffer& src)
wxWCharBuffer& operator=(/* non const! */ wxWCharBuffer& src)
{
m_wcs = src.m_wcs;
// no reference count yet...
((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
src.m_wcs = (wchar_t *)NULL;
return *this;
}
wchar_t *data() { return m_wcs; }
const wchar_t *data() const { return m_wcs; }
operator const wchar_t *() const { return m_wcs; }
wchar_t operator[](size_t n) const { return m_wcs[n]; }
@@ -128,16 +131,22 @@ private:
class wxMemoryBuffer
{
public:
enum { BLOCK_SIZE = 1024 };
// the initial size and also the size added by ResizeIfNeeded()
enum
{
BLOCK_SIZE = 1024
};
// ctor and dtor
wxMemoryBuffer(size_t size = wxMemoryBuffer::BLOCK_SIZE)
: m_data(malloc(size)), m_size(size), m_len(0)
: m_data(size ? malloc(size) : NULL), m_size(size), m_len(0)
{
}
~wxMemoryBuffer() { free(m_data); }
// Accessors
void* GetData() const { return m_data; }
void *GetData() const { return m_data; }
size_t GetBufSize() const { return m_size; }
size_t GetDataLen() const { return m_len; }
@@ -149,7 +158,7 @@ public:
}
// Ensure the buffer is big enough and return a pointer to it
void* GetWriteBuf(size_t sizeNeeded)
void *GetWriteBuf(size_t sizeNeeded)
{
ResizeIfNeeded(sizeNeeded);
return m_data;
@@ -158,7 +167,7 @@ public:
void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
// Like the above, but appends to the buffer
void* GetAppendBuf(size_t sizeNeeded)
void *GetAppendBuf(size_t sizeNeeded)
{
ResizeIfNeeded(m_len + sizeNeeded);
return (char*)m_data + m_len;
@@ -166,7 +175,10 @@ public:
void UngetAppendBuf(size_t sizeUsed) { SetDataLen(m_len + sizeUsed); }
// Other ways to append to the buffer
void AppendByte(char data) {
void AppendByte(char data)
{
wxCHECK_RET( m_data, _T("invalid wxMemoryBuffer") );
ResizeIfNeeded(m_len + 1);
*(((char*)m_data)+m_len) = data;
m_len += 1;
@@ -181,25 +193,25 @@ public:
// Copy and assignment
wxMemoryBuffer(const wxMemoryBuffer& src)
wxMemoryBuffer(/* non const! */ wxMemoryBuffer& src)
: m_data(src.m_data), m_size(src.m_size), m_len(src.m_len)
{
// no reference count yet...
((wxMemoryBuffer*)&src)->m_data = NULL;
((wxMemoryBuffer*)&src)->m_size = 0;
((wxMemoryBuffer*)&src)->m_len = 0;
src.m_data = NULL;
src.m_size = 0;
src.m_len = 0;
}
wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
wxMemoryBuffer& operator=(/* not const! */ wxMemoryBuffer& src)
{
m_data = src.m_data;
m_size = src.m_size;
m_len = src.m_len;
// no reference count yet...
((wxMemoryBuffer*)&src)->m_data = NULL;
((wxMemoryBuffer*)&src)->m_size = 0;
((wxMemoryBuffer*)&src)->m_len = 0;
src.m_data = NULL;
src.m_size = 0;
src.m_len = 0;
return *this;
}
@@ -210,16 +222,26 @@ protected:
{
if (newSize > m_size)
{
void *dataOld = m_data;
m_data = realloc(m_data, newSize + wxMemoryBuffer::BLOCK_SIZE);
wxASSERT(m_data != NULL);
if ( !m_data )
{
free(dataOld);
}
m_size = newSize + wxMemoryBuffer::BLOCK_SIZE;
}
}
private:
void* m_data;
size_t m_size;
size_t m_len;
// the buffer containing the data
void *m_data;
// the size of the buffer
size_t m_size;
// the amount of data currently in the buffer
size_t m_len;
};
// ----------------------------------------------------------------------------