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 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | class Q_CORE_EXPORT QNonContiguousByteDevice : public QObject |
29 | { |
30 | Q_OBJECT |
31 | public: |
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 | |
41 | protected: |
42 | QNonContiguousByteDevice(); |
43 | |
44 | Q_SIGNALS: |
45 | void readyRead(); |
46 | void readProgress(qint64 current, qint64 total); |
47 | }; |
48 | |
49 | class Q_CORE_EXPORT QNonContiguousByteDeviceFactory |
50 | { |
51 | public: |
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 | |
67 | class QNonContiguousByteDeviceByteArrayImpl : public QNonContiguousByteDevice |
68 | { |
69 | Q_OBJECT |
70 | public: |
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 | |
80 | protected: |
81 | QByteArray byteArray; |
82 | qint64 currentPosition; |
83 | }; |
84 | |
85 | class QNonContiguousByteDeviceRingBufferImpl : public QNonContiguousByteDevice |
86 | { |
87 | Q_OBJECT |
88 | public: |
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 | |
98 | protected: |
99 | std::shared_ptr<QRingBuffer> ringBuffer; |
100 | qint64 currentPosition = 0; |
101 | }; |
102 | |
103 | class QNonContiguousByteDeviceIoDeviceImpl : public QNonContiguousByteDevice |
104 | { |
105 | Q_OBJECT |
106 | public: |
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 | |
116 | protected: |
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 | |
127 | class QNonContiguousByteDeviceBufferImpl : public QNonContiguousByteDevice |
128 | { |
129 | Q_OBJECT |
130 | public: |
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 | |
139 | protected: |
140 | QByteArray byteArray; |
141 | QNonContiguousByteDeviceByteArrayImpl *arrayImpl; |
142 | }; |
143 | |
144 | // ... and the reverse thing |
145 | class QByteDeviceWrappingIoDevice : public QIODevice |
146 | { |
147 | Q_OBJECT |
148 | public: |
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 | |
156 | protected: |
157 | qint64 readData(char *data, qint64 maxSize) override; |
158 | qint64 writeData(const char *data, qint64 maxSize) override; |
159 | |
160 | QNonContiguousByteDevice *byteDevice; |
161 | }; |
162 | |
163 | QT_END_NAMESPACE |
164 | |
165 | #endif |
166 | |