OMNeT++ Simulation Library  6.0.3
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  bool end() const {return p == nullptr;}
108 
114  Iterator& operator++() {if (!end()) p = p->next; return *this;}
115 
121  Iterator operator++(int) {Iterator tmp(*this); if (!end()) p = p->next; return tmp;}
122 
128  Iterator& operator--() {if (!end()) p = p->prev; return *this;}
129 
135  Iterator operator--(int) {Iterator tmp(*this); if (!end()) p = p->prev; return tmp;}
136  };
137 
138  friend class Iterator;
139 
140  private:
141  bool takeOwnership = true;
142  QElem *frontp = nullptr, *backp = nullptr; // front and back pointers
143  int len = 0; // number of items in the queue
144  Comparator *comparator = nullptr; // comparison functor; nullptr for FIFO
145 
146  private:
147  void copy(const cQueue& other);
148 
149  protected:
150  // internal functions
151  QElem *find_qelem(cObject *obj) const;
152  void insbefore_qelem(QElem *p, cObject *obj);
153  void insafter_qelem(QElem *p, cObject *obj);
154  cObject *remove_qelem(QElem *p);
155 
156  public:
163  cQueue(const char *name=nullptr, Comparator *cmp=nullptr);
164 
168  cQueue(const char *name, CompareFunc cmp);
169 
175  cQueue(const cQueue& queue);
176 
180  virtual ~cQueue();
181 
188  cQueue& operator=(const cQueue& queue);
190 
193 
199  virtual cQueue *dup() const override {return new cQueue(*this);}
200 
205  virtual std::string str() const override;
206 
211  virtual void forEachChild(cVisitor *v) override;
212 
218  virtual void parsimPack(cCommBuffer *buffer) const override;
219 
225  virtual void parsimUnpack(cCommBuffer *buffer) override;
227 
234  virtual void setup(Comparator *cmp);
235 
240  virtual void setup(CompareFunc cmp);
241 
246  virtual void insert(cObject *obj);
247 
253  virtual void insertBefore(cObject *where, cObject *obj);
254 
260  virtual void insertAfter(cObject *where, cObject *obj);
261 
266  virtual cObject *remove(cObject *obj);
267 
272  virtual cObject *pop();
273 
278  virtual void clear();
280 
288  virtual cObject *front() const;
289 
295  virtual cObject *back() const;
296 
300  virtual int getLength() const;
301 
305  bool isEmpty() const {return getLength()==0;}
306 
310  [[deprecated("use getLength() instead")]] int length() const {return getLength();}
311 
315  [[deprecated("use isEmpty() instead")]] bool empty() const {return isEmpty();}
316 
322  virtual cObject *get(int i) const;
323 
327  virtual bool contains(cObject *obj) const;
329 
332 
345  void setTakeOwnership(bool tk) {takeOwnership=tk;}
346 
352  bool getTakeOwnership() const {return takeOwnership;}
354 };
355 
356 // for backward compatibility
357 typedef cQueue::CompareFunc CompareFunc;
358 
359 } // namespace omnetpp
360 
361 
362 #endif
363 
omnetpp::cQueue::length
int length() const
Definition: cqueue.h:310
omnetpp::cQueue::CompareFunc
int(* CompareFunc)(cObject *a, cObject *b)
Function for comparing two cObjects, used by cQueue for priority queuing.
Definition: cqueue.h:76
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cQueue::Iterator::operator--
Iterator operator--(int)
Definition: cqueue.h:135
omnetpp::cQueue::setTakeOwnership
void setTakeOwnership(bool tk)
Definition: cqueue.h:345
omnetpp::cQueue::dup
virtual cQueue * dup() const override
Definition: cqueue.h:199
omnetpp::cQueue::Iterator::operator++
Iterator & operator++()
Definition: cqueue.h:114
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cQueue::empty
bool empty() const
Definition: cqueue.h:315
omnetpp::cQueue::Iterator::operator++
Iterator operator++(int)
Definition: cqueue.h:121
omnetpp::cQueue::Comparator
Base class for object comparators, used by cQueue for priority queuing.
Definition: cqueue.h:57
omnetpp::cQueue::isEmpty
bool isEmpty() const
Definition: cqueue.h:305
omnetpp::cQueue::Iterator::end
bool end() const
Definition: cqueue.h:107
omnetpp::cQueue::Iterator::operator--
Iterator & operator--()
Definition: cqueue.h:128
omnetpp::cQueue::Iterator::operator*
cObject * operator*() const
Definition: cqueue.h:102
omnetpp::cQueue::Iterator::init
void init(const cQueue &q, bool reverse=false)
Definition: cqueue.h:97
omnetpp::cQueue::Iterator
Walks along a cQueue.
Definition: cqueue.h:81
omnetpp::cQueue::getTakeOwnership
bool getTakeOwnership() const
Definition: cqueue.h:352
omnetpp::cQueue
Queue class for objects derived from cObject.
Definition: cqueue.h:42
omnetpp::cCommBuffer
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
omnetpp::cQueue::Iterator::Iterator
Iterator(const cQueue &q, bool reverse=false)
Definition: cqueue.h:92
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