1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QFINITESTACK_P_H
5#define QFINITESTACK_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19
20QT_BEGIN_NAMESPACE
21
22template<typename T>
23struct QFiniteStack {
24 inline QFiniteStack();
25 inline ~QFiniteStack();
26
27 inline void deallocate();
28 inline void allocate(int size);
29
30 inline int capacity() const { return _alloc; }
31
32 inline bool isEmpty() const;
33 inline const T &top() const;
34 inline T &top();
35 inline void push(const T &o);
36 inline T pop();
37 inline int count() const;
38 inline const T &at(int index) const;
39 inline T &operator[](int index);
40private:
41 T *_array;
42 int _alloc;
43 int _size;
44};
45
46template<typename T>
47QFiniteStack<T>::QFiniteStack()
48: _array(nullptr), _alloc(0), _size(0)
49{
50}
51
52template<typename T>
53QFiniteStack<T>::~QFiniteStack()
54{
55 deallocate();
56}
57
58template<typename T>
59bool QFiniteStack<T>::isEmpty() const
60{
61 return _size == 0;
62}
63
64template<typename T>
65const T &QFiniteStack<T>::top() const
66{
67 return _array[_size - 1];
68}
69
70template<typename T>
71T &QFiniteStack<T>::top()
72{
73 return _array[_size - 1];
74}
75
76template<typename T>
77void QFiniteStack<T>::push(const T &o)
78{
79 Q_ASSERT(_size < _alloc);
80 if (QTypeInfo<T>::isComplex) {
81 new (_array + _size++) T(o);
82 } else {
83 _array[_size++] = o;
84 }
85}
86
87template<typename T>
88T QFiniteStack<T>::pop()
89{
90 Q_ASSERT(_size > 0);
91 --_size;
92
93 if (QTypeInfo<T>::isComplex) {
94 T rv = _array[_size];
95 (_array + _size)->~T();
96 return rv;
97 } else {
98 return _array[_size];
99 }
100}
101
102template<typename T>
103int QFiniteStack<T>::count() const
104{
105 return _size;
106}
107
108template<typename T>
109const T &QFiniteStack<T>::at(int index) const
110{
111 return _array[index];
112}
113
114template<typename T>
115T &QFiniteStack<T>::operator[](int index)
116{
117 return _array[index];
118}
119
120template<typename T>
121void QFiniteStack<T>::allocate(int size)
122{
123 Q_ASSERT(_array == nullptr);
124 Q_ASSERT(_alloc == 0);
125 Q_ASSERT(_size == 0);
126
127 if (!size) return;
128
129 _array = (T *)malloc(size: size * sizeof(T));
130 _alloc = size;
131}
132
133template<typename T>
134void QFiniteStack<T>::deallocate()
135{
136 if (QTypeInfo<T>::isComplex) {
137 T *i = _array + _size;
138 while (i != _array)
139 (--i)->~T();
140 }
141
142 free(_array);
143
144 _array = nullptr;
145 _alloc = 0;
146 _size = 0;
147}
148
149QT_END_NAMESPACE
150
151#endif // QFINITESTACK_P_H
152
153

source code of qtdeclarative/src/qml/qml/ftw/qfinitestack_p.h