cqueue.h

00001 //==========================================================================
00002 //  CQUEUE.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Declaration of the following classes:
00008 //    cQueue : queue of cObject descendants
00009 //
00010 //==========================================================================
00011 
00012 /*--------------------------------------------------------------*
00013   Copyright (C) 1992-2008 Andras Varga
00014   Copyright (C) 2006-2008 OpenSim Ltd.
00015 
00016   This file is distributed WITHOUT ANY WARRANTY. See the file
00017   `license' for details on this and other legal matters.
00018 *--------------------------------------------------------------*/
00019 
00020 #ifndef __CQUEUE_H
00021 #define __CQUEUE_H
00022 
00023 #include "cownedobject.h"
00024 
00025 NAMESPACE_BEGIN
00026 
00027 
00036 typedef int (*CompareFunc)(cObject *a, cObject *b);
00037 
00038 
00056 class SIM_API cQueue : public cOwnedObject
00057 {
00058   private:
00059     struct QElem
00060     {
00061         cObject *obj; // the contained object
00062         QElem *prev;  // element towards the front of the queue
00063         QElem *next;  // element towards the back of the queue
00064     };
00065 
00066   public:
00070     class Iterator
00071     {
00072       private:
00073         QElem *p;
00074 
00075       public:
00081         Iterator(const cQueue& q, bool reverse=false)
00082                 {p = reverse ? q.backp : q.frontp;}
00083 
00087         void init(const cQueue& q, bool reverse=false)
00088                 {p = reverse ? q.backp : q.frontp;}
00089 
00093         cObject *operator()()  {return p ? p->obj : NULL;}
00094 
00098         bool end() const   {return (bool)(p==NULL);}
00099 
00106         cObject *operator++(int)  {if (!p) return NULL; cObject *r=p->obj; p=p->next; return r;}
00107 
00114         cObject *operator--(int)  {if (!p) return NULL; cObject *r=p->obj; p=p->prev; return r;}
00115     };
00116 
00117     friend class Iterator;
00118 
00119   private:
00120     bool tkownership; //FIXME move it info flags
00121     QElem *frontp, *backp;  // inserting at back(), removal at front()
00122     int n;  // number of items in the queue
00123     CompareFunc compare;   // comparison function; NULL for FIFO
00124 
00125   private:
00126     void copy(const cQueue& other);
00127 
00128   protected:
00129     // internal functions
00130     QElem *find_qelem(cObject *obj) const;
00131     void insbefore_qelem(QElem *p, cObject *obj);
00132     void insafter_qelem(QElem *p, cObject *obj);
00133     cObject *remove_qelem(QElem *p);
00134 
00135   public:
00142     cQueue(const char *name=NULL, CompareFunc cmp=NULL);
00143 
00149     cQueue(const cQueue& queue);
00150 
00154     virtual ~cQueue();
00155 
00163     cQueue& operator=(const cQueue& queue);
00165 
00168 
00174     virtual cQueue *dup() const  {return new cQueue(*this);}
00175 
00180     virtual std::string info() const;
00181 
00186     virtual void forEachChild(cVisitor *v);
00187 
00193     virtual void parsimPack(cCommBuffer *buffer);
00194 
00200     virtual void parsimUnpack(cCommBuffer *buffer);
00202 
00209     virtual void setup(CompareFunc cmp);
00210 
00215     virtual void insert(cObject *obj);
00216 
00222     virtual void insertBefore(cObject *where, cObject *obj);
00223 
00229     virtual void insertAfter(cObject *where, cObject *obj);
00230 
00235     virtual cObject *remove(cObject *obj);
00236 
00241     virtual cObject *pop();
00242 
00247     virtual void clear();
00249 
00257     virtual cObject *front() const;
00258 
00264     virtual cObject *back() const;
00265 
00269     virtual int getLength() const;
00270 
00274     bool isEmpty() const {return getLength()==0;}
00275 
00279     int length() const {return getLength();}
00280 
00284     bool empty() const {return isEmpty();}
00285 
00291     cObject *get(int i) const;
00292 
00296     virtual bool contains(cObject *obj) const;
00298 
00301 
00314     void setTakeOwnership(bool tk) {tkownership=tk;}
00315 
00321     bool getTakeOwnership() const {return tkownership;}
00323 };
00324 
00325 NAMESPACE_END
00326 
00327 
00328 #endif
00329 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3