| 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 QDATABUFFER_P_H |
| 5 | #define QDATABUFFER_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 for the convenience |
| 12 | // of other Qt classes. 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 <QtGui/private/qtguiglobal_p.h> |
| 19 | |
| 20 | #include "QtCore/qalloc.h" |
| 21 | #include "QtCore/qbytearray.h" |
| 22 | #include "QtCore/qtypeinfo.h" |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | template <typename Type> class QDataBuffer |
| 29 | { |
| 30 | Q_DISABLE_COPY_MOVE(QDataBuffer) |
| 31 | public: |
| 32 | explicit QDataBuffer(qsizetype res) |
| 33 | { |
| 34 | capacity = res; |
| 35 | if (res) { |
| 36 | QT_WARNING_PUSH |
| 37 | QT_WARNING_DISABLE_GCC("-Walloc-size-larger-than=" ) |
| 38 | buffer = (Type*) QtPrivate::fittedMalloc(headerSize: 0, capacity: &capacity, elementSize: sizeof(Type)); |
| 39 | QT_WARNING_POP |
| 40 | Q_CHECK_PTR(buffer); |
| 41 | } else { |
| 42 | buffer = nullptr; |
| 43 | } |
| 44 | siz = 0; |
| 45 | } |
| 46 | |
| 47 | ~QDataBuffer() |
| 48 | { |
| 49 | static_assert(!QTypeInfo<Type>::isComplex); |
| 50 | if (buffer) |
| 51 | QtPrivate::sizedFree(buffer, capacity, sizeof(Type)); |
| 52 | } |
| 53 | |
| 54 | inline void reset() { siz = 0; } |
| 55 | |
| 56 | inline bool isEmpty() const { return siz==0; } |
| 57 | |
| 58 | qsizetype size() const { return siz; } |
| 59 | inline Type *data() const { return buffer; } |
| 60 | |
| 61 | Type &at(qsizetype i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } |
| 62 | const Type &at(qsizetype i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } |
| 63 | inline Type &last() { Q_ASSERT(!isEmpty()); return buffer[siz-1]; } |
| 64 | inline const Type &last() const { Q_ASSERT(!isEmpty()); return buffer[siz-1]; } |
| 65 | inline Type &first() { Q_ASSERT(!isEmpty()); return buffer[0]; } |
| 66 | inline const Type &first() const { Q_ASSERT(!isEmpty()); return buffer[0]; } |
| 67 | |
| 68 | inline void add(const Type &t) { |
| 69 | reserve(size: siz + 1); |
| 70 | buffer[siz] = t; |
| 71 | ++siz; |
| 72 | } |
| 73 | |
| 74 | inline void pop_back() { |
| 75 | Q_ASSERT(siz > 0); |
| 76 | --siz; |
| 77 | } |
| 78 | |
| 79 | void resize(qsizetype size) { |
| 80 | reserve(size); |
| 81 | siz = size; |
| 82 | } |
| 83 | |
| 84 | void reserve(qsizetype size) { |
| 85 | if (size > capacity) { |
| 86 | if (capacity == 0) |
| 87 | capacity = 1; |
| 88 | while (capacity < size) |
| 89 | capacity *= 2; |
| 90 | auto ptr = QtPrivate::fittedRealloc(ptr: static_cast<void*>(buffer), headerSize: 0, capacity: &capacity, elementSize: sizeof(Type)); |
| 91 | Q_CHECK_PTR(ptr); |
| 92 | buffer = static_cast<Type*>(ptr); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void shrink(qsizetype size) { |
| 97 | Q_ASSERT(capacity >= size); |
| 98 | if (size) { |
| 99 | capacity = size; |
| 100 | const auto ptr = QtPrivate::fittedRealloc(ptr: static_cast<void*>(buffer), headerSize: 0, capacity: &capacity, elementSize: sizeof(Type)); |
| 101 | Q_CHECK_PTR(ptr); |
| 102 | buffer = static_cast<Type*>(ptr); |
| 103 | siz = std::min(a: siz, b: size); |
| 104 | } else { |
| 105 | QtPrivate::sizedFree(buffer, capacity, sizeof(Type)); |
| 106 | capacity = size; |
| 107 | buffer = nullptr; |
| 108 | siz = 0; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | inline void swap(QDataBuffer<Type> &other) { |
| 113 | qSwap(value1&: capacity, value2&: other.capacity); |
| 114 | qSwap(value1&: siz, value2&: other.siz); |
| 115 | qSwap(buffer, other.buffer); |
| 116 | } |
| 117 | |
| 118 | inline QDataBuffer &operator<<(const Type &t) { add(t); return *this; } |
| 119 | |
| 120 | private: |
| 121 | qsizetype capacity; |
| 122 | qsizetype siz; |
| 123 | Type *buffer; |
| 124 | }; |
| 125 | |
| 126 | QT_END_NAMESPACE |
| 127 | |
| 128 | #endif // QDATABUFFER_P_H |
| 129 | |