style cleanup
[blender.git] / source / gameengine / SceneGraph / SG_QList.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file SG_QList.h
29  *  \ingroup bgesg
30  */
31  
32 #ifndef __SG_QLIST_H__
33 #define __SG_QLIST_H__
34
35 #include "SG_DList.h"
36
37 /**
38  * Double-Double circular linked list
39  * For storing an object is two lists simultaneously
40  */
41 class SG_QList : public SG_DList
42 {
43 protected :
44         SG_QList* m_fqlink;
45         SG_QList* m_bqlink;
46
47 public:
48         template<typename T> class iterator
49         {
50         private:
51                 SG_QList&       m_head;
52                 T*                      m_current;
53         public:
54                 typedef iterator<T> _myT;
55                 iterator(SG_QList& head, SG_QList* current=NULL) : m_head(head) { m_current = (T*)current; }
56                 ~iterator() {}
57
58                 void begin()
59                 {
60                         m_current = (T*)m_head.QPeek();
61                 }
62                 void back()
63                 {
64                         m_current = (T*)m_head.QBack();
65                 }
66                 bool end()
67                 {
68                         return (m_current == (T*)m_head.Self());
69                 }
70                 bool add_back(T* item)
71                 {
72                         return m_current->QAddBack(item);
73                 }
74                 T* operator*()
75                 {
76                         return m_current;
77                 }
78                 _myT& operator++()
79                 {
80                         m_current = (T*)m_current->QPeek();
81                         return *this;
82                 }
83                 _myT& operator--()
84                 {
85                         // no check on NULL! make sure you don't try to increment beyond end
86                         m_current = (T*)m_current->QBack();
87                         return *this;
88                 }
89         };
90
91         SG_QList() : SG_DList()
92         {
93                 m_fqlink = m_bqlink = this;
94         }
95         SG_QList(const SG_QList& other) : SG_DList()
96         {
97                 m_fqlink = m_bqlink = this;
98         }
99         virtual ~SG_QList()
100         {
101                 QDelink();
102         }
103
104         inline bool QEmpty()               // Check for empty queue
105         {
106                 return ( m_fqlink == this );
107         }
108         bool QAddBack( SG_QList *item )  // Add to the back
109         {
110                 if (!item->QEmpty())
111                         return false;
112                 item->m_bqlink = m_bqlink;
113                 item->m_fqlink = this;
114                 m_bqlink->m_fqlink = item;
115                 m_bqlink = item;
116                 return true;
117         }
118         bool QAddFront( SG_QList *item )  // Add to the back
119         {
120                 if (!item->Empty())
121                         return false;
122                 item->m_fqlink = m_fqlink;
123                 item->m_bqlink = this;
124                 m_fqlink->m_bqlink = item;
125                 m_fqlink = item;
126                 return true;
127         }
128         SG_QList *QRemove()           // Remove from the front
129         {
130                 if (QEmpty())
131                 {
132                         return NULL;
133                 }
134                 SG_QList* item = m_fqlink;
135                 m_fqlink = item->m_fqlink;
136                 m_fqlink->m_bqlink = this;
137                 item->m_fqlink = item->m_bqlink = item;
138                 return item;
139         }
140         bool QDelink()             // Remove from the middle
141         {
142                 if (QEmpty())
143                         return false;
144                 m_bqlink->m_fqlink = m_fqlink;
145                 m_fqlink->m_bqlink = m_bqlink;
146                 m_fqlink = m_bqlink = this;
147                 return true;
148         }
149         inline SG_QList *QPeek()                        // Look at front without removing
150         {
151                 return m_fqlink;
152         }
153         inline SG_QList *QBack()                        // Look at front without removing
154         {
155                 return m_bqlink;
156         }
157
158
159 #ifdef WITH_CXX_GUARDEDALLOC
160 public:
161         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:SG_QList"); }
162         void operator delete( void *mem ) { MEM_freeN(mem); }
163 #endif
164 };
165
166 #endif //__SG_QLIST_H__
167