f1691b31511edcd8c0b7fbaa3df1122901d20c73
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qintrusivelist_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QINTRUSIVELIST_P_H
43 #define QINTRUSIVELIST_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <QtCore/qglobal.h>
57
58 QT_BEGIN_NAMESPACE
59
60 class QIntrusiveListNode;
61 template<class N, QIntrusiveListNode N::*member>
62 class QIntrusiveList
63 {
64 public:
65     inline QIntrusiveList();
66     inline ~QIntrusiveList();
67
68     inline bool isEmpty() const;
69     inline void insert(N *n);
70     inline void remove(N *n);
71     inline bool contains(N *) const;
72
73     class iterator {
74     public:
75         inline iterator();
76         inline iterator(N *value);
77
78         inline N *operator*() const;
79         inline N *operator->() const;
80         inline bool operator==(const iterator &other) const;
81         inline bool operator!=(const iterator &other) const;
82         inline iterator &operator++();
83
84         inline iterator &erase();
85         
86     private:
87         N *_value;
88     };
89     typedef iterator Iterator;
90
91     inline N *first() const;
92     static inline N *next(N *current);
93
94     inline iterator begin();
95     inline iterator end();
96
97 private:
98     static inline N *nodeToN(QIntrusiveListNode *node);
99
100     QIntrusiveListNode *__first;
101 };
102
103 class QIntrusiveListNode
104 {
105 public:
106     inline QIntrusiveListNode();
107     inline ~QIntrusiveListNode();
108
109     inline void remove();
110     inline bool isInList() const;
111
112     QIntrusiveListNode *_next;
113     QIntrusiveListNode**_prev;
114 };
115
116 template<class N, QIntrusiveListNode N::*member>
117 QIntrusiveList<N, member>::iterator::iterator() 
118 : _value(0) 
119 {
120 }
121
122 template<class N, QIntrusiveListNode N::*member>
123 QIntrusiveList<N, member>::iterator::iterator(N *value) 
124 : _value(value) 
125 {
126 }
127
128 template<class N, QIntrusiveListNode N::*member>
129 N *QIntrusiveList<N, member>::iterator::operator*() const 
130
131     return _value; 
132 }
133
134 template<class N, QIntrusiveListNode N::*member>
135 N *QIntrusiveList<N, member>::iterator::operator->() const 
136
137     return _value; 
138 }
139
140 template<class N, QIntrusiveListNode N::*member>
141 bool QIntrusiveList<N, member>::iterator::operator==(const iterator &other) const 
142
143     return other._value == _value; 
144 }
145
146 template<class N, QIntrusiveListNode N::*member>
147 bool QIntrusiveList<N, member>::iterator::operator!=(const iterator &other) const 
148
149     return other._value != _value; 
150 }
151
152 template<class N, QIntrusiveListNode N::*member>
153 typename QIntrusiveList<N, member>::iterator &QIntrusiveList<N, member>::iterator::operator++() 
154
155     _value = QIntrusiveList<N, member>::next(_value); 
156     return *this; 
157 }
158
159 template<class N, QIntrusiveListNode N::*member>
160 typename QIntrusiveList<N, member>::iterator &QIntrusiveList<N, member>::iterator::erase() 
161
162     N *old = _value; 
163     _value = QIntrusiveList<N, member>::next(_value); 
164     (old->*member).remove(); 
165     return *this;
166 }
167         
168 template<class N, QIntrusiveListNode N::*member>
169 QIntrusiveList<N, member>::QIntrusiveList() 
170 : __first(0) 
171 {
172 }
173
174 template<class N, QIntrusiveListNode N::*member>
175 QIntrusiveList<N, member>::~QIntrusiveList()
176 {
177     while (__first) __first->remove();
178 }
179
180 template<class N, QIntrusiveListNode N::*member>
181 bool QIntrusiveList<N, member>::isEmpty() const
182 {
183     return __first == 0;
184 }
185
186 template<class N, QIntrusiveListNode N::*member>
187 void QIntrusiveList<N, member>::insert(N *n) 
188 {
189     QIntrusiveListNode *nnode = &(n->*member);
190     nnode->remove();
191
192     nnode->_next = __first;
193     if (nnode->_next) nnode->_next->_prev = &nnode->_next;
194     __first = nnode;
195     nnode->_prev = &__first;
196 }
197
198 template<class N, QIntrusiveListNode N::*member>
199 void QIntrusiveList<N, member>::remove(N *n)
200 {
201     QIntrusiveListNode *nnode = &(n->*member);
202     nnode->remove();
203 }
204
205 template<class N, QIntrusiveListNode N::*member>
206 bool QIntrusiveList<N, member>::contains(N *n) const
207 {
208     QIntrusiveListNode *nnode = __first;
209     while (nnode) {
210         if (nodeToN(nnode) == n)
211             return true;
212         nnode = nnode->_next;
213     }
214     return false;
215 }
216
217 template<class N, QIntrusiveListNode N::*member>
218 N *QIntrusiveList<N, member>::first() const 
219
220     return __first?nodeToN(__first):0; 
221 }
222
223 template<class N, QIntrusiveListNode N::*member>
224 N *QIntrusiveList<N, member>::next(N *current) 
225 {
226     QIntrusiveListNode *nextnode = (current->*member)._next;
227     N *nextstruct = nextnode?nodeToN(nextnode):0;
228     return nextstruct;
229 }
230
231 template<class N, QIntrusiveListNode N::*member>
232 typename QIntrusiveList<N, member>::iterator QIntrusiveList<N, member>::begin() 
233
234     return __first?iterator(nodeToN(__first)):iterator(); 
235 }
236
237 template<class N, QIntrusiveListNode N::*member>
238 typename QIntrusiveList<N, member>::iterator QIntrusiveList<N, member>::end() 
239
240     return iterator(); 
241 }
242
243 template<class N, QIntrusiveListNode N::*member>
244 N *QIntrusiveList<N, member>::nodeToN(QIntrusiveListNode *node) 
245 {
246     return (N *)((char *)node - ((char *)&(((N *)0)->*member) - (char *)0));
247 }
248
249 QIntrusiveListNode::QIntrusiveListNode()
250 : _next(0), _prev(0)
251 {
252 }
253
254 QIntrusiveListNode::~QIntrusiveListNode()
255 {
256     remove();
257 }
258
259 void QIntrusiveListNode::remove()
260 {
261     if (_prev) *_prev = _next;
262     if (_next) _next->_prev = _prev;
263     _prev = 0;
264     _next = 0;
265 }
266
267 bool QIntrusiveListNode::isInList() const
268 {
269     return _prev != 0;
270 }
271
272 QT_END_NAMESPACE
273
274 #endif // QINTRUSIVELIST_P_H