A Discrete-Event Network Simulator
API
ns3::Ptr< T > Class Template Reference

Smart pointer class similar to boost::intrusive_ptr. More...

#include "ptr.h"

+ Collaboration diagram for ns3::Ptr< T >:

Classes

class  Tester
 Helper to test for null pointer. More...
 

Public Member Functions

 Ptr ()
 Create an empty smart pointer. More...
 
 Ptr (T *ptr)
 Create a smart pointer which points to the object pointed to by the input raw pointer ptr. More...
 
 Ptr (T *ptr, bool ref)
 Create a smart pointer which points to the object pointed to by the input raw pointer ptr. More...
 
 Ptr (Ptr const &o)
 Copy by referencing the same underlying object. More...
 
template<typename U >
 Ptr (Ptr< U > const &o)
 Copy, removing const qualifier. More...
 
 ~Ptr ()
 Destructor. More...
 
 operator Tester * () const
 Test for non-NULL pointer. More...
 
bool operator! ()
 Test for NULL pointer. More...
 
const T & operator* () const
 A const dereference. More...
 
T & operator* ()
 A dereference. More...
 
T * operator-> () const
 An rvalue member access. More...
 
T * operator-> ()
 An lvalue member access. More...
 
Ptr< T > & operator= (Ptr const &o)
 Assignment operator by referencing the same underlying object. More...
 

Private Member Functions

void Acquire (void) const
 Mark this as a a reference by incrementing the reference count. More...
 

Private Attributes

T * m_ptr
 The pointer. More...
 

Friends

template<typename U >
U * GetPointer (const Ptr< U > &p)
 Get a permanent pointer to the underlying object. More...
 
template<typename U >
U * PeekPointer (const Ptr< U > &p)
 Get a temporary pointer to the underlying object. More...
 
class Ptr< const T >
 Interoperate with const instances. More...
 

Detailed Description

template<typename T>
class ns3::Ptr< T >

Smart pointer class similar to boost::intrusive_ptr.

This smart-pointer class assumes that the underlying type provides a pair of Ref and Unref methods which are expected to increment and decrement the internal reference count of the object instance. You can add Ref and Unref to a class simply by inheriting from ns3::SimpleRefCount.

This implementation allows you to manipulate the smart pointer as if it was a normal pointer: you can compare it with zero, compare it against other pointers, assign zero to it, etc.

It is possible to extract the raw pointer from this smart pointer with the GetPointer() and PeekPointer() methods.

If you want to store a new object into a smart pointer, we recommend you to use the Create() template functions to create the object and store it in a smart pointer to avoid memory leaks. These functions are really small convenience functions and their goal is just is save you a small bit of typing.

Template Parameters
T[explicit] The underlying type.

Definition at line 73 of file ptr.h.

Constructor & Destructor Documentation

template<typename T >
ns3::Ptr< T >::Ptr ( )

Create an empty smart pointer.

Definition at line 715 of file ptr.h.

template<typename T>
ns3::Ptr< T >::Ptr ( T *  ptr)

Create a smart pointer which points to the object pointed to by the input raw pointer ptr.

This method creates its own reference to the pointed object. The caller is responsible for Unref()'ing its own reference, and the smart pointer will eventually do the same, so that object is deleted if no more references to it remain.

Parameters
[in]ptrRaw pointer to manage

Definition at line 721 of file ptr.h.

References ns3::Ptr< T >::Acquire().

+ Here is the call graph for this function:

template<typename T>
ns3::Ptr< T >::Ptr ( T *  ptr,
bool  ref 
)

Create a smart pointer which points to the object pointed to by the input raw pointer ptr.

Parameters
[in]ptrRaw pointer to manage
[in]refif set to true, this method calls Ref, otherwise, it does not call Ref.

Definition at line 728 of file ptr.h.

References ns3::Ptr< T >::Acquire().

+ Here is the call graph for this function:

