OMNeT++ Simulation Library  5.4.1
cqueue.h
1 //==========================================================================
2 // CQUEUE.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_CQUEUE_H
17 #define __OMNETPP_CQUEUE_H
18 
19 #include "cownedobject.h"
20 
21 namespace omnetpp {
22 
23 
34 typedef int (*CompareFunc)(cObject *a, cObject *b);
35 
36 
55 class SIM_API cQueue : public cOwnedObject
56 {
57  private:
58  struct QElem
59  {
60  cObject *obj; // the contained object
61  QElem *prev; // element towards the front of the queue
62  QElem *next; // element towards the back of the queue
63  };
64 
65  public:
69  class Iterator
70  {
71  private:
72  QElem *p;
73 
74  public:
80  Iterator(const cQueue& q, bool reverse=false) { init(q, reverse);}
81 
85  void init(const cQueue& q, bool reverse=false) {p = reverse ? q.backp : q.frontp;}
86 
90  cObject *operator*() const {return p ? p->obj : nullptr;}
91 
95  _OPPDEPRECATED cObject *operator()() const {return operator*();}
96 
100  bool end() const {return p == nullptr;}
101 
107  Iterator& operator++() {if (!end()) p = p->next; return *this;}
108 
114  Iterator operator++(int) {Iterator tmp(*this); if (!end()) p = p->next; return tmp;}
115 
121  Iterator& operator--() {if (!end()) p = p->prev; return *this;}
122 
128  Iterator operator--(int) {Iterator tmp(*this); if (!end()) p = p->prev; return tmp;}
129  };
130 
131  friend class Iterator;
132 
133  private:
134  bool takeOwnership; //TODO move it info flags
135  QElem *frontp, *backp; // inserting at back(), removal at front()
136  int n; // number of items in the queue
137  CompareFunc compare; // comparison function; nullptr for FIFO
138 
139  private:
140  void copy(const cQueue& other);
141 
142  protected:
143  // internal functions
144  QElem *find_qelem(cObject *obj) const;
145  void insbefore_qelem(QElem *p, cObject *obj);
146  void insafter_qelem(QElem *p, cObject *obj);
147  cObject *remove_qelem(QElem *p);
148 
149  public:
156  cQueue(const char *name=nullptr, CompareFunc cmp=nullptr);
157 
163  cQueue(const cQueue& queue);
164 
168  virtual ~cQueue();
169 
177  cQueue& operator=(const cQueue& queue);
179 
182 
188  virtual cQueue *dup() const override {return new cQueue(*this);}
189 
194  virtual std::string str() const override;
195 
200  virtual void forEachChild(cVisitor *v) override;
201 
207  virtual void parsimPack(cCommBuffer *buffer) const override;
208 
214  virtual void parsimUnpack(cCommBuffer *buffer) override;
216 
223  virtual void setup(CompareFunc cmp);
224 
229  virtual void insert(cObject *obj);
230 
236  virtual void insertBefore(cObject *where, cObject *obj);
237 
243  virtual void insertAfter(cObject *where, cObject *obj);
244 
249  virtual cObject *remove(cObject *obj);
250 
255  virtual cObject *pop();
256 
261  virtual void clear();
263 
271  virtual cObject *front() const;
272 
278  virtual cObject *back() const;
279 
283  virtual int getLength() const;
284 
288  bool isEmpty() const {return getLength()==0;}
289 
293  _OPPDEPRECATED int length() const {return getLength();}
294 
298  _OPPDEPRECATED bool empty() const {return isEmpty();}
299 
305  virtual cObject *get(int i) const;
306 
310  virtual bool contains(cObject *obj) const;
312 
315 
328  void setTakeOwnership(bool tk) {takeOwnership=tk;}
329 
335  bool getTakeOwnership() const {return takeOwnership;}
337 };
338 
339 } // namespace omnetpp
340 
341 
342 #endif
343 
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
bool end() const
Definition: cqueue.h:100
_OPPDEPRECATED cObject * operator()() const
Definition: cqueue.h:95
Iterator & operator--()
Definition: cqueue.h:121
bool isEmpty() const
Definition: cqueue.h:288
Iterator & operator++()
Definition: cqueue.h:107
Iterator(const cQueue &q, bool reverse=false)
Definition: cqueue.h:80
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:104
Iterator operator++(int)
Definition: cqueue.h:114
int(* CompareFunc)(cObject *a, cObject *b)
Function for comparing two cObjects, used for example by cQueue.
Definition: cqueue.h:34
_OPPDEPRECATED int length() const
Definition: cqueue.h:293
bool getTakeOwnership() const
Definition: cqueue.h:335
cObject * operator*() const
Definition: cqueue.h:90
void setTakeOwnership(bool tk)
Definition: cqueue.h:328
Queue class for objects derived from cObject.
Definition: cqueue.h:55
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
Walks along a cQueue.
Definition: cqueue.h:69
Definition: cabstracthistogram.h:21
virtual cQueue * dup() const override
Definition: cqueue.h:188
_OPPDEPRECATED bool empty() const
Definition: cqueue.h:298
void init(const cQueue &q, bool reverse=false)
Definition: cqueue.h:85
Iterator operator--(int)
Definition: cqueue.h:128