OMNeT++ Simulation Library
5.6.1
|
#include <cownedobject.h>
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ library.
Instances of cOwnedObjects are kept track of by the simulation kernel, and may be inserted into cQueue and cArray.
It is not always a good idea to subclass your own classes from cOwnedObject, especially if they are small data objects. The more lightweight cObject is often a better choice.
Ownership management helps OMNeT++ catch common programming errors. The term ownership means the exclusive right and duty to delete owned objects.
cOwnedObjects hold a pointer to their owner objects; the getOwner() method returns this pointer. An example will help to understand how it is used:
The above ownership mechanisms are at work when any cOwnedObject-subclass object gets inserted into any cOwnedObject-subclass container (cQueue, cArray).
Some more details, in case you are writing a class that acts as a container:
Public Member Functions | |
Constructors, destructor, assignment. | |
cOwnedObject () | |
cOwnedObject (const char *name, bool namepooling=true) | |
cOwnedObject (const cOwnedObject &obj) | |
virtual | ~cOwnedObject () |
cOwnedObject & | operator= (const cOwnedObject &o) |
virtual void | parsimPack (cCommBuffer *buffer) const override |
virtual void | parsimUnpack (cCommBuffer *buffer) override |
Public Member Functions inherited from cNamedObject | |
cNamedObject () | |
cNamedObject (const char *name, bool namepooling=true) | |
cNamedObject (const cNamedObject &obj) | |
virtual | ~cNamedObject () |
cNamedObject & | operator= (const cNamedObject &o) |
virtual void | setName (const char *s) |
virtual const char * | getName () const override |
virtual void | setNamePooling (bool b) |
virtual bool | getNamePooling () |
Public Member Functions inherited from cObject | |
cObject () | |
cObject (const cObject &other) | |
virtual | ~cObject () |
virtual const char * | getClassName () const |
bool | isName (const char *s) const |
virtual const char * | getFullName () const |
virtual std::string | getFullPath () const |
const cObject * | getThisPtr () const |
virtual std::string | str () const |
virtual _OPPDEPRECATED std::string | info () const |
virtual _OPPDEPRECATED std::string | detailedInfo () const |
virtual cObject * | dup () const |
virtual void | forEachChild (cVisitor *v) |
cObject * | findObject (const char *name, bool deep=true) |
void | copyNotSupported () const |
Static Public Member Functions | |
Statistics. | |
static long | getTotalObjectCount () |
static long | getLiveObjectCount () |
static void | resetObjectCounters () |
Object ownership | |
virtual cObject * | getOwner () const override |
virtual bool | isOwnedObject () const override |
virtual bool | isSoftOwner () const |
static cDefaultList * | getDefaultOwner () |
Additional Inherited Members | |
Protected Member Functions inherited from cObject | |
virtual void | take (cOwnedObject *obj) |
virtual void | drop (cOwnedObject *obj) |
void | dropAndDelete (cOwnedObject *obj) |
cOwnedObject | ( | ) |
Create object without a name. The object will be initially owned by defaultOwer().
|
explicit |
Create object with given name. The object will be initially owned by defaultOwer(). Name pooling is an optimization feature.
cOwnedObject | ( | const cOwnedObject & | obj | ) |
Copy constructor.
|
virtual |
Destructor.
cOwnedObject& operator= | ( | const cOwnedObject & | o | ) |
The assignment operator. Derived classes should contain similar methods (cClassName& cClassName::operator=(cClassName&)
).
Assigment copies the contents of the object EXCEPT for the name string. If you want to copy the name string, you can do it by hand: setName(o.getName()
).
Ownership of the object is not affected by assigments.
|
overridevirtual |
Serializes the object into a buffer.
Reimplemented from cNamedObject.
Reimplemented in cTopology, cNoncopyableOwnedObject, cLegacyHistogram, cFSM, cKSplit, cMessage, cQueue, cArray, cMsgPar, cHistogram, cPacket, cChannel, cPrecollectionBasedDensityEst, cEvent, cDefaultList, cVarHistogram, cOutVector, cPacketQueue, cStdDev, cPSquare, cLegacyHistogramBase, and cStatistic.
|
overridevirtual |
Deserializes the object from a buffer.
Reimplemented from cNamedObject.
Reimplemented in cTopology, cNoncopyableOwnedObject, cLegacyHistogram, cFSM, cKSplit, cMessage, cQueue, cArray, cMsgPar, cHistogram, cPacket, cChannel, cPrecollectionBasedDensityEst, cEvent, cDefaultList, cVarHistogram, cPacketQueue, cOutVector, cStdDev, cPSquare, cLegacyHistogramBase, and cStatistic.
|
inlineoverridevirtual |
Returns pointer to the owner of the object.
Reimplemented from cObject.
|
inlineoverridevirtual |
Returns true.
Reimplemented from cObject.
|
inlinevirtual |
Returns false in cOwnedObject and in all derived classes except cDefaultList. An object A is a "soft owner" if it allows a B object take() an object A owns. "Hard owners" will raise an error if some other object tries to take() an object they own. The only soft owner class is cDefaultList.
Reimplemented in cDefaultList.
|
static |
The object that will be the owner of new or dropped (see drop()) objects. The default owner is set internally, it is usually the simple module processing the current event.
|
inlinestatic |
Returns the total number of objects created since the start of the program (or since the last reset). The counter is incremented by cOwnedObject constructor. Counter is signed
to make it easier to detect if it overflows during very long simulation runs. May be useful for profiling or debugging memory leaks.
|
inlinestatic |
Returns the number of objects that currently exist in the program. The counter is incremented by cOwnedObject constructor and decremented by the destructor. May be useful for profiling or debugging memory leaks.
|
inlinestatic |
Reset counters used by getTotalObjectCount() and getLiveObjectCount(). (Note that getLiveObjectCount() may go negative after a reset call.)