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
qiooperation.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
8#include <QtCore/private/qobject_p.h>
9#include <QtCore/private/qrandomaccessasyncfile_p_p.h>
10
12
13QIOOperationPrivate::QIOOperationPrivate(QtPrivate::QIOOperationDataStorage *storage)
14 : dataStorage(storage)
15{
16 Q_ASSERT(storage);
17}
18
19QIOOperationPrivate::~QIOOperationPrivate()
20{
21}
22
24{
25 processed += num;
26}
27
28void QIOOperationPrivate::operationComplete(QIOOperation::Error err)
29{
30 Q_Q(QIOOperation);
31
32 error = err;
33 state = State::Finished;
34 if (err != QIOOperation::Error::None)
35 Q_EMIT q->errorOccurred(err);
36 Q_EMIT q->finished();
37}
38
39void QIOOperationPrivate::setError(QIOOperation::Error err)
40{
41 Q_Q(QIOOperation);
42 error = err;
43 if (err != QIOOperation::Error::None) {
44 state = State::Finished;
45 Q_EMIT q->errorOccurred(err);
46 Q_EMIT q->finished();
47 }
48}
49
50QIOOperation::~QIOOperation()
51{
52 ensureCompleteOrCanceled();
53}
54
55QIOOperation::Type QIOOperation::type() const
56{
57 Q_D(const QIOOperation);
58 return d->type;
59}
60
61QIOOperation::Error QIOOperation::error() const
62{
63 Q_D(const QIOOperation);
64 return d->error;
65}
66
67bool QIOOperation::isFinished() const
68{
69 Q_D(const QIOOperation);
70 return d->state == QIOOperationPrivate::State::Finished;
71}
72
73QIOOperation::QIOOperation(QIOOperationPrivate &dd, QObject *parent)
74 : QObject(dd, parent)
75{
76 if (auto file = qobject_cast<QRandomAccessAsyncFile*>(parent))
77 d_func()->file = file;
78}
79
80void QIOOperation::ensureCompleteOrCanceled()
81{
82 // Block until the operation is either complete or canceled.
83 // Otherwise the write/read might use removed data
84 Q_D(QIOOperation);
85 if (d->state != QIOOperationPrivate::State::Finished) {
86 if (d->file) {
87 auto *filePriv = QRandomAccessAsyncFilePrivate::get(d->file);
88 filePriv->cancelAndWait(this);
89 }
90 }
91}
92
93QIOReadWriteOperationBase::~QIOReadWriteOperationBase()
94 = default;
95
96qint64 QIOReadWriteOperationBase::offset() const
97{
98 Q_D(const QIOOperation);
99 return d->offset;
100}
101
102qint64 QIOReadWriteOperationBase::numBytesProcessed() const
103{
104 if (!isFinished())
105 return -1;
106 Q_D(const QIOOperation);
107 return d->processed;
108}
109
110QIOReadWriteOperationBase::QIOReadWriteOperationBase(QIOOperationPrivate &dd, QObject *parent)
111 : QIOOperation(dd, parent)
112{
113}
114
115QIOReadOperation::~QIOReadOperation() = default;
116
117QByteArray QIOReadOperation::data() const
118{
119 if (!isFinished())
120 return {};
121 Q_D(const QIOOperation);
122 return d->dataStorage->getValue<QByteArray>();
123}
124
125QIOReadOperation::QIOReadOperation(QIOOperationPrivate &dd, QObject *parent)
126 : QIOReadWriteOperationBase(dd, parent)
127{
128 Q_ASSERT(dd.type == QIOOperation::Type::Read);
129 Q_ASSERT(dd.dataStorage->containsByteArray());
130}
131
132QIOWriteOperation::~QIOWriteOperation() = default;
133
134QByteArray QIOWriteOperation::data() const
135{
136 if (!isFinished())
137 return {};
138 Q_D(const QIOOperation);
139 return d->dataStorage->getValue<QByteArray>();
140}
141
142QIOWriteOperation::QIOWriteOperation(QIOOperationPrivate &dd, QObject *parent)
143 : QIOReadWriteOperationBase(dd, parent)
144{
145 Q_ASSERT(dd.type == QIOOperation::Type::Write);
146 Q_ASSERT(dd.dataStorage->containsByteArray());
147}
148
149QIOVectoredReadOperation::~QIOVectoredReadOperation() = default;
150
151QSpan<const QSpan<std::byte>> QIOVectoredReadOperation::data() const
152{
153 if (!isFinished())
154 return {};
155 Q_D(const QIOOperation);
156 return d->dataStorage->getValue<QSpan<const QSpan<std::byte>>>();
157}
158
159QIOVectoredReadOperation::QIOVectoredReadOperation(QIOOperationPrivate &dd, QObject *parent)
160 : QIOReadWriteOperationBase(dd, parent)
161{
162 Q_ASSERT(dd.type == QIOOperation::Type::Read);
163 Q_ASSERT(dd.dataStorage->containsReadSpans());
164}
165
166QIOVectoredWriteOperation::~QIOVectoredWriteOperation() = default;
167
168QSpan<const QSpan<const std::byte>> QIOVectoredWriteOperation::data() const
169{
170 if (!isFinished())
171 return {};
172 Q_D(const QIOOperation);
173 return d->dataStorage->getValue<QSpan<const QSpan<const std::byte>>>();
174}
175
176QIOVectoredWriteOperation::QIOVectoredWriteOperation(QIOOperationPrivate &dd, QObject *parent)
177 : QIOReadWriteOperationBase(dd, parent)
178{
179 Q_ASSERT(dd.type == QIOOperation::Type::Write);
180 Q_ASSERT(dd.dataStorage->containsWriteSpans());
181}
182
183QT_END_NAMESPACE
void setError(QIOOperation::Error err)
void operationComplete(QIOOperation::Error err)
void appendBytesProcessed(qint64 num)