4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
39 #ifdef WITH_CXX_GUARDEDALLOC
40 #include "MEM_guardedalloc.h"
44 * Double circular linked list
53 template<typename T> class iterator
59 typedef iterator<T> _myT;
60 iterator(SG_DList& head) : m_head(head), m_current(NULL) {}
65 m_current = (T*)m_head.Peek();
69 m_current = (T*)m_head.Back();
73 return (m_current == (T*)m_head.Self());
75 bool add_back(T* item)
77 return m_current->AddBack(item);
85 // no check of NULL! make sure you don't try to increment beyond end
86 m_current = (T*)m_current->Peek();
91 // no check of NULL! make sure you don't try to increment beyond end
92 m_current = (T*)m_current->Back();
97 template<typename T> class const_iterator
100 const SG_DList& m_head;
103 typedef const_iterator<T> _myT;
104 const_iterator(const SG_DList& head) : m_head(head), m_current(NULL) {}
109 m_current = (const T*)m_head.Peek();
113 m_current = (const T*)m_head.Back();
117 return (m_current == (const T*)m_head.Self());
125 // no check of NULL! make sure you don't try to increment beyond end
126 m_current = (const T*)m_current->Peek();
131 // no check of NULL! make sure you don't try to increment beyond end
132 m_current = (const T*)m_current->Back();
139 m_flink = m_blink = this;
141 SG_DList(const SG_DList& other)
143 m_flink = m_blink = this;
150 inline bool Empty() // Check for empty queue
152 return ( m_flink == this );
154 bool AddBack( SG_DList *item ) // Add to the back
158 item->m_blink = m_blink;
159 item->m_flink = this;
160 m_blink->m_flink = item;
164 bool AddFront( SG_DList *item ) // Add to the back
168 item->m_flink = m_flink;
169 item->m_blink = this;
170 m_flink->m_blink = item;
174 SG_DList *Remove() // Remove from the front
180 SG_DList* item = m_flink;
181 m_flink = item->m_flink;
182 m_flink->m_blink = this;
183 item->m_flink = item->m_blink = item;
186 bool Delink() // Remove from the middle
190 m_blink->m_flink = m_flink;
191 m_flink->m_blink = m_blink;
192 m_flink = m_blink = this;
195 inline SG_DList *Peek() // Look at front without removing
199 inline SG_DList *Back() // Look at front without removing
203 inline SG_DList *Self()
207 inline const SG_DList *Peek() const // Look at front without removing
209 return (const SG_DList*)m_flink;
211 inline const SG_DList *Back() const // Look at front without removing
213 return (const SG_DList*)m_blink;
215 inline const SG_DList *Self() const
221 #ifdef WITH_CXX_GUARDEDALLOC
223 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:SG_DList"); }
224 void operator delete( void *mem ) { MEM_freeN(mem); }
229 * SG_DListHead : Template class that implements copy constructor to duplicate list automatically
230 * The elements of the list must have themselves a copy constructor.
232 template<typename T> class SG_DListHead : public SG_DList
235 typedef SG_DListHead<T> _myT;
236 SG_DListHead() : SG_DList() {}
237 SG_DListHead(const _myT& other) : SG_DList()
239 // copy the list, assuming objects of type T
240 const_iterator<T> eit(other);
242 for (eit.begin(); !eit.end(); ++eit) {
243 elem = (*eit)->GetReplica();
247 virtual ~SG_DListHead() {}
250 return static_cast<T*>(SG_DList::Remove());