OMNeT++ Simulation Library  5.6.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 
42 class SIM_API cQueue : public cOwnedObject
43 {
44  private:
45  struct QElem
46  {
47  cObject *obj; // the contained object
48  QElem *prev; // element towards the front of the queue
49  QElem *next; // element towards the back of the queue
50  };
51 
52  public:
57  class SIM_API Comparator
58  {
59  public:
60  virtual ~Comparator() {}
61  virtual Comparator *dup() const = 0;
62  virtual bool less(cObject *a, cObject *b) = 0;
63  };
64 
76  typedef int (*CompareFunc)(cObject *a, cObject *b);
77 
81  class SIM_API Iterator
82  {
83  private:
84  QElem *p;
85 
86  public:
92  Iterator(const cQueue& q, bool reverse=false) { init(q, reverse);}
93 
97  void init(const cQueue& q, bool reverse=false) {p = reverse ? q.backp : q.frontp;}
98 
102  cObject *operator*() const {return p ? p->obj : nullptr;}
103 
107  _OPPDEPRECATED cObject *operator()() const {return operator*();}
108 
112  bool end() const {return p == nullptr;}
113 
119  Iterator& operator++() {if (!end()) p = p->next; return *this;}
120 
126  Iterator operator++(int) {Iterator tmp(*this); if (!end()) p = p->next; return tmp;}
127 
133  Iterator& operator--() {if (!end()) p = p->prev; return *this;}
134 
140  Iterator operator--(int) {Iterator tmp(*this); if (!end()) p = p->prev; return tmp;}
141  };
142 
143  friend class Iterator;
144 
145  private:
146  bool takeOwnership = true;
147  QElem *frontp = nullptr, *backp = nullptr; // front and back pointers
148  int len = 0; // number of items in the queue
149  Comparator *comparator = nullptr; // comparison functor; nullptr for FIFO
150 
151  private:
152  void copy(const cQueue& other);
153 
154  protected:
155  // internal functions
156  QElem *find_qelem(cObject *obj) const;
157  void insbefore_qelem(QElem *p, cObject *obj);
158  void insafter_qelem(QElem *p, cObject *obj);
159  cObject *remove_qelem(QElem *p);
160 
161  public:
168  cQueue(const char *name=nullptr, Comparator *cmp=nullptr);
169 
173  cQueue(const char *name, CompareFunc cmp);
174 
180  cQueue(const cQueue& queue);
181 
185  virtual ~cQueue();
186 
194  cQueue& operator=(const cQueue& queue);
196 
199 
205  virtual cQueue *dup() const override {return new cQueue(*this);}
206 
211  virtual std::string str() const override;
212 
217  virtual void forEachChild(cVisitor *v) override;
218 
224  virtual void parsimPack(cCommBuffer *buffer) const override;
225 
231  virtual void parsimUnpack(cCommBuffer *buffer) override;
233 
240  virtual void setup(Comparator *cmp);
241 
246  virtual void setup(CompareFunc cmp);
247 
252  virtual void insert(cObject *obj);
253 
259  virtual void insertBefore(cObject *where, cObject *obj);
260 
266  virtual void insertAfter(cObject *where, cObject *obj);
267 
272  virtual cObject *remove(cObject *obj);
273 
278  virtual cObject *pop();
279 
284  virtual void clear();
286 
294  virtual cObject *front() const;
295 
301  virtual cObject *back() const;
302 
306  virtual int getLength() const;
307 
311  bool isEmpty() const {return getLength()==0;}
312 
316  _OPPDEPRECATED int length() const {return getLength();}
317 
321  _OPPDEPRECATED bool empty() const {return isEmpty();}
322 
328  virtual cObject *get(int i) const;
329 
333  virtual bool contains(cObject *obj) const;
335 
338 
351  void setTakeOwnership(bool tk) {takeOwnership=tk;}
352 
358  bool getTakeOwnership() const {return takeOwnership;}
360 };
361 
362 // for backward compatibility
363 typedef cQueue::CompareFunc CompareFunc;
364 
365 } // namespace omnetpp
366 
367 
368 #endif
369 
bool isEmpty() const
Definition: cqueue.h:311
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
_OPPDEPRECATED cObject * operator()() const
Definition: cqueue.h:107
Iterator & operator--()
Definition: cqueue.h:133
_OPPDEPRECATED bool empty() const
Definition: cqueue.h:321
bool end() const
Definition: cqueue.h:112
Iterator & operator++()
Definition: cqueue.h:119
Iterator(const cQueue &q, bool reverse=false)
Definition: cqueue.h:92
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
_OPPDEPRECATED int length() const
Definition: cqueue.h:316
Iterator operator++(int)
Definition: cqueue.h:126
int(* CompareFunc)(cObject *a, cObject *b)
Function for comparing two cObjects, used by cQueue for priority queuing.
Definition: cqueue.h:76
void setTakeOwnership(bool tk)
Definition: cqueue.h:351
Queue class for objects derived from cObject.
Definition: cqueue.h:42
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
Walks along a cQueue.
Definition: cqueue.h:81
Definition: cabstracthistogram.h:21
bool getTakeOwnership() const
Definition: cqueue.h:358
virtual cQueue * dup() const override
Definition: cqueue.h:205
Base class for object comparators, used by cQueue for priority queuing.
Definition: cqueue.h:57
cObject * operator*() const
Definition: cqueue.h:102
void init(const cQueue &q, bool reverse=false)
Definition: cqueue.h:97
Iterator operator--(int)
Definition: cqueue.h:140