OMNeT++ Simulation Library  5.6.1
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 <typeinfo>
20 #include <iostream>
21 #include "simkerneldefs.h"
22 #include "simutil.h"
23 #include "cnamedobject.h"
24 #include "cexception.h"
25 
26 namespace omnetpp {
27 
28 
29 class cOwnedObject;
30 class cStaticFlag;
31 class cSimulation;
32 class cDefaultList;
33 class cMessage;
34 class cPacket;
35 
36 
104 class SIM_API cOwnedObject : public cNamedObject
105 {
106  friend class cObject;
107  friend class cDefaultList;
108  friend class cSimulation;
109  friend class cMessage; // because of refcounting business
110  friend class cPacket; // because of refcounting business
111 
112  private:
113  cObject *owner; // owner pointer
114  unsigned int pos; // used only when owner is a cDefaultList
115 
116  private:
117  // list in which objects are accumulated if there is no simple module in context
118  // (see also setDefaultOwner() and cSimulation::setContextModule())
119  static cDefaultList *defaultOwner;
120 
121  // global variables for statistics
122  static long totalObjectCount;
123  static long liveObjectCount;
124 
125  private:
126  void copy(const cOwnedObject& obj);
127 
128  public:
129  // internal
130  virtual void removeFromOwnershipTree();
131 
132  // internal
133  static void setDefaultOwner(cDefaultList *list);
134 
135  public:
142  cOwnedObject();
143 
148  explicit cOwnedObject(const char *name, bool namepooling=true);
149 
153  cOwnedObject(const cOwnedObject& obj);
154 
158  virtual ~cOwnedObject();
159 
170  cOwnedObject& operator=(const cOwnedObject& o);
171 
172  // Note: dup() is still the original cObject one, which throws an error
173  // to indicate that dup() has to be redefined in each subclass.
174 
178  virtual void parsimPack(cCommBuffer *buffer) const override;
179 
183  virtual void parsimUnpack(cCommBuffer *buffer) override;
185 
191  virtual cObject *getOwner() const override {return owner;}
192 
196  virtual bool isOwnedObject() const override {return true;}
197 
204  virtual bool isSoftOwner() const {return false;}
205 
211  static cDefaultList *getDefaultOwner();
213 
223  static long getTotalObjectCount() {return totalObjectCount;}
224 
231  static long getLiveObjectCount() {return liveObjectCount;}
232 
237  static void resetObjectCounters() {totalObjectCount=liveObjectCount=0L;}
239 };
240 
241 
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 
265  virtual void parsimPack(cCommBuffer *buffer) const override;
266 
270  virtual void parsimUnpack(cCommBuffer *buffer) override;
271 };
272 
273 
274 //
275 // Internal class: provides a flag that shows if control is in the main() function.
276 //
277 class SIM_API cStaticFlag
278 {
279  private:
280  static bool staticFlag; // set to true while in main()
281  static bool exitingFlag; // set on getting a TERM or INT signal (Windows)
282  public:
283  cStaticFlag() {staticFlag = true;}
284  ~cStaticFlag() {staticFlag = false;}
285  static bool insideMain() {return staticFlag;}
286  static bool isExiting() {return exitingFlag;}
287  static void setExiting() {exitingFlag = true;}
288 };
289 
290 SIM_API std::ostream& operator<< (std::ostream& os, const cOwnedObject *p);
291 SIM_API std::ostream& operator<< (std::ostream& os, const cOwnedObject& o);
292 
293 inline std::ostream& operator<< (std::ostream& os, cOwnedObject *p) {
294  return os << (const cOwnedObject *)p;
295 }
296 
297 inline std::ostream& operator<< (std::ostream& os, cOwnedObject& o) {
298  return os << (const cOwnedObject&)o;
299 }
300 
301 } // namespace omnetpp
302 
303 
304 #endif
305 
static void resetObjectCounters()
Definition: cownedobject.h:237
Internal class, used as a base class for modules and channels. It is not intended for subclassing out...
Definition: cdefaultlist.h:33
static long getLiveObjectCount()
Definition: cownedobject.h:231
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition: cmessage.h:95
virtual bool isOwnedObject() const override
Definition: cownedobject.h:196
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
Simulation manager class.
Definition: csimulation.h:62
Extends cObject with a name string. Also includes a "flags" member, with bits open for use by subclas...
Definition: cnamedobject.h:36
A subclass of cMessage to represent packets, frames, datagrams, application messages, and similar data.
Definition: cpacket.h:52
static long getTotalObjectCount()
Definition: cownedobject.h:223
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:104
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:311
Definition: cabstracthistogram.h:21
Base class for cOwnedObject-based classes that do not wish to support assignment and duplication...
Definition: cownedobject.h:248
virtual bool isSoftOwner() const
Definition: cownedobject.h:204
virtual cObject * getOwner() const override
Definition: cownedobject.h:191
cNoncopyableOwnedObject(const char *name=nullptr, bool namepooling=true)
Definition: cownedobject.h:254