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
qdatabuffer_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
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#include "QtCore/qbytearray.h"
20
21#include <stdlib.h>
22
24
25template <typename Type> class QDataBuffer
26{
27 Q_DISABLE_COPY_MOVE(QDataBuffer)
28public:
30 {
31 capacity = res;
32 if (res) {
34 QT_WARNING_DISABLE_GCC("-Walloc-size-larger-than=")
35 buffer = (Type*) malloc(capacity * sizeof(Type));
38 } else {
39 buffer = nullptr;
40 }
41 siz = 0;
42 }
43
45 {
46 if (buffer)
47 free(buffer);
48 }
49
50 inline void reset() { siz = 0; }
51
52 inline bool isEmpty() const { return siz==0; }
53
54 qsizetype size() const { return siz; }
55 inline Type *data() const { return buffer; }
56
57 Type &at(qsizetype i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; }
58 const Type &at(qsizetype i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; }
59 inline Type &last() { Q_ASSERT(!isEmpty()); return buffer[siz-1]; }
60 inline const Type &last() const { Q_ASSERT(!isEmpty()); return buffer[siz-1]; }
61 inline Type &first() { Q_ASSERT(!isEmpty()); return buffer[0]; }
62 inline const Type &first() const { Q_ASSERT(!isEmpty()); return buffer[0]; }
63
64 inline void add(const Type &t) {
65 reserve(siz + 1);
66 buffer[siz] = t;
67 ++siz;
68 }
69
70 inline void pop_back() {
71 Q_ASSERT(siz > 0);
72 --siz;
73 }
74
77 siz = size;
78 }
79
81 if (size > capacity) {
82 if (capacity == 0)
83 capacity = 1;
84 while (capacity < size)
85 capacity *= 2;
86 buffer = (Type*) realloc(static_cast<void*>(buffer), capacity * sizeof(Type));
88 }
89 }
90
92 Q_ASSERT(capacity >= size);
93 capacity = size;
94 if (size) {
95 buffer = (Type*) realloc(static_cast<void*>(buffer), capacity * sizeof(Type));
97 siz = std::min(siz, size);
98 } else {
99 free(buffer);
100 buffer = nullptr;
101 siz = 0;
102 }
103 }
104
105 inline void swap(QDataBuffer<Type> &other) {
106 qSwap(capacity, other.capacity);
107 qSwap(siz, other.siz);
108 qSwap(buffer, other.buffer);
109 }
110
111 inline QDataBuffer &operator<<(const Type &t) { add(t); return *this; }
112
113private:
114 qsizetype capacity;
115 qsizetype siz;
116 Type *buffer;
117};
118
120
121#endif // QDATABUFFER_P_H
void resize(qsizetype size)
Type & first()
void add(const Type &t)
void swap(QDataBuffer< Type > &other)
Type & at(qsizetype i)
QDataBuffer(qsizetype res)
qsizetype size() const
void shrink(qsizetype size)
QDataBuffer & operator<<(const Type &t)
Type & last()
const Type & last() const
bool isEmpty() const
void reserve(qsizetype size)
const Type & at(qsizetype i) const
Type * data() const
const Type & first() const
void pop_back()
Combined button and popup list for selecting options.
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_GCC(text)
#define QT_WARNING_PUSH
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint buffer
GLuint res
GLdouble GLdouble t
Definition qopenglext.h:243
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
ptrdiff_t qsizetype
Definition qtypes.h:165
Q_CHECK_PTR(a=new int[80])
QSharedPointer< T > other(t)
[5]
Definition moc.h:23