Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qrandomaccessasyncfile.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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// Qt-Security score:significant reason:default
4
7
9
14QRandomAccessAsyncFileBackend::~QRandomAccessAsyncFileBackend() = default;
15QRandomAccessAsyncFilePrivate::QRandomAccessAsyncFilePrivate() = default;
17
19{
20 Q_Q(QRandomAccessAsyncFile);
21
22#if defined(QT_RANDOMACCESSASYNCFILE_QIORING) || defined(Q_OS_DARWIN)
23 m_backend = std::make_unique<QRandomAccessAsyncFileNativeBackend>(q);
24#endif
25 if (!m_backend || !m_backend->init()) {
26#if QT_CONFIG(thread) && QT_CONFIG(future)
27 m_backend = std::make_unique<QRandomAccessAsyncFileThreadPoolBackend>(q);
28 [[maybe_unused]]
29 bool result = m_backend->init();
30 Q_ASSERT(result); // it always succeeds
31#endif
32 }
33}
34
35QRandomAccessAsyncFile::QRandomAccessAsyncFile(QObject *parent)
36 : QObject{*new QRandomAccessAsyncFilePrivate, parent}
37{
38 d_func()->init();
39}
40
41QRandomAccessAsyncFile::~QRandomAccessAsyncFile()
42{
43 close();
44}
45
46void QRandomAccessAsyncFile::close()
47{
48 Q_D(QRandomAccessAsyncFile);
49 d->close();
50}
51
52qint64 QRandomAccessAsyncFile::size() const
53{
54 Q_D(const QRandomAccessAsyncFile);
55 return d->size();
56}
57
58/*!
59 \internal
60
61 Attempts to open the file \a filePath with mode \a mode.
62
63 \include qrandomaccessasyncfile.cpp returns-qiooperation
64*/
65QIOOperation *QRandomAccessAsyncFile::open(const QString &filePath, QIODeviceBase::OpenMode mode)
66{
67 Q_D(QRandomAccessAsyncFile);
68 return d->open(filePath, mode);
69}
70
71/*!
72 \internal
73
74 Flushes any buffered data to the file.
75
76 \include qrandomaccessasyncfile.cpp returns-qiooperation
77*/
78QIOOperation *QRandomAccessAsyncFile::flush()
79{
80 Q_D(QRandomAccessAsyncFile);
81 return d->flush();
82}
83
84/*!
85 \internal
86
87 Reads at maximum \a maxSize bytes, starting from \a offset.
88
89 The data is written to the internal buffer managed by the returned
90 QIOOperation object.
91
92//! [returns-qiooperation]
93 Returns a QIOOperation object that would emit a QIOOperation::finished()
94 signal once the operation is complete.
95//! [returns-qiooperation]
96*/
97QIOReadOperation *QRandomAccessAsyncFile::read(qint64 offset, qint64 maxSize)
98{
99 Q_D(QRandomAccessAsyncFile);
100 if (maxSize < 0) {
101 qWarning("Using a negative maxSize in QRandomAccessAsyncFile::read() is incorrect. "
102 "Resetting to zero!");
103 maxSize = 0;
104 }
105 return d->read(offset, maxSize);
106}
107
108/*!
109 \internal
110 \overload
111
112 Reads the data from the file, starting from \a offset, and stores it into
113 \a buffer.
114
115 The amount of bytes to be read from the file is determined by the size of
116 the buffer. Note that the actual amount of read bytes can be less than that.
117
118 This operation does not take ownership of the provided buffer, so it is the
119 user's responsibility to make sure that the buffer is valid until the
120 returned QIOOperation completes.
121
122 \note The buffer might be populated from different threads, so the user
123 application should not access it until the returned QIOOperation completes.
124
125 \include qrandomaccessasyncfile.cpp returns-qiooperation
126*/
127QIOVectoredReadOperation *
128QRandomAccessAsyncFile::readInto(qint64 offset, QSpan<std::byte> buffer)
129{
130 Q_D(QRandomAccessAsyncFile);
131 return d->readInto(offset, buffer);
132}
133
134/*!
135 \internal
136
137 Reads the data from the file, starting from \a offset, and stores it into
138 \a buffers.
139
140 The amount of bytes to be read from the file is determined by the sum of
141 sizes of all buffers. Note that the actual amount of read bytes can be less
142 than that.
143
144 This operation does not take ownership of the provided buffers, so it is the
145 user's responsibility to make sure that the buffers are valid until the
146 returned QIOOperation completes.
147
148 \note The buffers might be populated from different threads, so the user
149 application should not access them until the returned QIOOperation completes.
150
151 \include qrandomaccessasyncfile.cpp returns-qiooperation
152*/
153QIOVectoredReadOperation *
154QRandomAccessAsyncFile::readInto(qint64 offset, QSpan<const QSpan<std::byte>> buffers)
155{
156 Q_D(QRandomAccessAsyncFile);
157 return d->readInto(offset, buffers);
158}
159
160/*!
161 \internal
162
163 Writes \a data into the file, starting from \a offset.
164
165 The \a data array is copied into the returned QIOOperation object.
166
167 \include qrandomaccessasyncfile.cpp returns-qiooperation
168*/
169QIOWriteOperation *QRandomAccessAsyncFile::write(qint64 offset, const QByteArray &data)
170{
171 Q_D(QRandomAccessAsyncFile);
172 return d->write(offset, data);
173}
174
175/*!
176 \internal
177 \overload
178
179 Writes \a data into the file, starting from \a offset.
180
181 The \a data array is moved into the returned QIOOperation object.
182
183 \include qrandomaccessasyncfile.cpp returns-qiooperation
184*/
185QIOWriteOperation *QRandomAccessAsyncFile::write(qint64 offset, QByteArray &&data)
186{
187 Q_D(QRandomAccessAsyncFile);
188 return d->write(offset, std::move(data));
189}
190
191/*!
192 \internal
193 \overload
194
195 Writes the content of \a buffer into the file, starting from \a offset.
196
197 This operation does not take ownership of the provided buffer, so it is the
198 user's responsibility to make sure that the buffer is valid until the
199 returned QIOOperation completes.
200
201 \note The buffer might be accessed from different threads, so the user
202 application should not modify it until the returned QIOOperation completes.
203*/
204QIOVectoredWriteOperation *
205QRandomAccessAsyncFile::writeFrom(qint64 offset, QSpan<const std::byte> buffer)
206{
207 Q_D(QRandomAccessAsyncFile);
208 return d->writeFrom(offset, buffer);
209}
210
211/*!
212 \internal
213
214 Writes the content of \a buffers into the file, starting from \a offset.
215
216 This operation does not take ownership of the provided buffers, so it is the
217 user's responsibility to make sure that the buffers are valid until the
218 returned QIOOperation completes.
219
220 \note The buffers might be accessed from different threads, so the user
221 application should not modify them until the returned QIOOperation
222 completes.
223*/
224QIOVectoredWriteOperation *
225QRandomAccessAsyncFile::writeFrom(qint64 offset, QSpan<const QSpan<const std::byte>> buffers)
226{
227 Q_D(QRandomAccessAsyncFile);
228 return d->writeFrom(offset, buffers);
229}
230
231QT_END_NAMESPACE
232
233#include "moc_qrandomaccessasyncfile_p.cpp"
~QRandomAccessAsyncFilePrivate() override
Combined button and popup list for selecting options.