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 QNONCONTIGUOUSBYTEDEVICE_P_H
5#define QNONCONTIGUOUSBYTEDEVICE_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/qobject.h>
19#include <QtCore/qbytearray.h>
20#include <QtCore/qbuffer.h>
21#include <QtCore/qiodevice.h>
22#include "private/qringbuffer_p.h"
23
24#include <memory>
25
26QT_BEGIN_NAMESPACE
27
28class Q_CORE_EXPORT QNonContiguousByteDevice : public QObject
29{
30 Q_OBJECT
31public:
32 virtual const char *readPointer(qint64 maximumLength, qint64 &len) = 0;
33 virtual bool advanceReadPointer(qint64 amount) = 0;
34 virtual bool atEnd() const = 0;
35 virtual qint64 pos() const { return -1; }
36 virtual bool reset() = 0;
37 virtual qint64 size() const = 0;
38
39 virtual ~QNonContiguousByteDevice();
40
41protected:
42 QNonContiguousByteDevice();
43
44Q_SIGNALS:
45 void readyRead();
46 void readProgress(qint64 current, qint64 total);
47};
48
49class Q_CORE_EXPORT QNonContiguousByteDeviceFactory
50{
51public:
52 static QNonContiguousByteDevice *create(QIODevice *device);
53 static std::shared_ptr<QNonContiguousByteDevice> createShared(QIODevice *device);
54
55 static QNonContiguousByteDevice *create(const QByteArray &byteArray);
56 static std::shared_ptr<QNonContiguousByteDevice> createShared(const QByteArray &byteArray);
57
58 static QNonContiguousByteDevice *create(std::shared_ptr<QRingBuffer> ringBuffer);
59 static std::shared_ptr<QNonContiguousByteDevice> createShared(std::shared_ptr<QRingBuffer> ringBuffer);
60
61 static QIODevice *wrap(QNonContiguousByteDevice *byteDevice);
62};
63
64// the actual implementations
65//
66
67class QNonContiguousByteDeviceByteArrayImpl : public QNonContiguousByteDevice
68{
69 Q_OBJECT
70public:
71 explicit QNonContiguousByteDeviceByteArrayImpl(QByteArray ba);
72 ~QNonContiguousByteDeviceByteArrayImpl();
73 const char *readPointer(qint64 maximumLength, qint64 &len) override;
74 bool advanceReadPointer(qint64 amount) override;
75 bool atEnd() const override;
76 bool reset() override;
77 qint64 size() const override;
78 qint64 pos() const override;
79
80protected:
81 QByteArray byteArray;
82 qint64 currentPosition;
83};
84
85class QNonContiguousByteDeviceRingBufferImpl : public QNonContiguousByteDevice
86{
87 Q_OBJECT
88public:
89 explicit QNonContiguousByteDeviceRingBufferImpl(std::shared_ptr<QRingBuffer> rb);
90 ~QNonContiguousByteDeviceRingBufferImpl();
91 const char *readPointer(qint64 maximumLength, qint64 &len) override;
92 bool advanceReadPointer(qint64 amount) override;
93 bool atEnd() const override;
94 bool reset() override;
95 qint64 size() const override;
96 qint64 pos() const override;
97
98protected:
99 std::shared_ptr<QRingBuffer> ringBuffer;
100 qint64 currentPosition = 0;
101};
102
103class QNonContiguousByteDeviceIoDeviceImpl : public QNonContiguousByteDevice
104{
105 Q_OBJECT
106public:
107 explicit QNonContiguousByteDeviceIoDeviceImpl(QIODevice *d);
108 ~QNonContiguousByteDeviceIoDeviceImpl();
109 const char *readPointer(qint64 maximumLength, qint64 &len) override;
110 bool advanceReadPointer(qint64 amount) override;
111 bool atEnd() const override;
112 bool reset() override;
113 qint64 size() const override;
114 qint64 pos() const override;
115
116protected:
117 QIODevice *device;
118 QByteArray *currentReadBuffer;
119 qint64 currentReadBufferSize;
120 qint64 currentReadBufferAmount;
121 qint64 currentReadBufferPosition;
122 qint64 totalAdvancements;
123 bool eof;
124 qint64 initialPosition;
125};
126
127class QNonContiguousByteDeviceBufferImpl : public QNonContiguousByteDevice
128{
129 Q_OBJECT
130public:
131 explicit QNonContiguousByteDeviceBufferImpl(QBuffer *b);
132 ~QNonContiguousByteDeviceBufferImpl();
133 const char *readPointer(qint64 maximumLength, qint64 &len) override;
134 bool advanceReadPointer(qint64 amount) override;
135 bool atEnd() const override;
136 bool reset() override;
137 qint64 size() const override;
138
139protected:
140 QByteArray byteArray;
141 QNonContiguousByteDeviceByteArrayImpl *arrayImpl;
142};
143
144// ... and the reverse thing
145class QByteDeviceWrappingIoDevice : public QIODevice
146{
147 Q_OBJECT
148public:
149 explicit QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd);
150 ~QByteDeviceWrappingIoDevice();
151 bool isSequential() const override;
152 bool atEnd() const override;
153 bool reset() override;
154 qint64 size() const override;
155
156protected:
157 qint64 readData(char *data, qint64 maxSize) override;
158 qint64 writeData(const char *data, qint64 maxSize) override;
159
160 QNonContiguousByteDevice *byteDevice;
161};
162
163QT_END_NAMESPACE
164
165#endif
166

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

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