clinkedlist.h

00001 //==========================================================================
00002 //  CLINKEDLIST.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //  Declaration of the following classes:
00007 //    cLinkedList        : linked list of pointers (cQueue-like interface)
00008 //
00009 //==========================================================================
00010 
00011 /*--------------------------------------------------------------*
00012   Copyright (C) 1992-2008 Andras Varga
00013   Copyright (C) 2006-2008 OpenSim Ltd.
00014 
00015   This file is distributed WITHOUT ANY WARRANTY. See the file
00016   `license' for details on this and other legal matters.
00017 *--------------------------------------------------------------*/
00018 
00019 #ifndef __CLINKEDLIST_H
00020 #define __CLINKEDLIST_H
00021 
00022 #include "cownedobject.h"
00023 
00024 NAMESPACE_BEGIN
00025 
00026 #ifdef _MSC_VER
00027 #pragma warning(push, 0)  // deprecation warnings inside the header
00028 #endif
00029 
00030 #if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ >= 3
00031 // gcc-4.3.1 is broken: it insists on spitting out deprecation warnings for
00032 // cLinkedList::Iterator when this header gets included anywhere, and
00033 // cannot be convinced otherwise. #pragma GCC diagnostic does not work.
00034 // So we better forget deprecation with that compiler.
00035 #define CLINKEDLIST_DEPRECATED
00036 #else
00037 #define CLINKEDLIST_DEPRECATED  _OPPDEPRECATED
00038 #endif
00039 
00054 class SIM_API CLINKEDLIST_DEPRECATED cLinkedList : public cOwnedObject
00055 {
00056     // a list elem
00057     struct Elem
00058     {
00059         void *item;
00060         Elem *prev, *next;
00061     };
00062 
00063   public:
00064 
00071     class CLINKEDLIST_DEPRECATED Iterator
00072     {
00073       private:
00074         Elem *p;
00075 
00076       public:
00082         Iterator(const cLinkedList& q, bool athead=true)
00083                 {p = athead ? q.headp : q.tailp;}
00084 
00088         void init(const cLinkedList& q, bool athead=true)
00089                 {p = athead ? q.headp : q.tailp;}
00090 
00094         void *operator()() const  {return p->item;}
00095 
00100         bool end() const   {return (bool)(p==NULL);}
00101 
00105         void *operator++(int)  {if (!p) return NULL; Elem *t=p; p=p->next; return t->item;}
00106 
00110         void *operator--(int)  {if (!p) return NULL; Elem *t=p; p=p->prev; return t->item;}
00111     };
00112 
00113     friend class Iterator;
00114 
00115   private:
00116     Elem *headp, *tailp;      // inserting at head, removal at tail
00117     int n;                    // number of items in list
00118 
00119     VoidDelFunc delfunc;      // user func to free up item (NULL-->delete)
00120     VoidDupFunc dupfunc;      // user func to dupl. item (NULL-->new+memcpy)
00121     size_t itemsize;          // used in making shallow copy if dupfunc==0
00122                               // if both dupfunc and itemsize are 0, we do
00123                               // no memory management (we treat pointers as
00124                               // mere pointers)
00125 
00126   private:
00127     void copy(const cLinkedList& other);
00128 
00129   protected:
00130     // internal use.
00131     // if both dupfunc and itemsize are 0, we do no memory management
00132     // (we treat pointers as mere pointers)
00133     Elem *find_llelem(void *item) const;
00134 
00135     // internal use
00136     void insbefore_llelem(Elem *p, void *item);
00137 
00138     // internal use
00139     void insafter_llelem(Elem *p, void *item);
00140 
00141     // internal use
00142     void *remove_llelem(Elem *p);
00143 
00144   public:
00147 
00154     cLinkedList(const cLinkedList& llist);
00155 
00159     explicit cLinkedList(const char *name=NULL);
00160 
00164     virtual ~cLinkedList();
00165 
00173     cLinkedList& operator=(const cLinkedList& queue);
00175 
00178 
00184     virtual cLinkedList *dup() const  {return new cLinkedList(*this);}
00185 
00190     virtual std::string info() const;
00191 
00197     virtual void parsimPack(cCommBuffer *buffer);
00198 
00204     virtual void parsimUnpack(cCommBuffer *buffer);
00206 
00209 
00228     void config( VoidDelFunc _delfunc, VoidDupFunc _dupfunc, size_t _itemsize=0);
00229 
00233     void insert(void *item);
00234 
00239     void insertBefore(void *where, void *item);
00240 
00245     void insertAfter(void *where, void *item);
00246 
00251     void *head() const  {return n!=0 ? headp->item : NULL;}
00252 
00257     void *tail() const  {return n!=0 ? tailp->item : NULL;}
00258 
00263     void *remove(void *item);
00264 
00269     void *pop();
00270 
00274     int getLength() const {return n;}
00275 
00279     bool isEmpty() const {return n==0;}
00280 
00284     int length() const {return getLength();}
00285 
00289     bool empty() const {return isEmpty();}
00290 
00294     bool contains(void *item) const  {return find_llelem(item)!=NULL;}
00295 
00300     void clear();
00302 };
00303 
00304 #ifdef _MSC_VER
00305 #pragma warning(pop)
00306 #endif
00307 
00308 
00309 NAMESPACE_END
00310 
00311 
00312 #endif
00313 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3