cqueue.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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;
00062 QElem *prev;
00063 QElem *next;
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;
00121 QElem *frontp, *backp;
00122 int n;
00123 CompareFunc compare;
00124
00125 private:
00126 void copy(const cQueue& other);
00127
00128 protected:
00129
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