1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QFINITESTACK_P_H |
41 | #define QFINITESTACK_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/qglobal.h> |
55 | |
56 | QT_BEGIN_NAMESPACE |
57 | |
58 | template<typename T> |
59 | struct QFiniteStack { |
60 | inline QFiniteStack(); |
61 | inline ~QFiniteStack(); |
62 | |
63 | inline void deallocate(); |
64 | inline void allocate(int size); |
65 | |
66 | inline int capacity() const { return _alloc; } |
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(nullptr), _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 | Q_ASSERT(_size < _alloc); |
116 | if (QTypeInfo<T>::isComplex) { |
117 | new (_array + _size++) T(o); |
118 | } else { |
119 | _array[_size++] = o; |
120 | } |
121 | } |
122 | |
123 | template<typename T> |
124 | T QFiniteStack<T>::pop() |
125 | { |
126 | Q_ASSERT(_size > 0); |
127 | --_size; |
128 | |
129 | if (QTypeInfo<T>::isComplex) { |
130 | T rv = _array[_size]; |
131 | (_array + _size)->~T(); |
132 | return rv; |
133 | } else { |
134 | return _array[_size]; |
135 | } |
136 | } |
137 | |
138 | template<typename T> |
139 | int QFiniteStack<T>::count() const |
140 | { |
141 | return _size; |
142 | } |
143 | |
144 | template<typename T> |
145 | const T &QFiniteStack<T>::at(int index) const |
146 | { |
147 | return _array[index]; |
148 | } |
149 | |
150 | template<typename T> |
151 | T &QFiniteStack<T>::operator[](int index) |
152 | { |
153 | return _array[index]; |
154 | } |
155 | |
156 | template<typename T> |
157 | void QFiniteStack<T>::allocate(int size) |
158 | { |
159 | Q_ASSERT(_array == nullptr); |
160 | Q_ASSERT(_alloc == 0); |
161 | Q_ASSERT(_size == 0); |
162 | |
163 | if (!size) return; |
164 | |
165 | _array = (T *)malloc(size: size * sizeof(T)); |
166 | _alloc = size; |
167 | } |
168 | |
169 | template<typename T> |
170 | void QFiniteStack<T>::deallocate() |
171 | { |
172 | if (QTypeInfo<T>::isComplex) { |
173 | T *i = _array + _size; |
174 | while (i != _array) |
175 | (--i)->~T(); |
176 | } |
177 | |
178 | free(_array); |
179 | |
180 | _array = nullptr; |
181 | _alloc = 0; |
182 | _size = 0; |
183 | } |
184 | |
185 | QT_END_NAMESPACE |
186 | |
187 | #endif // QFINITESTACK_P_H |
188 | |
189 | |