| 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 | |
| 4 | #ifndef QQMLJSFIXEDPOOLARRAY_P_H |
| 5 | #define QQMLJSFIXEDPOOLARRAY_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/qglobal.h> |
| 19 | #include <private/qqmljsmemorypool_p.h> |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | namespace QQmlJS { |
| 24 | |
| 25 | template <typename T> |
| 26 | class FixedPoolArray |
| 27 | { |
| 28 | T *data; |
| 29 | int count = 0; |
| 30 | |
| 31 | public: |
| 32 | FixedPoolArray() |
| 33 | : data(nullptr) |
| 34 | {} |
| 35 | |
| 36 | FixedPoolArray(MemoryPool *pool, int size) |
| 37 | { allocate(pool, size); } |
| 38 | |
| 39 | void allocate(MemoryPool *pool, int size) |
| 40 | { |
| 41 | count = size; |
| 42 | data = reinterpret_cast<T*>(pool->allocate(size: count * sizeof(T))); |
| 43 | } |
| 44 | |
| 45 | void allocate(MemoryPool *pool, const QVector<T> &vector) |
| 46 | { |
| 47 | count = vector.size(); |
| 48 | data = reinterpret_cast<T*>(pool->allocate(size: count * sizeof(T))); |
| 49 | |
| 50 | if (QTypeInfo<T>::isComplex) { |
| 51 | for (int i = 0; i < count; ++i) |
| 52 | new (data + i) T(vector.at(i)); |
| 53 | } else if (count) { |
| 54 | memcpy(data, static_cast<const void*>(vector.constData()), count * sizeof(T)); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | template <typename Container> |
| 59 | void allocate(MemoryPool *pool, const Container &container) |
| 60 | { |
| 61 | count = container.size(); |
| 62 | data = reinterpret_cast<T*>(pool->allocate(size: count * sizeof(T))); |
| 63 | typename Container::ConstIterator it = container.constBegin(); |
| 64 | for (int i = 0; i < count; ++i) |
| 65 | new (data + i) T(*it++); |
| 66 | } |
| 67 | |
| 68 | int size() const |
| 69 | { return count; } |
| 70 | |
| 71 | const T &at(int index) const { |
| 72 | Q_ASSERT(index >= 0 && index < count); |
| 73 | return data[index]; |
| 74 | } |
| 75 | |
| 76 | T &at(int index) { |
| 77 | Q_ASSERT(index >= 0 && index < count); |
| 78 | return data[index]; |
| 79 | } |
| 80 | |
| 81 | T &operator[](int index) { |
| 82 | return at(index); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | int indexOf(const T &value) const { |
| 87 | for (int i = 0; i < count; ++i) |
| 88 | if (data[i] == value) |
| 89 | return i; |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | const T *begin() const { return data; } |
| 94 | const T *end() const { return data + count; } |
| 95 | |
| 96 | T *begin() { return data; } |
| 97 | T *end() { return data + count; } |
| 98 | }; |
| 99 | |
| 100 | } // namespace QQmlJS |
| 101 | |
| 102 | QT_END_NAMESPACE |
| 103 | |
| 104 | #endif // QQMLJSFIXEDPOOLARRAY_P_H |
| 105 | |