ReactOS  0.4.15-dev-3303-g1ade494
list.h
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS HTTP Daemon
4  * FILE: include/list.h
5  */
6 #ifndef __LIST_H
7 #define __LIST_H
8 
9 #include <windows.h>
10 #include <iterator.h>
11 
12 class CListNode {
13 public:
14  CListNode();
16  ~CListNode() {};
17  void* operator new(size_t s);
18  VOID operator delete(void* p);
19 
22  VOID SetPrev(CListNode *prev);
23  PVOID GetElement();
24  CListNode *GetNext();
25  CListNode *GetPrev();
26 private:
30  static HANDLE hHeap;
31  static INT nRef;
32 };
33 
34 template <class Item> class CList {
35 public:
36  //CList(CList&);
37  CList();
38  ~CList();
40 
42  LONG Count() const;
43  Item& Get(const LONG index) const;
44  // Can throw bad_alloc
47  VOID RemoveAll();
48  CListNode *GetHeader() const;
49  CListNode *GetTrailer() const;
50 private:
51  CListNode *Search(Item& element) const;
55 };
56 
57 template <class Item> class CListIterator : public CIterator<Item> {
58 public:
60  virtual VOID First();
61  virtual VOID Next();
62  virtual BOOL IsDone() const;
63  virtual Item CurrentItem() const;
64 private:
65  const CList<Item> *List;
67 };
68 
69 // ****************************** CList ******************************
70 
71 // Default constructor
72 template <class Item>
74 {
75  // Create dummy nodes
76  Trailer = new CListNode;
77  Header = new CListNode;
78  Header->SetNext(Trailer);
79  Trailer->SetPrev(Header);
80 }
81 
82 // Default destructor
83 template <class Item>
85 {
86  RemoveAll();
87  delete Trailer;
88  delete Header;
89 }
90 
91 // Create an iterator for the list
92 template <class Item>
94 {
95  return new CListIterator<Item>((CList<Item> *) this);
96 }
97 
98 // Return number of elements in list
99 template <class Item>
101 {
102  return NodeCount;
103 }
104 
105 // Return element at index
106 template <class Item>
108 {
109  CListNode *node;
110 
111  if ((index < 0) || (index >= NodeCount))
112  return NULL;
113 
114  node = Header;
115  for (int i = 0; i <= index; i++)
116  node = node->GetNext();
117 
118  return (Item *) node->GetElement();
119 }
120 
121 // Insert an element into the list
122 template <class Item>
124 {
125  CListNode *node;
126 
127  node = new CListNode((PVOID)element, Trailer, Trailer->GetPrev());
128  Trailer->GetPrev()->SetNext(node);
129  Trailer->SetPrev(node);
130  NodeCount++;
131 }
132 
133 // Remove an element from the list
134 template <class Item>
136 {
137  CListNode *node;
138 
139  node = Search(element);
140  if (node != NULL) {
141  node->GetPrev()->SetNext(node->GetNext());
142  node->GetNext()->SetPrev(node->GetPrev());
143  NodeCount--;
144  delete node;
145  }
146 }
147 
148 // Remove all elements in list
149 template <class Item>
151 {
152  CListNode *node;
153  CListNode *tmp;
154 
155  node = Header->GetNext();
156  while (node != Trailer) {
157  tmp = node->GetNext();
158  delete node;
159  node = tmp;
160  }
161  Header->SetNext(Trailer);
162  Trailer->SetPrev(Header);
163  NodeCount = 0;
164 }
165 
166 // Return header node
167 template <class Item>
169 {
170  return Header;
171 }
172 
173 // Return trailer node
174 template <class Item>
176 {
177  return Trailer;
178 }
179 
180 // Searches for a node that contains the element. Returns NULL if element is not found
181 template <class Item>
183 {
184  CListNode *node;
185 
186  node = Header;
187  while (((node = node->GetNext()) != Trailer) && (node->GetElement() != element));
188  if (node != Trailer)
189  return node;
190  else
191  return NULL;
192 }
193 
194 
195 // ************************** CListIterator **************************
196 
197 // Default constructor
198 template <class Item>
200 {
201  First();
202 }
203 
204 // Go to first element in list
205 template <class Item>
207 {
208  Current = List->GetHeader()->GetNext();
209 }
210 
211 // Go to next element in list
212 template <class Item>
214 {
215  if (!IsDone())
216  Current = Current->GetNext();
217 }
218 
219 // Return FALSE when there are more elements in list and TRUE when there are no more
220 template <class Item>
222 {
223  return (Current == List->GetTrailer());
224 }
225 
226 // Return current element
227 template <class Item>
229 {
230  return IsDone()? NULL : (Item) Current->GetElement();
231 }
232 
233 #endif /* __LIST_H */
Item & Get(const LONG index) const
Definition: list.h:107
virtual VOID First()
Definition: list.h:206
CListNode()
Definition: list.cpp:22
CIterator< Item > * CreateIterator() const
Definition: list.h:93
CListNode * GetHeader() const
Definition: list.h:168
CList()
Definition: list.h:73
~CListNode()
Definition: list.h:16
static HANDLE hHeap
Definition: list.h:30
static INT nRef
Definition: list.h:31
int32_t INT
Definition: typedefs.h:58
struct node node
Definition: Header.h:8
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
CListNode * GetPrev()
Definition: list.cpp:91
GLuint index
Definition: glext.h:6031
VOID SetElement(PVOID element)
Definition: list.cpp:61
CListNode * Header
Definition: list.h:53
virtual Item CurrentItem() const
Definition: list.h:228
CListIterator(const CList< Item > *list)
Definition: list.h:199
virtual BOOL IsDone() const
Definition: list.h:221
static const WCHAR RemoveAll[]
Definition: msiexec.c:48
virtual VOID Next()
Definition: list.h:213
LONG NodeCount
Definition: list.h:52
LONG Count() const
Definition: list.h:100
CListNode * Trailer
Definition: list.h:54
#define index(s, c)
Definition: various.h:29
CListNode * GetNext()
Definition: list.cpp:85
_Must_inspect_result_ _In_ WDFCMRESLIST List
Definition: wdfresource.h:550
GLdouble s
Definition: gl.h:2039
Definition: _list.h:228
CListNode * GetTrailer() const
Definition: list.h:175
PVOID GetElement()
Definition: list.cpp:79
PVOID Element
Definition: list.h:27
CListNode * Next
Definition: list.h:28
static unsigned __int64 next
Definition: rand_nt.c:6
VOID Insert(Item &element)
Definition: list.h:123
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
Definition: list.h:34
CListNode * Prev
Definition: list.h:29
CList & operator=(CList &)
#define NULL
Definition: types.h:112
VOID SetNext(CListNode *next)
Definition: list.cpp:67
_In_ WDFCOLLECTION _In_ WDFOBJECT Item
Definition: list.h:12
~CList()
Definition: list.h:84
CListNode * Search(Item &element) const
Definition: list.h:182
CListNode * Current
Definition: list.h:66
const CList< Item > * List
Definition: list.h:65
VOID Remove(Item &element)
Definition: list.h:135
GLfloat GLfloat p
Definition: glext.h:8902
VOID SetPrev(CListNode *prev)
Definition: list.cpp:73
VOID RemoveAll()
Definition: list.h:150
Definition: dlist.c:348