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 QIODEVICE_P_H
5#define QIODEVICE_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 QIODevice. 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 "QtCore/qbytearray.h"
19#include "QtCore/qiodevice.h"
20#include "QtCore/qobjectdefs.h"
21#include "QtCore/qstring.h"
22#include "QtCore/qvarlengtharray.h"
23#include "private/qringbuffer_p.h"
24#ifndef QT_NO_QOBJECT
25#include "private/qobject_p.h"
26#endif
27
28QT_BEGIN_NAMESPACE
29
30#ifndef QIODEVICE_BUFFERSIZE
31#define QIODEVICE_BUFFERSIZE 16384
32#endif
33
34Q_CORE_EXPORT int qt_subtract_from_timeout(int timeout, int elapsed);
35
36class Q_CORE_EXPORT QIODevicePrivate
37#ifndef QT_NO_QOBJECT
38 : public QObjectPrivate
39#endif
40{
41 Q_DECLARE_PUBLIC(QIODevice)
42 Q_DISABLE_COPY_MOVE(QIODevicePrivate)
43
44public:
45 QIODevicePrivate();
46 virtual ~QIODevicePrivate();
47
48 // The size of this class is a subject of the library hook data.
49 // When adding a new member, do not make gaps and be aware
50 // about the padding. Accordingly, adjust offsets in
51 // tests/auto/other/toolsupport and bump the TypeInformationVersion
52 // field in src/corelib/global/qhooks.cpp, to notify the developers.
53 qint64 pos = 0;
54 qint64 devicePos = 0;
55 qint64 transactionPos = 0;
56
57 class QRingBufferRef
58 {
59 QRingBuffer *m_buf;
60 inline QRingBufferRef() : m_buf(nullptr) { }
61 friend class QIODevicePrivate;
62 public:
63 // wrap functions from QRingBuffer
64 inline void setChunkSize(int size) { Q_ASSERT(m_buf); m_buf->setChunkSize(size); }
65 inline int chunkSize() const { Q_ASSERT(m_buf); return m_buf->chunkSize(); }
66 inline qint64 nextDataBlockSize() const { return (m_buf ? m_buf->nextDataBlockSize() : Q_INT64_C(0)); }
67 inline const char *readPointer() const { return (m_buf ? m_buf->readPointer() : nullptr); }
68 inline const char *readPointerAtPosition(qint64 pos, qint64 &length) const { Q_ASSERT(m_buf); return m_buf->readPointerAtPosition(pos, length); }
69 inline void free(qint64 bytes) { Q_ASSERT(m_buf); m_buf->free(bytes); }
70 inline char *reserve(qint64 bytes) { Q_ASSERT(m_buf); return m_buf->reserve(bytes); }
71 inline char *reserveFront(qint64 bytes) { Q_ASSERT(m_buf); return m_buf->reserveFront(bytes); }
72 inline void truncate(qint64 pos) { Q_ASSERT(m_buf); m_buf->truncate(pos); }
73 inline void chop(qint64 bytes) { Q_ASSERT(m_buf); m_buf->chop(bytes); }
74 inline bool isEmpty() const { return !m_buf || m_buf->isEmpty(); }
75 inline int getChar() { return (m_buf ? m_buf->getChar() : -1); }
76 inline void putChar(char c) { Q_ASSERT(m_buf); m_buf->putChar(c); }
77 inline void ungetChar(char c) { Q_ASSERT(m_buf); m_buf->ungetChar(c); }
78 inline qint64 size() const { return (m_buf ? m_buf->size() : Q_INT64_C(0)); }
79 inline void clear() { if (m_buf) m_buf->clear(); }
80 inline qint64 indexOf(char c) const { return (m_buf ? m_buf->indexOf(c, maxLength: m_buf->size()) : Q_INT64_C(-1)); }
81 inline qint64 indexOf(char c, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->indexOf(c, maxLength, pos) : Q_INT64_C(-1)); }
82 inline qint64 read(char *data, qint64 maxLength) { return (m_buf ? m_buf->read(data, maxLength) : Q_INT64_C(0)); }
83 inline QByteArray read() { return (m_buf ? m_buf->read() : QByteArray()); }
84 inline qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->peek(data, maxLength, pos) : Q_INT64_C(0)); }
85 inline void append(const char *data, qint64 size) { Q_ASSERT(m_buf); m_buf->append(data, size); }
86 inline void append(const QByteArray &qba) { Q_ASSERT(m_buf); m_buf->append(qba); }
87 inline qint64 skip(qint64 length) { return (m_buf ? m_buf->skip(length) : Q_INT64_C(0)); }
88 inline qint64 readLine(char *data, qint64 maxLength) { return (m_buf ? m_buf->readLine(data, maxLength) : Q_INT64_C(-1)); }
89 inline bool canReadLine() const { return m_buf && m_buf->canReadLine(); }
90 };
91
92 QRingBufferRef buffer;
93 QRingBufferRef writeBuffer;
94 const QByteArray *currentWriteChunk = nullptr;
95 int readChannelCount = 0;
96 int writeChannelCount = 0;
97 int currentReadChannel = 0;
98 int currentWriteChannel = 0;
99 int readBufferChunkSize = QIODEVICE_BUFFERSIZE;
100 int writeBufferChunkSize = 0;
101
102 QVarLengthArray<QRingBuffer, 2> readBuffers;
103 QVarLengthArray<QRingBuffer, 1> writeBuffers;
104 QString errorString;
105 QIODevice::OpenMode openMode = QIODevice::NotOpen;
106
107 bool transactionStarted = false;
108 bool baseReadLineDataCalled = false;
109
110 virtual bool putCharHelper(char c);
111
112 enum AccessMode : quint8 {
113 Unset,
114 Sequential,
115 RandomAccess
116 };
117 mutable AccessMode accessMode = Unset;
118 inline bool isSequential() const
119 {
120 if (accessMode == Unset)
121 accessMode = q_func()->isSequential() ? Sequential : RandomAccess;
122 return accessMode == Sequential;
123 }
124
125 inline bool isBufferEmpty() const
126 {
127 return buffer.isEmpty() || (transactionStarted && isSequential()
128 && transactionPos == buffer.size());
129 }
130 bool allWriteBuffersEmpty() const;
131
132 void seekBuffer(qint64 newPos);
133
134 inline void setCurrentReadChannel(int channel)
135 {
136 buffer.m_buf = (channel < readBuffers.size() ? &readBuffers[channel] : nullptr);
137 currentReadChannel = channel;
138 }
139 inline void setCurrentWriteChannel(int channel)
140 {
141 writeBuffer.m_buf = (channel < writeBuffers.size() ? &writeBuffers[channel] : nullptr);
142 currentWriteChannel = channel;
143 }
144 void setReadChannelCount(int count);
145 void setWriteChannelCount(int count);
146
147 qint64 read(char *data, qint64 maxSize, bool peeking = false);
148 qint64 readLine(char *data, qint64 maxSize);
149 virtual qint64 peek(char *data, qint64 maxSize);
150 virtual QByteArray peek(qint64 maxSize);
151 qint64 skipByReading(qint64 maxSize);
152 void write(const char *data, qint64 size);
153
154 inline bool isWriteChunkCached(const char *data, qint64 size) const
155 {
156 return currentWriteChunk != nullptr
157 && currentWriteChunk->constData() == data
158 && currentWriteChunk->size() == size;
159 }
160
161#ifdef QT_NO_QOBJECT
162 QIODevice *q_ptr = nullptr;
163#endif
164};
165
166QT_END_NAMESPACE
167
168#endif // QIODEVICE_P_H
169

source code of qtbase/src/corelib/io/qiodevice_p.h