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)=default | |
virtual | ~cObject () |
virtual const char * | getClassName () const |
bool | isName (const char *s) const |
virtual const char * | getFullName () const |
virtual std::string | getFullPath () const |
virtual std::string | getClassAndFullName () const |
virtual std::string | getClassAndFullPath () const |
const cObject * | getThisPtr () const |
virtual std::string | str () const |
virtual std::ostream & | printOn (std::ostream &os) const |
virtual cObject * | dup () const |
virtual bool | isSoftOwner () const |
virtual void | forEachChild (cVisitor *v) |
cObject * | findObject (const char *name, bool deep=true) |
virtual cClassDescriptor * | getDescriptor () const |
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 |
static cSoftOwner * | getOwningContext () |
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&)
).
Assignment 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 assignments.
|
overridevirtual |
Serializes the object into a buffer.
Reimplemented from cNamedObject.
Reimplemented in cKSplit, cMessage, cQueue, cArray, cMsgPar, cHistogram, cPacket, cPrecollectionBasedDensityEst, cEvent, cPacketQueue, cStdDev, cPSquare, and cStatistic.
|
overridevirtual |
Deserializes the object from a buffer.
Reimplemented from cNamedObject.
Reimplemented in cKSplit, cMessage, cQueue, cArray, cMsgPar, cHistogram, cPacket, cPrecollectionBasedDensityEst, cEvent, cPacketQueue, cStdDev, cPSquare, and cStatistic.
|
inlineoverridevirtual |
Returns pointer to the owner of the object.
Reimplemented from cObject.
|
inlineoverridevirtual |
Returns true.
Reimplemented from cObject.
|
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 component in context (see cSimulation::getContext()).
|
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.)