xrootd
XrdOucDLlist.hh
Go to the documentation of this file.
1 #ifndef __OUC_DLIST__
2 #define __OUC_DLIST__
3 /******************************************************************************/
4 /* */
5 /* X r d O u c D L l i s t . h h */
6 /* */
7 /*(c) 2003 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /*Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC03-76-SFO0515 with the Deprtment of Energy */
11 /******************************************************************************/
12 
13 // $Id$
14 
15 template<class T>
17 {
18 public:
19 
20  XrdOucDLlist(T *itemval=0) {prev=this; next=this; item=itemval;}
21  ~XrdOucDLlist() {if (prev != next) Remove();}
22 
23 // Apply() applies the specified function to every item in the list. Apply()
24 // is pointer-safe in that the current node pointers may be changed
25 // without affecting the traversal of the list. An argument may be
26 // passed to the function. A null pointer is returned if the list
27 // was completely traversed. Otherwise, the pointer to the node on
28 // which the applied function returned a non-zero value is returned.
29 // An optional starting point may be passed.
30 //
31 T *Apply(int (*func)(T *, void *), void *Arg, XrdOucDLlist *Start=0)
32  {XrdOucDLlist *nextnode, *node;
33  if (Start) node = Start; // Set correct starting point
34  else node = this;
35 
36  // Iterate through the list until we hit ourselves again. We do the
37  // loop once on the current node to allow for anchorless lists.
38  //
39  do {nextnode = node->next;
40  if (node->item && (*func)(node->item, Arg)) return node->item;
41  node = nextnode;
42  } while (node != this);
43 
44  // All done, indicate we went through the whole list
45  //
46  return (T *)0;
47  }
48 
49 // Insert() inserts the specified node immediately off itself. If an item value
50 // is not given, it is not changed.
51 //
52 void Insert(XrdOucDLlist *Node, T *Item=0)
53  {Node->next = next; // Chain in the item;
54  next->prev = Node;
55  next = Node;
56  Node->prev = this;
57  if (Item) Node->item = Item;
58  }
59 
60 // Item() supplies the item value associated with itself (used with Next()).
61 //
62 T *Item() {return item;}
63 
64 // Remove() removes itself from whatever list it happens to be in.
65 //
66 void Remove()
67  {prev->next = next; // Unchain the item
68  next->prev = prev;
69  next = this;
70  prev = this;
71  }
72 
73 // Next() supplies the next list node.
74 //
75 XrdOucDLlist *Next() {return next;}
76 
77 // Prev() supplies the prev list node.
78 //
79 XrdOucDLlist *Prev() {return prev;}
80 
81 // Set the item pointer
82 //
83 void setItem(T *ival) {item = ival;}
84 
85 // Singleton() indicates whether or not the node points to something
86 //
87 int Singleton() {return next == this;}
88 
89 private:
92 T *item;
93 };
94 #endif