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
20#include "QtCore/qbytearray.h"
21#include "QtCore/qtypeinfo.h"
22
23#include <stdlib.h>
24
26
27template <typename Type> class QDataBuffer
28{
29 Q_DISABLE_COPY_MOVE(QDataBuffer)
30public:
32 {
33 capacity = res;
34 if (res) {
36 QT_WARNING_DISABLE_GCC("-Walloc-size-larger-than=")
37 buffer = (Type*) malloc(capacity * sizeof(Type));
40 } else {
41 buffer = nullptr;
42 }
43 siz = 0;
44 }
45
47 {
48 static_assert(!QTypeInfo<Type>::isComplex);
49 if (buffer)
50 free(buffer);
51 }
52
53 inline void reset() { siz = 0; }
54
55 inline bool isEmpty() const { return siz==0; }
56
57 qsizetype size() const { return siz; }
58 inline Type *data() const { return buffer; }
59
60 Type &at(qsizetype i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; }
61 const Type &at(qsizetype i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; }
62 inline Type &last() { Q_ASSERT(!isEmpty()); return buffer[siz-1]; }
63 inline const Type &last() const { Q_ASSERT(!isEmpty()); return buffer[siz-1]; }
64 inline Type &first() { Q_ASSERT(!isEmpty()); return buffer[0]; }
65 inline const Type &first() const { Q_ASSERT(!isEmpty()); return buffer[0]; }
66
67 inline void add(const Type &t) {
68 reserve(siz + 1);
69 buffer[siz] = t;
70 ++siz;
71 }
72
73 inline void pop_back() {
74 Q_ASSERT(siz > 0);
75 --siz;
76 }
77
80 siz = size;
81 }
82
84 if (size > capacity) {
85 if (capacity == 0)
86 capacity = 1;
87 while (capacity < size)
88 capacity *= 2;
89 buffer = (Type*) realloc(static_cast<void*>(buffer), capacity * sizeof(Type));
91 }
92 }
93
95 Q_ASSERT(capacity >= size);
96 capacity = size;
97 if (size) {
98 buffer = (Type*) realloc(static_cast<void*>(buffer), capacity * sizeof(Type));
100 siz = std::min(siz, size);
101 } else {
102 free(buffer);
103 buffer = nullptr;
104 siz = 0;
105 }
106 }
107
108 inline void swap(QDataBuffer<Type> &other) {
109 qSwap(capacity, other.capacity);
110 qSwap(siz, other.siz);
111 qSwap(buffer, other.buffer);
112 }
113
114 inline QDataBuffer &operator<<(const Type &t) { add(t); return *this; }
115
116private:
117 qsizetype capacity;
118 qsizetype siz;
119 Type *buffer;
120};
121
123
124#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