diff options
Diffstat (limited to 'src/gui/painting/qdatabuffer_p.h')
-rw-r--r-- | src/gui/painting/qdatabuffer_p.h | 67 |
1 files changed, 20 insertions, 47 deletions
diff --git a/src/gui/painting/qdatabuffer_p.h b/src/gui/painting/qdatabuffer_p.h index 676750b716..aa8335542d 100644 --- a/src/gui/painting/qdatabuffer_p.h +++ b/src/gui/painting/qdatabuffer_p.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QDATABUFFER_P_H #define QDATABUFFER_P_H @@ -52,7 +16,9 @@ // #include <QtGui/private/qtguiglobal_p.h> + #include "QtCore/qbytearray.h" +#include "QtCore/qtypeinfo.h" #include <stdlib.h> @@ -62,11 +28,14 @@ template <typename Type> class QDataBuffer { Q_DISABLE_COPY_MOVE(QDataBuffer) public: - QDataBuffer(int res) + explicit QDataBuffer(qsizetype res) { capacity = res; if (res) { + QT_WARNING_PUSH + QT_WARNING_DISABLE_GCC("-Walloc-size-larger-than=") buffer = (Type*) malloc(capacity * sizeof(Type)); + QT_WARNING_POP Q_CHECK_PTR(buffer); } else { buffer = nullptr; @@ -76,6 +45,7 @@ public: ~QDataBuffer() { + static_assert(!QTypeInfo<Type>::isComplex); if (buffer) free(buffer); } @@ -84,11 +54,11 @@ public: inline bool isEmpty() const { return siz==0; } - inline int size() const { return siz; } + qsizetype size() const { return siz; } inline Type *data() const { return buffer; } - inline Type &at(int i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } - inline const Type &at(int i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } + Type &at(qsizetype i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } + const Type &at(qsizetype i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } inline Type &last() { Q_ASSERT(!isEmpty()); return buffer[siz-1]; } inline const Type &last() const { Q_ASSERT(!isEmpty()); return buffer[siz-1]; } inline Type &first() { Q_ASSERT(!isEmpty()); return buffer[0]; } @@ -105,12 +75,12 @@ public: --siz; } - inline void resize(int size) { + void resize(qsizetype size) { reserve(size); siz = size; } - inline void reserve(int size) { + void reserve(qsizetype size) { if (size > capacity) { if (capacity == 0) capacity = 1; @@ -121,14 +91,17 @@ public: } } - inline void shrink(int size) { + void shrink(qsizetype size) { + Q_ASSERT(capacity >= size); capacity = size; if (size) { buffer = (Type*) realloc(static_cast<void*>(buffer), capacity * sizeof(Type)); Q_CHECK_PTR(buffer); + siz = std::min(siz, size); } else { free(buffer); buffer = nullptr; + siz = 0; } } @@ -141,8 +114,8 @@ public: inline QDataBuffer &operator<<(const Type &t) { add(t); return *this; } private: - int capacity; - int siz; + qsizetype capacity; + qsizetype siz; Type *buffer; }; |