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 |
|
inline |
Initializes the pointer to nullptr.
|
inline |
Initializes the pointer to the given value. The pointer must be able to be dynamic_cast to a cComponent pointer.
|
inline |
Initializes a pointer to reference the same object (or nullptr) as the argument.
|
inline |
Destructor. It has no effect on the referenced object.
|
inline |
Returns a reference to the referenced object. Throws an error if the stored pointer is nullptr.
|
inline |
Allows member access on the referenced object. Throws an error if the stored pointer is nullptr.
|
inline |
Returns a pointer to the referenced object, or nullptr.
|
inlineexplicit |
Returns true if the stored pointer is not a nullptr, and false if it is.
|
inline |
Returns true if this object references the same object (or nullptr) as the argument.
|
inline |
Returns true if this object references the same object (or nullptr) as the argument.
|
inline |
Changes this object to reference the object given as the argument. A nullptr argument is also allowed.
|
inline |
Changes this object to reference the same object as the one given in the argument.
|
inline |
Returns a pointer to the referenced object. Throws an error if the stored pointer is nullptr.
|
inline |
Returns a pointer to the referenced object, or nullptr.