AutoPtr< Tp > Class Template Reference

A simple smart pointer providing strict ownership semantics. More...

#include <auto_ptr.h>

List of all members.

Public Types

typedef Tp element_type

Public Member Functions

 AutoPtr (element_type *p=0)
 An AutoPtr is usually constructed from a raw pointer.
 AutoPtr (AutoPtr &a)
 An AutoPtr can be constructed from another AutoPtr.
template<typename Tp1 >
 AutoPtr (AutoPtr< Tp1 > &a)
 An AutoPtr can be constructed from another AutoPtr.
AutoPtroperator= (AutoPtr &a)
 AutoPtr assignment operator.
template<typename Tp1 >
AutoPtroperator= (AutoPtr< Tp1 > &a)
 AutoPtr assignment operator.
 ~AutoPtr ()
element_typeoperator* () const
 Smart pointer dereferencing.
element_typeoperator-> () const
 Smart pointer dereferencing.
element_typeget () const
 Bypassing the smart pointer.
element_typerelease ()
 Bypassing the smart pointer.
void reset (element_type *p=0)
 Forcibly deletes the managed object.
 AutoPtr (AutoPtrRef< element_type > ref)
 Automatic conversions.
AutoPtroperator= (AutoPtrRef< element_type > ref)
template<typename Tp1 >
 operator AutoPtrRef< Tp1 > ()
template<typename Tp1 >
 operator AutoPtr< Tp1 > ()

Private Attributes

Tp * _ptr


Detailed Description

template<typename Tp>
class AutoPtr< Tp >

A simple smart pointer providing strict ownership semantics.

The Standard says:

  An AutoPtr owns the object it holds a pointer to.  Copying an
  AutoPtr copies the pointer and transfers ownership to the destination.
  If more than one AutoPtr owns the same object at the same time the
  behavior of the program is undefined.

  The uses of AutoPtr include providing temporary exception-safety for
  dynamically allocated memory, passing ownership of dynamically allocated
  memory to a function, and returning dynamically allocated memory from a
  function.  AutoPtr does not meet the CopyConstructible and Assignable
  requirements for Standard Library container
  elements and thus instantiating a Standard Library container with an
  AutoPtr results in undefined behavior.
  
Quoted from [20.4.5]/3.

This class is adopted from the GCC 3.2.1 source tree and should function as a replacement for std::auto_ptr<>. Unfortunately the std::auto_ptr<> is not particularly portable since various compilers implement various revisions of the standard. Using AutoPtr<> instead of std::auto_ptr<> allows for easy portability.

Definition at line 103 of file auto_ptr.h.


Member Typedef Documentation

template<typename Tp >
typedef Tp AutoPtr< Tp >::element_type

The pointed-to type.

Definition at line 116 of file auto_ptr.h.


Constructor & Destructor Documentation

template<typename Tp >
AutoPtr< Tp >::AutoPtr ( element_type p = 0  )  [inline, explicit]

An AutoPtr is usually constructed from a raw pointer.

Parameters:
p A pointer (defaults to NULL).
This object now owns the object pointed to by p.

Definition at line 125 of file auto_ptr.h.

00126     : _ptr(p) {}

template<typename Tp >
AutoPtr< Tp >::AutoPtr ( AutoPtr< Tp > &  a  )  [inline]

An AutoPtr can be constructed from another AutoPtr.

Parameters:
a Another AutoPtr of the same type.
This object now owns the object previously owned by a, which has given up ownsership.

Definition at line 135 of file auto_ptr.h.

00136     : _ptr(a.release()) {}

template<typename Tp >
template<typename Tp1 >
AutoPtr< Tp >::AutoPtr ( AutoPtr< Tp1 > &  a  )  [inline]

An AutoPtr can be constructed from another AutoPtr.

Parameters:
a Another AutoPtr of a different but related type.
A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.

This object now owns the object previously owned by a, which has given up ownsership.

Definition at line 148 of file auto_ptr.h.

00149     : _ptr(a.release()) {}

template<typename Tp >
AutoPtr< Tp >::~AutoPtr (  )  [inline]

When the AutoPtr goes out of scope, the object it owns is deleted. If it no longer owns anything (i.e., get() is NULL), then this has no effect.

maint

Definition at line 196 of file auto_ptr.h.

00196 { delete _ptr; }

template<typename Tp >
AutoPtr< Tp >::AutoPtr ( AutoPtrRef< element_type ref  )  [inline]

Automatic conversions.

These operations convert an AutoPtr into and from an AutoPtrRef automatically as needed. This allows constructs such as

    AutoPtr<Derived>  func_returning_AutoPtr(.....);
    ...
    AutoPtr<Base> ptr = func_returning_AutoPtr(.....);

Definition at line 278 of file auto_ptr.h.

00279     : _ptr(ref._ptr) {}


