# 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        : (optionally) sorted queue of cObjects
    00009 //    cQueueIterator: walks along a queue
    00010 //
    00011 //==========================================================================
    00012
    00013 /*--------------------------------------------------------------*
    00014   Copyright (C) 1992-2005 Andras Varga
    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 "cobject.h"
    00024
    00025
    00048 class SIM_API cQueue : public cObject
    00049 {
    00050   private:
    00051     struct QElem
    00052     {
    00053         cObject *obj;
    00054         QElem *prev, *next;
    00055     };
    00056
    00057   public:
    00061     class Iterator
    00062     {
    00063       private:
    00064         QElem *p;
    00065
    00066       public:
    00072         Iterator(const cQueue& q, bool athead=true)
    00073                 {p=&q ? (athead ? q.headp : q.tailp) : NULL;}
    00074
    00078         void init(const cQueue& q, bool athead=true)
    00079                 {p=&q ? (athead ? q.headp : q.tailp) : NULL;}
    00080
    00084         cObject& operator[](int)  {return p ? *(p->obj) : *(cObject *)NULL;}
    00085
    00089         cObject *operator()()  {return p ? p->obj : NULL;}
    00090
    00094         bool end() const   {return (bool)(p==NULL);}
    00095
    00101         cObject *operator++(int)  {if (!p) return NULL; cObject *r=p->obj; p=p->next; return r;}
    00102
    00108         cObject *operator--(int)  {if (!p) return NULL; cObject *r=p->obj; p=p->prev; return r;}
    00109     };
    00110
    00111     friend class Iterator;
    00112
    00113   private:
    00114     bool tkownership;
    00115     QElem *headp, *tailp;           // inserting at head, removal at tail
    00116     int n;                          // number of items in queue
    00117     CompareFunc compare;            // compare function
    00118     bool asc;                       // order: true=ascending
    00119
    00120   protected:
    00121     // internal functions
    00122     QElem *find_qelem(cObject *obj) const;
    00123     void insbefore_qelem(QElem *p, cObject *obj);
    00124     void insafter_qelem(QElem *p, cObject *obj);
    00125     cObject *remove_qelem(QElem *p);
    00126
    00127   public:
    00130
    00136     cQueue(const cQueue& queue);
    00137
    00142     explicit cQueue(const char *name=NULL, CompareFunc cmp=NULL, bool a=false);
    00143
    00147     virtual ~cQueue();
    00148
    00156     cQueue& operator=(const cQueue& queue);
    00158
    00161
    00167     virtual cPolymorphic *dup() const  {return new cQueue(*this);}
    00168
    00173     virtual std::string info() const;
    00174
    00179     virtual void forEachChild(cVisitor *v);
    00180
    00186     virtual void netPack(cCommBuffer *buffer);
    00187
    00193     virtual void netUnpack(cCommBuffer *buffer);
    00195
    00198
    00203     virtual void setup(CompareFunc cmp, bool a=false);
    00204
    00209     virtual void insert(cObject *obj);
    00210
    00216     virtual void insertBefore(cObject *where, cObject *obj);
    00217
    00223     virtual void insertAfter(cObject *where, cObject *obj);
    00224
    00229     virtual cObject *remove(cObject *obj);
    00230
    00235     virtual cObject *pop();
    00236
    00241     virtual void clear();
    00243
    00246
    00251     virtual cObject *head() const;
    00252
    00257     virtual cObject *tail() const;
    00258
    00262     virtual int length() const;
    00263
    00267     bool empty() const {return length()==0;}
    00268
    00272     virtual bool contains(cObject *obj) const;
    00274
    00277
    00283     void takeOwnership(bool tk) {tkownership=tk;}
    00284
    00290     bool takeOwnership() const   {return tkownership;}
    00292 };
    00293
    00294 #endif
    00295