00001 #ifndef __OUC_DLIST__ 00002 #define __OUC_DLIST__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d O u c D L l i s t . h h */ 00006 /* */ 00007 /*(c) 2003 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /*Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 00031 /******************************************************************************/ 00032 00033 00034 template<class T> 00035 class XrdOucDLlist 00036 { 00037 public: 00038 00039 XrdOucDLlist(T *itemval=0) {prev=this; next=this; item=itemval;} 00040 ~XrdOucDLlist() {if (prev != next) Remove();} 00041 00042 // Apply() applies the specified function to every item in the list. Apply() 00043 // is pointer-safe in that the current node pointers may be changed 00044 // without affecting the traversal of the list. An argument may be 00045 // passed to the function. A null pointer is returned if the list 00046 // was completely traversed. Otherwise, the pointer to the node on 00047 // which the applied function returned a non-zero value is returned. 00048 // An optional starting point may be passed. 00049 // 00050 T *Apply(int (*func)(T *, void *), void *Arg, XrdOucDLlist *Start=0) 00051 {XrdOucDLlist *nextnode, *node; 00052 if (Start) node = Start; // Set correct starting point 00053 else node = this; 00054 00055 // Iterate through the list until we hit ourselves again. We do the 00056 // loop once on the current node to allow for anchorless lists. 00057 // 00058 do {nextnode = node->next; 00059 if (node->item && (*func)(node->item, Arg)) return node->item; 00060 node = nextnode; 00061 } while (node != this); 00062 00063 // All done, indicate we went through the whole list 00064 // 00065 return (T *)0; 00066 } 00067 00068 // Insert() inserts the specified node immediately off itself. If an item value 00069 // is not given, it is not changed. 00070 // 00071 void Insert(XrdOucDLlist *Node, T *Item=0) 00072 {Node->next = next; // Chain in the item; 00073 next->prev = Node; 00074 next = Node; 00075 Node->prev = this; 00076 if (Item) Node->item = Item; 00077 } 00078 00079 // Item() supplies the item value associated with itself (used with Next()). 00080 // 00081 T *Item() {return item;} 00082 00083 // Remove() removes itself from whatever list it happens to be in. 00084 // 00085 void Remove() 00086 {prev->next = next; // Unchain the item 00087 next->prev = prev; 00088 next = this; 00089 prev = this; 00090 } 00091 00092 // Next() supplies the next list node. 00093 // 00094 XrdOucDLlist *Next() {return next;} 00095 00096 // Prev() supplies the prev list node. 00097 // 00098 XrdOucDLlist *Prev() {return prev;} 00099 00100 // Set the item pointer 00101 // 00102 void setItem(T *ival) {item = ival;} 00103 00104 // Singleton() indicates whether or not the node points to something 00105 // 00106 int Singleton() {return next == this;} 00107 00108 private: 00109 XrdOucDLlist *next; 00110 XrdOucDLlist *prev; 00111 T *item; 00112 }; 00113 #endif