Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Public Member Functions | Private Attributes | List of all members
SmartPointer< T, P > Class Template Reference

#include <deal.II/base/smartpointer.h>

Public Member Functions

 SmartPointer ()
 
template<class Q >
 SmartPointer (const SmartPointer< T, Q > &tt)
 
 SmartPointer (const SmartPointer< T, P > &tt)
 
 SmartPointer (T *t, const std::string &id)
 
 SmartPointer (T *t)
 
 ~SmartPointer ()
 
SmartPointer< T, P > & operator= (T *tt)
 
template<class Q >
SmartPointer< T, P > & operator= (const SmartPointer< T, Q > &tt)
 
SmartPointer< T, P > & operator= (const SmartPointer< T, P > &tt)
 
void clear ()
 
 operator T* () const
 
T & operator* () const
 
T * operator-> () const
 
template<class Q >
void swap (SmartPointer< T, Q > &tt)
 
void swap (T *&tt)
 
std::size_t memory_consumption () const
 

Private Attributes

T * t
 
const std::string id
 
std::atomic< boolpointed_to_object_is_alive
 

Detailed Description

template<typename T, typename P = void>
class SmartPointer< T, P >

Smart pointers avoid using dangling pointers. They can be used just like a pointer (i.e. using the * and -> operators and through casting) but make sure that the object pointed to is not deleted or moved from in the course of use of the pointer by signaling the pointee its use.

Objects pointed to, i.e. the class T, should inherit Subscriptor or must implement the same functionality. Null pointers are an exception from this rule and are allowed, too.

The second template argument P only serves a single purpose: if a constructor without a debug string is used, then the name of P is used as the debug string.

SmartPointer does NOT implement any memory handling! Especially, deleting a SmartPointer does not delete the object. Writing

SmartPointer<T,P> dont_do_this = new T;

is a sure way to program a memory leak! The secure version is

T* p = new T;
{
...
}
delete p;

Note that a smart pointer can handle constness of an object, i.e. a SmartPointer<const ABC> really behaves as if it were a pointer to a constant object (disallowing write access when dereferenced), while SmartPointer<ABC> is a mutable pointer.

Author
Guido Kanschat, Wolfgang Bangerth, 1998 - 2009

Definition at line 68 of file smartpointer.h.

Constructor & Destructor Documentation

◆ SmartPointer() [1/5]

template<typename T , typename P >
SmartPointer< T, P >::SmartPointer
inline

Standard constructor for null pointer. The id of this pointer is set to the name of the class P.

Definition at line 217 of file smartpointer.h.

◆ SmartPointer() [2/5]

template<typename T , typename P >
template<class Q >
SmartPointer< T, P >::SmartPointer ( const SmartPointer< T, Q > &  tt)
inline

Copy constructor for SmartPointer. We do not copy the object subscribed to from tt, but subscribe ourselves to it again.

Definition at line 251 of file smartpointer.h.

◆ SmartPointer() [3/5]

template<typename T , typename P >
SmartPointer< T, P >::SmartPointer ( const SmartPointer< T, P > &  tt)
inline

Copy constructor for SmartPointer. We do not copy the object subscribed to from tt, but subscribe ourselves to it again.

Definition at line 263 of file smartpointer.h.

◆ SmartPointer() [4/5]

template<typename T , typename P >
SmartPointer< T, P >::SmartPointer ( T *  t,
const std::string &  id 
)
inline

Constructor taking a normal pointer. If possible, i.e. if the pointer is not a null pointer, the constructor subscribes to the given object to lock it, i.e. to prevent its destruction before the end of its use.

The id is used in the call to Subscriptor::subscribe(id) and by ~SmartPointer() in the call to Subscriptor::unsubscribe().

Definition at line 238 of file smartpointer.h.

◆ SmartPointer() [5/5]

template<typename T , typename P >
SmartPointer< T, P >::SmartPointer ( T *  t)
inline

Constructor taking a normal pointer. If possible, i.e. if the pointer is not a null pointer, the constructor subscribes to the given object to lock it, i.e. to prevent its destruction before the end of its use. The id of this pointer is set to the name of the class P.

Definition at line 226 of file smartpointer.h.

◆ ~SmartPointer()

template<typename T , typename P >
SmartPointer< T, P >::~SmartPointer
inline

