OMNeT++ Simulation Library  5.6.1
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  _OPPDEPRECATED cObject *operator()() const {return operator*();}
86 
90  bool end() const {return k<0 || k>=array->size();}
91 
97  Iterator& operator++() {if (!end()) advance(); return *this;}
98 
104  Iterator operator++(int) {Iterator tmp(*this); if (!end()) advance(); return tmp;}
105 
111  Iterator& operator--() {if (!end()) retreat(); return *this;}
112 
118  Iterator operator--(int) {Iterator tmp(*this); if (!end()) retreat(); return tmp;}
119  };
120 
121  private:
122  enum {FL_TKOWNERSHIP = 4};
123  cObject **vect; // vector of objects
124  int capacity; // allocated size of vect[]
125  int delta; // if needed, grows by delta
126  int firstfree; // first free position in vect[]
127  int last; // last used position
128 
129  private:
130  void copy(const cArray& other);
131 
132  public:
135 
141  cArray(const cArray& list);
142 
147  explicit cArray(const char *name=nullptr, int capacity=0, int delta=10);
148 
153  virtual ~cArray();
154 
162  cArray& operator=(const cArray& list);
164 
167 
173  virtual cArray *dup() const override {return new cArray(*this);}
174 
179  virtual std::string str() const override;
180 
185  virtual void forEachChild(cVisitor *v) override;
186 
192  virtual void parsimPack(cCommBuffer *buffer) const override;
193 
199  virtual void parsimUnpack(cCommBuffer *buffer) override;
201 
204 
210  virtual int size() const {return last+1;}
211 
216  virtual void clear();
217 
221  virtual int getCapacity() const {return capacity;}
222 
227  virtual void setCapacity(int capacity);
228 
234  virtual int add(cObject *obj);
235 
241  virtual int addAt(int m, cObject *obj);
242 
250  virtual int set(cObject *obj);
251 
257  virtual int find(cObject *obj) const;
258 
264  virtual int find(const char *objname) const;
265 
270  virtual cObject *get(int m);
271 
276  virtual cObject *get(const char *objname);
277 
282  virtual const cObject *get(int m) const;
283 
288  virtual const cObject *get(const char *objname) const;
289 
294  cObject *operator[](int m) {return get(m);}
295 
300  cObject *operator[](const char *objname) {return get(objname);}
301 
306  const cObject *operator[](int m) const {return get(m);}
307 
312  const cObject *operator[](const char *objname) const {return get(objname);}
313 
317  virtual bool exist(int m) const {return m>=0 && m<=last && vect[m]!=nullptr;}
318 
323  virtual bool exist(const char *objname) const {return find(objname)!=-1;}
324 
330  virtual cObject *remove(int m);
331 
337  virtual cObject *remove(const char *objname);
338 
345  virtual cObject *remove(cObject *obj);
347 
355 
368  void setTakeOwnership(bool tk) {setFlag(FL_TKOWNERSHIP,tk);}
369 
375  bool getTakeOwnership() const {return flags&FL_TKOWNERSHIP;}
377 };
378 
379 } // namespace omnetpp
380 
381 
382 #endif
383 
Iterates through elements in a cArray, skipping holes (slots containing nullptr). ...
Definition: carray.h:53
Iterator operator--(int)
Definition: carray.h:118
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
virtual bool exist(int m) const
Definition: carray.h:317
Iterator operator++(int)
Definition: carray.h:104
void setTakeOwnership(bool tk)
Definition: carray.h:368
virtual cArray * dup() const override
Definition: carray.h:173
Vector-like container for objects derived from cObject.
Definition: carray.h:38
Iterator & operator++()
Definition: carray.h:97
cObject * operator[](int m)
Definition: carray.h:294
bool end() const
Definition: carray.h:90
bool getTakeOwnership() const
Definition: carray.h:375
_OPPDEPRECATED cObject * operator()() const
Definition: carray.h:85
virtual bool exist(const char *objname) const
Definition: carray.h:323
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
virtual int getCapacity() const
Definition: carray.h:221
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:104
const cObject * operator[](const char *objname) const
Definition: carray.h:312
virtual cObject * get(int m)
const cObject * operator[](int m) const
Definition: carray.h:306
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
cObject * operator[](const char *objname)
Definition: carray.h:300
Definition: cabstracthistogram.h:21
Iterator & operator--()
Definition: carray.h:111
virtual int size() const
Definition: carray.h:210
cObject * operator*() const
Definition: carray.h:80
Iterator(const cArray &a, bool atHead=true)
Definition: carray.h:69