1 #pragma once
2 
3 /** @file the_Foundation/objectlist.h  List of objects.
4 
5 ObjectList is itself an Object.
6 
7 ObjectList owns its nodes, so deleting the list will delete all the nodes and
8 release references to the corresponding objects.
9 
10 @authors Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
11 
12 @par License
13 
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
16 
17 1. Redistributions of source code must retain the above copyright notice, this
18    list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright notice,
20    this list of conditions and the following disclaimer in the documentation
21    and/or other materials provided with the distribution.
22 
23 <small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
33 */
34 
35 #include "list.h"
36 #include "object.h"
37 
38 iBeginPublic
39 
40 iDeclareClass(ObjectList)
41 
42 iDeclareType(ObjectListNode)
43 
44 struct Impl_ObjectList {
45     iObject object;
46     iList list;
47 };
48 
49 struct Impl_ObjectListNode {
50     iListNode node;
51     iObject *object;
52 };
53 
54 #define next_ObjectListNode(d)      ((iObjectListNode *) ((d) ? (d)->node.next : NULL))
55 #define prev_ObjectListNode(d)      ((iObjectListNode *) ((d) ? (d)->node.prev : NULL))
56 #define object_ObjectListNode(d)    ((iAnyObject *) ((const iObjectListNode *) (d))->object)
57 
58 iDeclareObjectConstruction(ObjectList)
59 
60 iObjectList *       copy_ObjectList     (const iObjectList *);
61 
isEmpty_ObjectList(const iObjectList * d)62 iLocalDef iBool     isEmpty_ObjectList  (const iObjectList *d) { return d == NULL || isEmpty_List(&d->list); }
size_ObjectList(const iObjectList * d)63 iLocalDef size_t    size_ObjectList     (const iObjectList *d) { return d ? size_List(&d->list) : 0; }
64 #define             list_ObjectList(d)  (&(d)->list)
65 
66 iObject *       front_ObjectList        (const iObjectList *);
67 iObject *       back_ObjectList         (const iObjectList *);
68 
69 #define         begin_ObjectList(d)         ((iObjectListNode *) (d)->list.root.next)
70 #define         end_ObjectList(d)           ((iObjectListNode *) &(d)->list.root)
71 #define         constBegin_ObjectList(d)    ((const iObjectListNode *) (d)->list.root.next)
72 #define         constEnd_ObjectList(d)      ((const iObjectListNode *) &(d)->list.root)
73 
74 void            clear_ObjectList        (iObjectList *);
75 
76 iAnyObject *    pushBack_ObjectList     (iObjectList *, iAnyObject *object);
77 iAnyObject *    pushFront_ObjectList    (iObjectList *, iAnyObject *object);
78 iAnyObject *    insertAfter_ObjectList  (iObjectList *, iObjectListNode *after, iObject *object);
79 iAnyObject *    insertBefore_ObjectList (iObjectList *, iObjectListNode *before, iObject *object);
80 void            removeNode_ObjectList   (iObjectList *, iObjectListNode *node);
81 void            popFront_ObjectList     (iObjectList *);
82 void            popBack_ObjectList      (iObjectList *);
83 
84 /**
85  * Pops the front object. Caller is responsible for releasing the returned object.
86  */
takeFront_ObjectList(iObjectList * d)87 iLocalDef iAnyObject *takeFront_ObjectList(iObjectList *d) {
88     iAnyObject *obj = ref_Object(front_ObjectList(d));
89     if (obj) iAssertIsObject(d);
90     popFront_ObjectList(d);
91     return obj;
92 }
93 
94 /**
95  * Pops the back object. Caller is responsible for releasing the returned object.
96  */
takeBack_ObjectList(iObjectList * d)97 iLocalDef iAnyObject *takeBack_ObjectList(iObjectList *d) {
98     iAnyObject *obj = ref_Object(back_ObjectList(d));
99     popBack_ObjectList(d);
100     return obj;
101 }
102 
103 /** @name Iterators */
104 ///@{
105 iDeclareIterator(ObjectList, iObjectList *)
106 void            remove_ObjectListIterator(iObjectListIterator *);
107 struct IteratorImpl_ObjectList {
108     iObjectListNode *value;
109     iObjectListNode *next;
110     iAnyObject *object;
111     iObjectList *list;
112 };
113 
114 iDeclareConstIterator(ObjectList, const iObjectList *)
115 struct ConstIteratorImpl_ObjectList {
116     const iObjectListNode *value;
117     const iAnyObject *object;
118     const iObjectList *list;
119 };
120 ///@}
121 
122 iEndPublic
123