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(QByteArray *byteArray); |
56 | static std::shared_ptr<QNonContiguousByteDevice> createShared(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 | public: |
70 | explicit QNonContiguousByteDeviceByteArrayImpl(QByteArray *ba); |
71 | ~QNonContiguousByteDeviceByteArrayImpl(); |
72 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
73 | bool advanceReadPointer(qint64 amount) override; |
74 | bool atEnd() const override; |
75 | bool reset() override; |
76 | qint64 size() const override; |
77 | qint64 pos() const override; |
78 | |
79 | protected: |
80 | QByteArray *byteArray; |
81 | qint64 currentPosition; |
82 | }; |
83 | |
84 | class QNonContiguousByteDeviceRingBufferImpl : public QNonContiguousByteDevice |
85 | { |
86 | public: |
87 | explicit QNonContiguousByteDeviceRingBufferImpl(std::shared_ptr<QRingBuffer> rb); |
88 | ~QNonContiguousByteDeviceRingBufferImpl(); |
89 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
90 | bool advanceReadPointer(qint64 amount) override; |
91 | bool atEnd() const override; |
92 | bool reset() override; |
93 | qint64 size() const override; |
94 | qint64 pos() const override; |
95 | |
96 | protected: |
97 | std::shared_ptr<QRingBuffer> ringBuffer; |
98 | qint64 currentPosition = 0; |
99 | }; |
100 | |
101 | class QNonContiguousByteDeviceIoDeviceImpl : public QNonContiguousByteDevice |
102 | { |
103 | Q_OBJECT |
104 | public: |
105 | explicit QNonContiguousByteDeviceIoDeviceImpl(QIODevice *d); |
106 | ~QNonContiguousByteDeviceIoDeviceImpl(); |
107 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
108 | bool advanceReadPointer(qint64 amount) override; |
109 | bool atEnd() const override; |
110 | bool reset() override; |
111 | qint64 size() const override; |
112 | qint64 pos() const override; |
113 | |
114 | protected: |
115 | QIODevice *device; |
116 | QByteArray *currentReadBuffer; |
117 | qint64 currentReadBufferSize; |
118 | qint64 currentReadBufferAmount; |
119 | qint64 currentReadBufferPosition; |
120 | qint64 totalAdvancements; |
121 | bool eof; |
122 | qint64 initialPosition; |
123 | }; |
124 | |
125 | class QNonContiguousByteDeviceBufferImpl : public QNonContiguousByteDevice |
126 | { |
127 | Q_OBJECT |
128 | public: |
129 | explicit QNonContiguousByteDeviceBufferImpl(QBuffer *b); |
130 | ~QNonContiguousByteDeviceBufferImpl(); |
131 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
132 | bool advanceReadPointer(qint64 amount) override; |
133 | bool atEnd() const override; |
134 | bool reset() override; |
135 | qint64 size() const override; |
136 | |
137 | protected: |
138 | QBuffer *buffer; |
139 | QByteArray byteArray; |
140 | QNonContiguousByteDeviceByteArrayImpl *arrayImpl; |
141 | }; |
142 | |
143 | // ... and the reverse thing |
144 | class QByteDeviceWrappingIoDevice : public QIODevice |
145 | { |
146 | public: |
147 | explicit QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd); |
148 | ~QByteDeviceWrappingIoDevice(); |
149 | bool isSequential() const override; |
150 | bool atEnd() const override; |
151 | bool reset() override; |
152 | qint64 size() const override; |
153 | |
154 | protected: |
155 | qint64 readData(char *data, qint64 maxSize) override; |
156 | qint64 writeData(const char *data, qint64 maxSize) override; |
157 | |
158 | QNonContiguousByteDevice *byteDevice; |
159 | }; |
160 | |
161 | QT_END_NAMESPACE |
162 | |
163 | #endif |
164 | |