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
qringbuffer_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 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 QRINGBUFFER_P_H
5#define QRINGBUFFER_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 a number of Qt sources files. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19#include <QtCore/qbytearray.h>
20#include <QtCore/qlist.h>
21
23
24#ifndef QRINGBUFFER_CHUNKSIZE
25#define QRINGBUFFER_CHUNKSIZE 4096
26#endif
27
29{
30public:
31 // initialization and cleanup
32 QRingChunk() noexcept = default;
34 chunk(alloc, Qt::Uninitialized), tailOffset(0)
35 {
36 }
37 explicit inline QRingChunk(const QByteArray &qba) noexcept :
38 chunk(qba), tailOffset(qba.size())
39 {
40 }
41 explicit QRingChunk(QByteArray &&qba) noexcept :
42 chunk(std::move(qba)), tailOffset(chunk.size())
43 {
44 }
45
46 inline void swap(QRingChunk &other) noexcept
47 {
48 chunk.swap(other.chunk);
49 qSwap(headOffset, other.headOffset);
50 qSwap(tailOffset, other.tailOffset);
51 }
52
53 // allocating and sharing
54 void allocate(qsizetype alloc);
55 inline bool isShared() const
56 {
57 return !chunk.isDetached();
58 }
59 Q_CORE_EXPORT void detach();
61
62 // getters
63 inline qsizetype head() const
64 {
65 return headOffset;
66 }
67 inline qsizetype size() const
68 {
69 return tailOffset - headOffset;
70 }
71 inline qsizetype capacity() const
72 {
73 return chunk.size();
74 }
75 inline qsizetype available() const
76 {
77 return chunk.size() - tailOffset;
78 }
79 inline const char *data() const
80 {
81 return chunk.constData() + headOffset;
82 }
83 inline char *data()
84 {
85 if (isShared())
86 detach();
87 return chunk.data() + headOffset;
88 }
89
90 // array management
92 {
93 Q_ASSERT(headOffset + offset >= 0);
94 Q_ASSERT(size() - offset > 0);
95
96 headOffset += offset;
97 }
98 inline void grow(qsizetype offset)
99 {
100 Q_ASSERT(size() + offset > 0);
101 Q_ASSERT(head() + size() + offset <= capacity());
102
103 tailOffset += offset;
104 }
105 inline void assign(const QByteArray &qba)
106 {
107 chunk = qba;
108 headOffset = 0;
109 tailOffset = qba.size();
110 }
111 void assign(QByteArray &&qba)
112 {
113 chunk = std::move(qba);
114 headOffset = 0;
115 tailOffset = chunk.size();
116 }
117 inline void reset()
118 {
119 headOffset = tailOffset = 0;
120 }
121 inline void clear()
122 {
123 *this = {};
124 }
125
126private:
127 QByteArray chunk;
128 qsizetype headOffset = 0;
129 qsizetype tailOffset = 0;
130};
131Q_DECLARE_SHARED(QRingChunk)
132
134{
135 Q_DISABLE_COPY(QRingBuffer)
136public:
137 explicit inline QRingBuffer(int growth = QRINGBUFFER_CHUNKSIZE) :
138 bufferSize(0), basicBlockSize(growth) { }
139
140 QRingBuffer(QRingBuffer &&) noexcept = default;
141 QRingBuffer &operator=(QRingBuffer &&) noexcept = default;
142
143 inline void setChunkSize(int size) {
144 basicBlockSize = size;
145 }
146
147 inline int chunkSize() const {
148 return basicBlockSize;
149 }
150
151 inline qint64 nextDataBlockSize() const {
152 return bufferSize == 0 ? Q_INT64_C(0) : buffers.first().size();
153 }
154
155 inline const char *readPointer() const {
156 return bufferSize == 0 ? nullptr : buffers.first().data();
157 }
158
159 Q_CORE_EXPORT const char *readPointerAtPosition(qint64 pos, qint64 &length) const;
160 Q_CORE_EXPORT void free(qint64 bytes);
161 Q_CORE_EXPORT char *reserve(qint64 bytes);
162 Q_CORE_EXPORT char *reserveFront(qint64 bytes);
163
164 inline void truncate(qint64 pos) {
165 Q_ASSERT(pos >= 0 && pos <= size());
166
167 chop(size() - pos);
168 }
169
170 Q_CORE_EXPORT void chop(qint64 bytes);
171
172 inline bool isEmpty() const {
173 return bufferSize == 0;
174 }
175
176 inline int getChar() {
177 if (isEmpty())
178 return -1;
179 char c = *readPointer();
180 free(1);
181 return int(uchar(c));
182 }
183
184 inline void putChar(char c) {
185 char *ptr = reserve(1);
186 *ptr = c;
187 }
188
189 void ungetChar(char c)
190 {
191 char *ptr = reserveFront(1);
192 *ptr = c;
193 }
194
195
196 inline qint64 size() const {
197 return bufferSize;
198 }
199
200 Q_CORE_EXPORT void clear();
201 inline qint64 indexOf(char c) const { return indexOf(c, size()); }
202 Q_CORE_EXPORT qint64 indexOf(char c, qint64 maxLength, qint64 pos = 0) const;
203 Q_CORE_EXPORT qint64 read(char *data, qint64 maxLength);
204 Q_CORE_EXPORT QByteArray read();
205 Q_CORE_EXPORT qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const;
206 Q_CORE_EXPORT void append(const char *data, qint64 size);
207 Q_CORE_EXPORT void append(const QByteArray &qba);
208 Q_CORE_EXPORT void append(QByteArray &&qba);
209
211 qint64 bytesToSkip = qMin(length, bufferSize);
212
213 free(bytesToSkip);
214 return bytesToSkip;
215 }
216
217 Q_CORE_EXPORT qint64 readLine(char *data, qint64 maxLength);
218
219 inline bool canReadLine() const {
220 return indexOf('\n') >= 0;
221 }
222
223private:
224 QList<QRingChunk> buffers;
225 qint64 bufferSize;
226 int basicBlockSize;
227};
228
230
232
233#endif // QRINGBUFFER_P_H
\inmodule QtCore
Definition qbytearray.h:57
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:611
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
bool isDetached() const
Definition qbytearray.h:627
void swap(QByteArray &other) noexcept
Definition qbytearray.h:104
bool isEmpty() const
qint64 size() const
QRingBuffer(QRingBuffer &&) noexcept=default
const char * readPointer() const
qint64 skip(qint64 length)
bool canReadLine() const
void truncate(qint64 pos)
qint64 indexOf(char c) const
QRingBuffer(int growth=QRINGBUFFER_CHUNKSIZE)
qint64 nextDataBlockSize() const
int chunkSize() const
void ungetChar(char c)
void putChar(char c)
void advance(qsizetype offset)
void allocate(qsizetype alloc)
void grow(qsizetype offset)
char * data()
QRingChunk() noexcept=default
bool isShared() const
qsizetype available() const
const char * data() const
qsizetype size() const
qsizetype capacity() const
void assign(const QByteArray &qba)
Q_CORE_EXPORT void detach()
QRingChunk(const QByteArray &qba) noexcept
QByteArray toByteArray() &&
QRingChunk(QByteArray &&qba) noexcept
qsizetype head() const
void swap(QRingChunk &other) noexcept
void assign(QByteArray &&qba)
b clear()
list append(new Employee("Blackpool", "Stephen"))
set reserve(20000)
Combined button and popup list for selecting options.
Definition qcompare.h:63
constexpr int Uninitialized
static ControlElement< T > * ptr(QWidget *widget)
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLuint const GLuint * buffers
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint GLenum GLsizei length
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
const GLubyte * c
GLsizei maxLength
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QRINGBUFFER_CHUNKSIZE
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:158
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
unsigned char uchar
Definition qtypes.h:32
ptrdiff_t qsizetype
Definition qtypes.h:165
long long qint64
Definition qtypes.h:60
#define Q_INT64_C(c)
Definition qtypes.h:57
ReturnedValue read(const char *data)
#define explicit
QObject::connect nullptr
list indexOf("B")
QSharedPointer< T > other(t)
[5]