Merge branch 'v8'
[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 ** 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
72     class iterator {
73     public:
74         inline iterator();
75         inline iterator(N *value);
76
77         inline N *operator*() const;
78         inline N *operator->() const;
79         inline bool operator==(const iterator &other) const;
80         inline bool operator!=(const iterator &other) const;
81         inline iterator &operator++();
82
83         inline iterator &erase();
84         
85     private:
86         N *_value;
87     };
88     typedef iterator Iterator;
89
90     inline N *first() const;
91     static inline N *next(N *current);
92
93     inline iterator begin();
94     inline iterator end();
95
96 private:
97     static inline N *nodeToN(QIntrusiveListNode *node);
98
99     QIntrusiveListNode *__first;
100 };
101
102 class QIntrusiveListNode
103 {
104 public:
105     inline QIntrusiveListNode();
106     inline ~QIntrusiveListNode();
107
108     inline void remove();
109     inline bool isInList() const;
110
111     QIntrusiveListNode *_next;
112     QIntrusiveListNode**_prev;
113 };
114
115 template<class N, QIntrusiveListNode N::*member>
116 QIntrusiveList<N, member>::iterator::iterator() 
117 : _value(0) 
118 {
119 }
120
121 template<class N, QIntrusiveListNode N::*member>
122 QIntrusiveList<N, member>::iterator::iterator(N *value) 
123 : _value(value) 
124 {
125 }
126
127 template<class N, QIntrusiveListNode N::*member>
128 N *QIntrusiveList<N, member>::iterator::operator*() const 
129
130     return _value; 
131 }
132
133 template<class N, QIntrusiveListNode N::*member>
134 N *QIntrusiveList<N, member>::iterator::operator->() const 
135
136     return _value; 
137 }
138
139 template<class N, QIntrusiveListNode N::*member>
140 bool QIntrusiveList<N, member>::iterator::operator==(const iterator &other) const 
141
142     return other._value == _value; 
143 }
144
145 template<class N, QIntrusiveListNode N::*member>
146 bool QIntrusiveList<N, member>::iterator::operator!=(const iterator &other) const 
147
148     return other._value != _value; 
149 }
150
151 template<class N, QIntrusiveListNode N::*member>
152 typename QIntrusiveList<N, member>::iterator &QIntrusiveList<N, member>::iterator::operator++() 
153
154     _value = QIntrusiveList<N, member>::next(_value); 
155     return *this; 
156 }
157
158 template<class N, QIntrusiveListNode N::*member>
159 typename QIntrusiveList<N, member>::iterator &QIntrusiveList<N, member>::iterator::erase() 
160
161     N *old = _value; 
162     _value = QIntrusiveList<N, member>::next(_value); 
163     (old->*member).remove(); 
164     return *this;
165 }
166         
167 template<class N, QIntrusiveListNode N::*member>
168 QIntrusiveList<N, member>::QIntrusiveList() 
169 : __first(0) 
170 {
171 }
172
173 template<class N, QIntrusiveListNode N::*member>
174 QIntrusiveList<N, member>::~QIntrusiveList()
175 {
176     while (__first) __first->remove();
177 }
178
179 template<class N, QIntrusiveListNode N::*member>
180 bool QIntrusiveList<N, member>::isEmpty() const
181 {
182     return __first == 0;
183 }
184
185 template<class N, QIntrusiveListNode N::*member>
186 void QIntrusiveList<N, member>::insert(N *n) 
187 {
188     QIntrusiveListNode *nnode = &(n->*member);
189     nnode->remove();
190
191     nnode->_next = __first;
192     if (nnode->_next) nnode->_next->_prev = &nnode->_next;
193     __first = nnode;
194     nnode->_prev = &__first;
195 }
196
197 template<class N, QIntrusiveListNode N::*member>
198 void QIntrusiveList<N, member>::remove(N *n)
199 {
200     QIntrusiveListNode *nnode = &(n->*member);
201     nnode->remove();
202 }
203
204 template<class N, QIntrusiveListNode N::*member>
205 N *QIntrusiveList<N, member>::first() const 
206
207     return __first?nodeToN(__first):0; 
208 }
209
210 template<class N, QIntrusiveListNode N::*member>
211 N *QIntrusiveList<N, member>::next(N *current) 
212 {
213     QIntrusiveListNode *nextnode = (current->*member)._next;
214     N *nextstruct = nextnode?nodeToN(nextnode):0;
215     return nextstruct;
216 }
217
218 template<class N, QIntrusiveListNode N::*member>
219 typename QIntrusiveList<N, member>::iterator QIntrusiveList<N, member>::begin() 
220
221     return __first?iterator(nodeToN(__first)):iterator(); 
222 }
223
224 template<class N, QIntrusiveListNode N::*member>
225 typename QIntrusiveList<N, member>::iterator QIntrusiveList<N, member>::end() 
226
227     return iterator(); 
228 }
229
230 template<class N, QIntrusiveListNode N::*member>
231 N *QIntrusiveList<N, member>::nodeToN(QIntrusiveListNode *node) 
232 {
233     return (N *)((char *)node - ((char *)&(((N *)0)->*member) - (char *)0));
234 }
235
236 QIntrusiveListNode::QIntrusiveListNode()
237 : _next(0), _prev(0)
238 {
239 }
240
241 QIntrusiveListNode::~QIntrusiveListNode()
242 {
243     remove();
244 }
245
246 void QIntrusiveListNode::remove()
247 {
248     if (_prev) *_prev = _next;
249     if (_next) _next->_prev = _prev;
250     _prev = 0;
251     _next = 0;
252 }
253
254 bool QIntrusiveListNode::isInList() const
255 {
256     return _prev != 0;
257 }
258
259 QT_END_NAMESPACE
260
261 #endif // QINTRUSIVELIST_P_H