Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / cobjectfactory.cc @ 3e29b8a0

History | View | Annotate | Download (1.94 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  COBJECTFACTORY.CC - part of
3
//
4
//                    OMNeT++/OMNEST
5
//             Discrete System Simulation in C++
6
//
7
//=========================================================================
8
9
/*--------------------------------------------------------------*
10
  Copyright (C) 1992-2008 Andras Varga
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#include <string.h>
18
#include "cobjectfactory.h"
19
#include "cexception.h"
20
21
#ifdef WITH_PARSIM
22
#include "ccommbuffer.h"
23
#include "parsim/cplaceholdermod.h"
24
#endif
25
26
USING_NAMESPACE
27
28
29
cObjectFactory::cObjectFactory(const char *name, cObject *(*f)(), const char *description)
30
  : cNoncopyableOwnedObject(name, false)
31
{
32
    creatorfunc = f;
33
    descr = description ? description : "";
34
}
35
36
std::string cObjectFactory::info() const
37
{
38
    if (descr.empty())
39
        return "";
40
    return std::string("(") + descr + ")";
41
}
42
43
cObjectFactory *cObjectFactory::find(const char *classname)
44
{
45
    return dynamic_cast<cObjectFactory *>(classes.getInstance()->lookup(classname));
46
}
47
48
cObjectFactory *cObjectFactory::get(const char *classname)
49
{
50
    cObjectFactory *p = find(classname);
51
    if (!p)
52
        throw cRuntimeError("Class \"%s\" not found -- perhaps its code was not linked in, "
53
                            "or the class wasn't registered with Register_Class(), or in the case of "
54
                            "modules and channels, with Define_Module()/Define_Channel()", classname);
55
    return p;
56
}
57
58
cObject *cObjectFactory::createOne(const char *classname)
59
{
60
    cObjectFactory *p = get(classname);
61
    return p->createOne();
62
}
63
64
cObject *cObjectFactory::createOneIfClassIsKnown(const char *classname)
65
{
66
    cObjectFactory *p = find(classname);
67
    return p ? p->createOne() : NULL;
68
}
69