OMNeT++ Simulation Library  6.0.3
opp_component_ptr< T > Class Template Reference

#include <opp_component_ptr.h>

Description

template<typename T>
class omnetpp::opp_component_ptr< T >

A smart pointer that points to a component (module or channel) object, and automatically becomes nullptr when the referenced object is deleted.

This is a non-owning ("weak") pointer, i.e. the pointer going out of scope has no effect on the referenced object.

It is implemented as a template class to provide type safety to users by letting them store typed pointers in it instead of "raw" cComponent pointers.

The class also contains protection against accidental dereference of null pointers. Only the pointer cast operator (operator T *) is willing to return nullptr, other members refuse to do so and throw an exception instead.

Note 1: T is often a subclass of cComponent, but that is not necessary. In particular, T may be an unrelated (but polymorphic) type, as long as actual objects passed into the smart pointer can be dynamically cast to cComponent. For example, given an unrelated abstract class IQueue, a opp_component_ptr<IQueue> smart pointer may be used with objects that inherit from both IQueue and cComponent.

Note 2: This class always stores pointers to mutable (non-const) objects. Making a const opp_component_ptr only makes it impossible to re-set the pointer to refer to another object, but it is still possible to extract mutable pointers from it. This interface is consistent with C++ smart pointers like std::unique_ptr and std::shared_ptr.

Public Member Functions

 opp_component_ptr ()
 
 opp_component_ptr (T *ptr)
 
 opp_component_ptr (const opp_component_ptr< T > &other)
 
 ~opp_component_ptr ()
 
T & operator* () const
 
T * operator-> () const
 
 operator T* () const
 
 operator bool () const
 
bool operator== (const T *ptr)
 
bool operator== (const opp_component_ptr< T > &other)
 
void operator= (T *newPtr)
 
void operator= (const opp_component_ptr< T > &ref)
 
T * get () const
 
T * getNullable () const
 

Constructor & Destructor Documentation

◆ opp_component_ptr() [1/3]

opp_component_ptr ( )
inline

Initializes the pointer to nullptr.

◆ opp_component_ptr() [2/3]

opp_component_ptr ( T *  ptr)
inline

Initializes the pointer to the given value. The pointer must be able to be dynamic_cast to a cComponent pointer.

◆ opp_component_ptr() [3/3]

opp_component_ptr ( const opp_component_ptr< T > &  other)
inline

Initializes a pointer to reference the same object (or nullptr) as the argument.

◆ ~opp_component_ptr()

~opp_component_ptr ( )
inline

Destructor. It has no effect on the referenced object.

Member Function Documentation

◆ operator*()

T& operator* ( ) const
inline

Returns a reference to the referenced object. Throws an error if the stored pointer is nullptr.

◆ operator->()

T* operator-> ( ) const
inline

Allows member access on the referenced object. Throws an error if the stored pointer is nullptr.

◆ operator T*()

operator T* ( ) const
inline

Returns a pointer to the referenced object, or nullptr.

◆ operator bool()

operator bool ( ) const
inlineexplicit

Returns true if the stored pointer is not a nullptr, and false if it is.

◆ operator==() [1/2]

bool operator== ( const T *  ptr)
inline

Returns true if this object references the same object (or nullptr) as the argument.

◆ operator==() [2/2]

bool operator== ( const opp_component_ptr< T > &  other)
inline

Returns true if this object references the same object (or nullptr) as the argument.

◆ operator=() [1/2]

void operator= ( T *  newPtr)
inline

Changes this object to reference the object given as the argument. A nullptr argument is also allowed.

◆ operator=() [2/2]

void operator= ( const opp_component_ptr< T > &  ref)
inline

Changes this object to reference the same object as the one given in the argument.

◆ get()

T* get ( ) const
inline

Returns a pointer to the referenced object. Throws an error if the stored pointer is nullptr.

◆ getNullable()

T* getNullable ( ) const
inline

Returns a pointer to the referenced object, or nullptr.


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