Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qfinitestack_p.h
Go to the documentation of this file.
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
21
22template<typename T>
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>
48: _array(nullptr), _alloc(0), _size(0)
49{
50}
51
52template<typename T>
54{
55 deallocate();
56}
57
58template<typename T>
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>
72{
73 return _array[_size - 1];
74}
75
76template<typename T>
78{
79 Q_ASSERT(_size < _alloc);
81 new (_array + _size++) T(o);
82 } else {
83 _array[_size++] = o;
84 }
85}
86
87template<typename T>
89{
90 Q_ASSERT(_size > 0);
91 --_size;
92
94 T rv = _array[_size];
95 (_array + _size)->~T();
96 return rv;
97 } else {
98 return _array[_size];
99 }
100}
101
102template<typename T>
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>
116{
117 return _array[index];
118}
119
120template<typename T>
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 * sizeof(T));
130 _alloc = size;
131}
132
133template<typename T>
135{
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
150
151#endif // QFINITESTACK_P_H
152
Combined button and popup list for selecting options.
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QObject::connect nullptr
T & operator[](int index)
const T & top() const
int count() const
const T & at(int index) const
void push(const T &o)
bool isEmpty() const
int capacity() const
void allocate(int size)