1999-04-12 05:57:22 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2002-08-20 11:07:24 -04:00
|
|
|
// Name: wx/buffer.h
|
1999-04-12 05:57:22 -04:00
|
|
|
// Purpose: auto buffer classes: buffers which automatically free memory
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Modified by:
|
|
|
|
// Created: 12.04.99
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
1999-04-12 05:57:22 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_BUFFER_H
|
|
|
|
#define _WX_BUFFER_H
|
|
|
|
|
2007-04-19 03:44:16 -04:00
|
|
|
#include "wx/chartype.h"
|
2007-06-10 13:14:14 -04:00
|
|
|
#include "wx/wxcrtbase.h"
|
1999-10-04 16:15:38 -04:00
|
|
|
|
2008-03-01 19:43:06 -05:00
|
|
|
#ifndef __WXPALMOS5__
|
2004-02-14 21:57:43 -05:00
|
|
|
#include <stdlib.h> // malloc() and free()
|
2008-03-01 19:43:06 -05:00
|
|
|
#endif // ! __WXPALMOS5__
|
2004-02-14 21:57:43 -05:00
|
|
|
|
2007-07-09 06:09:52 -04:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxCStrData;
|
2007-04-19 07:25:34 -04:00
|
|
|
|
1999-04-12 05:57:22 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Special classes for (wide) character strings: they use malloc/free instead
|
|
|
|
// of new/delete
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2008-11-01 21:48:36 -04:00
|
|
|
// helpers used by wxCharTypeBuffer
|
|
|
|
namespace wxPrivate
|
|
|
|
{
|
|
|
|
|
|
|
|
struct UntypedBufferData
|
|
|
|
{
|
|
|
|
enum Kind
|
|
|
|
{
|
|
|
|
Owned,
|
|
|
|
NonOwned
|
|
|
|
};
|
|
|
|
|
|
|
|
UntypedBufferData(void *str, Kind kind = Owned)
|
|
|
|
: m_str(str), m_ref(1), m_owned(kind == Owned) {}
|
|
|
|
|
|
|
|
~UntypedBufferData()
|
|
|
|
{
|
|
|
|
if ( m_owned )
|
|
|
|
free(m_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *m_str;
|
|
|
|
|
|
|
|
// "short" to have sizeof(Data)=8 on 32bit archs
|
|
|
|
unsigned short m_ref;
|
|
|
|
|
|
|
|
bool m_owned;
|
|
|
|
};
|
|
|
|
|
|
|
|
// this has to be defined inside the DLL (and not e.g. as a static variable
|
|
|
|
// inside an inline function) as otherwise MSVC gives link errors when the
|
|
|
|
// functions are effectively inlined (i.e. in non-debug build)
|
|
|
|
//
|
|
|
|
// NB: this is defined in string.cpp and not the (non-existent) buffer.cpp
|
|
|
|
extern WXDLLIMPEXP_DATA_BASE(UntypedBufferData * const) untypedNullDataPtr;
|
|
|
|
|
|
|
|
} // namespace wxPrivate
|
|
|
|
|
2007-03-22 19:31:08 -04:00
|
|
|
template <typename T>
|
2008-10-30 19:04:29 -04:00
|
|
|
class wxCharTypeBuffer
|
2007-03-22 19:31:08 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T CharType;
|
|
|
|
|
|
|
|
wxCharTypeBuffer(const CharType *str = NULL)
|
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
if ( str )
|
|
|
|
m_data = new Data(wxStrdup(str));
|
|
|
|
else
|
2008-10-30 19:04:29 -04:00
|
|
|
m_data = GetNullData();
|
2007-03-22 19:31:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
wxCharTypeBuffer(size_t len)
|
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
m_data = new Data((CharType *)malloc((len + 1)*sizeof(CharType)));
|
2008-11-01 21:48:36 -04:00
|
|
|
m_data->Get()[len] = (CharType)0;
|
2007-03-22 19:31:08 -04:00
|
|
|
}
|
|
|
|
|
2007-04-16 02:59:04 -04:00
|
|
|
static const wxCharTypeBuffer CreateNonOwned(const CharType *str)
|
2007-04-14 12:58:48 -04:00
|
|
|
{
|
|
|
|
wxCharTypeBuffer buf;
|
2008-08-10 15:37:51 -04:00
|
|
|
if ( str )
|
2008-11-01 22:39:52 -04:00
|
|
|
buf.m_data = new Data(const_cast<CharType*>(str), Data::NonOwned);
|
2007-04-14 12:58:48 -04:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
~wxCharTypeBuffer()
|
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
DecRef();
|
2007-04-14 12:58:48 -04:00
|
|
|
}
|
2007-03-22 19:31:08 -04:00
|
|
|
|
2008-08-12 11:10:57 -04:00
|
|
|
// NB: this method is only const for backward compatibility. It used to
|
|
|
|
// be needed for auto_ptr-like semantics of the copy ctor, but now
|
|
|
|
// that ref-counting is used, it's not really needed.
|
|
|
|
CharType *release() const
|
2008-08-10 15:37:51 -04:00
|
|
|
{
|
2008-10-30 19:04:29 -04:00
|
|
|
if ( m_data == GetNullData() )
|
2008-08-10 15:37:51 -04:00
|
|
|
return NULL;
|
2007-03-22 19:31:08 -04:00
|
|
|
|
2008-08-10 15:37:51 -04:00
|
|
|
wxASSERT_MSG( m_data->m_owned, _T("can't release non-owned buffer") );
|
|
|
|
wxASSERT_MSG( m_data->m_ref == 1, _T("can't release shared buffer") );
|
1999-04-12 05:57:22 -04:00
|
|
|
|
2008-11-01 21:48:36 -04:00
|
|
|
CharType * const p = m_data->Get();
|
2008-08-12 11:10:57 -04:00
|
|
|
|
2008-11-01 22:39:52 -04:00
|
|
|
wxCharTypeBuffer *self = const_cast<wxCharTypeBuffer*>(this);
|
2008-11-01 21:48:36 -04:00
|
|
|
self->m_data->Set(NULL);
|
2008-08-12 11:10:57 -04:00
|
|
|
self->DecRef();
|
|
|
|
|
2008-08-10 15:37:51 -04:00
|
|
|
return p;
|
2007-03-22 19:31:08 -04:00
|
|
|
}
|
2002-08-20 11:07:24 -04:00
|
|
|
|
2007-03-22 19:31:08 -04:00
|
|
|
void reset()
|
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
DecRef();
|
2007-03-22 19:31:08 -04:00
|
|
|
}
|
2002-08-20 11:07:24 -04:00
|
|
|
|
2007-03-22 19:31:08 -04:00
|
|
|
wxCharTypeBuffer(const wxCharTypeBuffer& src)
|
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
m_data = src.m_data;
|
|
|
|
IncRef();
|
2007-03-22 19:31:08 -04:00
|
|
|
}
|
1999-04-12 05:57:22 -04:00
|
|
|
|
2007-03-22 19:31:08 -04:00
|
|
|
wxCharTypeBuffer& operator=(const CharType *str)
|
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
DecRef();
|
|
|
|
|
|
|
|
if ( str )
|
|
|
|
m_data = new Data(wxStrdup(str));
|
2007-03-22 19:31:08 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src)
|
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
if ( &src == this )
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
DecRef();
|
|
|
|
m_data = src.m_data;
|
|
|
|
IncRef();
|
|
|
|
|
2007-03-22 19:31:08 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool extend(size_t len)
|
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
wxASSERT_MSG( m_data->m_owned, _T("cannot extend non-owned buffer") );
|
|
|
|
wxASSERT_MSG( m_data->m_ref == 1, _T("can't extend shared buffer") );
|
2007-04-14 12:58:48 -04:00
|
|
|
|
2008-08-10 15:37:51 -04:00
|
|
|
CharType *str =
|
|
|
|
(CharType *)realloc(data(), (len + 1) * sizeof(CharType));
|
2007-03-22 19:31:08 -04:00
|
|
|
if ( !str )
|
|
|
|
return false;
|
|
|
|
|
2008-10-30 19:04:29 -04:00
|
|
|
if ( m_data == GetNullData() )
|
2008-08-10 15:37:51 -04:00
|
|
|
{
|
|
|
|
m_data = new Data(str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-01 21:48:36 -04:00
|
|
|
m_data->Set(str);
|
2008-08-10 15:37:51 -04:00
|
|
|
m_data->m_owned = true;
|
|
|
|
}
|
2007-03-22 19:31:08 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-01 21:48:36 -04:00
|
|
|
CharType *data() { return m_data->Get(); }
|
|
|
|
const CharType *data() const { return m_data->Get(); }
|
2008-08-10 15:37:51 -04:00
|
|
|
operator const CharType *() const { return data(); }
|
|
|
|
CharType operator[](size_t n) const { return data()[n]; }
|
2007-04-14 12:58:48 -04:00
|
|
|
|
|
|
|
private:
|
2008-08-10 15:37:51 -04:00
|
|
|
// reference-counted data
|
2008-11-16 09:24:13 -05:00
|
|
|
struct Data : public wxPrivate::UntypedBufferData
|
2007-04-14 12:58:48 -04:00
|
|
|
{
|
2008-08-10 15:37:51 -04:00
|
|
|
Data(CharType *str, Kind kind = Owned)
|
2008-12-17 04:26:58 -05:00
|
|
|
: wxPrivate::UntypedBufferData(str, kind)
|
2008-08-10 15:37:51 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-01 21:48:36 -04:00
|
|
|
CharType *Get() const { return static_cast<CharType *>(m_str); }
|
|
|
|
void Set(CharType *str) { m_str = str; }
|
2008-08-10 15:37:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// placeholder for NULL string, to simplify this code
|
2008-10-30 19:04:29 -04:00
|
|
|
static Data *GetNullData()
|
|
|
|
{
|
2008-11-01 21:48:36 -04:00
|
|
|
return static_cast<Data *>(wxPrivate::untypedNullDataPtr);
|
2008-10-30 19:04:29 -04:00
|
|
|
}
|
2008-08-10 15:37:51 -04:00
|
|
|
|
|
|
|
void IncRef()
|
|
|
|
{
|
2008-10-30 19:04:29 -04:00
|
|
|
if ( m_data == GetNullData() ) // exception, not ref-counted
|
2008-08-10 15:37:51 -04:00
|
|
|
return;
|
|
|
|
m_data->m_ref++;
|
2007-04-14 12:58:48 -04:00
|
|
|
}
|
|
|
|
|
2008-08-10 15:37:51 -04:00
|
|
|
void DecRef()
|
2007-04-14 12:58:48 -04:00
|
|
|
{
|
2008-10-30 19:04:29 -04:00
|
|
|
if ( m_data == GetNullData() ) // exception, not ref-counted
|
2008-08-10 15:37:51 -04:00
|
|
|
return;
|
|
|
|
if ( --m_data->m_ref == 0 )
|
|
|
|
delete m_data;
|
2008-10-30 19:04:29 -04:00
|
|
|
m_data = GetNullData();
|
2007-04-14 12:58:48 -04:00
|
|
|
}
|
|
|
|
|
2007-03-22 19:31:08 -04:00
|
|
|
private:
|
2008-08-10 15:37:51 -04:00
|
|
|
Data *m_data;
|
2007-03-22 19:31:08 -04:00
|
|
|
};
|
|
|
|
|
2007-09-14 17:02:09 -04:00
|
|
|
WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<char> )
|
|
|
|
|
2008-10-30 19:04:29 -04:00
|
|
|
class wxCharBuffer : public wxCharTypeBuffer<char>
|
2007-03-22 19:31:08 -04:00
|
|
|
{
|
|
|
|
public:
|
2007-03-22 20:04:15 -04:00
|
|
|
typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
|
|
|
|
|
2007-04-16 02:59:04 -04:00
|
|
|
wxCharBuffer(const wxCharTypeBufferBase& buf)
|
|
|
|
: wxCharTypeBufferBase(buf) {}
|
|
|
|
|
2007-03-22 20:04:15 -04:00
|
|
|
wxCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
|
|
|
|
wxCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
|
|
|
|
|
|
|
|
wxCharBuffer(const wxCStrData& cstr);
|
2007-03-22 19:31:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#if wxUSE_WCHAR_T
|
2007-09-14 17:02:09 -04:00
|
|
|
WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<wchar_t> )
|
|
|
|
|
2008-10-30 19:04:29 -04:00
|
|
|
class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
|
2007-03-22 19:31:08 -04:00
|
|
|
{
|
|
|
|
public:
|
2007-03-22 20:04:15 -04:00
|
|
|
typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
|
|
|
|
|
2007-04-16 02:59:04 -04:00
|
|
|
wxWCharBuffer(const wxCharTypeBufferBase& buf)
|
|
|
|
: wxCharTypeBufferBase(buf) {}
|
|
|
|
|
2007-03-22 20:04:15 -04:00
|
|
|
wxWCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
|
|
|
|
wxWCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
|
|
|
|
|
|
|
|
wxWCharBuffer(const wxCStrData& cstr);
|
2007-03-22 19:31:08 -04:00
|
|
|
};
|
|
|
|
#endif // wxUSE_WCHAR_T
|
2002-12-04 09:11:26 -05:00
|
|
|
|
2007-03-30 16:09:02 -04:00
|
|
|
// wxCharTypeBuffer<T> implicitly convertible to T*
|
|
|
|
template <typename T>
|
2007-03-30 16:30:34 -04:00
|
|
|
class wxWritableCharTypeBuffer : public wxCharTypeBuffer<T>
|
2007-03-30 16:09:02 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename wxCharTypeBuffer<T>::CharType CharType;
|
|
|
|
|
|
|
|
wxWritableCharTypeBuffer(const wxCharTypeBuffer<T>& src)
|
|
|
|
: wxCharTypeBuffer<T>(src) {}
|
|
|
|
// FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to
|
|
|
|
// always return a buffer
|
|
|
|
wxWritableCharTypeBuffer(const CharType *str = NULL)
|
|
|
|
: wxCharTypeBuffer<T>(str) {}
|
|
|
|
|
|
|
|
operator CharType*() { return this->data(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef wxWritableCharTypeBuffer<char> wxWritableCharBuffer;
|
|
|
|
typedef wxWritableCharTypeBuffer<wchar_t> wxWritableWCharBuffer;
|
|
|
|
|
|
|
|
|
1999-04-12 16:10:56 -04:00
|
|
|
#if wxUSE_UNICODE
|
2007-03-22 20:04:15 -04:00
|
|
|
#define wxWxCharBuffer wxWCharBuffer
|
2006-04-09 21:35:21 -04:00
|
|
|
|
1999-10-04 16:15:38 -04:00
|
|
|
#define wxMB2WXbuf wxWCharBuffer
|
|
|
|
#define wxWX2MBbuf wxCharBuffer
|
2007-04-12 17:15:07 -04:00
|
|
|
#if wxUSE_UNICODE_WCHAR
|
|
|
|
#define wxWC2WXbuf wxChar*
|
|
|
|
#define wxWX2WCbuf wxChar*
|
|
|
|
#elif wxUSE_UNICODE_UTF8
|
|
|
|
#define wxWC2WXbuf wxWCharBuffer
|
|
|
|
#define wxWX2WCbuf wxWCharBuffer
|
|
|
|
#endif
|
1999-10-04 16:15:38 -04:00
|
|
|
#else // ANSI
|
2007-03-22 20:04:15 -04:00
|
|
|
#define wxWxCharBuffer wxCharBuffer
|
2006-04-09 21:35:21 -04:00
|
|
|
|
1999-10-04 16:15:38 -04:00
|
|
|
#define wxMB2WXbuf wxChar*
|
|
|
|
#define wxWX2MBbuf wxChar*
|
|
|
|
#define wxWC2WXbuf wxCharBuffer
|
|
|
|
#define wxWX2WCbuf wxWCharBuffer
|
|
|
|
#endif // Unicode/ANSI
|
1999-04-12 16:10:56 -04:00
|
|
|
|
2007-10-20 18:52:36 -04:00
|
|
|
// type of the value returned by wxString::utf8_str()
|
|
|
|
#if wxUSE_UNICODE_UTF8
|
|
|
|
#define wxUTF8Buf char *
|
2007-12-01 10:14:39 -05:00
|
|
|
#else
|
|
|
|
#define wxUTF8Buf wxCharBuffer
|
2007-10-20 18:52:36 -04:00
|
|
|
#endif
|
|
|
|
|
2002-03-13 17:05:05 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// A class for holding growable data buffers (not necessarily strings)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
// This class manages the actual data buffer pointer and is ref-counted.
|
|
|
|
class wxMemoryBufferData
|
2002-03-13 17:05:05 -05:00
|
|
|
{
|
|
|
|
public:
|
2002-12-04 09:11:26 -05:00
|
|
|
// the initial size and also the size added by ResizeIfNeeded()
|
2005-10-29 13:46:12 -04:00
|
|
|
enum { DefBufSize = 1024 };
|
2002-12-04 09:11:26 -05:00
|
|
|
|
|
|
|
friend class wxMemoryBuffer;
|
|
|
|
|
|
|
|
// everyting is private as it can only be used by wxMemoryBuffer
|
|
|
|
private:
|
2005-10-29 13:46:12 -04:00
|
|
|
wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize)
|
2002-12-04 09:11:26 -05:00
|
|
|
: m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0)
|
2002-03-13 17:05:05 -05:00
|
|
|
{
|
|
|
|
}
|
2002-12-04 09:11:26 -05:00
|
|
|
~wxMemoryBufferData() { free(m_data); }
|
2002-03-13 17:05:05 -05:00
|
|
|
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
void ResizeIfNeeded(size_t newSize)
|
2002-03-13 17:05:05 -05:00
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
if (newSize > m_size)
|
|
|
|
{
|
|
|
|
void *dataOld = m_data;
|
2005-10-29 13:46:12 -04:00
|
|
|
m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize);
|
2002-12-04 09:11:26 -05:00
|
|
|
if ( !m_data )
|
|
|
|
{
|
|
|
|
free(dataOld);
|
|
|
|
}
|
|
|
|
|
2005-10-29 13:46:12 -04:00
|
|
|
m_size = newSize + wxMemoryBufferData::DefBufSize;
|
2002-12-04 09:11:26 -05:00
|
|
|
}
|
2002-03-13 17:05:05 -05:00
|
|
|
}
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
void IncRef() { m_ref += 1; }
|
|
|
|
void DecRef()
|
2002-03-13 17:05:05 -05:00
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
m_ref -= 1;
|
|
|
|
if (m_ref == 0) // are there no more references?
|
|
|
|
delete this;
|
2002-03-13 17:05:05 -05:00
|
|
|
}
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// the reference count
|
|
|
|
size_t m_ref;
|
2003-01-02 18:38:11 -05:00
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(wxMemoryBufferData)
|
2002-12-04 09:11:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-10-30 19:04:29 -04:00
|
|
|
class wxMemoryBuffer
|
2002-12-04 09:11:26 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor and dtor
|
2005-10-29 13:46:12 -04:00
|
|
|
wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize)
|
2002-03-13 17:05:05 -05:00
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
m_bufdata = new wxMemoryBufferData(size);
|
|
|
|
m_bufdata->IncRef();
|
2002-03-13 17:05:05 -05:00
|
|
|
}
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
~wxMemoryBuffer() { m_bufdata->DecRef(); }
|
|
|
|
|
|
|
|
|
|
|
|
// copy and assignment
|
|
|
|
wxMemoryBuffer(const wxMemoryBuffer& src)
|
|
|
|
: m_bufdata(src.m_bufdata)
|
|
|
|
{
|
|
|
|
m_bufdata->IncRef();
|
2002-03-13 17:05:05 -05:00
|
|
|
}
|
2002-12-04 09:11:26 -05:00
|
|
|
|
|
|
|
wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
|
2002-03-13 17:05:05 -05:00
|
|
|
{
|
2008-01-08 23:08:33 -05:00
|
|
|
if (&src != this)
|
|
|
|
{
|
|
|
|
m_bufdata->DecRef();
|
|
|
|
m_bufdata = src.m_bufdata;
|
|
|
|
m_bufdata->IncRef();
|
|
|
|
}
|
2002-12-04 09:11:26 -05:00
|
|
|
return *this;
|
2002-03-13 17:05:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
// Accessors
|
|
|
|
void *GetData() const { return m_bufdata->m_data; }
|
|
|
|
size_t GetBufSize() const { return m_bufdata->m_size; }
|
|
|
|
size_t GetDataLen() const { return m_bufdata->m_len; }
|
2002-03-20 15:13:17 -05:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); }
|
|
|
|
void SetDataLen(size_t len)
|
2002-03-20 15:13:17 -05:00
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
wxASSERT(len <= m_bufdata->m_size);
|
|
|
|
m_bufdata->m_len = len;
|
2002-03-20 15:13:17 -05:00
|
|
|
}
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
// Ensure the buffer is big enough and return a pointer to it
|
|
|
|
void *GetWriteBuf(size_t sizeNeeded)
|
2002-03-20 15:13:17 -05:00
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
m_bufdata->ResizeIfNeeded(sizeNeeded);
|
|
|
|
return m_bufdata->m_data;
|
|
|
|
}
|
2002-08-20 11:07:24 -04:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
// Update the length after the write
|
|
|
|
void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
|
2002-03-20 15:13:17 -05:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
// Like the above, but appends to the buffer
|
|
|
|
void *GetAppendBuf(size_t sizeNeeded)
|
|
|
|
{
|
|
|
|
m_bufdata->ResizeIfNeeded(m_bufdata->m_len + sizeNeeded);
|
|
|
|
return (char*)m_bufdata->m_data + m_bufdata->m_len;
|
|
|
|
}
|
2002-03-20 15:13:17 -05:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
// Update the length after the append
|
|
|
|
void UngetAppendBuf(size_t sizeUsed)
|
|
|
|
{
|
|
|
|
SetDataLen(m_bufdata->m_len + sizeUsed);
|
|
|
|
}
|
2002-03-20 15:13:17 -05:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
// Other ways to append to the buffer
|
|
|
|
void AppendByte(char data)
|
2002-03-13 17:05:05 -05:00
|
|
|
{
|
2002-12-04 09:11:26 -05:00
|
|
|
wxCHECK_RET( m_bufdata->m_data, _T("invalid wxMemoryBuffer") );
|
|
|
|
|
|
|
|
m_bufdata->ResizeIfNeeded(m_bufdata->m_len + 1);
|
|
|
|
*(((char*)m_bufdata->m_data) + m_bufdata->m_len) = data;
|
|
|
|
m_bufdata->m_len += 1;
|
|
|
|
}
|
|
|
|
|
2005-09-24 17:28:15 -04:00
|
|
|
void AppendData(const void *data, size_t len)
|
2002-12-04 09:11:26 -05:00
|
|
|
{
|
|
|
|
memcpy(GetAppendBuf(len), data, len);
|
|
|
|
UngetAppendBuf(len);
|
2002-03-13 17:05:05 -05:00
|
|
|
}
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
operator const char *() const { return (const char*)GetData(); }
|
|
|
|
|
2002-03-13 17:05:05 -05:00
|
|
|
private:
|
2002-12-04 09:11:26 -05:00
|
|
|
wxMemoryBufferData* m_bufdata;
|
2002-03-13 17:05:05 -05:00
|
|
|
};
|
|
|
|
|
1999-04-12 05:57:22 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// template class for any kind of data
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
#endif // _WX_BUFFER_H
|