OMNeT++ Simulation Library  6.0.3
cobjectfactory.h
1 //==========================================================================
2 // COBJECTFACTORY.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_COBJECTFACTORY_H
17 #define __OMNETPP_COBJECTFACTORY_H
18 
19 #include "simkerneldefs.h"
20 #include "globals.h"
21 #include "cownedobject.h"
22 
23 namespace omnetpp {
24 
25 
36 {
37  private:
38  cObject *(*creatorFunc)();
39  void *(*castFunc)(cObject *);
40  std::string description;
41 
42  public:
48  cObjectFactory(const char *name, cObject *(*creatorfunc)(), void *(*castfunc)(cObject *), const char *description=nullptr);
50 
57  virtual std::string str() const override;
59 
66  virtual bool isAbstract() const {return creatorFunc==nullptr;}
67 
74  virtual cObject *createOne() const;
75 
80  virtual bool isInstance(cObject *obj) const {return castFunc(obj)!=nullptr;}
81 
85  const char *getDescription() const {return description.c_str();}
87 
95  static cObjectFactory *find(const char *className, const char *contextNamespace=nullptr, bool fallbackToOmnetpp=true);
96 
100  static cObjectFactory *get(const char *className, const char *contextNamespace=nullptr, bool fallbackToOmnetpp=true);
101 
123  static cObject *createOne(const char *classname);
124 
132  static cObject *createOneIfClassIsKnown(const char *classname);
134 };
135 
136 
141 inline cObject *createOne(const char *classname) {
142  return cObjectFactory::createOne(classname);
143 }
144 
149 inline cObject *createOneIfClassIsKnown(const char *classname) {
150  return cObjectFactory::createOneIfClassIsKnown(classname);
151 }
152 
153 } // namespace omnetpp
154 
155 
156 #endif
157 
158 
omnetpp::createOne
cObject * createOne(const char *classname)
Shortcut to cObjectFactory::createOne().
Definition: cobjectfactory.h:141
omnetpp::cObjectFactory::isInstance
virtual bool isInstance(cObject *obj) const
Definition: cobjectfactory.h:80
omnetpp::cObjectFactory::createOne
virtual cObject * createOne() const
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cObjectFactory
The class behind the createOne() function and the Register_Class() macro.
Definition: cobjectfactory.h:35
omnetpp::createOneIfClassIsKnown
cObject * createOneIfClassIsKnown(const char *classname)
Shortcut to cObjectFactory::createOneIfClassIsKnown().
Definition: cobjectfactory.h:149
omnetpp::cObjectFactory::isAbstract
virtual bool isAbstract() const
Definition: cobjectfactory.h:66
omnetpp::cObjectFactory::getDescription
const char * getDescription() const
Definition: cobjectfactory.h:85
omnetpp::cObjectFactory::createOneIfClassIsKnown
static cObject * createOneIfClassIsKnown(const char *classname)
omnetpp::cNoncopyableOwnedObject
Base class for cOwnedObject-based classes that do not wish to support assignment and duplication.
Definition: cownedobject.h:242