OMNeT++ API 6.1
Discrete Event Simulation Library
opp_component_ptr.h
1 //==========================================================================
2 // OPP_COMPONENT_PTR.H - header for
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_OPP_COMPONENT_PTR_H
17 #define __OMNETPP_OPP_COMPONENT_PTR_H
18 
19 #include "ccomponent.h"
20 #include "cexception.h"
21 
22 namespace omnetpp {
23 
55 template <typename T>
56 class SIM_API opp_component_ptr
57 {
58  private:
59  T *ptr = nullptr;
60 
61  void registerPtr() {
62  if (ptr) {
63  if (cComponent *component = dynamic_cast<cComponent*>(ptr))
64  component->registerSelfPointer(ptr);
65  else
66  throw cRuntimeError("opp_component_ptr<%s>: Pointer cannot be dynamic_cast to cComponent", opp_typename(typeid(T)));
67  }
68  }
69 
70  void deregisterPtr() {
71  if (ptr)
72  dynamic_cast<cComponent*>(ptr)->deregisterSelfPointer(ptr);
73  }
74 
75  void checkNull() const {
76  if (ptr == nullptr)
77  throw cRuntimeError("opp_component_ptr<%s>: Cannot dereference nullptr", opp_typename(typeid(T)));
78  }
79 
80  public:
85 
90  opp_component_ptr(T *ptr) : ptr(ptr) {registerPtr();}
91 
96  opp_component_ptr(const opp_component_ptr<T>& other) : ptr(other.ptr) {registerPtr();}
97 
101  ~opp_component_ptr() {deregisterPtr();}
102 
107  T& operator*() const {checkNull(); return *ptr;}
108 
113  T *operator->() const {checkNull(); return ptr;}
114 
118  operator T *() const {return ptr;}
119 
123  explicit operator bool() const {return ptr != nullptr;}
124 
129  bool operator==(const T *ptr) {return ptr == this->ptr;}
130 
135  bool operator==(const opp_component_ptr<T>& other) {return ptr == other.ptr;}
136 
141  void operator=(T *newPtr) {
142  if (ptr != newPtr) {
143  deregisterPtr();
144  ptr = newPtr;
145  registerPtr();
146  }
147  }
148 
153  void operator=(const opp_component_ptr<T>& ref) {*this = ref.ptr;}
154 
159  T *get() const {checkNull(); return ptr;}
160 
164  T *getNullable() const {return ptr;}
165 };
166 
167 } // namespace omnetpp
168 
169 #endif
170 
omnetpp::opp_component_ptr::get
T * get() const
Definition: opp_component_ptr.h:159
omnetpp::opp_component_ptr::opp_component_ptr
opp_component_ptr(const opp_component_ptr< T > &other)
Definition: opp_component_ptr.h:96
omnetpp::opp_component_ptr::operator=
void operator=(T *newPtr)
Definition: opp_component_ptr.h:141
omnetpp::opp_component_ptr::~opp_component_ptr
~opp_component_ptr()
Definition: opp_component_ptr.h:101
omnetpp::opp_component_ptr::operator==
bool operator==(const T *ptr)
Definition: opp_component_ptr.h:129
omnetpp::opp_component_ptr
Definition: opp_component_ptr.h:56
omnetpp::opp_component_ptr::opp_component_ptr
opp_component_ptr()
Definition: opp_component_ptr.h:84
omnetpp::opp_component_ptr::operator=
void operator=(const opp_component_ptr< T > &ref)
Definition: opp_component_ptr.h:153
omnetpp::opp_component_ptr::operator==
bool operator==(const opp_component_ptr< T > &other)
Definition: opp_component_ptr.h:135
omnetpp::opp_component_ptr::getNullable
T * getNullable() const
Definition: opp_component_ptr.h:164
omnetpp::opp_component_ptr::operator->
T * operator->() const
Definition: opp_component_ptr.h:113
omnetpp::cComponent
Common base for module and channel classes.
Definition: ccomponent.h:49
omnetpp::opp_component_ptr::operator*
T & operator*() const
Definition: opp_component_ptr.h:107
omnetpp::cRuntimeError
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:286
omnetpp::opp_component_ptr::opp_component_ptr
opp_component_ptr(T *ptr)
Definition: opp_component_ptr.h:90
omnetpp::opp_typename
const SIM_API char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.