OMNeT++ Simulation Library  6.0.3
cownedobject.h
1 //==========================================================================
2 // COWNEDOBJECT.H - part of
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_COWNEDOBJECT_H
17 #define __OMNETPP_COWNEDOBJECT_H
18 
19 #include <utility>
20 #include <typeinfo>
21 #include <iostream>
22 #include "simkerneldefs.h"
23 #include "cnamedobject.h"
24 #include "cexception.h"
25 
26 namespace omnetpp {
27 
28 class cOwnedObject;
29 class cNoncopyableOwnedObject;
30 class cStaticFlag;
31 class cSimulation;
32 class cSoftOwner;
33 class cMessage;
34 class cPacket;
35 
36 namespace internal { class Void; }
37 
105 class SIM_API cOwnedObject : public cNamedObject
106 {
107  friend class cObject;
108  friend class cNoncopyableOwnedObject;
109  friend class cSoftOwner;
110  friend class cSimulation;
111  friend class cMessage; // because of refcounting business
112  friend class cPacket; // because of refcounting business
113 
114  private:
115  cObject *owner; // owner pointer
116  unsigned int pos; // used only when owner is a cSoftOwner
117 
118  // list in which objects are accumulated if there is no simple module in context
119  // (see also setOwningContext() and cSimulation::setContextModule())
120  static cSoftOwner *owningContext;
121 
122  // global variables for statistics
123  static long totalObjectCount;
124  static long liveObjectCount;
125 
126  private:
127  cOwnedObject(const char *name, bool namepooling, internal::Void *dummy);
128  void copy(const cOwnedObject& obj);
129 
130  public:
131  // internal
132  virtual void removeFromOwnershipTree();
133 
134  // internal
135  static void setOwningContext(cSoftOwner *list);
136 
137  public:
144  cOwnedObject();
145 
150  explicit cOwnedObject(const char *name, bool namepooling=true);
151 
155  cOwnedObject(const cOwnedObject& obj);
156 
160  virtual ~cOwnedObject();
161 
172  cOwnedObject& operator=(const cOwnedObject& o);
173 
174  // Note: dup() is still the original cObject one, which throws an error
175  // to indicate that dup() has to be redefined in each subclass.
176 
180  virtual void parsimPack(cCommBuffer *buffer) const override;
181 
185  virtual void parsimUnpack(cCommBuffer *buffer) override;
187 
193  virtual cObject *getOwner() const override {return owner;}
194 
198  virtual bool isOwnedObject() const override {return true;}
199 
205  static cSoftOwner *getOwningContext();
207 
217  static long getTotalObjectCount() {return totalObjectCount;}
218 
225  static long getLiveObjectCount() {return liveObjectCount;}
226 
231  static void resetObjectCounters() {totalObjectCount=liveObjectCount=0L;}
233 };
234 
235 
243 {
244  friend class cSoftOwner;
245  private:
246  cNoncopyableOwnedObject(const char *name, bool namepooling, internal::Void *dummy) : cOwnedObject(name, namepooling, dummy) {}
247  virtual void parsimPack(cCommBuffer *) const override {throw cRuntimeError(this, E_CANTPACK);}
248  virtual void parsimUnpack(cCommBuffer *) override {throw cRuntimeError(this, E_CANTPACK);}
249 
250  public:
254  explicit cNoncopyableOwnedObject(const char *name=nullptr, bool namepooling=true) :
255  cOwnedObject(name, namepooling) {}
256 
260  virtual cNoncopyableOwnedObject *dup() const override;
261 };
262 
263 
264 //
265 // Internal class: provides a flag that shows if control is in the main() function.
266 //
267 class SIM_API cStaticFlag
268 {
269  public:
270  cStaticFlag();
271  ~cStaticFlag();
272  static bool insideMain();
273  static bool isExiting();
274  static void setExiting();
275 };
276 
277 } // namespace omnetpp
278 
279 
280 #endif
281 
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cOwnedObject::getLiveObjectCount
static long getLiveObjectCount()
Definition: cownedobject.h:225
omnetpp::noncopyable
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:415
omnetpp::cSimulation
Simulation manager class.
Definition: csimulation.h:63
omnetpp::cOwnedObject::getTotalObjectCount
static long getTotalObjectCount()
Definition: cownedobject.h:217
omnetpp::cOwnedObject::resetObjectCounters
static void resetObjectCounters()
Definition: cownedobject.h:231
omnetpp::cSoftOwner
Internal class, used as a base class for modules and channels. It is not intended for subclassing out...
Definition: csoftowner.h:33
omnetpp::cNoncopyableOwnedObject::cNoncopyableOwnedObject
cNoncopyableOwnedObject(const char *name=nullptr, bool namepooling=true)
Definition: cownedobject.h:254
omnetpp::cOwnedObject::getOwner
virtual cObject * getOwner() const override
Definition: cownedobject.h:193
omnetpp::cOwnedObject::isOwnedObject
virtual bool isOwnedObject() const override
Definition: cownedobject.h:198
omnetpp::cMessage
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition: cmessage.h:95
omnetpp::cNamedObject
Extends cObject with a name string. Also includes a "flags" member, with bits open for use by subclas...
Definition: cnamedobject.h:34
omnetpp::cNoncopyableOwnedObject
Base class for cOwnedObject-based classes that do not wish to support assignment and duplication.
Definition: cownedobject.h:242
omnetpp::cRuntimeError
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:286
omnetpp::cPacket
A subclass of cMessage to represent packets, frames, datagrams, application messages,...
Definition: cpacket.h:52
omnetpp::cCommBuffer
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
omnetpp::cOwnedObject
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:105