Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members  

cqueue.h

00001 //==========================================================================
00002 //   CQUEUE.H  - header for
00003 //                             OMNeT++
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-2001 Andras Varga
00015   Technical University of Budapest, Dept. of Telecommunications,
00016   Stoczek u.2, H-1111 Budapest, Hungary.
00017 
00018   This file is distributed WITHOUT ANY WARRANTY. See the file
00019   `license' for details on this and other legal matters.
00020 *--------------------------------------------------------------*/
00021 
00022 #ifndef __CQUEUE_H
00023 #define __CQUEUE_H
00024 
00025 #include "cobject.h"
00026 
00027 //=== classes declared here
00028 class  cQueue;
00029 class  cQueueIterator;
00030 
00031 //==========================================================================
00032 
00033 //
00034 // Used internally by cQueue and cQueueIterator.
00035 //
00036 struct sQElem
00037 {
00038     cObject *obj;
00039     sQElem *prev, *next;
00040 };
00041 
00042 //-------------------------------------------------------------------------
00043 
00065 class SIM_API cQueue : public cObject
00066 {
00067     friend class cQueueIterator;
00068   private:
00069     sQElem *headp, *tailp;          // inserting at head, removal at tail
00070     int n;                          // number of items in queue
00071     CompareFunc compare;            // compare function
00072     bool asc;                       // order: true=ascending
00073 
00074   protected:
00075     // internal functions
00076     sQElem *find_qelem(cObject *obj) const;
00077     void insbefore_qelem(sQElem *p, cObject *obj);
00078     void insafter_qelem(sQElem *p, cObject *obj);
00079     cObject *remove_qelem(sQElem *p);
00080 
00081   public:
00084 
00090     cQueue(const cQueue& queue);
00091 
00096     explicit cQueue(const char *name=NULL, CompareFunc cmp=NULL, bool a=false);
00097 
00101     virtual ~cQueue();
00102 
00110     cQueue& operator=(const cQueue& queue);
00112 
00115 
00119     virtual const char *className() const {return "cQueue";}
00120 
00126     virtual cObject *dup() const  {return new cQueue(*this);}
00127 
00132     virtual void info(char *buf);
00133 
00138     virtual const char *inspectorFactoryName() const {return "cQueueIFC";}
00139 
00144     virtual void forEach(ForeachFunc f);
00145 
00151     virtual int netPack();
00152 
00158     virtual int netUnpack();
00160 
00163 
00168     virtual void setup(CompareFunc cmp, bool a=false);
00169 
00174     virtual void insert(cObject *obj);
00175 
00181     virtual void insertBefore(cObject *where, cObject *obj);
00182 
00188     virtual void insertAfter(cObject *where, cObject *obj);
00189 
00194     virtual cObject *remove(cObject *obj);
00195 
00200     virtual cObject *pop();
00201 
00206     virtual void clear();
00208 
00211 
00216     virtual cObject *head() const;
00217 
00222     virtual cObject *tail() const;
00223 
00227     virtual int length() const;
00228 
00232     bool empty() const {return length()==0;}
00233 
00237     virtual bool contains(cObject *obj) const;
00239 };
00240 
00241 //==========================================================================
00242 
00248 class SIM_API cQueueIterator
00249 {
00250   private:
00251     sQElem *p;
00252 
00253   public:
00259     cQueueIterator(const cQueue& q, int athead=1)  //FIXME: make bool!
00260             {p=&q ? (athead ? q.headp : q.tailp) : NO(sQElem);}
00261 
00265     void init(const cQueue& q, int athead=1)
00266             {p=&q ? (athead ? q.headp : q.tailp) : NO(sQElem);}
00267 
00271     cObject& operator[](int)  {return *p->obj;}
00272 
00276     cObject *operator()()     {return p->obj;}
00277 
00281     bool end() const                {return (bool)(p==NULL);}
00282 
00288     cObject *operator++(int)  {sQElem *t=p; if(p) p=p->next; return t->obj;} // FIXME: fails if p=NULL!!!
00289 
00295     cObject *operator--(int)  {sQElem *t=p; if(p) p=p->prev; return t->obj;} // FIXME: fails if p=NULL!!!
00296 };
00297 
00298 #endif
00299 

Generated at Sat May 4 15:45:48 2002 for OMNeT++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001