#include <cobject.h>
Inheritance diagram for cObject:
It is usually NOT a good idea to subclass your own classes (esp. data storage classes) from cObject, because it is a relatively heavyweight class (about 24 bytes on a 32-bit architecture) with many virtual functions that need to be redefined. You may consider choosing cPolymorphic instead as a base class.
The main areas covered by cObject are:
When subclassing cObject, some virtual member functions are expected to be redefined: dup() are mandatory to be redefined, and often you'll want to redefine info() and writeContents() as well.
Ownership management helps OMNeT++ catch common programming errors. As a definition, ownership means the exclusive right and duty to delete owned objects.
cObjects hold a pointer to their owner objects; the owner() method returns this pointer. An example will help to understand how it is used:
The above ownership mechanisms are at work when any cObject-subclass object gets inserted into any cObject-subclass container (cQueue, cArray). Not only obviously container classes act as containers: e.g. cChannel also owns the channel parameters (cPar objects).
Some more details, in case you're writing a class that acts as a container:
Object ownership | |
cObject * | owner () const |
virtual bool | isSoftOwner () |
static cDefaultList * | defaultOwner () |
Miscellaneous functions. | |
virtual void | forEachChild (cVisitor *v) |
cObject * | findObject (const char *name, bool deep=true) |
static int | cmpbyname (cObject *one, cObject *other) |
Public Member Functions | |
Constructors, destructor, assignment. | |
cObject (const cObject &obj) | |
cObject () | |
cObject (const char *name) | |
virtual | ~cObject () |
virtual cPolymorphic * | dup () const |
cObject & | operator= (const cObject &o) |
Handling the name string. | |
NOTE: "" and NULL are treated liberally: "" is stored as NULL and NULL is returned as "". | |
virtual void | setName (const char *s) |
const char * | name () const |
bool | isName (const char *s) const |
virtual const char * | fullName () const |
virtual std::string | fullPath () const |
virtual const char * | fullPath (char *buffer, int buffersize) const |
Reflection, support for debugging and snapshots. | |
virtual void | writeContents (std::ostream &os) |
Support for parallel execution. | |
Packs/unpacks object from/to a buffer. These functions are used by parallel execution. These functions should be redefined in all derived classes whose instances are expected to travel across partitions. | |
virtual void | netPack (cCommBuffer *buffer) |
virtual void | netUnpack (cCommBuffer *buffer) |
Static Public Member Functions | |
Statistics. | |
static long | totalObjectCount () |
static long | liveObjectCount () |
static void | resetMessageCounters () |
Protected Member Functions | |
Ownership control. | |
The following functions are intended to be used by derived container classes to manage ownership of their contained objects. See object description for more info on ownership management. | |
virtual void | take (cObject *obj) |
virtual void | drop (cObject *obj) |
void | dropAndDelete (cObject *obj) |
Helper functions. | |
void | copyNotSupported () const |
Friends | |
class | cDefaultList |
class | cSimulation |
class | cMessage |
|
Copy constructor.
In derived classes, it is usually implemented as |
|
Create object without a name. The initial owner of the object will be defaultOwer(); during the simulation, this the 'locals' member of the currently active simple module; otherwise it is the 'defaultList' global variable. |
|
Create object with given name. See cObject() constructor about initial ownership of the object. |
|
Destructor. It does the following:
Subclasses should delete owned objects here. |
|
This function compares to objects by name. It can be used in a priority queue (class cQueue) as a sorting criterion. |
|
Convenience function: throws a cRuntimeError ("copying not supported") stating that assignment, copy constructor and dup() won't work for this object. You can call this from operator=() if you don't want to implement object copying. |
|
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. |
|
Releases ownership of `object'. Actually it gives ownership of `object' back to its default owner. The function called by the container object when obj is removed from the container -- releases the ownership of the object and hands it over to its default owner. The obj pointer should not be NULL. Reimplemented in cDefaultList. |
|
This is a shortcut for the sequence.
drop(obj); delete obj; It is especially useful when writing destructors and assignment operators. Passing NULL is allowed.
|
|
Duplicates this object.
Duplicates the object and returns a pointer to the new one. Must be redefined in derived classes! In derived classes, it is usually implemented as Reimplemented from cPolymorphic. Reimplemented in cBag, cArray, cChannel, cBasicChannel, cDefaultList, cTDExpandingWindows, cADByStddev, cEnum, cFSM, cGate, cLongHistogram, cDoubleHistogram, cKSplit, cLinkedList, cMessage, cCompoundModule, cMessageHeap, cOutVector, cPacket, cPar, cModulePar, cPSquare, cQueue, cSimpleModule, cSimulation, cStdDev, cWeightedStdDev, cTopology, cModuleInterface, cModuleType, cLinkType, cFunctionType, cClassRegister, cVarHistogram, and cWatchBase. |
|
Finds the object with the given name. This function is useful when called on cObject subclasses that are containers. This method finds the object with the given name in a container object and returns a pointer to it or NULL if the object hasn't been found. If deep is false, only objects directly contained will be searched, otherwise the function searches the whole subtree for the object. It uses the forEachChild() mechanism. Do not use it for finding submodules! See cModule::moduleByRelativePath(). |
|
Enables traversing the object tree, performing some operation on each object. The operation is encapsulated in the particular subclass of cVisitor. This method should be redefined in every subclass to call v->visit(obj) for every obj object contained. Reimplemented in cArray, cChannel, cBasicChannel, cDefaultList, cGate, cMessage, cModule, cMessageHeap, cPar, cQueue, cSimpleModule, and cSimulation. |
|
Returns a name that includes the object 'index' (e.g. in a module vector), like "modem[5]". To be redefined in descendants. E.g., see cModule::fullName(). Reimplemented from cPolymorphic. |
|
Returns the full path of the object in the object hierarchy, like "comp.modem[5].baud-rate". The result is placed into the buffer passed. Reimplemented in cGate, cModule, cModulePar, and cSimulation. |
|
Returns the full path of the object in the object hierarchy, like "comp.modem[5].baud-rate". NOTE: the returned pointer points into a static buffer, which is overwritten by subsequent calls! Reimplemented from cPolymorphic. Reimplemented in cGate, cModule, cModulePar, and cSimulation. |
|
Returns true if the object's name is identical to the string passed.
|
|
Returns false, which means if this A object is the owner of some other object B, then B cannot be taken by any other object (see take()) -- an error will be raised saying the object is owned by A. This method only returns true in cDefaultList. Reimplemented in cDefaultList. |
|
Returns the number of objects that currently exist in the program. The counter is incremented by cObject constructor and decremented by the destructor. May be useful for profiling or debugging memory leaks. |
|
Returns pointer to the object's name. The function never returns NULL; rather, it returns ptr to "". |
|
Serializes the object into a buffer.
Reimplemented in cBag, cArray, cChannel, cBasicChannel, cDefaultList, cDensityEstBase, cFSM, cHistogramBase, cEqdHistogramBase, cKSplit, cLinkedList, cMessage, cOutVector, cPacket, cPar, cPSquare, cQueue, cStatistic, cStdDev, cWeightedStdDev, cTopology, and cVarHistogram. |
|
Deserializes the object from a buffer.
Reimplemented in cBag, cArray, cChannel, cBasicChannel, cDefaultList, cDensityEstBase, cFSM, cHistogramBase, cEqdHistogramBase, cKSplit, cLinkedList, cMessage, cOutVector, cPacket, cPar, cPSquare, cQueue, cStatistic, cStdDev, cWeightedStdDev, cTopology, and cVarHistogram. |
|
The assignment operator.
Derived classes should contain similar methods (
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: Ownership of the object is not affected by assigments. |
|
Returns pointer to the owner of the object.
|
|
Reset counters used by totalObjectCount() and liveObjectCount(). (Note that liveObjectCount() may go negative after a reset call.) Reimplemented in cMessage. |
|
Sets object's name. The object creates its own copy of the string. NULL pointer may also be passed. Reimplemented in cGate, cModule, and cOutVector. |
|
Makes this object the owner of 'object'. The function called by the container object when it takes ownership of the obj object that is inserted into it. The obj pointer should not be NULL. Reimplemented in cDefaultList. |
|
Returns the total number of objects created since the start of the program (or since the last reset).
The counter is incremented by cObject constructor. Counter is |
|
This function is indirectly invoked by snapshot(). It is expected to write textual information about the object and other objects it contains to the stream. This default version (cObject::writeContents()) prints detailedInfo() and uses forEachChild() to call info() for contained objects. It only needs to be redefined if this behaviour is should be customized. Reimplemented in cChannel, cBasicChannel, cDensityEstBase, cFSM, cGate, cKSplit, cMessage, cPar, cPSquare, cSimulation, and cStdDev. |