OMNeT++ Simulation Library  5.6.1
cObjectFactory Class Reference

#include <cobjectfactory.h>

Description

The class behind the createOne() function and the Register_Class() macro.

Each instance is a factory for a particular class: it knows how to create an object of that class.

See also
Register_Class(), Define_Module() macros
Inheritance diagram for cObjectFactory:
cNoncopyableOwnedObject cOwnedObject noncopyable cNamedObject cObject

Public Member Functions

Constructors, destructor, assignment.
 cObjectFactory (const char *name, cObject *(*creatorfunc)(), void *(*castfunc)(cObject *), const char *description=nullptr)
 
Redefined cObject member functions.
virtual std::string str () const override
 
New methods
virtual bool isAbstract () const
 
virtual cObjectcreateOne () const
 
virtual bool isInstance (cObject *obj) const
 
const char * getDescription () const
 
- Public Member Functions inherited from cNoncopyableOwnedObject
 cNoncopyableOwnedObject (const char *name=nullptr, bool namepooling=true)
 
virtual cNoncopyableOwnedObjectdup () const override
 
virtual void parsimPack (cCommBuffer *buffer) const override
 
virtual void parsimUnpack (cCommBuffer *buffer) override
 
- Public Member Functions inherited from cOwnedObject
 cOwnedObject ()
 
 cOwnedObject (const char *name, bool namepooling=true)
 
 cOwnedObject (const cOwnedObject &obj)
 
virtual ~cOwnedObject ()
 
cOwnedObjectoperator= (const cOwnedObject &o)
 
virtual cObjectgetOwner () const override
 
virtual bool isOwnedObject () const override
 
virtual bool isSoftOwner () const
 
- Public Member Functions inherited from cNamedObject
 cNamedObject ()
 
 cNamedObject (const char *name, bool namepooling=true)
 
 cNamedObject (const cNamedObject &obj)
 
virtual ~cNamedObject ()
 
cNamedObjectoperator= (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 cObjectgetThisPtr () const
 
virtual _OPPDEPRECATED std::string info () const
 
virtual _OPPDEPRECATED std::string detailedInfo () const
 
virtual void forEachChild (cVisitor *v)
 
cObjectfindObject (const char *name, bool deep=true)
 
void copyNotSupported () const
 

Static Public Member Functions

Static factory methods
static cObjectFactoryfind (const char *className, const char *contextNamespace=nullptr, bool fallbackToOmnetpp=true)
 
static cObjectFactoryget (const char *className, const char *contextNamespace=nullptr, bool fallbackToOmnetpp=true)
 
static cObjectcreateOne (const char *classname)
 
static cObjectcreateOneIfClassIsKnown (const char *classname)
 
- Static Public Member Functions inherited from cOwnedObject
static long getTotalObjectCount ()
 
static long getLiveObjectCount ()
 
static void resetObjectCounters ()
 
static cDefaultListgetDefaultOwner ()
 

Additional Inherited Members

- Protected Member Functions inherited from cObject
virtual void take (cOwnedObject *obj)
 
virtual void drop (cOwnedObject *obj)
 
void dropAndDelete (cOwnedObject *obj)
 

Constructor & Destructor Documentation

◆ cObjectFactory()

cObjectFactory ( const char *  name,
cObject *(*)()  creatorfunc,
void *(*)(cObject *)  castfunc,
const char *  description = nullptr 
)

Constructor.

Member Function Documentation

◆ str()

virtual std::string str ( ) const
overridevirtual

Produces a one-line description of the object's contents. See cObject for more details.

Reimplemented from cObject.

◆ isAbstract()

virtual bool isAbstract ( ) const
inlinevirtual

Returns true if the class this object stands for is abstract. createOne() cannot be called for abstract classes.

References omnetpp::createOne().

◆ createOne() [1/2]

virtual cObject* createOne ( ) const
virtual

Creates an instance of a particular class by calling the creator function. The result has to be cast to the appropriate type (preferably by dynamic_cast or check_and_cast). The method will throw an error if the class is abstract (see isAbstract()).

Referenced by omnetpp::createOne().

◆ isInstance()

virtual bool isInstance ( cObject obj) const
inlinevirtual

Returns true if the given object can be cast (via dynamic_cast) to the class represented by this factory object, and false otherwise.

◆ getDescription()

const char* getDescription ( ) const
inline

Returns a description string.

References omnetpp::createOne(), and omnetpp::createOneIfClassIsKnown().

◆ find()

static cObjectFactory* find ( const char *  className,
const char *  contextNamespace = nullptr,
bool  fallbackToOmnetpp = true 
)
static

Finds the factory object for the class given in the className parameter, or nullptr if not found. The class must have been registered previously with the Register_Class() macro.

◆ get()

static cObjectFactory* get ( const char *  className,
const char *  contextNamespace = nullptr,
bool  fallbackToOmnetpp = true 
)
static

Like find(), but throws an error if the object was not found.

◆ createOne() [2/2]

static cObject* createOne ( const char *  classname)
static

Creates an instance of a particular class; the result has to be cast to the appropriate type by hand. The class must have been registered previously with the Register_Class() macro. The class name string should be given with any potential namespace, enclosing class etc.

If the class is not registered, this function throws an exception. If you'd prefer having nullptr returned instead, use the createOneIfClassIsKnown() function.

Example:

cMessage *msg = cObjectFactory::createOne("INET::EthernetFrame");

createOne() is used e.g. in parallel simulation, when an object is received from another partition in serialized form and has to be demarshalled.

See also
createOneIfClassIsKnown()
Register_Class() macro
cObjectFactory class

◆ createOneIfClassIsKnown()

static cObject* createOneIfClassIsKnown ( const char *  classname)
static

A variant of the createOne() function; this function doesn't throw an exception if the class is not registered, but returns nullptr instead.

See also
createOne()

Referenced by omnetpp::createOneIfClassIsKnown().


The documentation for this class was generated from the following file: