OpenMoHAA 0.82.1
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
27{
28public:
29 type item;
30 class UListItem<type> *next;
31 class UListItem<type> *prev;
32
33 UListItem();
34};
35
36template<typename type>
37class UList
38{
39 UListItem<type> m_top;
40 UListItem<type> *m_at;
41 int m_count;
42
43public:
44 UList();
45 ~UList();
46
47 void AddHead(type& head);
48 void AddTail(const type& tail);
49 bool IterateFromHead(void);
50 bool IterateFromTail(void);
51 bool IterateNext(void);
52 bool IteratePrev(void);
53 type& getCurrent(void);
54 void RemoveCurrentSetNext(void);
55 void RemoveCurrentSetPrev(void);
56 bool IsCurrentValid(void);
57 bool IsCurrentHead(void);
58 bool IsCurrentTail(void);
59 void InsertBeforeCurrent(type& which);
60 void InsertAfterCurrent(type& which);
61 int getCount(void);
62 void RemoveAllItems(void);
63 void *getPosition(void);
64 void setPosition(void *pos);
65};
66
67template<typename type>
68UListItem<type>::UListItem()
69{
70 next = prev = NULL;
71}
72
73template<typename type>
74UList<type>::UList()
75{
76 m_at = NULL;
77 m_count = 0;
78 m_top.next = &m_top;
79 m_top.prev = &m_top;
80}
81
82template<typename type>
83UList<type>::~UList()
84{
85 RemoveAllItems();
86}
87
88template<typename type>
89void UList<type>::AddHead(type& head)
90{
91 // FIXME: stub
92}
93
94template<typename type>
95void UList<type>::AddTail(const type& tail)
96{
98
99 item->item = tail;
100 item->next = &m_top;
101 item->prev = m_top.prev;
102 m_top.prev->next = item;
103 m_top.prev = item;
104
105 m_count++;
106}
107
108template<typename type>
109bool UList<type>::IterateFromHead(void)
110{
111 if (m_top.next == &m_top) {
112 m_at = NULL;
113 return false;
114 } else {
115 m_at = m_top.next;
116 return true;
117 }
118}
119
120template<typename type>
121bool UList<type>::IterateFromTail(void)
122{
123 if (m_top.prev == &m_top) {
124 m_at = NULL;
125 return false;
126 } else {
127 m_at = m_top.prev;
128 return true;
129 }
130}
131
132template<typename type>
133bool UList<type>::IterateNext(void)
134{
135 m_at = m_at->next;
136
137 if (m_at == &m_top) {
138 m_at = NULL;
139 return false;
140 } else {
141 return true;
142 }
143}
144
145template<typename type>
146bool UList<type>::IteratePrev(void)
147{
148 m_at = m_at->prev;
149
150 if (m_at == &m_top) {
151 m_at = NULL;
152 return false;
153 } else {
154 return true;
155 }
156}
157
158template<typename type>
159type& UList<type>::getCurrent(void)
160{
161 return m_at->item;
162}
163
164template<typename type>
165void UList<type>::RemoveCurrentSetNext(void)
166{
168
169 item = m_at;
170 IterateNext();
171
172 item->next->prev = item->prev;
173 item->prev->next = item->next;
174
175 delete item;
176
177 m_count--;
178}
179
180template<typename type>
181void UList<type>::RemoveCurrentSetPrev(void)
182{
184
185 item = m_at;
186 IteratePrev();
187
188 item->next->prev = item->prev;
189 item->prev->next = item->next;
190
191 delete item;
192
193 m_count--;
194}
195
196template<typename type>
197bool UList<type>::IsCurrentValid(void)
198{
199 return m_at != NULL;
200}
201
202template<typename type>
203bool UList<type>::IsCurrentHead(void)
204{
205 return m_at == &m_top;
206}
207
208template<typename type>
209bool UList<type>::IsCurrentTail(void)
210{
211 // FIXME: stub
212 return false;
213}
214
215template<typename type>
216void UList<type>::InsertBeforeCurrent(type& which)
217{
218 // FIXME: stub
219}
220
221template<typename type>
222void UList<type>::InsertAfterCurrent(type& which)
223{
225
226 item->item = which;
227 item->next = m_at->next;
228 item->prev = m_at;
229 m_at->next->prev = item;
230 item->prev->next = item;
231
232 m_count++;
233}
234
235template<typename type>
236int UList<type>::getCount(void)
237{
238 return m_count;
239}
240
241template<typename type>
242void UList<type>::RemoveAllItems(void)
243{
245 UListItem<type> *next;
246
247 for (item = m_top.next; item && item != &m_top; item = next) {
248 next = item->next;
249 delete item;
250 }
251
252 m_top.next = &m_top;
253 m_top.prev = &m_top;
254 m_count = 0;
255}
256
257template<typename type>
258void *UList<type>::getPosition(void)
259{
260 return (void *)m_at;
261}
262
263template<typename type>
264void UList<type>::setPosition(void *pos)
265{
266 m_at = static_cast<UListItem<type> *>(pos);
267}
Definition ulist.h:27
Definition uiconsole.h:28