opp_component_ptr< T > Class Template Reference¶
|
API
|
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) |
| cGate * | resolveFromGateOptional (cGate *gate, int direction=0) |
| cGate * | resolveFromGate (cGate *gate, int direction=0) |
Description
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]
|
inline |
Initializes the pointer to nullptr.
Referenced by operator=(), and opp_component_ptr().
◆ opp_component_ptr() [2/3]
|
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]
|
inline |
Initializes a pointer to reference the same object (or nullptr) as the argument.
References opp_component_ptr().
◆ ~opp_component_ptr()
|
inline |
Destructor. It has no effect on the referenced object.
Member Function Documentation
◆ operator*()
|
inline |
Returns a reference to the referenced object. Throws an error if the stored pointer is nullptr.
◆ operator->()
|
inline |
Allows member access on the referenced object. Throws an error if the stored pointer is nullptr.
◆ operator T*()
|
inline |
Returns a pointer to the referenced object, or nullptr.
◆ operator bool()
|
inlineexplicit |
Returns true if the stored pointer is not a nullptr, and false if it is.
◆ operator=() [1/2]
|
inline |
Changes this object to reference the object given as the argument. A nullptr argument is also allowed.
◆ operator=() [2/2]
|
inline |
Changes this object to reference the same object as the one given in the argument.
References opp_component_ptr().
◆ get()
|
inline |
Returns a pointer to the referenced object. Throws an error if the stored pointer is nullptr.
◆ getNullable()
|
inline |
Returns a pointer to the referenced object, or nullptr.
◆ resolveFromParOptional()
|
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()
|
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()
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()
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).
Generated on for API by