OMNeT++ Simulation Library  6.0.3
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 
21 namespace omnetpp {
22 
23 
38 class SIM_API cArray : public cOwnedObject
39 {
40  public:
53  class Iterator
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 
omnetpp::cArray::getCapacity
virtual int getCapacity() const
Definition: carray.h:215
omnetpp::cArray::get
virtual cObject * get(int m)
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cArray::Iterator
Iterates through elements in a cArray, skipping holes (slots containing nullptr).
Definition: carray.h:53
omnetpp::cArray::Iterator::operator++
Iterator & operator++()
Definition: carray.h:92
omnetpp::cArray::operator[]
cObject * operator[](int m)
Definition: carray.h:288
omnetpp::cArray::operator[]
const cObject * operator[](int m) const
Definition: carray.h:300
omnetpp::cArray::dup
virtual cArray * dup() const override
Definition: carray.h:167
omnetpp::cArray::Iterator::operator--
Iterator & operator--()
Definition: carray.h:106
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cArray::Iterator::operator++
Iterator operator++(int)
Definition: carray.h:99
omnetpp::cArray::Iterator::operator--
Iterator operator--(int)
Definition: carray.h:113
omnetpp::cArray::Iterator::operator*
cObject * operator*() const
Definition: carray.h:80
omnetpp::cArray::setTakeOwnership
void setTakeOwnership(bool tk)
Definition: carray.h:362
omnetpp::cArray
Vector-like container for objects derived from cObject.
Definition: carray.h:38
omnetpp::cArray::Iterator::end
bool end() const
Definition: carray.h:85
omnetpp::cArray::size
virtual int size() const
Definition: carray.h:204
omnetpp::cArray::operator[]
cObject * operator[](const char *objname)
Definition: carray.h:294
omnetpp::cArray::exist
virtual bool exist(const char *objname) const
Definition: carray.h:317
omnetpp::cArray::operator[]
const cObject * operator[](const char *objname) const
Definition: carray.h:306
omnetpp::cArray::exist
virtual bool exist(int m) const
Definition: carray.h:311
omnetpp::cArray::getTakeOwnership
bool getTakeOwnership() const
Definition: carray.h:369
omnetpp::cArray::Iterator::Iterator
Iterator(const cArray &a, bool atHead=true)
Definition: carray.h:69
omnetpp::cCommBuffer
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
omnetpp::cOwnedObject
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:105