Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qfinitestack_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QFINITESTACK_P_H
43 #define QFINITESTACK_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 template<typename T>
61 struct QFiniteStack {
62     inline QFiniteStack();
63     inline ~QFiniteStack();
64
65     inline void deallocate();
66     inline void allocate(int size);
67
68     inline bool isEmpty() const;
69     inline const T &top() const;
70     inline T &top();
71     inline void push(const T &o);
72     inline T pop();
73     inline int count() const;
74     inline const T &at(int index) const;
75     inline T &operator[](int index);
76 private:
77     T *_array;
78     int _alloc;
79     int _size;
80 };
81
82 template<typename T>
83 QFiniteStack<T>::QFiniteStack()
84 : _array(0), _alloc(0), _size(0) 
85 {
86 }
87
88 template<typename T>
89 QFiniteStack<T>::~QFiniteStack()
90 {
91     deallocate();
92 }
93
94 template<typename T>
95 bool QFiniteStack<T>::isEmpty() const
96 {
97     return _size == 0;
98 }
99
100 template<typename T>
101 const T &QFiniteStack<T>::top() const
102 {
103     return _array[_size - 1];
104 }
105
106 template<typename T>
107 T &QFiniteStack<T>::top()
108 {
109     return _array[_size - 1];
110 }
111
112 template<typename T>
113 void QFiniteStack<T>::push(const T &o)
114 {
115     if (QTypeInfo<T>::isComplex) {
116         new (_array + _size++) T(o);
117     } else {
118         _array[_size++] = o;
119     }
120 }
121
122 template<typename T>
123 T QFiniteStack<T>::pop()
124 {
125     --_size;
126
127     if (QTypeInfo<T>::isComplex) {
128         T rv = _array[_size];
129         (_array + _size)->~T();
130         return rv;
131     } else {
132         return _array[_size];
133     }
134 }
135     
136 template<typename T>
137 int QFiniteStack<T>::count() const
138 {
139     return _size;
140 }
141
142 template<typename T>
143 const T &QFiniteStack<T>::at(int index) const
144 {
145     return _array[index];
146 }
147
148 template<typename T>
149 T &QFiniteStack<T>::operator[](int index)
150 {
151     return _array[index];
152 }
153
154 template<typename T>
155 void QFiniteStack<T>::allocate(int size) 
156 {
157     Q_ASSERT(_array == 0);
158     Q_ASSERT(_alloc == 0);
159     Q_ASSERT(_size == 0);
160
161     if (!size) return;
162
163     _array = (T *)qMalloc(size * sizeof(T));
164     _alloc = size;
165 }
166
167 template<typename T>
168 void QFiniteStack<T>::deallocate()
169 {
170     if (QTypeInfo<T>::isComplex) {
171         T *i = _array + _size;
172         while (i != _array) 
173             (--i)->~T();
174     }
175
176     qFree(_array);
177
178     _array = 0;
179     _alloc = 0;
180     _size = 0;
181 }
182
183 QT_END_NAMESPACE
184
185 #endif // QFINITESTACK_P_H
186