Project

General

Profile

Statistics
| Branch: | Revision:

root / include / cdefaultlist.h @ a3be1d55

History | View | Annotate | Download (5.46 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  CDEFAULTLIST.H - part of
3
//
4
//                     OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//
8
//  Declaration of the following classes:
9
//    cDefaultList : holds a set of cOwnedObjects
10
//
11
//==========================================================================
12
13
/*--------------------------------------------------------------*
14
  Copyright (C) 1992-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20
21
#ifndef __CDEFAULTLIST_H
22
#define __CDEFAULTLIST_H
23
24
#include "cownedobject.h"
25
26
NAMESPACE_BEGIN
27
28
29
/**
30
 * Internal class, used as a base class for modules and channels. It is not
31
 * intended for subclassing outside the simulation kernel.
32
 *
33
 * cDefaultList acts as a "soft owner" (see object ownership discussion
34
 * in cOwnedObject documentation).
35
 *
36
 * @ingroup Internals
37
 */
38
class SIM_API cDefaultList : public cNoncopyableOwnedObject
39
{
40
    friend class cObject;
41
    friend class cOwnedObject;
42
    friend class cChannelType;
43
44
  private:
45
    enum {FL_PERFORMFINALGC = 2};  // whether to delete owned objects in the destructor
46
47
  private:
48
    cOwnedObject **vect; // vector of objects
49
    int capacity;        // allocated size of vect[]
50
    int size;            // number of elements used in vect[] (0..size-1)
51
52
  protected:
53
    void construct();
54
    virtual void doInsert(cOwnedObject *obj);
55
    virtual void ownedObjectDeleted(cOwnedObject *obj);
56
    virtual void yieldOwnership(cOwnedObject *obj, cObject *newOwner);
57
58
  public:
59
    // internal: called from module creation code in ctypes.cc
60
    void takeAllObjectsFrom(cDefaultList& other);
61
62
  protected:
63
    /** @name Redefined cObject member functions */
64
    //@{
65
66
    /**
67
     * Redefined.
68
     */
69
    void take(cOwnedObject *obj);
70
71
    /**
72
     * Redefined.
73
     */
74
    void drop(cOwnedObject *obj);
75
76
  public:
77
    /** @name Constructors, destructor, assignment. */
78
    //@{
79
    /**
80
     * Constructor.
81
     */
82
    explicit cDefaultList(const char *name=NULL);
83
84
    /**
85
     * Destructor. The contained objects will be deleted.
86
     */
87
    virtual ~cDefaultList();
88
    //@}
89
90
    /** @name Redefined cObject methods. */
91
    //@{
92
    /**
93
     * Returns true.
94
     */
95
    virtual bool isSoftOwner() const {return true;}
96
97
    /**
98
     * Produces a one-line description of the object's contents.
99
     * See cObject for more details.
100
     */
101
    virtual std::string info() const;
102
103
    /**
104
     * Calls v->visit(this) for each contained object.
105
     * See cObject for more details.
106
     */
107
    virtual void forEachChild(cVisitor *v);
108
109
    /**
110
     * Packing and unpacking cannot be supported with this class.
111
     * This method raises an error.
112
     */
113
    virtual void parsimPack(cCommBuffer *buffer);
114
115
    /**
116
     * Packing and unpacking cannot be supported with this class.
117
     * This method raises an error.
118
     */
119
    virtual void parsimUnpack(cCommBuffer *buffer);
120
    //@}
121
122
    /** @name Container functions. */
123
    // Note: we need long method names here because cModule subclasses from this class
124
    //@{
125
126
    /**
127
     * Whether the destructor will delete garbage objects (owned objects
128
     * that have not been deallocated by destructors of derived classes);
129
     * see setPerformFinalGC() for details. The default setting is false.
130
     */
131
    bool getPerformFinalGC() const  {return flags&FL_PERFORMFINALGC;}
132
133
    /**
134
     * Enables deleting of garbage objects in the destructor. Garbage objects
135
     * are objects that are on this default list at destruct time (see
136
     * defaultListSize() and defaultListGet()); they are practically objects
137
     * owned by this module or channel that have not been deallocated
138
     * by destructors of derived classes.
139
     *
140
     * This feature is turned off by default, because it cannot be implemented
141
     * to work correctly in all cases. The garbage collection routine invokes
142
     * the <tt>delete</tt> operator on all objects on the default list. This,
143
     * however, causes problems (crash) in some cases: when the object has
144
     * been allocated as an element in an array (e.g. <tt>new cQueue[10]</tt>),
145
     * when the object is part of another object (e.g. <tt>struct X {cQueue q;};</tt>),
146
     * or any other case when the object's pointer is not deleteable.
147
     * Note that the presence of such data structures in a module
148
     * does not automatically rule out the use of the final GC mechanism.
149
     * Final GC can still be turned on if care is taken that the module's
150
     * destructor deallocates those arrays and structs/objects, so that
151
     * the contained objects disappear before the GC process could see them.
152
     * Conclusion: only turn on final GC if you know what you are doing.
153
     */
154
    virtual void setPerformFinalGC(bool b)  {setFlag(FL_PERFORMFINALGC,b);}
155
156
    /**
157
     * Returns the number of elements stored.
158
     */
159
    int defaultListSize() const {return size;}
160
161
    /**
162
     * Get the element at the given position. k must be between 0 and
163
     * defaultListSize()-1 (inclusive). If the index is out of bounds,
164
     * NULL is returned.
165
     */
166
    cOwnedObject *defaultListGet(int k);
167
168
    /**
169
     * Returns true if the set contains the given object, false otherwise.
170
     */
171
    // Note: we need a long name here because cModule subclasses from this
172
    bool defaultListContains(cOwnedObject *obj) const;
173
    //@}
174
};
175
176
NAMESPACE_END
177
178
179
#endif