Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
SingleList.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 ****************************************************************************
9 * HEADER
10 *
11 * $Id: SingleList.h 5853 2026-08-17 09:48:31Z gianluca $
12 *
13 * COPYRIGHT: Real Time Logic, 2002 - 2026
14 *
15 * This software is copyrighted by and is the sole property of Real
16 * Time Logic LLC. All rights, title, ownership, or other interests in
17 * the software remain the property of Real Time Logic LLC. This
18 * software may only be used in accordance with the terms and
19 * conditions stipulated in the corresponding license agreement under
20 * which the software has been supplied. Any unauthorized use,
21 * duplication, transmission, distribution, or disclosure of this
22 * software is expressly forbidden.
23 *
24 * This Copyright notice may not be removed or modified without prior
25 * written consent of Real Time Logic LLC.
26 *
27 * Real Time Logic LLC. reserves the right to modify this software
28 * without notice.
29 *
30 * http://www.realtimelogic.com
31 ****************************************************************************
32
33 CONTENTS
34 --------
35
36 1 Description
37 2 History of developmentDoubleLink
38 3 Macros
39 4 Include files
40 5 types Constants Variables
41 6 Function prototypes
42
43 ****************************************************************************
44 */
45#ifndef _SingleList_h
46#define _SingleList_h
47
48/*
49 ****************************************************************************
50 * 1 DESCRIPTION.
51 ****************************************************************************
52 *
53 */
54
55/*
56 ****************************************************************************
57 *
58 */
59
60/*
61 ****************************************************************************
62 * 3 MACROS.
63 ****************************************************************************
64 */
65
66/*
67 ****************************************************************************
68 * 4 INCLUDE FILES.
69 ****************************************************************************
70 */
71#include <TargConfig.h>
72
73
74/*
75 ****************************************************************************
76 * 5 TYPES CONSTANTS VARIABLES
77 ****************************************************************************
78 */
79
80/*Forward declarations*/
81struct SingleList;
82struct SingleListEnumerator;
83
84/*===========================================================================
85 *
86 * Class: SingleLink
87 *---------------------------------------------------------------------------
88 * Description:
89 * Contains the link chain for the next element in the SingleList. Subclass
90 * o link for data stored in the SingleList.
91 * Note, o class contains no virtual destructor and the subclassed node
92 * will not be informed when the node is deleted.
93 */
94typedef struct SingleLink
95{
96#ifdef __cplusplus
97 SingleLink();
98 SingleLink* getNext();
99 private:
100 friend struct SingleList;
101 friend struct SingleListEnumerator;
102#endif
103 struct SingleLink* next;
104} SingleLink;
105#define SingleLink_constructor(o) ((SingleLink*)(o))->next = 0
106#define SingleLink_getNext(o) ((SingleLink*)(o))->next
107#define SingleLink_isLinked(o) \
108 (((SingleLink*)o)->next ? TRUE : FALSE)
109
110#ifdef __cplusplus
111inline SingleLink::SingleLink() {SingleLink_constructor(this);}
112inline SingleLink* SingleLink::getNext() {return SingleLink_getNext(this);}
113#endif
114
115/*===========================================================================
116 *
117 * Class: SingleList
118 *---------------------------------------------------------------------------
119 * Description:
120 * Contains nodes of type SingleLink.
121 */
122typedef struct SingleList
123{
124#ifdef __cplusplus
125 SingleList();
126 void insertLast(SingleLink* link);
127 SingleLink* removeFirst();
128 SingleLink* peekFirst();
129 bool isEmpty();
130 bool isLast(SingleLink* link);
131 private:
132 friend struct SingleListEnumerator;
133#endif
134 SingleLink link;
135 SingleLink* last;
136} SingleList;
137
138#define SingleList_insertLast(o, linkMA) do \
139{ \
140 baAssert((SingleLink*)(linkMA) != (SingleLink*)(o)); \
141 baAssert(((SingleLink*)(linkMA))->next == 0); \
142 (o)->last->next = (SingleLink*)(linkMA); \
143 (o)->last = (SingleLink*)(linkMA); \
144 ((SingleLink*)(linkMA))->next = (SingleLink*)(o); \
145} while(0)
146
147#define SingleList_peekFirst(o) ((o)->link.next == (SingleLink*)(o) ? 0 : (o)->link.next)
148
149#define SingleList_isEmpty(o) ((o)->link.next == (SingleLink*)(o))
150#define SingleList_isLast(o, n) ((n)->next == (SingleLink*)(o))
151
152#ifdef __cplusplus
153extern "C" {
154#endif
155BA_API void SingleList_constructor(SingleList* o);
156BA_API SingleLink* SingleList_removeFirst(SingleList* o);
157#ifdef __cplusplus
158}
159inline
160SingleList::SingleList() { SingleList_constructor(this); }
161inline void
162SingleList::insertLast(SingleLink* link) { SingleList_insertLast(this, link); }
163inline SingleLink*
164SingleList::peekFirst() { return SingleList_peekFirst(this); }
165inline bool
166SingleList::isEmpty() { return SingleList_isEmpty(this); }
167inline bool
168SingleList::isLast(SingleLink* link) { return SingleList_isLast(this,link); }
169inline SingleLink*
170SingleList::removeFirst() { return SingleList_removeFirst(this); }
171#endif
172
173/*===========================================================================
174 *
175 * Class: SingleListEnumerator
176 *---------------------------------------------------------------------------
177 * Description:
178 * Usage:
179 * SingleListEnumerator e(list);
180 * for(Slink* link = e.getElement() ; link ; link = e.nextElement())
181 * or
182 * SingleListEnumerator e(list);
183 * Slink* link = e.getElement();
184 * while(link)
185 * {
186 * if(link bla bla)
187 * //Deletes current element and returns next element
188 * link = e.deleteElement();
189 * else
190 * link = e.nextElement();
191 * }
192 */
193typedef struct SingleListEnumerator
194{
195#ifdef __cplusplus
196 SingleListEnumerator(){}
197 SingleListEnumerator(SingleList* list);
198 SingleLink* getElement();
199 SingleLink* nextElement();
200 SingleLink* removeElement();
201 int insertBefore(SingleLink* l);
202 private:
203#endif
204 SingleList* list;
205 SingleLink* prevElement;
206 SingleLink* curElement;
207} SingleListEnumerator;
208
209#define SingleListEnumerator_constructor(o, listMA) do \
210{ \
211 (o)->list = listMA; \
212 (o)->prevElement = (SingleLink*)listMA; \
213 (o)->curElement = SingleList_isEmpty(listMA) ? 0 : (o)->list->link.next; \
214} while(0)
215
216#define SingleListEnumerator_getElement(o) (o)->curElement
217
218#define SingleListEnumerator_nextElement(o) \
219 ((o)->curElement ? ( \
220 (o)->prevElement = (o)->prevElement->next, \
221 (o)->curElement = (o)->curElement == (o)->list->last ? 0 : (o)->curElement->next, \
222 (o)->curElement \
223 ) : 0)
224
225#ifdef __cplusplus
226extern "C" {
227#endif
228BA_API int SingleListEnumerator_insertBefore(
229 SingleListEnumerator*, SingleLink*);
230BA_API SingleLink* SingleListEnumerator_removeElement(SingleListEnumerator* o);
231#ifdef __cplusplus
232}
233inline SingleListEnumerator::SingleListEnumerator(SingleList* list) {
234 SingleListEnumerator_constructor(this, list); }
235inline SingleLink*
236SingleListEnumerator::removeElement() {
237 return SingleListEnumerator_removeElement(this); }
238inline SingleLink*
239SingleListEnumerator::getElement() {return SingleListEnumerator_getElement(this);}
240inline SingleLink*
241SingleListEnumerator::nextElement() {return SingleListEnumerator_nextElement(this); }
242inline int SingleListEnumerator::insertBefore(SingleLink* l) {
243 return SingleListEnumerator_insertBefore(this, l); }
244#endif
245
246
247#endif /*_SingleList_h*/
248
249
250#if defined(SingleListCode) && ! defined(SingleListCodeIncluded)
251#define SingleListCodeIncluded
252
253BA_API void
254SingleList_constructor(SingleList* o)
255{
256 SingleLink_constructor((SingleLink*)o);
257 o->last = ((SingleLink*)o);
258 o->last->next = ((SingleLink*)o);
259 o->link.next = ((SingleLink*)o);
260}
261
262
263BA_API int
264SingleListEnumerator_insertBefore(SingleListEnumerator* o, SingleLink* l)
265{
266 if(l->next)
267 return -1;
268
269 if(SingleList_isEmpty(o->list))
270 SingleList_insertLast(o->list, l);
271 else
272 {
273 l->next = o->prevElement->next;
274 o->prevElement->next = l;
275 }
276 o->prevElement = l;
277 return 0;
278}
279
280
281BA_API SingleLink*
282SingleListEnumerator_removeElement(SingleListEnumerator* o)
283{
284 if(o->curElement)
285 {
286 /*Store current position and iterate iterator*/
287 SingleLink* cur = o->curElement;
288 /*If element to remove is last element in list*/
289 if(cur == o->list->last)
290 {
291 o->curElement = 0;
292 o->list->last = o->prevElement;
293 o->prevElement->next = (SingleLink*)o->list;
294 }
295 else
296 {
297 o->curElement = o->curElement->next;
298 o->prevElement->next = o->curElement;
299 }
300 cur->next = 0;
301 }
302 return o->curElement;
303}
304
305
306BA_API SingleLink*
307SingleList_removeFirst(SingleList* o)
308{
309 SingleLink* link2Remove;
310 link2Remove = o->link.next;
311 if(o->link.next == o->last)
312 {
313 if(o->link.next == (SingleLink*)o)
314 return 0;
315 o->link.next = o->last = (SingleLink*)o;
316 }
317 else
318 o->link.next = o->link.next->next;
319 link2Remove->next = 0;
320 return link2Remove;
321}
322
323#endif /* SingleListCode */