Statistics
| Branch: | Revision:

root / src / sim / cobject.cc @ 08285dff

History | View | Annotate | Download (5.19 KB)

1
//========================================================================
2
//  COBJECT.CC - part of
3
//
4
//                 OMNeT++/OMNEST
5
//              Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga
8
//
9
//========================================================================
10

    
11
/*--------------------------------------------------------------*
12
  Copyright (C) 1992-2008 Andras Varga
13
  Copyright (C) 2006-2008 OpenSim Ltd.
14

15
  This file is distributed WITHOUT ANY WARRANTY. See the file
16
  `license' for details on this and other legal matters.
17
*--------------------------------------------------------------*/
18

    
19
#include "cobject.h"
20
#include "cownedobject.h"
21
#include "cdefaultlist.h"
22
#include "cexception.h"
23
#include "cclassdescriptor.h"
24
#include "cenvir.h"
25

    
26
USING_NAMESPACE
27

    
28

    
29
cObject::~cObject()
30
{
31
    // notify environment
32
    ev.objectDeleted(this);
33
}
34

    
35
const char *cObject::getClassName() const
36
{
37
    return opp_typename(typeid(*this));
38
}
39

    
40
cClassDescriptor *cObject::getDescriptor()
41
{
42
    return cClassDescriptor::getDescriptorFor(this);
43
}
44

    
45
std::string cObject::getFullPath() const
46
{
47
#if ENABLE_OWNERSHIP
48
    if (getOwner()==NULL)
49
        return getFullName();
50
    else
51
        return getOwner()->getFullPath() + "." + getFullName();
52
#else
53
    return getFullName();
54
#endif
55
}
56

    
57
std::string cObject::info() const
58
{
59
    return std::string();
60
}
61

    
62
std::string cObject::detailedInfo() const
63
{
64
    return std::string();
65
}
66

    
67
cObject *cObject::dup() const
68
{
69
    throw cRuntimeError("The dup() method, declared in cObject, is not "
70
                        "redefined in class %s", getClassName());
71
}
72

    
73
void cObject::ownedObjectDeleted(cOwnedObject *obj)
74
{
75
    // Note: too late to call obj->getClassName(), at this point it'll aways return "cOwnedObject"
76
    throw cRuntimeError("Object %s is currently in (%s)%s, it cannot be deleted. "
77
                        "If this error occurs inside %s, it needs to be changed "
78
                        "to call drop() before it can delete that object. "
79
                        "If this error occurs inside %s's destructor and %s is a class member, "
80
                        "%s needs to call drop() in the destructor",
81
                        obj->getFullName(), getClassName(), getFullPath().c_str(),
82
                        getClassName(),
83
                        getClassName(), obj->getFullName(),
84
                        getClassName());
85
}
86

    
87
void cObject::yieldOwnership(cOwnedObject *obj, cObject *newowner)
88
{
89
    throw cRuntimeError("(%s)%s is currently in (%s)%s, it cannot be inserted into (%s)%s",
90
                        obj->getClassName(), obj->getFullName(),
91
                        getClassName(), getFullPath().c_str(),
92
                        newowner->getClassName(), newowner->getFullPath().c_str());
93
}
94

    
95

    
96
void cObject::take(cOwnedObject *obj)
97
{
98
#if ENABLE_OWNERSHIP
99
    // ask current owner to release it -- if it's a cDefaultList, it will.
100
    obj->ownerp->yieldOwnership(obj, this);
101
#else
102
    obj->ownerp = this;
103
#endif
104
}
105

    
106
void cObject::drop(cOwnedObject *obj)
107
{
108
#if ENABLE_OWNERSHIP
109
    if (obj->ownerp!=this)
110
        throw cRuntimeError(this, "drop(): not owner of object (%s)%s",
111
                                  obj->getClassName(), obj->getFullPath().c_str());
112
    cThreadPool::getDefaultOwner()->doInsert(obj);
113
#else
114
    obj->ownerp = cThreadPool::getDefaultOwner();
115
#endif
116
}
117

    
118
void cObject::dropAndDelete(cOwnedObject *obj)
119
{
120
    if (!obj)
121
        return;
122
#if ENABLE_OWNERSHIP
123
    if (obj->ownerp!=this)
124
        throw cRuntimeError(this, "dropAndDelete(): not owner of object (%s)%s",
125
                                  obj->getClassName(), obj->getFullPath().c_str());
126
    obj->ownerp = NULL;
127
#endif
128
    delete obj;
129
}
130

    
131
void cObject::parsimPack(cCommBuffer *buffer)
132
{
133
}
134

    
135
void cObject::parsimUnpack(cCommBuffer *buffer)
136
{
137
}
138

    
139
void cObject::copyNotSupported() const
140
{
141
    throw cRuntimeError(this,eCANTCOPY);
142
}
143

    
144
void cObject::forEachChild(cVisitor *v)
145
{
146
}
147

    
148
// Internally used visitors
149

    
150
/**
151
 * Finds a child object by name.
152
 */
153
class cChildObjectFinderVisitor : public cVisitor
154
{
155
  protected:
156
    const char *name;
157
    cObject *result;
158
  public:
159
    cChildObjectFinderVisitor(const char *objname) {
160
        name = objname; result = NULL;
161
    }
162
    virtual void visit(cObject *obj) {
163
        if (obj->isName(name)) {
164
            result = obj;
165
            throw EndTraversalException();
166
        }
167
    }
168
    cObject *getResult() {return result;}
169
};
170

    
171
/**
172
 * Recursively finds an object by name.
173
 */
174
class cRecursiveObjectFinderVisitor : public cVisitor
175
{
176
  protected:
177
    const char *name;
178
    cObject *result;
179
  public:
180
    cRecursiveObjectFinderVisitor(const char *objname) {
181
        name = objname; result = NULL;
182
    }
183
    virtual void visit(cObject *obj) {
184
        if (obj->isName(name)) {
185
            result = obj;
186
            throw EndTraversalException();
187
        }
188
        obj->forEachChild(this);
189
    }
190
    cObject *getResult() {return result;}
191
};
192

    
193
cObject *cObject::findObject(const char *objname, bool deep)
194
{
195
    if (deep)
196
    {
197
        // recursively
198
        cRecursiveObjectFinderVisitor v(objname);
199
        v.processChildrenOf(this);
200
        return v.getResult();
201
    }
202
    else
203
    {
204
        // among children
205
        cChildObjectFinderVisitor v(objname);
206
        v.processChildrenOf(this);
207
        return v.getResult();
208
    }
209
}
210

    
211