2009-02-03 07:01:46 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/scopedarray.h
|
|
|
|
// Purpose: scoped smart pointer class
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2009-02-03
|
|
|
|
// Copyright: (c) Jesse Lovelace and original Boost authors (see below)
|
|
|
|
// (c) 2009 Vadim Zeitlin
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_SCOPED_ARRAY_H_
|
|
|
|
#define _WX_SCOPED_ARRAY_H_
|
|
|
|
|
|
|
|
#include "wx/defs.h"
|
|
|
|
#include "wx/checkeddelete.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxScopedArray: A scoped array
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class wxScopedArray
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T element_type;
|
|
|
|
|
2016-08-30 04:44:32 -04:00
|
|
|
explicit wxScopedArray(T * array = NULL) : m_array(array) { }
|
|
|
|
explicit wxScopedArray(size_t count) : m_array(new T[count]) { }
|
2009-02-03 07:01:46 -05:00
|
|
|
|
|
|
|
~wxScopedArray() { delete [] m_array; }
|
|
|
|
|
|
|
|
// test for pointer validity: defining conversion to unspecified_bool_type
|
|
|
|
// and not more obvious bool to avoid implicit conversions to integer types
|
|
|
|
typedef T *(wxScopedArray<T>::*unspecified_bool_type)() const;
|
|
|
|
operator unspecified_bool_type() const
|
|
|
|
{
|
|
|
|
return m_array ? &wxScopedArray<T>::get : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(T *array = NULL)
|
|
|
|
{
|
|
|
|
if ( array != m_array )
|
|
|
|
{
|
2011-12-24 10:43:14 -05:00
|
|
|
delete [] m_array;
|
2009-02-03 07:01:46 -05:00
|
|
|
m_array = array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](size_t n) const { return m_array[n]; }
|
|
|
|
|
|
|
|
T *get() const { return m_array; }
|
|
|
|
|
|
|
|
void swap(wxScopedArray &other)
|
|
|
|
{
|
|
|
|
T * const tmp = other.m_array;
|
|
|
|
other.m_array = m_array;
|
|
|
|
m_array = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *m_array;
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedArray, T);
|
2009-02-03 07:01:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// old macro based implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the same but for arrays instead of simple pointers
|
|
|
|
#define wxDECLARE_SCOPED_ARRAY(T, name)\
|
|
|
|
class name \
|
|
|
|
{ \
|
|
|
|
private: \
|
|
|
|
T * m_ptr; \
|
|
|
|
name(name const &); \
|
|
|
|
name & operator=(name const &); \
|
|
|
|
\
|
|
|
|
public: \
|
2016-08-30 04:44:32 -04:00
|
|
|
explicit name(T * p = NULL) : m_ptr(p) \
|
2009-02-03 07:01:46 -05:00
|
|
|
{} \
|
|
|
|
\
|
|
|
|
~name(); \
|
|
|
|
void reset(T * p = NULL); \
|
|
|
|
\
|
|
|
|
T & operator[](long int i) const\
|
|
|
|
{ \
|
|
|
|
wxASSERT(m_ptr != NULL); \
|
|
|
|
wxASSERT(i >= 0); \
|
|
|
|
return m_ptr[i]; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
T * get() const \
|
|
|
|
{ \
|
|
|
|
return m_ptr; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
void swap(name & ot) \
|
|
|
|
{ \
|
|
|
|
T * tmp = ot.m_ptr; \
|
|
|
|
ot.m_ptr = m_ptr; \
|
|
|
|
m_ptr = tmp; \
|
|
|
|
} \
|
|
|
|
};
|
|
|
|
|
|
|
|
#define wxDEFINE_SCOPED_ARRAY(T, name) \
|
|
|
|
name::~name() \
|
|
|
|
{ \
|
|
|
|
wxCHECKED_DELETE_ARRAY(m_ptr); \
|
|
|
|
} \
|
|
|
|
void name::reset(T * p){ \
|
|
|
|
if (m_ptr != p) \
|
|
|
|
{ \
|
|
|
|
wxCHECKED_DELETE_ARRAY(m_ptr); \
|
|
|
|
m_ptr = p; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _WX_SCOPED_ARRAY_H_
|
|
|
|
|