wxWidgets/include/wx/scopedptr.h
Vadim Zeitlin 3f66f6a5b3 Remove all lines containing cvs/svn "$Id$" keyword.
This keyword is not expanded by Git which means it's not replaced with the
correct revision value in the releases made using git-based scripts and it's
confusing to have lines with unexpanded "$Id$" in the released files. As
expanding them with Git is not that simple (it could be done with git archive
and export-subst attribute) and there are not many benefits in having them in
the first place, just remove all these lines.

If nothing else, this will make an eventual transition to Git simpler.

Closes #14487.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74602 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2013-07-26 16:02:46 +00:00

215 lines
7.6 KiB
C++

/////////////////////////////////////////////////////////////////////////////
// Name: wx/scopedptr.h
// 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)
// (c) 2009 Vadim Zeitlin
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// This class closely follows the implementation of the boost
// library scoped_ptr and is an adaption for c++ macro's in
// the wxWidgets project. The original authors of the boost
// 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.
//
#ifndef _WX_SCOPED_PTR_H_
#define _WX_SCOPED_PTR_H_
#include "wx/defs.h"
#include "wx/checkeddelete.h"
// ----------------------------------------------------------------------------
// wxScopedPtr: A scoped pointer
// ----------------------------------------------------------------------------
template <class T>
class wxScopedPtr
{
public:
typedef T element_type;
wxEXPLICIT wxScopedPtr(T * ptr = NULL) : m_ptr(ptr) { }
~wxScopedPtr() { wxCHECKED_DELETE(m_ptr); }
// test for pointer validity: defining conversion to unspecified_bool_type
// and not more obvious bool to avoid implicit conversions to integer types
#ifdef __BORLANDC__
// this compiler is too dumb to use unspecified_bool_type operator in tests
// of the form "if ( !ptr )"
typedef bool unspecified_bool_type;
#else
typedef T *(wxScopedPtr<T>::*unspecified_bool_type)() const;
#endif // __BORLANDC__
operator unspecified_bool_type() const
{
return m_ptr ? &wxScopedPtr<T>::get : NULL;
}
void reset(T * ptr = NULL)
{
if ( ptr != m_ptr )
{
wxCHECKED_DELETE(m_ptr);
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;
DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedPtr, T)
};
// ----------------------------------------------------------------------------
// old macro based implementation
// ----------------------------------------------------------------------------
/* The type being used *must* be complete at the time
that wxDEFINE_SCOPED_* is called or a compiler error will result.
This is because the class checks for the completeness of the type
being used. */
#define wxDECLARE_SCOPED_PTR(T, name) \
class name \
{ \
private: \
T * m_ptr; \
\
name(name const &); \
name & operator=(name const &); \
\
public: \
wxEXPLICIT name(T * ptr = NULL) \
: m_ptr(ptr) { } \
\
~name(); \
\
void reset(T * ptr = NULL); \
\
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(name & ot) \
{ \
T * tmp = ot.m_ptr; \
ot.m_ptr = m_ptr; \
m_ptr = tmp; \
} \
};
#define wxDEFINE_SCOPED_PTR(T, name)\
void name::reset(T * ptr) \
{ \
if (m_ptr != ptr) \
{ \
wxCHECKED_DELETE(m_ptr); \
m_ptr = ptr; \
} \
} \
name::~name() \
{ \
wxCHECKED_DELETE(m_ptr); \
}
// 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
#define wxDEFINE_SCOPED_PTR_TYPE(T) \
wxDECLARE_SCOPED_PTR(T, T ## Ptr) \
wxDEFINE_SCOPED_PTR(T, T ## Ptr)
// ----------------------------------------------------------------------------
// "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) \
wxDEFINE_SCOPED_PTR_TYPE(T) \
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; \
};
#endif // _WX_SCOPED_PTR_H_