OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
ulist.h
1/*
2===========================================================================
3Copyright (C) 2024 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23#pragma once
24
25template< typename type >
26class UListItem {
27public:
28 type item;
29 class UListItem< type > *next;
30 class UListItem< type > *prev;
31
32 UListItem();
33};
34
35template< typename type >
36class UList {
37 UListItem<type> m_top;
38 UListItem<type> *m_at;
39 int m_count;
40
41public:
42 UList();
43 ~UList();
44
45 void AddHead( type& head );
46 void AddTail( const type& tail );
47 bool IterateFromHead( void );
48 bool IterateFromTail( void );
49 bool IterateNext( void );
50 bool IteratePrev( void );
51 type& getCurrent( void );
52 void RemoveCurrentSetNext( void );
53 void RemoveCurrentSetPrev( void );
54 bool IsCurrentValid( void );
55 bool IsCurrentHead( void );
56 bool IsCurrentTail( void );
57 void InsertBeforeCurrent( type& which );
58 void InsertAfterCurrent( type& which );
59 int getCount( void );
60 void RemoveAllItems( void );
61 void *getPosition( void );
62 void setPosition( void *pos );
63};
64
65template< typename type >
66UListItem< type >::UListItem()
67{
68 next = prev = NULL;
69}
70
71template< typename type >
72UList< type >::UList()
73{
74 m_at = NULL;
75 m_count = 0;
76 m_top.next = &m_top;
77 m_top.prev = &m_top;
78}
79
80template< typename type >
81UList< type >::~UList()
82{
83 RemoveAllItems();
84}
85
86template< typename type >
87void UList< type >::AddHead
88 (
89 type& head
90 )
91
92{
93 // FIXME: stub
94}
95
96template< typename type >
97void UList< type >::AddTail
98 (
99 const type& tail
100 )
101
102{
103 UListItem<type> *item = new UListItem <type>;
104
105 item->item = tail;
106 item->next = &m_top;
107 item->prev = m_top.prev;
108 m_top.prev->next = item;
109 m_top.prev = item;
110
111 m_count++;
112}
113
114template< typename type >
115bool UList< type >::IterateFromHead
116 (
117 void
118 )
119
120{
121 if( m_top.next == &m_top )
122 {
123 m_at = NULL;
124 return false;
125 }
126 else
127 {
128 m_at = m_top.next;
129 return true;
130 }
131}
132
133template< typename type >
134bool UList< type >::IterateFromTail
135 (
136 void
137 )
138
139{
140 if (m_top.prev == &m_top)
141 {
142 m_at = NULL;
143 return false;
144 }
145 else
146 {
147 m_at = m_top.prev;
148 return true;
149 }
150}
151
152template< typename type >
153bool UList< type >::IterateNext
154 (
155 void
156 )
157
158{
159 m_at = m_at->next;
160
161 if( m_at == &m_top )
162 {
163 m_at = NULL;
164 return false;
165 }
166 else
167 {
168 return true;
169 }
170}
171
172template< typename type >
173bool UList< type >::IteratePrev
174 (
175 void
176 )
177
178{
179 m_at = m_at->prev;
180
181 if( m_at == &m_top )
182 {
183 m_at = NULL;
184 return false;
185 }
186 else
187 {
188 return true;
189 }
190}
191
192template< typename type >
193type& UList< type >::getCurrent
194 (
195 void
196 )
197
198{
199 return m_at->item;
200}
201
202template< typename type >
203void UList< type >::RemoveCurrentSetNext
204 (
205 void
206 )
207
208{
210
211 item = m_at;
212 IterateNext();
213
214 item->next->prev = item->prev;
215 item->prev->next = item->next;
216
217 delete item;
218
219 m_count--;
220}
221
222template< typename type >
223void UList< type >::RemoveCurrentSetPrev
224 (
225 void
226 )
227
228{
230
231 item = m_at;
232 IteratePrev();
233
234 item->next->prev = item->prev;
235 item->prev->next = item->next;
236
237 delete item;
238
239 m_count--;
240}
241
242template< typename type >
243bool UList< type >::IsCurrentValid
244 (
245 void
246 )
247
248{
249 return m_at != NULL;
250}
251
252template< typename type >
253bool UList< type >::IsCurrentHead
254 (
255 void
256 )
257
258{
259 return m_at == &m_top;
260}
261
262template< typename type >
263bool UList< type >::IsCurrentTail
264 (
265 void
266 )
267
268{
269 // FIXME: stub
270 return false;
271}
272
273template< typename type >
274void UList< type >::InsertBeforeCurrent
275 (
276 type& which
277 )
278
279{
280 // FIXME: stub
281}
282
283template< typename type >
284void UList< type >::InsertAfterCurrent
285 (
286 type& which
287 )
288
289{
290 UListItem<type> *item = new UListItem <type>;
291
292 item->item = which;
293 item->next = m_at->next;
294 item->prev = m_at;
295 m_at->next->prev = item;
296 item->prev->next = item;
297
298 m_count++;
299}
300
301template< typename type >
302int UList< type >::getCount
303 (
304 void
305 )
306
307{
308 return m_count;
309}
310
311template< typename type >
312void UList< type >::RemoveAllItems
313 (
314 void
315 )
316
317{
319 UListItem<type> *next;
320
321 for( item = m_top.next; item && item != &m_top; item = next )
322 {
323 next = item->next;
324 delete item;
325 }
326
327 m_top.next = &m_top;
328 m_top.prev = &m_top;
329 m_count = 0;
330}
331
332template< typename type >
333void *UList< type >::getPosition
334 (
335 void
336 )
337
338{
339 return (void*)m_at;
340}
341
342template< typename type >
343void UList< type >::setPosition
344 (
345 void *pos
346 )
347
348{
349 m_at = static_cast<UListItem<type>*>(pos);
350}
Definition ulist.h:26
Definition uiconsole.h:27