Teuchos::Ptr< T > Class Template Reference

Simple wrapper class for raw pointers to single objects where no persisting relationship exists. More...

#include <Teuchos_PtrDecl.hpp>

List of all members.

Public Member Functions

 Ptr (ENull null_in=null)
 Default construct to NULL.
 Ptr (T *ptr)
 Construct given a raw pointer.
 Ptr (const Ptr< T > &ptr)
 Copy construct from same type.
template<class T2 >
 Ptr (const Ptr< T2 > &ptr)
 Copy construct from another type.
Ptr< T > & operator= (const Ptr< T > &ptr)
 Shallow copy of the underlying pointer.
T * operator-> () const
 Pointer (->) access to members of underlying object.
T & operator* () const
 Dereference the underlying object.
T * get () const
 Get the raw C++ pointer to the underlying object.
T * getRawPtr () const
 Get the raw C++ pointer to the underlying object.
const Ptr< T > & assert_not_null () const
 Throws std::logic_error if this->get()==NULL, otherwise returns reference to *this.
const Ptr< T > ptr () const
 Return a copy of *this.

Private Member Functions

void debug_assert_not_null () const
void debug_assert_valid_ptr () const

Private Attributes

T * ptr_

Related Functions

(Note that these are not member functions.)



template<typename T >
Ptr< T > outArg (T &arg)
 create a non-persisting (required or optional) output argument for a function call.
template<typename T >
Ptr< T > inOutArg (T &arg)
 create a non-persisting (required or optional) input/output argument for a function call.
template<typename T >
Ptr< const T > ptrInArg (T &arg)
 create a general Ptr input argument for a function call from a reference.
template<typename T >
Ptr< T > optInArg (T &arg)
 create a non-persisting non-const optional input argument for a function call.
template<typename T >
Ptr< const T > constOptInArg (T &arg)
 create a non-persisting const optional input argument for a function call.
template<typename T >
Ptr< T > ptrFromRef (T &arg)
 Create a pointer to a object from an object reference.
template<typename T >
Ptr< T > ptr (T *p)
 Create a pointer to an object from a raw pointer.
template<typename T >
Ptr< const T > constPtr (T &arg)
 Create a pointer from a const object given a non-const object reference.
template<class T2 , class T1 >
Ptr< T2 > ptr_implicit_cast (const Ptr< T1 > &p1)
 Implicit cast of underlying Ptr type from T1* to T2*.
template<class T2 , class T1 >
Ptr< T2 > ptr_static_cast (const Ptr< T1 > &p1)
 Static cast of underlying Ptr type from T1* to T2*.
template<class T2 , class T1 >
Ptr< T2 > ptr_const_cast (const Ptr< T1 > &p1)
 Constant cast of underlying Ptr type from T1* to T2*.
template<class T2 , class T1 >
Ptr< T2 > ptr_dynamic_cast (const Ptr< T1 > &p1, bool throw_on_fail=false)
 Dynamic cast of underlying Ptr type from T1* to T2*.

Detailed Description

template<class T>
class Teuchos::Ptr< T >

Simple wrapper class for raw pointers to single objects where no persisting relationship exists.

This class is meant to replace all but the lowest-level use of raw pointers that point to single objects where the use of RCP is not justified for performance or semantic reasons. When built in optimized mode, this class should impart little time overhead and should be exactly equivalent in the memory footprint to a raw C++ pointer and the only extra runtime overhead will be the default initalization to NULL.

The main advantages of using this class over a raw pointer however are:

Debugging support is compiled in when the macro TEUCHOS_DEBUG is defined which happens automatically when --enable-teuchos-debug is specified on the configure line. When debugging support is not compiled in, the only overhead imparted by this class is it's default initialization to null. Therefore, this class can provide for very high performance on optimized builds of the code.

An implicit conversion from a raw pointer to a Ptr object is okay since we don't assume any ownership of the object, hense the constructor taking a raw pointer is not declared explicit. However, this class does not support an implicit conversion to a raw pointer since we want to limit the exposure of raw pointers in our software. If we have to convert back to a raw pointer, then we want to make that explicit by calling get().

