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 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | template<typename T> |
23 | struct 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); |
40 | private: |
41 | T *_array; |
42 | int _alloc; |
43 | int _size; |
44 | }; |
45 | |
46 | template<typename T> |
47 | QFiniteStack<T>::QFiniteStack() |
48 | : _array(nullptr), _alloc(0), _size(0) |
49 | { |
50 | } |
51 | |
52 | template<typename T> |
53 | QFiniteStack<T>::~QFiniteStack() |
54 | { |
55 | deallocate(); |
56 | } |
57 | |
58 | template<typename T> |
59 | bool QFiniteStack<T>::isEmpty() const |
60 | { |
61 | return _size == 0; |
62 | } |
63 | |
64 | template<typename T> |
65 | const T &QFiniteStack<T>::top() const |
66 | { |
67 | return _array[_size - 1]; |
68 | } |
69 | |
70 | template<typename T> |
71 | T &QFiniteStack<T>::top() |
72 | { |
73 | return _array[_size - 1]; |
74 | } |
75 | |
76 | template<typename T> |
77 | void 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 | |
87 | template<typename T> |
88 | T 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 | |
102 | template<typename T> |
103 | int QFiniteStack<T>::count() const |
104 | { |
105 | return _size; |
106 | } |
107 | |
108 | template<typename T> |
109 | const T &QFiniteStack<T>::at(int index) const |
110 | { |
111 | return _array[index]; |
112 | } |
113 | |
114 | template<typename T> |
115 | T &QFiniteStack<T>::operator[](int index) |
116 | { |
117 | return _array[index]; |
118 | } |
119 | |
120 | template<typename T> |
121 | void 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 | |
133 | template<typename T> |
134 | void 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 | |
149 | QT_END_NAMESPACE |
150 | |
151 | #endif // QFINITESTACK_P_H |
152 | |
153 | |