libMesh::AutoPtr< Tp > Class Template Reference

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

#include <auto_ptr.h>

Inheritance diagram for libMesh::AutoPtr< Tp >:

Public Types

typedef Tp element_type
 

Public Member Functions

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

Private Attributes

Tp * _ptr
 

Detailed Description

template<typename Tp>
class libMesh::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.

The following are the original copyright declarations distributed with this class:

Copyright (C) 2001, 2002 Free Software Foundation, Inc.

This file is part of the GNU ISO C++ Library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this library; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

As a special exception, you may use this file as part of a free software library without restriction. Specifically, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other files to produce an executable, this file does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License.

Copyright (c) 1997-1999 Silicon Graphics Computer Systems, Inc.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Silicon Graphics makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.

Definition at line 210 of file auto_ptr.h.

Member Typedef Documentation

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

The pointed-to type.

Definition at line 223 of file auto_ptr.h.

Constructor & Destructor Documentation

template<typename Tp>
libMesh::AutoPtr< Tp >::AutoPtr ( element_type p = 0)
inlineexplicit

An AutoPtr is usually constructed from a raw pointer.

Parameters
pA pointer (defaults to NULL).

This object now owns the object pointed to by p.

Note
We can't call libmesh_deprecated() in this function, since global AutoPtr variables are sometimes created before the libMesh::out stream is ready.

Definition at line 236 of file auto_ptr.h.

237  : _ptr(p)
238  {
239  }
template<typename Tp>
libMesh::AutoPtr< Tp >::AutoPtr ( AutoPtr< Tp > &  a)
inline

An AutoPtr can be constructed from another AutoPtr.

Parameters
aAnother AutoPtr of the same type.

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

Definition at line 248 of file auto_ptr.h.

249  : _ptr(a.release())
250  {
251  }
template<typename Tp>
template<typename Tp1 >
libMesh::AutoPtr< Tp >::AutoPtr ( AutoPtr< Tp1 > &  a)
inline

An AutoPtr can be constructed from another AutoPtr.

Parameters
aAnother 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 ownership.

Definition at line 263 of file auto_ptr.h.

264  : _ptr(a.release())
265  {
266  }
template<typename Tp>
libMesh::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.

The C++ standard says there is supposed to be an empty throw specification here, but omitting it is standard conforming. Its presence can be detected only if _Tp::~_Tp() throws, but this is prohibited. [17.4.3.6]/2

Definition at line 311 of file auto_ptr.h.

References libMesh::AutoPtrRef< Tp1 >::_ptr, and libMesh::warned_about_auto_ptr.

312  {
314  {
316  libmesh_deprecated();
317  }
318  delete _ptr;
319  }
bool warned_about_auto_ptr
template<typename Tp>
libMesh::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 401 of file auto_ptr.h.

402  : _ptr(ref._ptr) {}

Member Function Documentation

template<typename Tp>
bool libMesh::AutoPtr< Tp >::boolean_test ( ) const
inline

A "safe" replacement for operator bool () that behaves more like an explicit conversion operator even in C++98. This allows code like:

if(!foo)

to work with AutoPtr.

Definition at line 433 of file auto_ptr.h.

434  {
435  return (this->get() != NULL);
436  }
template<typename Tp>
element_type* libMesh::AutoPtr< Tp >::get ( ) const
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 still owns the memory.

Definition at line 352 of file auto_ptr.h.

References libMesh::AutoPtrRef< Tp1 >::_ptr.

352 { return _ptr; }
template<typename Tp>
template<typename Tp1 >
libMesh::AutoPtr< Tp >::operator AutoPtr< Tp1 > ( )
inline

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

Definition at line 449 of file auto_ptr.h.

450  { return AutoPtr<Tp1>(this->release()); }
element_type * release()
Bypassing the smart pointer.
Definition: auto_ptr.h:366
template<typename Tp>
template<typename Tp1 >
libMesh::AutoPtr< Tp >::operator AutoPtrRef< Tp1 > ( )
inline

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

Definition at line 442 of file auto_ptr.h.

443  { return AutoPtrRef<Tp1>(this->release()); }
element_type * release()
Bypassing the smart pointer.
Definition: auto_ptr.h:366
libMesh::safe_bool< AutoPtr< Tp > >::operator bool_type ( ) const
inlineinherited

Definition at line 65 of file safe_bool.h.

References libMesh::safe_bool_base::this_type_does_not_support_comparisons().

66  {
67  return (static_cast<const T *>(this))->boolean_test()
68  ? &safe_bool_base::this_type_does_not_support_comparisons : 0;
69  }
template<typename Tp>
element_type& libMesh::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 330 of file auto_ptr.h.

References libMesh::AutoPtrRef< Tp1 >::_ptr.

330 { return *_ptr; }
template<typename Tp>
element_type* libMesh::AutoPtr< Tp >::operator-> ( ) const
inline

Smart pointer dereferencing.

Returns
The pointer itself, which the language then will automatically cause to be dereferenced.

Definition at line 339 of file auto_ptr.h.

References libMesh::AutoPtrRef< Tp1 >::_ptr.

339 { return _ptr; }
template<typename Tp>
AutoPtr& libMesh::AutoPtr< Tp >::operator= ( AutoPtr< Tp > &  a)
inline

AutoPtr assignment operator.

Parameters
aAnother AutoPtr of the same type.

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

Definition at line 277 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::release().

278  {
279  reset(a.release());
280  return *this;
281  }
void reset(element_type *p=0)
Forcibly deletes the managed object.
Definition: auto_ptr.h:381
template<typename Tp>
template<typename Tp1 >
AutoPtr& libMesh::AutoPtr< Tp >::operator= ( AutoPtr< Tp1 > &  a)
inline

AutoPtr assignment operator.

Parameters
aAnother 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 ownership. The object that this one used to own and track has been deleted.

Definition at line 295 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::release().

296  {
297  reset(a.release());
298  return *this;
299  }
void reset(element_type *p=0)
Forcibly deletes the managed object.
Definition: auto_ptr.h:381
template<typename Tp>
AutoPtr& libMesh::AutoPtr< Tp >::operator= ( AutoPtrRef< element_type ref)
inline

op= for AutoPtr. Allows you to write:

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

Definition at line 412 of file auto_ptr.h.

References libMesh::AutoPtrRef< Tp1 >::_ptr.

413  {
414  if (ref._ptr != this->get())
415  {
416  delete _ptr;
417  _ptr = ref._ptr;
418  }
419  return *this;
420  }
template<typename Tp>
element_type* libMesh::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 366 of file auto_ptr.h.

References libMesh::AutoPtrRef< Tp1 >::_ptr.

Referenced by libMesh::AutoPtr< Tp >::operator=().

367  {
368  element_type * tmp = _ptr;
369  _ptr = 0;
370  return tmp;
371  }
template<typename Tp>
void libMesh::AutoPtr< Tp >::reset ( element_type p = 0)
inline

Forcibly deletes the managed object.

Parameters
pA pointer (defaults to NULL).

This object now owns the object pointed to by p. The previous object has been deleted.

Definition at line 381 of file auto_ptr.h.

References libMesh::AutoPtrRef< Tp1 >::_ptr.

382  {
383  if (p != _ptr)
384  {
385  delete _ptr;
386  _ptr = p;
387  }
388  }

Member Data Documentation

template<typename Tp>
Tp* libMesh::AutoPtr< Tp >::_ptr
private

The actual dumb pointer this class wraps.

Definition at line 217 of file auto_ptr.h.


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