This class should be used to replace most raw uses of C++ pointers to single objects where using the RCP class is not appropriate, unless the runtime cost of null-initialization it too expensive.

Definition at line 91 of file Teuchos_PtrDecl.hpp.


Constructor & Destructor Documentation

template<class T >
Teuchos::Ptr< T >::Ptr ( ENull  null_in = null  )  [inline]

Default construct to NULL.

Postconditons:

Definition at line 47 of file Teuchos_Ptr.hpp.

template<class T >
Teuchos::Ptr< T >::Ptr ( T *  ptr  )  [inline, explicit]

Construct given a raw pointer.

Postconditons:

Note: This constructor is declared explicit so there is no implicit conversion from a raw C++ pointer to a Ptr object. This is meant to avoid cases where an uninitialized pointer is used to implicitly initialize one of these objects.

Definition at line 53 of file Teuchos_Ptr.hpp.

template<class T >
Teuchos::Ptr< T >::Ptr ( const Ptr< T > &  ptr  )  [inline]

Copy construct from same type.

Postconditons:

  • this->get() == ptr.get()

Definition at line 59 of file Teuchos_Ptr.hpp.

template<class T>
template<class T2 >
Teuchos::Ptr< T >::Ptr ( const Ptr< T2 > &  ptr  )  [inline]

Copy construct from another type.

Postconditons:

  • this->get() == ptr.get() (unless virtual base classes are involved)

Member Function Documentation

template<class T >
Ptr< T > & Teuchos::Ptr< T >::operator= ( const Ptr< T > &  ptr  )  [inline]

Shallow copy of the underlying pointer.

Postconditons:

  • this->get() == ptr.get()

Definition at line 72 of file Teuchos_Ptr.hpp.

template<class T >
T * Teuchos::Ptr< T >::operator-> (  )  const [inline]

Pointer (->) access to members of underlying object.

Preconditions:

  • this->get() != NULL (throws std::logic_error)

Definition at line 80 of file Teuchos_Ptr.hpp.

template<class T >
T & Teuchos::Ptr< T >::operator* (  )  const [inline]

Dereference the underlying object.

Preconditions:

  • this->get() != NULL (throws std::logic_error)

Definition at line 89 of file Teuchos_Ptr.hpp.

template<class T >
T * Teuchos::Ptr< T >::get (  )  const [inline]

Get the raw C++ pointer to the underlying object.

Definition at line 98 of file Teuchos_Ptr.hpp.

template<class T >
T * Teuchos::Ptr< T >::getRawPtr (  )  const [inline]

Get the raw C++ pointer to the underlying object.

Definition at line 106 of file Teuchos_Ptr.hpp.

template<class T >
const Ptr< T > & Teuchos::Ptr< T >::assert_not_null (  )  const [inline]

Throws std::logic_error if this->get()==NULL, otherwise returns reference to *this.

Definition at line 113 of file Teuchos_Ptr.hpp.

template<class T >
const Ptr< T > Teuchos::Ptr< T >::ptr (  )  const [inline]

Return a copy of *this.

Definition at line 122 of file Teuchos_Ptr.hpp.

template<class T>
void Teuchos::Ptr< T >::debug_assert_not_null (  )  const [inline, private]

Definition at line 179 of file Teuchos_PtrDecl.hpp.

template<class T >
void Teuchos::Ptr< T >::debug_assert_valid_ptr (  )  const [inline, private]

Definition at line 129 of file Teuchos_Ptr.hpp.


Friends And Related Function Documentation

template<typename T >
Ptr< T > outArg ( T &  arg  )  [related]

create a non-persisting (required or optional) output argument for a function call.

Definition at line 208 of file Teuchos_PtrDecl.hpp.

template<typename T >
Ptr< T > inOutArg ( T &  arg  )  [related]

create a non-persisting (required or optional) input/output argument for a function call.

Definition at line 220 of file Teuchos_PtrDecl.hpp.

