Merge branch 'master' of git://scm.dev.nokia.troll.no/qt/qtdeclarative
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qintrusivelist_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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 void insert(N *n);
69     inline void remove(N *n);
70
71     class iterator {
72     public:
73         inline iterator();
74         inline iterator(N *value);
75
76         inline N *operator*() const;
77         inline N *operator->() const;
78         inline bool operator==(const iterator &other) const;
79         inline bool operator!=(const iterator &other) const;
80         inline iterator &operator++();
81
82         inline iterator &erase();
83         
84     private:
85         N *_value;
86     };
87     typedef iterator Iterator;
88
89     inline N *first() const;
90     static inline N *next(N *current);
91
92     inline iterator begin();
93     inline iterator end();
94
95 private:
96     static inline N *nodeToN(QIntrusiveListNode *node);
97
98     QIntrusiveListNode *__first;
99 };
100
101 class QIntrusiveListNode
102 {
103 public:
104     inline QIntrusiveListNode();
105     inline ~QIntrusiveListNode();
106
107     inline void remove();
108     inline bool isInList() const;
109
110     QIntrusiveListNode *_next;
111     QIntrusiveListNode**_prev;
112 };
113
114 template<class N, QIntrusiveListNode N::*member>
115 QIntrusiveList<N, member>::iterator::iterator() 
116 : _value(0) 
117 {
118 }
119
120 template<class N, QIntrusiveListNode N::*member>
121 QIntrusiveList<N, member>::iterator::iterator(N *value) 
122 : _value(value) 
123 {
124 }
125
126 template<class N, QIntrusiveListNode N::*member>
127 N *QIntrusiveList<N, member>::iterator::operator*() const 
128
129     return _value; 
130 }
131
132 template<class N, QIntrusiveListNode N::*member>
133 N *QIntrusiveList<N, member>::iterator::operator->() const 
134
135     return _value; 
136 }
137
138 template<class N, QIntrusiveListNode N::*member>
139 bool QIntrusiveList<N, member>::iterator::operator==(const iterator &other) const 
140
141     return other._value == _value; 
142 }
143
144 template<class N, QIntrusiveListNode N::*member>
145 bool QIntrusiveList<N, member>::iterator::operator!=(const iterator &other) const 
146
147     return other._value != _value; 
148 }
149
150 template<class N, QIntrusiveListNode N::*member>
151 typename QIntrusiveList<N, member>::iterator &QIntrusiveList<N, member>::iterator::operator++() 
152
153     _value = QIntrusiveList<N, member>::next(_value); 
154     return *this; 
155 }
156
157 template<class N, QIntrusiveListNode N::*member>
158 typename QIntrusiveList<N, member>::iterator &QIntrusiveList<N, member>::iterator::erase() 
159
160     N *old = _value; 
161     _value = QIntrusiveList<N, member>::next(_value); 
162     (old->*member).remove(); 
163     return *this;
164 }
165         
166 template<class N, QIntrusiveListNode N::*member>
167 QIntrusiveList<N, member>::QIntrusiveList() 
168 : __first(0) 
169 {
170 }
171
172 template<class N, QIntrusiveListNode N::*member>
173 QIntrusiveList<N, member>::~QIntrusiveList()
174 {
175     while (__first) __first->remove();
176 }
177
178 template<class N, QIntrusiveListNode N::*member>
179 void QIntrusiveList<N, member>::insert(N *n) 
180 {
181     QIntrusiveListNode *nnode = &(n->*member);
182     nnode->remove();
183
184     nnode->_next = __first;
185     if (nnode->_next) nnode->_next->_prev = &nnode->_next;
186     __first = nnode;
187     nnode->_prev = &__first;
188 }
189
190 template<class N, QIntrusiveListNode N::*member>
191 void QIntrusiveList<N, member>::remove(N *n)
192 {
193     QIntrusiveListNode *nnode = &(n->*member);
194     nnode->remove();
195 }
196
197 template<class N, QIntrusiveListNode N::*member>
198 N *QIntrusiveList<N, member>::first() const 
199
200     return __first?nodeToN(__first):0; 
201 }
202
203 template<class N, QIntrusiveListNode N::*member>
204 N *QIntrusiveList<N, member>::next(N *current) 
205 {
206     QIntrusiveListNode *nextnode = (current->*member)._next;
207     N *nextstruct = nextnode?nodeToN(nextnode):0;
208     return nextstruct;
209 }
210
211 template<class N, QIntrusiveListNode N::*member>
212 typename QIntrusiveList<N, member>::iterator QIntrusiveList<N, member>::begin() 
213
214     return __first?iterator(nodeToN(__first)):iterator(); 
215 }
216
217 template<class N, QIntrusiveListNode N::*member>
218 typename QIntrusiveList<N, member>::iterator QIntrusiveList<N, member>::end() 
219
220     return iterator(); 
221 }
222
223 template<class N, QIntrusiveListNode N::*member>
224 N *QIntrusiveList<N, member>::nodeToN(QIntrusiveListNode *node) 
225 {
226     return (N *)((char *)node - ((char *)&(((N *)0)->*member) - (char *)0));
227 }
228
229 QIntrusiveListNode::QIntrusiveListNode()
230 : _next(0), _prev(0)
231 {
232 }
233
234 QIntrusiveListNode::~QIntrusiveListNode()
235 {
236     remove();
237 }
238
239 void QIntrusiveListNode::remove()
240 {
241     if (_prev) *_prev = _next;
242     if (_next) _next->_prev = _prev;
243     _prev = 0;
244     _next = 0;
245 }
246
247 bool QIntrusiveListNode::isInList() const
248 {
249     return _prev != 0;
250 }
251
252 QT_END_NAMESPACE
253
254 #endif // QINTRUSIVELIST_P_H