1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QNONCONTIGUOUSBYTEDEVICE_P_H |
41 | #define QNONCONTIGUOUSBYTEDEVICE_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists for the convenience |
48 | // of a number of Qt sources files. This header file may change from |
49 | // version to version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/qobject.h> |
55 | #include <QtCore/qbytearray.h> |
56 | #include <QtCore/qbuffer.h> |
57 | #include <QtCore/qiodevice.h> |
58 | #include <QtCore/QSharedPointer> |
59 | #include "private/qringbuffer_p.h" |
60 | |
61 | QT_BEGIN_NAMESPACE |
62 | |
63 | class Q_CORE_EXPORT QNonContiguousByteDevice : public QObject |
64 | { |
65 | Q_OBJECT |
66 | public: |
67 | virtual const char* readPointer(qint64 maximumLength, qint64 &len) = 0; |
68 | virtual bool advanceReadPointer(qint64 amount) = 0; |
69 | virtual bool atEnd() const = 0; |
70 | virtual qint64 pos() const { return -1; } |
71 | virtual bool reset() = 0; |
72 | virtual qint64 size() const = 0; |
73 | |
74 | virtual ~QNonContiguousByteDevice(); |
75 | |
76 | protected: |
77 | QNonContiguousByteDevice(); |
78 | |
79 | |
80 | Q_SIGNALS: |
81 | void readyRead(); |
82 | void readProgress(qint64 current, qint64 total); |
83 | }; |
84 | |
85 | class Q_CORE_EXPORT QNonContiguousByteDeviceFactory |
86 | { |
87 | public: |
88 | static QNonContiguousByteDevice* create(QIODevice *device); |
89 | static QSharedPointer<QNonContiguousByteDevice> createShared(QIODevice *device); |
90 | |
91 | static QNonContiguousByteDevice* create(QByteArray *byteArray); |
92 | static QSharedPointer<QNonContiguousByteDevice> createShared(QByteArray *byteArray); |
93 | |
94 | static QNonContiguousByteDevice* create(QSharedPointer<QRingBuffer> ringBuffer); |
95 | static QSharedPointer<QNonContiguousByteDevice> createShared(QSharedPointer<QRingBuffer> ringBuffer); |
96 | |
97 | static QIODevice* wrap(QNonContiguousByteDevice* byteDevice); |
98 | }; |
99 | |
100 | // the actual implementations |
101 | // |
102 | |
103 | class QNonContiguousByteDeviceByteArrayImpl : public QNonContiguousByteDevice |
104 | { |
105 | public: |
106 | QNonContiguousByteDeviceByteArrayImpl(QByteArray *ba); |
107 | ~QNonContiguousByteDeviceByteArrayImpl(); |
108 | const char* readPointer(qint64 maximumLength, qint64 &len) override; |
109 | bool advanceReadPointer(qint64 amount) override; |
110 | bool atEnd() const override; |
111 | bool reset() override; |
112 | qint64 size() const override; |
113 | qint64 pos() const override; |
114 | protected: |
115 | QByteArray* byteArray; |
116 | qint64 currentPosition; |
117 | }; |
118 | |
119 | class QNonContiguousByteDeviceRingBufferImpl : public QNonContiguousByteDevice |
120 | { |
121 | public: |
122 | QNonContiguousByteDeviceRingBufferImpl(QSharedPointer<QRingBuffer> rb); |
123 | ~QNonContiguousByteDeviceRingBufferImpl(); |
124 | const char* readPointer(qint64 maximumLength, qint64 &len) override; |
125 | bool advanceReadPointer(qint64 amount) override; |
126 | bool atEnd() const override; |
127 | bool reset() override; |
128 | qint64 size() const override; |
129 | qint64 pos() const override; |
130 | protected: |
131 | QSharedPointer<QRingBuffer> ringBuffer; |
132 | qint64 currentPosition; |
133 | }; |
134 | |
135 | |
136 | class QNonContiguousByteDeviceIoDeviceImpl : public QNonContiguousByteDevice |
137 | { |
138 | Q_OBJECT |
139 | public: |
140 | QNonContiguousByteDeviceIoDeviceImpl(QIODevice *d); |
141 | ~QNonContiguousByteDeviceIoDeviceImpl(); |
142 | const char* readPointer(qint64 maximumLength, qint64 &len) override; |
143 | bool advanceReadPointer(qint64 amount) override; |
144 | bool atEnd() const override; |
145 | bool reset() override; |
146 | qint64 size() const override; |
147 | qint64 pos() const override; |
148 | protected: |
149 | QIODevice* device; |
150 | QByteArray* currentReadBuffer; |
151 | qint64 currentReadBufferSize; |
152 | qint64 currentReadBufferAmount; |
153 | qint64 currentReadBufferPosition; |
154 | qint64 totalAdvancements; |
155 | bool eof; |
156 | qint64 initialPosition; |
157 | }; |
158 | |
159 | class QNonContiguousByteDeviceBufferImpl : public QNonContiguousByteDevice |
160 | { |
161 | Q_OBJECT |
162 | public: |
163 | QNonContiguousByteDeviceBufferImpl(QBuffer *b); |
164 | ~QNonContiguousByteDeviceBufferImpl(); |
165 | const char* readPointer(qint64 maximumLength, qint64 &len) override; |
166 | bool advanceReadPointer(qint64 amount) override; |
167 | bool atEnd() const override; |
168 | bool reset() override; |
169 | qint64 size() const override; |
170 | protected: |
171 | QBuffer* buffer; |
172 | QByteArray byteArray; |
173 | QNonContiguousByteDeviceByteArrayImpl* arrayImpl; |
174 | }; |
175 | |
176 | // ... and the reverse thing |
177 | class QByteDeviceWrappingIoDevice : public QIODevice |
178 | { |
179 | public: |
180 | QByteDeviceWrappingIoDevice (QNonContiguousByteDevice *bd); |
181 | ~QByteDeviceWrappingIoDevice (); |
182 | bool isSequential() const override; |
183 | bool atEnd() const override; |
184 | bool reset() override; |
185 | qint64 size() const override; |
186 | protected: |
187 | qint64 readData(char *data, qint64 maxSize) override; |
188 | qint64 writeData(const char *data, qint64 maxSize) override; |
189 | |
190 | QNonContiguousByteDevice *byteDevice; |
191 | }; |
192 | |
193 | QT_END_NAMESPACE |
194 | |
195 | #endif |
196 | |