template<typename T>
ns3::Ptr< T >::Ptr ( Ptr< T > const &  o)

Copy by referencing the same underlying object.

Parameters
[in]oThe other Ptr instance.

Definition at line 738 of file ptr.h.

References ns3::Ptr< T >::Acquire().

+ Here is the call graph for this function:

template<typename T >
template<typename U >
ns3::Ptr< T >::Ptr ( Ptr< U > const &  o)

Copy, removing const qualifier.

Template Parameters
U[deduced] The underlying type of the const object.
Parameters
[in]oThe Ptr to copy.

Definition at line 745 of file ptr.h.

References ns3::Ptr< T >::Acquire().

+ Here is the call graph for this function:

template<typename T >
ns3::Ptr< T >::~Ptr ( )

Destructor.

Definition at line 752 of file ptr.h.

Member Function Documentation

template<typename T >
void ns3::Ptr< T >::Acquire ( void  ) const
inlineprivate

Mark this as a a reference by incrementing the reference count.

Definition at line 706 of file ptr.h.

Referenced by ns3::GetPointer(), and ns3::Ptr< T >::Ptr().

+ Here is the caller graph for this function:

template<typename T >
ns3::Ptr< T >::operator Tester * ( ) const

Test for non-NULL pointer.

This enables simple pointer checks like

Ptr<...> p = ...;
if (p) ...

This also disables deleting a Ptr

Definition at line 813 of file ptr.h.

References test().

+ Here is the call graph for this function:

template<typename T >
bool ns3::Ptr< T >::operator! ( )

Test for NULL pointer.

This enables simple NULL pointer checks like

Ptr<..> p = ...;
if (!p) ...
Returns
true if the underlying pointer is NULL.

Definition at line 807 of file ptr.h.

template<typename T >
const T & ns3::Ptr< T >::operator* ( ) const

A const dereference.

Returns
A pointer to the underlying object.

Definition at line 793 of file ptr.h.

template<typename T >
T & ns3::Ptr< T >::operator* ( )

A dereference.

Returns
A pointer to the underlying object.

Definition at line 800 of file ptr.h.

template<typename T >
T * ns3::Ptr< T >::operator-> ( ) const

An rvalue member access.

Returns
A pointer to the underlying object.

Definition at line 786 of file ptr.h.

template<typename T >
T * ns3::Ptr< T >::operator-> ( )

An lvalue member access.

Returns
A pointer to the underlying object.

Definition at line 779 of file ptr.h.

template<typename T >
Ptr< T > & ns3::Ptr< T >::operator= ( Ptr< T > const &  o)

Assignment operator by referencing the same underlying object.

Parameters
[in]oThe other Ptr instance.
Returns
A reference to self.

Definition at line 762 of file ptr.h.

References ns3::Ptr< T >::m_ptr.

Friends And Related Function Documentation

template<typename T>
template<typename U >
U* GetPointer ( const Ptr< U > &  p)
friend

Get a permanent pointer to the underlying object.

The underlying refcount is incremented prior to returning to the caller so the caller is responsible for calling Unref himself.

Parameters
[in]pSmart pointer
Returns
The pointer managed by this smart pointer.

Definition at line 568 of file ptr.h.

template<typename T>
template<typename U >
U* PeekPointer ( const Ptr< U > &  p)
friend

Get a temporary pointer to the underlying object.

The underlying refcount is not incremented prior to returning to the caller so the caller is not responsible for calling Unref himself.

Parameters
[in]pSmart pointer
Returns
The pointer managed by this smart pointer.

Definition at line 562 of file ptr.h.

template<typename T>
friend class Ptr< const T >
friend

Interoperate with const instances.

Definition at line 88 of file ptr.h.

Member Data Documentation

template<typename T>
T* ns3::Ptr< T >::m_ptr
private

The pointer.

Definition at line 78 of file ptr.h.

Referenced by ns3::GetPointer(), ns3::Ptr< T >::operator=(), and ns3::PeekPointer().


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