Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljsfixedpoolarray_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant
4
5#ifndef QQMLJSFIXEDPOOLARRAY_P_H
6#define QQMLJSFIXEDPOOLARRAY_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qglobal.h>
20#include <private/qqmljsmemorypool_p.h>
21
22QT_BEGIN_NAMESPACE
23
24namespace QQmlJS {
25
26template <typename T>
28{
29 T *data;
30 int count = 0;
31
32public:
34 : data(nullptr)
35 {}
36
37 FixedPoolArray(MemoryPool *pool, int size)
38 { allocate(pool, size); }
39
40 void allocate(MemoryPool *pool, int size)
41 {
42 count = size;
43 data = reinterpret_cast<T*>(pool->allocate(count * sizeof(T)));
44 }
45
46 void allocate(MemoryPool *pool, const QList<T> &vector)
47 {
48 count = vector.size();
49 data = reinterpret_cast<T*>(pool->allocate(count * sizeof(T)));
50
51 if (QTypeInfo<T>::isComplex) {
52 for (int i = 0; i < count; ++i)
53 new (data + i) T(vector.at(i));
54 } else if (count) {
55 memcpy(data, static_cast<const void*>(vector.constData()), count * sizeof(T));
56 }
57 }
58
59 template <typename Container>
60 void allocate(MemoryPool *pool, const Container &container)
61 {
62 count = container.size();
63 data = reinterpret_cast<T*>(pool->allocate(count * sizeof(T)));
64 typename Container::ConstIterator it = container.constBegin();
65 for (int i = 0; i < count; ++i)
66 new (data + i) T(*it++);
67 }
68
69 int size() const
70 { return count; }
71
72 const T &at(int index) const {
73 Q_ASSERT(index >= 0 && index < count);
74 return data[index];
75 }
76
77 T &at(int index) {
78 Q_ASSERT(index >= 0 && index < count);
79 return data[index];
80 }
81
82 T &operator[](int index) {
83 return at(index);
84 }
85
86
87 int indexOf(const T &value) const {
88 for (int i = 0; i < count; ++i)
89 if (data[i] == value)
90 return i;
91 return -1;
92 }
93
94 const T *begin() const { return data; }
95 const T *end() const { return data + count; }
96
97 T *begin() { return data; }
98 T *end() { return data + count; }
99};
100
101} // namespace QQmlJS
102
103QT_END_NAMESPACE
104
105#endif // QQMLJSFIXEDPOOLARRAY_P_H
const T & at(int index) const
void allocate(MemoryPool *pool, const QList< T > &vector)
void allocate(MemoryPool *pool, int size)
void allocate(MemoryPool *pool, const Container &container)
int indexOf(const T &value) const
FixedPoolArray(MemoryPool *pool, int size)