$extrastylesheet
libMesh::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 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 148 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 161 of file auto_ptr.h.


Constructor & Destructor Documentation

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

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.

Definition at line 170 of file auto_ptr.h.

    : _ptr(p)
  {
    // Note: we can't call libmesh_deprecated() here, since global
    // AutoPtr variables are sometimes created before the libMesh::out
    // stream is ready.
  }
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 ownsership.

Definition at line 185 of file auto_ptr.h.

    : _ptr(a.release())
  {
  }
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 ownsership.

Definition at line 200 of file auto_ptr.h.

    : _ptr(a.release())
  {
  }
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.

maint

Definition at line 250 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::_ptr, libMesh::out, and libMesh::warned_about_auto_ptr.

  {
    if (!libMesh::warned_about_auto_ptr)
      {
        libMesh::warned_about_auto_ptr = true;
        libMesh::out << "*** Warning, AutoPtr is deprecated and will be removed in a future library version! "
                     << __FILE__ << ", line " << __LINE__ << ", compiled " << __DATE__ << " at " << __TIME__ << " ***" << std::endl;
      }
    delete _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 341 of file auto_ptr.h.

    : _ptr(ref._ptr) {}

Member Function Documentation

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 292 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::_ptr.

{ 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 372 of file auto_ptr.h.

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

  { return AutoPtr<Tp1>(this->release()); }
template<typename Tp>
template<typename Tp1 >
libMesh::AutoPtr< Tp >::operator AutoPtrRef< Tp1 > ( ) [inline]

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

Definition at line 365 of file auto_ptr.h.

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

  { return AutoPtrRef<Tp1>(this->release()); }
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 270 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::_ptr.

{ return *_ptr; }
template<typename Tp>
element_type* libMesh::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 279 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::_ptr.

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

Definition at line 214 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::release(), and libMesh::AutoPtr< Tp >::reset().

  {
    reset(a.release());
    return *this;
  }
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 ownsership. The object that this one used to own and track has been deleted.

Definition at line 232 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::release(), and libMesh::AutoPtr< Tp >::reset().

  {
    reset(a.release());
    return *this;
  }
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 351 of file auto_ptr.h.

References libMesh::AutoPtrRef< Tp1 >::_ptr, and libMesh::AutoPtr< Tp >::_ptr.

  {
    if (ref._ptr != this->get())
      {
        delete _ptr;
        _ptr = ref._ptr;
      }
    return *this;
  }
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 306 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::_ptr.

Referenced by libMesh::AutoPtr< Tp >::operator AutoPtr< Tp1 >(), libMesh::AutoPtr< Tp >::operator AutoPtrRef< Tp1 >(), and libMesh::AutoPtr< Tp >::operator=().

  {
    element_type* tmp = _ptr;
    _ptr = 0;
    return tmp;
  }
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 321 of file auto_ptr.h.

References libMesh::AutoPtr< Tp >::_ptr.

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

  {
    if (p != _ptr)
      {
        delete _ptr;
        _ptr = p;
      }
  }

Member Data Documentation


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