template<typename T >
Ptr< const T > ptrInArg ( T &  arg  )  [related]

create a general Ptr input argument for a function call from a reference.

Definition at line 232 of file Teuchos_PtrDecl.hpp.

template<typename T >
Ptr< T > optInArg ( T &  arg  )  [related]

create a non-persisting non-const optional input argument for a function call.

Definition at line 244 of file Teuchos_PtrDecl.hpp.

template<typename T >
Ptr< const T > constOptInArg ( T &  arg  )  [related]

create a non-persisting const optional input argument for a function call.

Definition at line 256 of file Teuchos_PtrDecl.hpp.

template<typename T >
Ptr< T > ptrFromRef ( T &  arg  )  [related]

Create a pointer to a object from an object reference.

Definition at line 267 of file Teuchos_PtrDecl.hpp.

template<typename T >
Ptr< T > ptr ( T *  p  )  [related]

Create a pointer to an object from a raw pointer.

Definition at line 298 of file Teuchos_PtrDecl.hpp.

template<typename T >
Ptr< const T > constPtr ( T &  arg  )  [related]

Create a pointer from a const object given a non-const object reference.

Warning! Do not call this function if T is already const or a compilation error will occur!

Definition at line 313 of file Teuchos_PtrDecl.hpp.

template<class T2 , class T1 >
Ptr< T2 > ptr_implicit_cast ( const Ptr< T1 > &  p1  )  [related]

Implicit cast of underlying Ptr type from T1* to T2*.

The function will compile only if (T2* p2 = p1.get();) compiles.

This is to be used for conversions up an inheritance hierarchy and from non-const to const and any other standard implicit pointer conversions allowed by C++.

Definition at line 398 of file Teuchos_PtrDecl.hpp.

template<class T2 , class T1 >
Ptr< T2 > ptr_static_cast ( const Ptr< T1 > &  p1  )  [related]

Static cast of underlying Ptr type from T1* to T2*.

The function will compile only if (static_cast<T2*>(p1.get());) compiles.

This can safely be used for conversion down an inheritance hierarchy with polymorphic types only if dynamic_cast<T2>(p1.get()) == static_cast<T2>(p1.get()). If not then you have to use ptr_dynamic_cast<T2>(p1).

Definition at line 418 of file Teuchos_PtrDecl.hpp.

template<class T2 , class T1 >
Ptr< T2 > ptr_const_cast ( const Ptr< T1 > &  p1  )  [related]

Constant cast of underlying Ptr type from T1* to T2*.

This function will compile only if (const_cast<T2*>(p1.get());) compiles.

Definition at line 433 of file Teuchos_PtrDecl.hpp.

template<class T2 , class T1 >
Ptr< T2 > ptr_dynamic_cast ( const Ptr< T1 > &  p1,
bool  throw_on_fail = false 
) [related]

Dynamic cast of underlying Ptr type from T1* to T2*.

Parameters:
p1 [in] The smart pointer casting from
throw_on_fail [in] If true then if the cast fails (for p1.get()!=NULL) then a std::bad_cast std::exception is thrown with a very informative error message.

Postconditions:

  • If ( p1.get()!=NULL && throw_on_fail==true && dynamic_cast<T2*>(p1.get())==NULL ) == true then an std::bad_cast std::exception is thrown with a very informative error message.
  • If ( p1.get()!=NULL && dynamic_cast<T2*>(p1.get())!=NULL ) == true then return.get() == dynamic_cast<T2*>(p1.get()).
  • If ( p1.get()!=NULL && throw_on_fail==false && dynamic_cast<T2*>(p1.get())==NULL ) == true then return.get() == NULL.
  • If ( p1.get()==NULL ) == true then return.get() == NULL.

This function will compile only if (dynamic_cast<T2*>(p1.get());) compiles.

Definition at line 465 of file Teuchos_PtrDecl.hpp.


Member Data Documentation

template<class T>
T* Teuchos::Ptr< T >::ptr_ [private]

Definition at line 173 of file Teuchos_PtrDecl.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:14:05 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1