Member Function Documentation

template<typename Tp >
template<typename Tp1 >
AutoPtr< Tp >::operator AutoPtr< Tp1 > (  )  [inline]

op() for AutoPtr<Tp1>. Calls the release member.

Definition at line 309 of file auto_ptr.h.

00310   { return AutoPtr<Tp1>(this->release()); }

template<typename Tp >
template<typename Tp1 >
AutoPtr< Tp >::operator AutoPtrRef< Tp1 > (  )  [inline]

op() for AutoPtrRef<Tp1>. Calls the release member.

Definition at line 302 of file auto_ptr.h.

00303   { return AutoPtrRef<Tp1>(this->release()); }

template<typename Tp >
element_type& AutoPtr< Tp >::operator* (  )  const [inline]

Smart pointer dereferencing.

If this AutoPtr no longer owns anything, then this operation will crash. (For a smart pointer, "no longer owns anything" is the same as being a null pointer, and you know what happens when you dereference one of those...)

Definition at line 207 of file auto_ptr.h.

00207 { return *_ptr; }

template<typename Tp >
element_type* AutoPtr< Tp >::operator-> (  )  const [inline]

Smart pointer dereferencing.

This returns the pointer itself, which the language then will automatically cause to be dereferenced.

Definition at line 216 of file auto_ptr.h.

00216 { return _ptr; }

template<typename Tp >
AutoPtr& AutoPtr< Tp >::operator= ( AutoPtrRef< element_type ref  )  [inline]

op= for AutoPtr. Allows you to write:

 AutoPtr<Base> ptr = func_returning_AutoPtr(.....);

Definition at line 288 of file auto_ptr.h.

00289   {
00290     if (ref._ptr != this->get())
00291       {
00292         delete _ptr;
00293         _ptr = ref._ptr;
00294       }
00295     return *this;
00296   }

template<typename Tp >
template<typename Tp1 >
AutoPtr& AutoPtr< Tp >::operator= ( AutoPtr< Tp1 > &  a  )  [inline]

AutoPtr assignment operator.

Parameters:
a Another AutoPtr of a different but related type.
A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.

This object now owns the object previously owned by a, which has given up ownsership. The object that this one used to own and track has been deleted.

Definition at line 178 of file auto_ptr.h.

00179   {
00180     reset(a.release());
00181     return *this;
00182   }

template<typename Tp >
AutoPtr& AutoPtr< Tp >::operator= ( AutoPtr< Tp > &  a  )  [inline]

AutoPtr assignment operator.

Parameters:
a Another AutoPtr of the same type.
This object now owns the object previously owned by a, which has given up ownsership. The object that this one used to own and track has been deleted.

Definition at line 160 of file auto_ptr.h.

00161   {
00162     reset(a.release());
00163     return *this;
00164   }

template<typename Tp >
element_type* AutoPtr< Tp >::release (  )  [inline]

Bypassing the smart pointer.

Returns:
The raw pointer being managed.
You can get a copy of the pointer that this object owns, for situations such as passing to a function which only accepts a raw pointer.

Note:
This AutoPtr no longer owns the memory. When this object goes out of scope, nothing will happen.

Definition at line 243 of file auto_ptr.h.

Referenced by InfFE< Dim, T_radial, T_map >::attach_quadrature_rule(), FEMContext::FEMContext(), InfFE< Dim, T_radial, T_map >::InfFE(), InfFE< Dim, T_radial, T_map >::init_face_shape_functions(), AutoPtr< PointLocatorBase >::operator AutoPtr< Tp1 >(), AutoPtr< PointLocatorBase >::operator AutoPtrRef< Tp1 >(), AutoPtr< PointLocatorBase >::operator=(), MeshBase::point_locator(), and InfFE< Dim, T_radial, T_map >::reinit().

00244   {
00245     element_type* tmp = _ptr;
00246     _ptr = 0;
00247     return tmp;
00248   }

template<typename Tp >
void AutoPtr< Tp >::reset ( element_type p = 0  )  [inline]

Forcibly deletes the managed object.

Parameters:
p A pointer (defaults to NULL).
This object now owns the object pointed to by p. The previous object has been deleted.

Definition at line 258 of file auto_ptr.h.

Referenced by NonlinearSolver< T >::build(), MeshBase::clear_point_locator(), Xdr::close(), Xdr::open(), AutoPtr< PointLocatorBase >::operator=(), MeshBase::point_locator(), BoundaryInfo::sync(), and TwostepTimeSolver::TwostepTimeSolver().

00259   {
00260     if (p != _ptr)
00261       {
00262         delete _ptr;
00263         _ptr = p;
00264       }
00265   }


Member Data Documentation


The documentation for this class was generated from the following file:

Site Created By: libMesh Developers
Last modified: November 25 2009 03:43:54.

Hosted By:
SourceForge.net Logo