2002-08-08 05:57:05 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2009-02-03 07:01:46 -05:00
|
|
|
// Name: wx/scopedptr.h
|
2002-08-08 05:57:05 -04:00
|
|
|
// Purpose: scoped smart pointer class
|
|
|
|
// Author: Jesse Lovelace <jllovela@eos.ncsu.edu>
|
|
|
|
// Created: 06/01/02
|
|
|
|
// Copyright: (c) Jesse Lovelace and original Boost authors (see below)
|
2009-01-27 11:40:51 -05:00
|
|
|
// (c) 2009 Vadim Zeitlin
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
2002-08-08 05:57:05 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// This class closely follows the implementation of the boost
|
2016-04-12 11:02:14 -04:00
|
|
|
// library scoped_ptr and is an adaptation for c++ macro's in
|
2004-05-23 10:56:36 -04:00
|
|
|
// the wxWidgets project. The original authors of the boost
|
2002-08-08 05:57:05 -04:00
|
|
|
// scoped_ptr are given below with their respective copyrights.
|
|
|
|
|
|
|
|
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
|
|
|
|
// Copyright (c) 2001, 2002 Peter Dimov
|
|
|
|
//
|
|
|
|
// Permission to copy, use, modify, sell and distribute this software
|
|
|
|
// is granted provided this copyright notice appears in all copies.
|
|
|
|
// This software is provided "as is" without express or implied
|
|
|
|
// warranty, and with no claim as to its suitability for any purpose.
|
|
|
|
//
|
|
|
|
// See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
|
|
|
|
//
|
|
|
|
|
2009-02-03 07:01:46 -05:00
|
|
|
#ifndef _WX_SCOPED_PTR_H_
|
|
|
|
#define _WX_SCOPED_PTR_H_
|
2002-08-08 05:57:05 -04:00
|
|
|
|
|
|
|
#include "wx/defs.h"
|
2009-02-03 07:01:46 -05:00
|
|
|
#include "wx/checkeddelete.h"
|
2002-08-08 05:57:05 -04:00
|
|
|
|
2008-01-07 07:09:22 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-01-27 11:40:51 -05:00
|
|
|
// wxScopedPtr: A scoped pointer
|
2008-01-07 07:09:22 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-01-27 11:40:51 -05:00
|
|
|
template <class T>
|
2008-01-07 07:09:22 -05:00
|
|
|
class wxScopedPtr
|
2009-01-27 11:40:51 -05:00
|
|
|
{
|
|
|
|
public:
|
2008-01-07 07:09:22 -05:00
|
|
|
typedef T element_type;
|
2009-01-27 11:40:51 -05:00
|
|
|
|
2016-08-30 04:44:32 -04:00
|
|
|
explicit wxScopedPtr(T * ptr = NULL) : m_ptr(ptr) { }
|
2009-01-27 11:40:51 -05:00
|
|
|
|
2010-05-06 01:42:52 -04:00
|
|
|
~wxScopedPtr() { wxCHECKED_DELETE(m_ptr); }
|
2009-01-27 11:40:51 -05:00
|
|
|
|
2008-01-08 17:02:12 -05:00
|
|
|
// test for pointer validity: defining conversion to unspecified_bool_type
|
|
|
|
// and not more obvious bool to avoid implicit conversions to integer types
|
|
|
|
typedef T *(wxScopedPtr<T>::*unspecified_bool_type)() const;
|
2020-10-13 12:32:49 -04:00
|
|
|
|
2008-01-08 17:02:12 -05:00
|
|
|
operator unspecified_bool_type() const
|
|
|
|
{
|
|
|
|
return m_ptr ? &wxScopedPtr<T>::get : NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-27 11:40:51 -05:00
|
|
|
void reset(T * ptr = NULL)
|
|
|
|
{
|
|
|
|
if ( ptr != m_ptr )
|
|
|
|
{
|
2010-05-06 01:42:57 -04:00
|
|
|
wxCHECKED_DELETE(m_ptr);
|
2009-01-27 11:40:51 -05:00
|
|
|
m_ptr = ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T *release()
|
|
|
|
{
|
|
|
|
T *ptr = m_ptr;
|
|
|
|
m_ptr = NULL;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T & operator*() const
|
|
|
|
{
|
|
|
|
wxASSERT(m_ptr != NULL);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T * operator->() const
|
|
|
|
{
|
|
|
|
wxASSERT(m_ptr != NULL);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T * get() const
|
|
|
|
{
|
|
|
|
return m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(wxScopedPtr& other)
|
|
|
|
{
|
|
|
|
T * const tmp = other.m_ptr;
|
|
|
|
other.m_ptr = m_ptr;
|
|
|
|
m_ptr = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T * m_ptr;
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedPtr, T);
|
2009-01-27 11:40:51 -05:00
|
|
|
};
|
|
|
|
|
2008-01-07 07:09:22 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// old macro based implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/* The type being used *must* be complete at the time
|
2002-08-08 05:57:05 -04:00
|
|
|
that wxDEFINE_SCOPED_* is called or a compiler error will result.
|
|
|
|
This is because the class checks for the completeness of the type
|
2008-01-07 07:09:22 -05:00
|
|
|
being used. */
|
2002-08-08 05:57:05 -04:00
|
|
|
|
|
|
|
#define wxDECLARE_SCOPED_PTR(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 * ptr = NULL) \
|
2002-08-08 05:57:05 -04:00
|
|
|
: m_ptr(ptr) { } \
|
|
|
|
\
|
|
|
|
~name(); \
|
|
|
|
\
|
2010-05-06 01:43:01 -04:00
|
|
|
void reset(T * ptr = NULL); \
|
2002-08-08 05:57:05 -04:00
|
|
|
\
|
2003-06-29 11:44:08 -04:00
|
|
|
T *release() \
|
|
|
|
{ \
|
|
|
|
T *ptr = m_ptr; \
|
|
|
|
m_ptr = NULL; \
|
|
|
|
return ptr; \
|
|
|
|
} \
|
|
|
|
\
|
2002-08-08 05:57:05 -04:00
|
|
|
T & operator*() const \
|
|
|
|
{ \
|
|
|
|
wxASSERT(m_ptr != NULL); \
|
|
|
|
return *m_ptr; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
T * operator->() const \
|
|
|
|
{ \
|
|
|
|
wxASSERT(m_ptr != NULL); \
|
|
|
|
return m_ptr; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
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_PTR(T, name)\
|
2010-05-06 01:43:01 -04:00
|
|
|
void name::reset(T * ptr) \
|
|
|
|
{ \
|
|
|
|
if (m_ptr != ptr) \
|
|
|
|
{ \
|
|
|
|
wxCHECKED_DELETE(m_ptr); \
|
|
|
|
m_ptr = ptr; \
|
|
|
|
} \
|
|
|
|
} \
|
2002-08-08 05:57:05 -04:00
|
|
|
name::~name() \
|
|
|
|
{ \
|
2003-08-14 19:00:33 -04:00
|
|
|
wxCHECKED_DELETE(m_ptr); \
|
2002-08-08 05:57:05 -04:00
|
|
|
}
|
|
|
|
|
2003-09-15 12:10:06 -04:00
|
|
|
// this macro can be used for the most common case when you want to declare and
|
|
|
|
// define the scoped pointer at the same time and want to use the standard
|
|
|
|
// naming convention: auto pointer to Foo is called FooPtr
|
2005-06-13 08:19:33 -04:00
|
|
|
#define wxDEFINE_SCOPED_PTR_TYPE(T) \
|
|
|
|
wxDECLARE_SCOPED_PTR(T, T ## Ptr) \
|
2003-09-15 12:10:06 -04:00
|
|
|
wxDEFINE_SCOPED_PTR(T, T ## Ptr)
|
|
|
|
|
2003-09-17 13:27:36 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// "Tied" scoped pointer: same as normal one but also sets the value of
|
|
|
|
// some other variable to the pointer value
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
|
2005-09-25 07:16:57 -04:00
|
|
|
wxDEFINE_SCOPED_PTR_TYPE(T) \
|
2003-09-17 13:27:36 -04:00
|
|
|
class T ## TiedPtr : public T ## Ptr \
|
|
|
|
{ \
|
|
|
|
public: \
|
|
|
|
T ## TiedPtr(T **pp, T *p) \
|
|
|
|
: T ## Ptr(p), m_pp(pp) \
|
|
|
|
{ \
|
|
|
|
m_pOld = *pp; \
|
|
|
|
*pp = p; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
~ T ## TiedPtr() \
|
|
|
|
{ \
|
|
|
|
*m_pp = m_pOld; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
private: \
|
|
|
|
T **m_pp; \
|
|
|
|
T *m_pOld; \
|
2005-09-25 07:16:57 -04:00
|
|
|
};
|
2003-09-17 13:27:36 -04:00
|
|
|
|
2009-02-03 07:01:46 -05:00
|
|
|
#endif // _WX_SCOPED_PTR_H_
|
2003-09-15 12:10:06 -04:00
|
|
|
|