OMNeT++ Simulation Library  6.0.3
cobject.h
1 //==========================================================================
2 // COBJECT.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_COBJECT_H
17 #define __OMNETPP_COBJECT_H
18 
19 #include <string>
20 #include <iostream>
21 #include "simkerneldefs.h"
22 #include "cvisitor.h"
23 
24 // Note: we include regmacros.h here in order to prevent the compiler from
25 // misunderstanding Register_Class() in other source files as a function declaration
26 #include "regmacros.h"
27 
28 namespace omnetpp {
29 
30 class cCommBuffer;
31 class cClassDescriptor;
32 class cOwnedObject;
33 class cSoftOwner;
34 
35 
92 class SIM_API cObject
93 {
94  friend class cOwnedObject;
95  friend class cSoftOwner;
96 
97 #ifdef SIMFRONTEND_SUPPORT
98  // internal: used by the UI to optimize refreshes
99  virtual bool hasChangedSince(int64_t refreshSerial);
100  static int64_t getChangeCounter() {return changeCounter;}
101 #endif
102 
103  protected:
104 #ifdef SIMFRONTEND_SUPPORT
105  // internal
106  static int64_t changeCounter;
107 #endif
108 
109  // internal
110  virtual void ownedObjectDeleted(cOwnedObject *obj);
111 
112  // internal
113  virtual void yieldOwnership(cOwnedObject *obj, cObject *to);
114 
115  public:
116  // internal
117  void takeAllObjectsFrom(cSoftOwner *list);
118 
119  public:
124  cObject() {}
125 
132  cObject(const cObject& other) = default;
133 
139  virtual ~cObject();
140 
146  virtual const char *getClassName() const;
147 
154  virtual const char *getName() const {return "";}
155 
159  bool isName(const char *s) const;
160 
169  virtual const char *getFullName() const {return getName();}
170 
178  virtual std::string getFullPath() const;
179 
186  virtual std::string getClassAndFullName() const;
187 
194  virtual std::string getClassAndFullPath() const;
195 
199  const cObject *getThisPtr() const {return this;} //Note: nonvirtual
200 
207  virtual std::string str() const;
208 
217  virtual std::ostream& printOn(std::ostream& os) const;
218 
224  virtual cObject *dup() const;
226 
227  protected:
235 
243  virtual void take(cOwnedObject *obj);
244 
252  virtual void drop(cOwnedObject *obj);
253 
267  void dropAndDelete(cOwnedObject *obj);
269 
270  public:
282  virtual void parsimPack(cCommBuffer *buffer) const;
283 
288  virtual void parsimUnpack(cCommBuffer *buffer);
290 
300  virtual cObject *getOwner() const {return nullptr;}
301 
309  virtual bool isOwnedObject() const {return false;}
310 
319  virtual bool isSoftOwner() const {return false;}
320 
329  virtual void forEachChild(cVisitor *v);
330 
343  cObject *findObject(const char *name, bool deep=true);
344 
348  virtual cClassDescriptor *getDescriptor() const;
350 
359  void copyNotSupported() const;
361 };
362 
363 // operator<<
364 
365 template<typename T>
366 typename std::enable_if<std::is_base_of<cObject, T>::value, std::ostream&>::type
367 operator<<(std::ostream& os, const T *p) {
368  if (p)
369  return p->printOn(os);
370  else
371  return os << "<nullptr>";
372 }
373 
374 template<typename T>
375 typename std::enable_if<std::is_base_of<cObject, T>::value, std::ostream&>::type
376 operator<<(std::ostream& os, const T& o) {
377  return o.printOn(os);
378 }
379 
380 // as_cObject
381 
382 template <typename T>
383 typename std::enable_if<std::is_polymorphic<T>::value, cObject*>::type
384 as_cObject(T *p) { return dynamic_cast<cObject*>(p); }
385 
386 template <typename T>
387 typename std::enable_if<!std::is_polymorphic<T>::value, cObject*>::type
388 as_cObject(T *p) { return nullptr; }
389 
390 template <typename T>
391 typename std::enable_if<std::is_polymorphic<T>::value, const cObject*>::type
392 as_cObject(const T *p) { return dynamic_cast<const cObject*>(p); }
393 
394 template <typename T>
395 typename std::enable_if<!std::is_polymorphic<T>::value, const cObject*>::type
396 as_cObject(const T *p) { return nullptr; }
397 
416 {
417  protected:
418  noncopyable() {}
419  ~noncopyable() {}
420  private:
421  // private, and in addition deleted
422  noncopyable(const noncopyable& x) = delete;
423  const noncopyable& operator=(const noncopyable&) = delete;
424 };
425 
426 } // namespace omnetpp
427 
428 
429 #endif
430 
omnetpp::cClassDescriptor
Abstract base class for class descriptors.
Definition: cclassdescriptor.h:39
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::noncopyable
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:415
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cObject::cObject
cObject()
Definition: cobject.h:124
omnetpp::cObject::isOwnedObject
virtual bool isOwnedObject() const
Definition: cobject.h:309
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::cObject::getOwner
virtual cObject * getOwner() const
Definition: cobject.h:300
omnetpp::cObject::getFullName
virtual const char * getFullName() const
Definition: cobject.h:169
omnetpp::cObject::getName
virtual const char * getName() const
Definition: cobject.h:154
omnetpp::cObject::isSoftOwner
virtual bool isSoftOwner() const
Definition: cobject.h:319
omnetpp::cObject::getThisPtr
const cObject * getThisPtr() const
Definition: cobject.h:199
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