carray.h Source File

API: carray.h Source File
API
carray.h
1//==========================================================================
2// CARRAY.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_CARRAY_H
17#define __OMNETPP_CARRAY_H
18
19#include "cownedobject.h"
20
21namespace omnetpp {
22
23
38class SIM_API cArray : public cOwnedObject
39{
40 public:
54 {
55 private:
56 cArray *array;
57 int k;
58
59 private:
60 void advance();
61 void retreat();
62
63 public:
69 Iterator(const cArray& a, bool atHead=true) {init(a, atHead);}
70
74 void init(const cArray& a, bool atHead=true);
75
80 cObject *operator*() const {return array->get(k);}
81
85 bool end() const {return k<0 || k>=array->size();}
86
92 Iterator& operator++() {if (!end()) advance(); return *this;}
93
99 Iterator operator++(int) {Iterator tmp(*this); if (!end()) advance(); return tmp;}
100
106 Iterator& operator--() {if (!end()) retreat(); return *this;}
107
113 Iterator operator--(int) {Iterator tmp(*this); if (!end()) retreat(); return tmp;}
114 };
115
116 private:
117 enum {FL_TKOWNERSHIP = 4};
118 cObject **vect = nullptr; // vector of objects
119 int capacity = 0; // allocated size of vect[]
120 int delta; // if needed, grows by delta
121 int firstfree = 0; // first free position in vect[]
122 int last = -1; // last used position
123
124 private:
125 void copy(const cArray& other);
126
127 public:
130
136 cArray(const cArray& list);
137
142 explicit cArray(const char *name=nullptr, int capacity=0, int delta=10);
143
148 virtual ~cArray();
149
156 cArray& operator=(const cArray& list);
158
161
167 virtual cArray *dup() const override {return new cArray(*this);}
168
173 virtual std::string str() const override;
174
179 virtual void forEachChild(cVisitor *v) override;
180
186 virtual void parsimPack(cCommBuffer *buffer) const override;
187
193 virtual void parsimUnpack(cCommBuffer *buffer) override;
195
198
204 virtual int size() const {return last+1;}
205
210 virtual void clear();
211
215 virtual int getCapacity() const {return capacity;}
216
221 virtual void setCapacity(int capacity);
222
228 virtual int add(cObject *obj);
229
235 virtual int addAt(int m, cObject *obj);
236
244 virtual int set(cObject *obj);
245
251 virtual int find(cObject *obj) const;
252
258 virtual int find(const char *objname) const;
259
264 virtual cObject *get(int m);
265
270 virtual cObject *get(const char *objname);
271
276 virtual const cObject *get(int m) const;
277
282 virtual const cObject *get(const char *objname) const;
283
288 cObject *operator[](int m) {return get(m);}
289
294 cObject *operator[](const char *objname) {return get(objname);}
295
300 const cObject *operator[](int m) const {return get(m);}
301
306 const cObject *operator[](const char *objname) const {return get(objname);}
307
311 virtual bool exist(int m) const {return m>=0 && m<=last && vect[m]!=nullptr;}
312
317 virtual bool exist(const char *objname) const {return find(objname)!=-1;}
318
324 virtual cObject *remove(int m);
325
331 virtual cObject *remove(const char *objname);
332
339 virtual cObject *remove(cObject *obj);
341
349
362 void setTakeOwnership(bool tk) {setFlag(FL_TKOWNERSHIP,tk);}
363
369 bool getTakeOwnership() const {return flags&FL_TKOWNERSHIP;}
371};
372
373} // namespace omnetpp
374
375
376#endif
377
cObject * operator*() const
Definition carray.h:80
Iterator(const cArray &a, bool atHead=true)
Definition carray.h:69
Iterator operator--(int)
Definition carray.h:113
void init(const cArray &a, bool atHead=true)
Iterator operator++(int)
Definition carray.h:99
Iterator & operator++()
Definition carray.h:92
Iterator & operator--()
Definition carray.h:106
bool end() const
Definition carray.h:85
virtual int getCapacity() const
Definition carray.h:215
virtual int find(const char *objname) const
void setTakeOwnership(bool tk)
Definition carray.h:362
virtual int add(cObject *obj)
cArray(const cArray &list)
cArray(const char *name=nullptr, int capacity=0, int delta=10)
virtual int size() const
Definition carray.h:204
virtual const cObject * get(const char *objname) const
virtual bool exist(int m) const
Definition carray.h:311
virtual void parsimPack(cCommBuffer *buffer) const override
virtual cObject * get(const char *objname)
cArray & operator=(const cArray &list)
virtual bool exist(const char *objname) const
Definition carray.h:317
const cObject * operator[](int m) const
Definition carray.h:300
cObject * operator[](int m)
Definition carray.h:288
virtual cObject * remove(cObject *obj)
virtual void parsimUnpack(cCommBuffer *buffer) override
virtual cObject * remove(const char *objname)
virtual void setCapacity(int capacity)
virtual cObject * get(int m)
virtual const cObject * get(int m) const
cObject * operator[](const char *objname)
Definition carray.h:294
virtual void clear()
const cObject * operator[](const char *objname) const
Definition carray.h:306
virtual int find(cObject *obj) const
virtual int set(cObject *obj)
virtual cArray * dup() const override
Definition carray.h:167
bool getTakeOwnership() const
Definition carray.h:369
virtual void forEachChild(cVisitor *v) override
virtual int addAt(int m, cObject *obj)
virtual std::string str() const override
virtual ~cArray()
virtual cObject * remove(int m)
Buffer for the communications layer of parallel simulation.
Definition ccommbuffer.h:42
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition cvisitor.h:57