cobject.h Source File

API: cobject.h Source File
API
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
28namespace omnetpp {
29
30class cCommBuffer;
32class cOwnedObject;
33class cSoftOwner;
34
35
92class 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:
125
132 cObject(const cObject& other) = default;
133
139 virtual ~cObject();
140
146 virtual const char *getClassName() const;
147
150
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:
278
282 virtual void parsimPack(cCommBuffer *buffer) const;
283
288 virtual void parsimUnpack(cCommBuffer *buffer);
290
293
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
350
353
359 void copyNotSupported() const;
361};
362
363// operator<<
364
365template<typename T>
366typename std::enable_if<std::is_base_of<cObject, T>::value, std::ostream&>::type
367operator<<(std::ostream& os, const T *p) {
368 if (p)
369 return p->printOn(os);
370 else
371 return os << "<nullptr>";
372}
373
374template<typename T>
375typename std::enable_if<std::is_base_of<cObject, T>::value, std::ostream&>::type
376operator<<(std::ostream& os, const T& o) {
377 return o.printOn(os);
378}
379
380// as_cObject
381
382template <typename T>
383typename std::enable_if<std::is_polymorphic<T>::value, cObject*>::type
384as_cObject(T *p) { return dynamic_cast<cObject*>(p); }
385
386template <typename T>
387typename std::enable_if<!std::is_polymorphic<T>::value, cObject*>::type
388as_cObject(T *p) { return nullptr; }
389
390template <typename T>
391typename std::enable_if<std::is_polymorphic<T>::value, const cObject*>::type
392as_cObject(const T *p) { return dynamic_cast<const cObject*>(p); }
393
394template <typename T>
395typename std::enable_if<!std::is_polymorphic<T>::value, const cObject*>::type
396as_cObject(const T *p) { return nullptr; }
397
415class noncopyable
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
Abstract base class for class descriptors.
Definition cclassdescriptor.h:40
Buffer for the communications layer of parallel simulation.
Definition ccommbuffer.h:42
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition cobject.h:93
virtual cObject * getOwner() const
Definition cobject.h:300
virtual const char * getFullName() const
Definition cobject.h:169
virtual void take(cOwnedObject *obj)
void copyNotSupported() const
cObject()
Definition cobject.h:124
virtual void drop(cOwnedObject *obj)
bool isName(const char *s) const
cObject * findObject(const char *name, bool deep=true)
cObject(const cObject &other)=default
virtual const char * getClassName() const
virtual const char * getName() const
Definition cobject.h:154
virtual void forEachChild(cVisitor *v)
virtual std::string getClassAndFullPath() const
virtual void parsimPack(cCommBuffer *buffer) const
virtual std::string str() const
void dropAndDelete(cOwnedObject *obj)
virtual void parsimUnpack(cCommBuffer *buffer)
virtual std::string getClassAndFullName() const
virtual cObject * dup() const
virtual ~cObject()
virtual bool isSoftOwner() const
Definition cobject.h:319
virtual bool isOwnedObject() const
Definition cobject.h:309
virtual std::ostream & printOn(std::ostream &os) const
virtual std::string getFullPath() const
const cObject * getThisPtr() const
Definition cobject.h:199
virtual cClassDescriptor * getDescriptor() const
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition cownedobject.h:106
Internal class, used as a base class for modules and channels. It is not intended for subclassing out...
Definition csoftowner.h:34
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition cvisitor.h:57