opp_component_ptr< T > Class Template Reference

API: opp_component_ptr< T > Class Template Reference
API
opp_component_ptr< T > Class Template Reference

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
void operator= (T *newPtr)
void operator= (const opp_component_ptr< T > &ref)
T * get () const
T * getNullable () const
void resolveFromParOptional (cModule *context, const char *parName)
void resolveFromPar (cModule *context, const char *parName)
cGateresolveFromGateOptional (cGate *gate, int direction=0)
cGateresolveFromGate (cGate *gate, int direction=0)

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.

Constructor & Destructor Documentation

◆ opp_component_ptr() [1/3]

template<typename T>
opp_component_ptr ( )
inline

Initializes the pointer to nullptr.

Referenced by operator=(), and opp_component_ptr().

◆ opp_component_ptr() [2/3]

template<typename T>
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]

template<typename T>
opp_component_ptr ( const opp_component_ptr< T > & other)
inline

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

References opp_component_ptr().

◆ ~opp_component_ptr()

template<typename T>
~opp_component_ptr ( )
inline

Destructor. It has no effect on the referenced object.

Member Function Documentation

◆ operator*()

template<typename T>
T & operator* ( ) const
inline

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

◆ operator->()

template<typename T>
T * operator-> ( ) const
inline

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

◆ operator T*()

template<typename T>
operator T* ( ) const
inline

Returns a pointer to the referenced object, or nullptr.

◆ operator bool()

template<typename T>
operator bool ( ) const
inlineexplicit

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

◆ operator=() [1/2]

template<typename T>
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]

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

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

References opp_component_ptr().

◆ get()

template<typename T>
T * get ( ) const
inline

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

◆ getNullable()

template<typename T>
T * getNullable ( ) const
inline

Returns a pointer to the referenced object, or nullptr.

◆ resolveFromParOptional()

template<typename T>
void resolveFromParOptional ( cModule * context,
const char * parName )
inline

Sets the pointer from a string-valued module parameter that contains a module path (absolute or relative to the given context module). If the parameter is empty, the pointer is set to nullptr. Throws an error if the path doesn't resolve to a module, or the resolved module cannot be cast to T.

◆ resolveFromPar()

template<typename T>
void resolveFromPar ( cModule * context,
const char * parName )
inline

Sets the pointer from a string-valued module parameter that contains a module path (absolute or relative to the given context module). Throws an error if the parameter is empty, the path doesn't resolve to a module, or the module cannot be cast to T.

◆ resolveFromGateOptional()

template<typename T>
cGate * resolveFromGateOptional ( cGate * gate,
int direction = 0 )
inline

Sets the pointer by following the connection path from the given gate, looking for the first module of type T. The direction parameter controls the traversal: positive means forward (getNextGate()), negative means backward (getPreviousGate()), and zero (default) means auto-detect based on gate type (forward for output gates, backward for input gates).

If no matching module is found, the pointer is set to nullptr.

Returns the gate of the found module (i.e. the gate at the other end of the connection to the found module), or nullptr if not found.

◆ resolveFromGate()

template<typename T>
cGate * resolveFromGate ( cGate * gate,
int direction = 0 )
inline

Sets the pointer by following the connection path from the given gate, looking for the first module of type T. The direction parameter controls the traversal: positive means forward (getNextGate()), negative means backward (getPreviousGate()), and zero (default) means auto-detect based on gate type (forward for output gates, backward for input gates).

Throws an error if no matching module is found.

Returns the gate of the found module (i.e. the gate at the other end of the connection to the found module).