Destructor, removing the subscription.

Definition at line 275 of file smartpointer.h.

Member Function Documentation

◆ operator=() [1/3]

template<typename T , typename P >
SmartPointer< T, P > & SmartPointer< T, P >::operator= ( T *  tt)
inline

Assignment operator for normal pointers. The pointer subscribes to the new object automatically and unsubscribes to an old one if it exists. It will not try to subscribe to a null-pointer, but still delete the old subscription.

Definition at line 300 of file smartpointer.h.

◆ operator=() [2/3]

template<typename T , typename P >
template<class Q >
SmartPointer< T, P > & SmartPointer< T, P >::operator= ( const SmartPointer< T, Q > &  tt)
inline

Assignment operator for SmartPointer. The pointer subscribes to the new object automatically and unsubscribes to an old one if it exists.

Definition at line 320 of file smartpointer.h.

◆ operator=() [3/3]

template<typename T , typename P >
SmartPointer< T, P > & SmartPointer< T, P >::operator= ( const SmartPointer< T, P > &  tt)
inline

Assignment operator for SmartPointer. The pointer subscribes to the new object automatically and unsubscribes to an old one if it exists.

Definition at line 340 of file smartpointer.h.

◆ clear()

template<typename T , typename P >
void SmartPointer< T, P >::clear
inline

Delete the object pointed to and set the pointer to zero.

Definition at line 285 of file smartpointer.h.

◆ operator T*()

template<typename T , typename P >
SmartPointer< T, P >::operator T*
inline

Conversion to normal pointer.

Definition at line 359 of file smartpointer.h.

◆ operator*()

template<typename T , typename P >
T & SmartPointer< T, P >::operator*
inline

Dereferencing operator. This operator throws an ExcNotInitialized() if the pointer is a null pointer.

Definition at line 367 of file smartpointer.h.

◆ operator->()

template<typename T , typename P >
T * SmartPointer< T, P >::operator->
inline

Dereferencing operator. This operator throws an ExcNotInitializedi() if the pointer is a null pointer.

Definition at line 378 of file smartpointer.h.

◆ swap() [1/2]

template<typename T , typename P >
template<class Q >
void SmartPointer< T, P >::swap ( SmartPointer< T, Q > &  tt)
inline

Exchange the pointers of this object and the argument. Since both the objects to which is pointed are subscribed to before and after, we do not have to change their subscription counters.

Note that this function (with two arguments) and the respective functions where one of the arguments is a pointer and the other one is a C-style pointer are implemented in global namespace.

Definition at line 391 of file smartpointer.h.

◆ swap() [2/2]

template<typename T , typename P >
void SmartPointer< T, P >::swap ( T *&  tt)
inline

Swap pointers between this object and the pointer given. As this releases the object pointed to presently, we reduce its subscription count by one, and increase it at the object which we will point to in the future.

Note that we indeed need a reference of a pointer, as we want to change the pointer variable which we are given.

Definition at line 406 of file smartpointer.h.

◆ memory_consumption()

template<typename T , typename P >
std::size_t SmartPointer< T, P >::memory_consumption
inline

Return an estimate of the amount of memory (in bytes) used by this class. Note in particular, that this only includes the amount of memory used by this object, not by the object pointed to.

Definition at line 421 of file smartpointer.h.

Member Data Documentation

◆ t

template<typename T , typename P = void>
T* SmartPointer< T, P >::t
private

Pointer to the object we want to subscribe to. Since it is often necessary to follow this pointer when debugging, we have deliberately chosen a short name.

Definition at line 198 of file smartpointer.h.

◆ id

template<typename T , typename P = void>
const std::string SmartPointer< T, P >::id
private

The identification for the subscriptor.

Definition at line 203 of file smartpointer.h.

◆ pointed_to_object_is_alive

template<typename T , typename P = void>
std::atomic<bool> SmartPointer< T, P >::pointed_to_object_is_alive
private

The Smartpointer is invalidated when the object pointed to is destroyed or moved from.

Definition at line 209 of file smartpointer.h.


The documentation for this class was generated from the following file:
LAPACKSupport::T
static const char T
Definition: lapack_support.h:163
SmartPointer
Definition: smartpointer.h:68
SmartPointer::t
T * t
Definition: smartpointer.h:198