1999-04-12 09:57:22 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2002-08-20 15:07:24 +00:00
|
|
|
// Name: wx/buffer.h
|
1999-04-12 09:57:22 +00: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 20:53:33 +00:00
|
|
|
// Licence: wxWindows licence
|
1999-04-12 09:57:22 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_BUFFER_H
|
|
|
|
#define _WX_BUFFER_H
|
|
|
|
|
2007-04-19 07:44:16 +00:00
|
|
|
#include "wx/chartype.h"
|
2007-06-10 17:14:14 +00:00
|
|
|
#include "wx/wxcrtbase.h"
|
1999-10-04 20:15:38 +00:00
|
|
|
|
2008-03-02 00:43:06 +00:00
|
|
|
#ifndef __WXPALMOS5__
|
2004-02-15 02:57:43 +00:00
|
|
|
#include <stdlib.h> // malloc() and free()
|
2008-03-02 00:43:06 +00:00
|
|
|
#endif // ! __WXPALMOS5__
|
2004-02-15 02:57:43 +00:00
|
|
|
|
2007-07-09 10:09:52 +00:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxCStrData;
|
2007-04-19 11:25:34 +00:00
|
|
|
|
1999-04-12 09:57:22 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Special classes for (wide) character strings: they use malloc/free instead
|
|
|
|
// of new/delete
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2008-11-02 01:48:36 +00: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 23:31:08 +00:00
|
|
|
template <typename T>
|
2008-10-30 23:04:29 +00:00
|
|
|
class wxCharTypeBuffer
|
2007-03-22 23:31:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T CharType;
|
|
|
|
|
|
|
|
wxCharTypeBuffer(const CharType *str = NULL)
|
|
|
|
{
|
2008-08-10 19:37:51 +00:00
|
|
|
if ( str )
|
|
|
|
m_data = new Data(wxStrdup(str));
|
|
|
|
else
|
2008-10-30 23:04:29 +00:00
|
|
|
m_data = GetNullData();
|
2007-03-22 23:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxCharTypeBuffer(size_t len)
|
|
|
|
{
|
2008-08-10 19:37:51 +00:00
|
|
|
m_data = new Data((CharType *)malloc((len + 1)*sizeof(CharType)));
|
2008-11-02 01:48:36 +00:00
|
|
|
m_data->Get()[len] = (CharType)0;
|
2007-03-22 23:31:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-16 06:59:04 +00:00
|
|
|
static const wxCharTypeBuffer CreateNonOwned(const CharType *str)
|
2007-04-14 16:58:48 +00:00
|
|
|
{
|
|
|
|
wxCharTypeBuffer buf;
|
2008-08-10 19:37:51 +00:00
|
|
|
if ( str )
|
2008-11-02 02:39:52 +00:00
|
|
|
buf.m_data = new Data(const_cast<CharType*>(str), Data::NonOwned);
|
2007-04-14 16:58:48 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
~wxCharTypeBuffer()
|
|
|
|
{
|
2008-08-10 19:37:51 +00:00
|
|
|
DecRef();
|
2007-04-14 16:58:48 +00:00
|
|
|
}
|
2007-03-22 23:31:08 +00:00
|
|
|
|
2008-08-12 15:10:57 +00: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 19:37:51 +00:00
|
|
|
{
|
2008-10-30 23:04:29 +00:00
|
|
|
if ( m_data == GetNullData() )
|
2008-08-10 19:37:51 +00:00
|
|
|
return NULL;
|
2007-03-22 23:31:08 +00:00
|
|
|
|
2008-08-10 19:37:51 +00: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 09:57:22 +00:00
|
|
|
|
2008-11-02 01:48:36 +00:00
|
|
|
CharType * const p = m_data->Get();
|
2008-08-12 15:10:57 +00:00
|
|
|
|
2008-11-02 02:39:52 +00:00
|
|
|
wxCharTypeBuffer *self = const_cast<wxCharTypeBuffer*>(this);
|
2008-11-02 01:48:36 +00:00
|
|
|
self->m_data->Set(NULL);
|
2008-08-12 15:10:57 +00:00
|
|
|
self->DecRef();
|
|
|
|
|
2008-08-10 19:37:51 +00:00
|
|
|
return p;
|
2007-03-22 23:31:08 +00:00
|
|
|
}
|
2002-08-20 15:07:24 +00:00
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
void reset()
|
|
|
|
{
|
2008-08-10 19:37:51 +00:00
|
|
|
DecRef();
|
2007-03-22 23:31:08 +00:00
|
|
|
}
|
2002-08-20 15:07:24 +00:00
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
wxCharTypeBuffer(const wxCharTypeBuffer& src)
|
|
|
|
{
|
2008-08-10 19:37:51 +00:00
|
|
|
m_data = src.m_data;
|
|
|
|
IncRef();
|
2007-03-22 23:31:08 +00:00
|
|
|
}
|
1999-04-12 09:57:22 +00:00
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
wxCharTypeBuffer& operator=(const CharType *str)
|
|
|
|
{
|
2008-08-10 19:37:51 +00:00
|
|
|
DecRef();
|
|
|
|
|
|
|
|
if ( str )
|
|
|
|
m_data = new Data(wxStrdup(str));
|
2007-03-22 23:31:08 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src)
|
|
|
|
{
|
2008-08-10 19:37:51 +00:00
|
|
|
if ( &src == this )
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
DecRef();
|
|
|
|
m_data = src.m_data;
|
|
|
|
IncRef();
|
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool extend(size_t len)
|
|
|
|
{
|
2008-08-10 19:37:51 +00: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 16:58:48 +00:00
|
|
|
|
2008-08-10 19:37:51 +00:00
|
|
|
CharType *str =
|
|
|
|
(CharType *)realloc(data(), (len + 1) * sizeof(CharType));
|
2007-03-22 23:31:08 +00:00
|
|
|
if ( !str )
|
|
|
|
return false;
|
|
|
|
|
2008-10-30 23:04:29 +00:00
|
|
|
if ( m_data == GetNullData() )
|
2008-08-10 19:37:51 +00:00
|
|
|
{
|
|
|
|
m_data = new Data(str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-02 01:48:36 +00:00
|
|
|
m_data->Set(str);
|
2008-08-10 19:37:51 +00:00
|
|
|
m_data->m_owned = true;
|
|
|
|
}
|
2007-03-22 23:31:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-02 01:48:36 +00:00
|
|
|
CharType *data() { return m_data->Get(); }
|
|
|
|
const CharType *data() const { return m_data->Get(); }
|
2008-08-10 19:37:51 +00:00
|
|
|
operator const CharType *() const { return data(); }
|
|
|
|
CharType operator[](size_t n) const { return data()[n]; }
|
2007-04-14 16:58:48 +00:00
|
|
|
|
|
|
|
private:
|
2008-08-10 19:37:51 +00:00
|
|
|
// reference-counted data
|
2008-11-16 14:24:13 +00:00
|
|
|
struct Data : public wxPrivate::UntypedBufferData
|
2007-04-14 16:58:48 +00:00
|
|
|
{
|
2008-08-10 19:37:51 +00:00
|
|
|
Data(CharType *str, Kind kind = Owned)
|
2008-12-17 09:26:58 +00:00
|
|
|
: wxPrivate::UntypedBufferData(str, kind)
|
2008-08-10 19:37:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-02 01:48:36 +00:00
|
|
|
CharType *Get() const { return static_cast<CharType *>(m_str); }
|
|
|
|
void Set(CharType *str) { m_str = str; }
|
2008-08-10 19:37:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// placeholder for NULL string, to simplify this code
|
2008-10-30 23:04:29 +00:00
|
|
|
static Data *GetNullData()
|
|
|
|
{
|
2008-11-02 01:48:36 +00:00
|
|
|
return static_cast<Data *>(wxPrivate::untypedNullDataPtr);
|
2008-10-30 23:04:29 +00:00
|
|
|
}
|
2008-08-10 19:37:51 +00:00
|
|
|
|
|
|
|
void IncRef()
|
|
|
|
{
|
2008-10-30 23:04:29 +00:00
|
|
|
if ( m_data == GetNullData() ) // exception, not ref-counted
|
2008-08-10 19:37:51 +00:00
|
|
|
return;
|
|
|
|
m_data->m_ref++;
|
2007-04-14 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2008-08-10 19:37:51 +00:00
|
|
|
void DecRef()
|
2007-04-14 16:58:48 +00:00
|
|
|
{
|
2008-10-30 23:04:29 +00:00
|
|
|
if ( m_data == GetNullData() ) // exception, not ref-counted
|
2008-08-10 19:37:51 +00:00
|
|
|
return;
|
|
|
|
if ( --m_data->m_ref == 0 )
|
|
|
|
delete m_data;
|
2008-10-30 23:04:29 +00:00
|
|
|
m_data = GetNullData();
|
2007-04-14 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
private:
|
2008-08-10 19:37:51 +00:00
|
|
|
Data *m_data;
|
2007-03-22 23:31:08 +00:00
|
|
|
};
|
|
|
|
|
2007-09-14 21:02:09 +00:00
|
|
|
WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<char> )
|
|
|
|
|
2008-10-30 23:04:29 +00:00
|
|
|
class wxCharBuffer : public wxCharTypeBuffer<char>
|
2007-03-22 23:31:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2007-03-23 00:04:15 +00:00
|
|
|
typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
|
|
|
|
|
2007-04-16 06:59:04 +00:00
|
|
|
wxCharBuffer(const wxCharTypeBufferBase& buf)
|
|
|
|
: wxCharTypeBufferBase(buf) {}
|
|
|
|
|
2007-03-23 00:04:15 +00:00
|
|
|
wxCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
|
|
|
|
wxCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
|
|
|
|
|
|
|
|
wxCharBuffer(const wxCStrData& cstr);
|
2007-03-22 23:31:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#if wxUSE_WCHAR_T
|
2007-09-14 21:02:09 +00:00
|
|
|
WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<wchar_t> )
|
|
|
|
|
2008-10-30 23:04:29 +00:00
|
|
|
class wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
|
2007-03-22 23:31:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2007-03-23 00:04:15 +00:00
|
|
|
typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
|
|
|
|
|
2007-04-16 06:59:04 +00:00
|
|
|
wxWCharBuffer(const wxCharTypeBufferBase& buf)
|
|
|
|
: wxCharTypeBufferBase(buf) {}
|
|
|
|
|
2007-03-23 00:04:15 +00:00
|
|
|
wxWCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
|
|
|
|
wxWCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
|
|
|
|
|
|
|
|
wxWCharBuffer(const wxCStrData& cstr);
|
2007-03-22 23:31:08 +00:00
|
|
|
};
|
|
|
|
#endif // wxUSE_WCHAR_T
|
2002-12-04 14:11:26 +00:00
|
|
|
|
2007-03-30 20:09:02 +00:00
|
|
|
// wxCharTypeBuffer<T> implicitly convertible to T*
|
|
|
|
template <typename T>
|
2007-03-30 20:30:34 +00:00
|
|
|
class wxWritableCharTypeBuffer : public wxCharTypeBuffer<T>
|
2007-03-30 20:09:02 +00: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 20:10:56 +00:00
|
|
|
#if wxUSE_UNICODE
|
2007-03-23 00:04:15 +00:00
|
|
|
#define wxWxCharBuffer wxWCharBuffer
|
2006-04-10 01:35:21 +00:00
|
|
|
|
1999-10-04 20:15:38 +00:00
|
|
|
#define wxMB2WXbuf wxWCharBuffer
|
|
|
|
#define wxWX2MBbuf wxCharBuffer
|
2007-04-12 21:15:07 +00: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 20:15:38 +00:00
|
|
|
#else // ANSI
|
2007-03-23 00:04:15 +00:00
|
|
|
#define wxWxCharBuffer wxCharBuffer
|
2006-04-10 01:35:21 +00:00
|
|
|
|
1999-10-04 20:15:38 +00:00
|
|
|
#define wxMB2WXbuf wxChar*
|
|
|
|
#define wxWX2MBbuf wxChar*
|
|
|
|
#define wxWC2WXbuf wxCharBuffer
|
|
|
|
#define wxWX2WCbuf wxWCharBuffer
|
|
|
|
#endif // Unicode/ANSI
|
1999-04-12 20:10:56 +00:00
|
|
|
|
2007-10-20 22:52:36 +00:00
|
|
|
// type of the value returned by wxString::utf8_str()
|
|
|
|
#if wxUSE_UNICODE_UTF8
|
|
|
|
#define wxUTF8Buf char *
|
2007-12-01 15:14:39 +00:00
|
|
|
#else
|
|
|
|
#define wxUTF8Buf wxCharBuffer
|
2007-10-20 22:52:36 +00:00
|
|
|
#endif
|
|
|
|
|
2002-03-13 22:05:05 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// A class for holding growable data buffers (not necessarily strings)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// This class manages the actual data buffer pointer and is ref-counted.
|
|
|
|
class wxMemoryBufferData
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2002-12-04 14:11:26 +00:00
|
|
|
// the initial size and also the size added by ResizeIfNeeded()
|
2005-10-29 17:46:12 +00:00
|
|
|
enum { DefBufSize = 1024 };
|
2002-12-04 14:11:26 +00:00
|
|
|
|
|
|
|
friend class wxMemoryBuffer;
|
|
|
|
|
|
|
|
// everyting is private as it can only be used by wxMemoryBuffer
|
|
|
|
private:
|
2005-10-29 17:46:12 +00:00
|
|
|
wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize)
|
2002-12-04 14:11:26 +00:00
|
|
|
: m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
|
|
|
}
|
2002-12-04 14:11:26 +00:00
|
|
|
~wxMemoryBufferData() { free(m_data); }
|
2002-03-13 22:05:05 +00:00
|
|
|
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
void ResizeIfNeeded(size_t newSize)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
if (newSize > m_size)
|
|
|
|
{
|
|
|
|
void *dataOld = m_data;
|
2005-10-29 17:46:12 +00:00
|
|
|
m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize);
|
2002-12-04 14:11:26 +00:00
|
|
|
if ( !m_data )
|
|
|
|
{
|
|
|
|
free(dataOld);
|
|
|
|
}
|
|
|
|
|
2005-10-29 17:46:12 +00:00
|
|
|
m_size = newSize + wxMemoryBufferData::DefBufSize;
|
2002-12-04 14:11:26 +00:00
|
|
|
}
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
void IncRef() { m_ref += 1; }
|
|
|
|
void DecRef()
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
m_ref -= 1;
|
|
|
|
if (m_ref == 0) // are there no more references?
|
|
|
|
delete this;
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00: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 23:38:11 +00:00
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxMemoryBufferData);
|
2002-12-04 14:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-10-30 23:04:29 +00:00
|
|
|
class wxMemoryBuffer
|
2002-12-04 14:11:26 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor and dtor
|
2005-10-29 17:46:12 +00:00
|
|
|
wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
m_bufdata = new wxMemoryBufferData(size);
|
|
|
|
m_bufdata->IncRef();
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
~wxMemoryBuffer() { m_bufdata->DecRef(); }
|
|
|
|
|
|
|
|
|
|
|
|
// copy and assignment
|
|
|
|
wxMemoryBuffer(const wxMemoryBuffer& src)
|
|
|
|
: m_bufdata(src.m_bufdata)
|
|
|
|
{
|
|
|
|
m_bufdata->IncRef();
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
2002-12-04 14:11:26 +00:00
|
|
|
|
|
|
|
wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2008-01-09 04:08:33 +00:00
|
|
|
if (&src != this)
|
|
|
|
{
|
|
|
|
m_bufdata->DecRef();
|
|
|
|
m_bufdata = src.m_bufdata;
|
|
|
|
m_bufdata->IncRef();
|
|
|
|
}
|
2002-12-04 14:11:26 +00:00
|
|
|
return *this;
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-04 14:11:26 +00: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 20:13:17 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); }
|
|
|
|
void SetDataLen(size_t len)
|
2002-03-20 20:13:17 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
wxASSERT(len <= m_bufdata->m_size);
|
|
|
|
m_bufdata->m_len = len;
|
2002-03-20 20:13:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Ensure the buffer is big enough and return a pointer to it
|
|
|
|
void *GetWriteBuf(size_t sizeNeeded)
|
2002-03-20 20:13:17 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
m_bufdata->ResizeIfNeeded(sizeNeeded);
|
|
|
|
return m_bufdata->m_data;
|
|
|
|
}
|
2002-08-20 15:07:24 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Update the length after the write
|
|
|
|
void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
|
2002-03-20 20:13:17 +00:00
|
|
|
|
2002-12-04 14:11:26 +00: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 20:13:17 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Update the length after the append
|
|
|
|
void UngetAppendBuf(size_t sizeUsed)
|
|
|
|
{
|
|
|
|
SetDataLen(m_bufdata->m_len + sizeUsed);
|
|
|
|
}
|
2002-03-20 20:13:17 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Other ways to append to the buffer
|
|
|
|
void AppendByte(char data)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00: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 21:28:15 +00:00
|
|
|
void AppendData(const void *data, size_t len)
|
2002-12-04 14:11:26 +00:00
|
|
|
{
|
|
|
|
memcpy(GetAppendBuf(len), data, len);
|
|
|
|
UngetAppendBuf(len);
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
operator const char *() const { return (const char*)GetData(); }
|
|
|
|
|
2002-03-13 22:05:05 +00:00
|
|
|
private:
|
2002-12-04 14:11:26 +00:00
|
|
|
wxMemoryBufferData* m_bufdata;
|
2002-03-13 22:05:05 +00:00
|
|
|
};
|
|
|
|
|
1999-04-12 09:57:22 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// template class for any kind of data
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
#endif // _WX_BUFFER_H
|