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