1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:default
#ifndef QIOOPERATION_P_P_H
#define QIOOPERATION_P_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include "qiooperation_p.h"
#include "qrandomaccessasyncfile_p.h"
#include <QtCore/private/qobject_p.h>
#include <QtCore/qspan.h>
#include <QtCore/qvarlengtharray.h>
#ifdef QT_RANDOMACCESSASYNCFILE_QIORING
#include <QtCore/private/qioring_p.h>
#endif
#include <variant>
QT_BEGIN_NAMESPACE
namespace QtPrivate {
class QIOOperationDataStorage
{
public:
// When passing QSpan<QSpan<T>>, we'd better have an underlying storage
// for an outer span, so that users could pass in a temporary object.
// We'd use QVarLengthArray for that. Having 256 elements (the default)
// seems to be unneeded for vectored IO. For now I picked 10 as a reasonable
// default. But maybe even less?
static constexpr qsizetype DefaultNumOfBuffers = 10;
using ReadSpans = QVarLengthArray<QSpan<std::byte>, DefaultNumOfBuffers>;
using WriteSpans = QVarLengthArray<QSpan<const std::byte>, DefaultNumOfBuffers>;
explicit QIOOperationDataStorage()
: data(std::monostate{})
{}
explicit QIOOperationDataStorage(QSpan<const QSpan<std::byte>> s)
: data(ReadSpans(s.begin(), s.end()))
{}
explicit QIOOperationDataStorage(QSpan<const QSpan<const std::byte>> s)
: data(WriteSpans(s.begin(), s.end()))
{}
explicit QIOOperationDataStorage(const QByteArray &a)
: data(a)
{}
explicit QIOOperationDataStorage(QByteArray &&a)
: data(std::move(a))
{}
bool isEmpty() const
{ return std::holds_alternative<std::monostate>(data); }
bool containsReadSpans() const
{ return std::holds_alternative<ReadSpans>(data); }
bool containsWriteSpans() const
{ return std::holds_alternative<WriteSpans>(data); }
bool containsByteArray() const
{ return std::holds_alternative<QByteArray>(data); }
ReadSpans &getReadSpans()
{
Q_ASSERT(containsReadSpans());
return *std::get_if<ReadSpans>(&data);
}
const ReadSpans &getReadSpans() const
{
Q_ASSERT(containsReadSpans());
return *std::get_if<ReadSpans>(&data);
}
WriteSpans &getWriteSpans()
{
Q_ASSERT(containsWriteSpans());
return *std::get_if<WriteSpans>(&data);
}
const WriteSpans &getWriteSpans() const
{
Q_ASSERT(containsWriteSpans());
return *std::get_if<WriteSpans>(&data);
}
QByteArray &getByteArray()
{
Q_ASSERT(containsByteArray());
return *std::get_if<QByteArray>(&data);
}
const QByteArray &getByteArray() const
{
Q_ASSERT(containsByteArray());
return *std::get_if<QByteArray>(&data);
}
// Potentially can be extended to return a QVariant::value<T>().
template <typename T>
T getValue() const = delete;
private:
std::variant<std::monostate, ReadSpans, WriteSpans, QByteArray> data;
};
template <>
inline QSpan<const QSpan<std::byte>> QIOOperationDataStorage::getValue() const
{
Q_ASSERT(std::holds_alternative<ReadSpans>(data));
const auto *val = std::get_if<ReadSpans>(&data);
if (val)
return QSpan(*val);
return {};
}
template <>
inline QSpan<const QSpan<const std::byte>> QIOOperationDataStorage::getValue() const
{
Q_ASSERT(std::holds_alternative<WriteSpans>(data));
const auto *val = std::get_if<WriteSpans>(&data);
if (val)
return QSpan(*val);
return {};
}
template <>
inline QByteArray QIOOperationDataStorage::getValue() const
{
Q_ASSERT(std::holds_alternative<QByteArray>(data));
const auto *val = std::get_if<QByteArray>(&data);
if (val)
return *val;
return {};
}
} // namespace QtPrivate
class QIOOperationPrivate : public QObjectPrivate
{
public:
Q_DECLARE_PUBLIC(QIOOperation)
enum class State : quint8
{
Running,
Finished,
};
explicit QIOOperationPrivate(QtPrivate::QIOOperationDataStorage *storage);
~QIOOperationPrivate();
static QIOOperationPrivate *get(QIOOperation *op)
{ return op->d_func(); }
void appendBytesProcessed(qint64 num);
void operationComplete(QIOOperation::Error err);
void setError(QIOOperation::Error err);
QPointer<QRandomAccessAsyncFile> file;
qint64 offset = 0;
qint64 processed = 0;
QIOOperation::Error error = QIOOperation::Error::None;
QIOOperation::Type type = QIOOperation::Type::Unknown;
State state = State::Running;
// takes ownership
std::unique_ptr<QtPrivate::QIOOperationDataStorage> dataStorage;
};
QT_END_NAMESPACE
#endif // QIOOPERATION_P_P_H
|