Statistics
| Branch: | Revision:

root / include / cobjectfactory.h @ fbe00e73

History | View | Annotate | Download (4.15 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  COBJECTFACTORY.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//==========================================================================
7
8
/*--------------------------------------------------------------*
9
  Copyright (C) 1992-2008 Andras Varga
10
  Copyright (C) 2006-2008 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 __COBJECTFACTORY_H
17
#define __COBJECTFACTORY_H
18
19
#include "simkerneldefs.h"
20
#include "globals.h"
21
#include "cownedobject.h"
22
23
NAMESPACE_BEGIN
24
25
26
/**
27
 * The class behind the createOne() function and the Register_Class() macro.
28
 * Each instance is a factory for a particular class: it knows how to create
29
 * an object of that class.
30
 *
31
 * @see Register_Class(), Define_Module() macros
32
 * @ingroup Internals
33
 */
34
class SIM_API cObjectFactory : public cNoncopyableOwnedObject
35
{
36
  private:
37
    cObject *(*creatorfunc)();
38
    std::string descr;
39
40
  public:
41
    /** @name Constructors, destructor, assignment. */
42
    //@{
43
    /**
44
     * Constructor.
45
     */
46
    cObjectFactory(const char *name, cObject *(*f)(), const char *description=NULL);
47
    //@}
48
49
    /** @name Redefined cObject member functions. */
50
    //@{
51
    /**
52
     * Produces a one-line description of the object's contents.
53
     * See cObject for more details.
54
     */
55
    virtual std::string info() const;
56
    //@}
57
58
    /** @name New methods */
59
    //@{
60
    /**
61
     * Creates an instance of a particular class by calling the creator
62
     * function. The result has to be cast to the appropriate type
63
     * (preferably by dynamic_cast or check_and_cast).
64
     */
65
    cObject *createOne() const  {return creatorfunc();}
66
67
    /**
68
     * Returns a description string.
69
     */
70
    const char *getDescription() const  {return descr.c_str();}
71
    //@}
72
73
    /** @name Static factory methods */
74
    //@{
75
    /**
76
     * Finds the factory object for the class given in the classname parameter,
77
     * or NULL if not found. The class name string should be given with any
78
     * potential namespace, enclosing class etc. The class must have been
79
     * registered previously with the Register_Class() macro.
80
     */
81
    static cObjectFactory *find(const char *classname);
82
83
    /**
84
     * Like find(), but throws an error if the object was not found.
85
     */
86
    static cObjectFactory *get(const char *classname);
87
88
    /**
89
     * Creates an instance of a particular class; the result has to be cast
90
     * to the appropriate type by hand. The class must have been registered
91
     * previously with the Register_Class() macro. The class name string
92
     * should be given with any potential namespace, enclosing class etc.
93
     *
94
     * If the class is not registered, this function throws an exception.
95
     * If you'd prefer having a NULL pointer returned instead, use the
96
     * createOneIfClassIsKnown() function.
97
     *
98
     * Example:
99
     *
100
     * <tt>cMessage *msg = cObjectFactory::createOne("INET::EthernetFrame");</tt>
101
     *
102
     * createOne() is used e.g. in parallel simulation, when an object is received
103
     * from another partition in serialized form and has to be demarshalled.
104
     *
105
     * @see createOneIfClassIsKnown()
106
     * @see Register_Class() macro
107
     * @see cObjectFactory class
108
     */
109
    static cObject *createOne(const char *classname);
110
111
    /**
112
     * A variant of the createOne() function; this function doesn't throw an
113
     * exception if the class is not registered, but returns a NULL pointer
114
     * instead.
115
     *
116
     * @see createOne()
117
     */
118
    static cObject *createOneIfClassIsKnown(const char *classname);
119
    //@}
120
};
121
122
123
/**
124
 *
125
 * @addtogroup Functions
126
 */
127
//@{
128
/**
129
 * Shortcut to cObjectFactory::createOne().
130
 */
131
inline cObject *createOne(const char *classname) {
132
    return cObjectFactory::createOne(classname);
133
}
134
135
/**
136
 * Shortcut to cObjectFactory::createOneIfClassIsKnown().
137
 */
138
inline cObject *createOneIfClassIsKnown(const char *classname) {
139
    return cObjectFactory::createOneIfClassIsKnown(classname);
140
}
141
//@}
142
143
NAMESPACE_END
144
145
146
#endif
147