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_p_p.h
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
5#ifndef QRANDOMACCESSASYNCFILE_P_P_H
6#define QRANDOMACCESSASYNCFILE_P_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
20
21#include <QtCore/private/qobject_p.h>
22
23#include <QtCore/qstring.h>
24
25#ifdef QT_RANDOMACCESSASYNCFILE_THREAD
26
27#include <QtCore/private/qfsfileengine_p.h>
28
29#include <QtCore/qfuturewatcher.h>
30#include <QtCore/qmutex.h>
31#include <QtCore/qqueue.h>
32
33#endif // QT_RANDOMACCESSASYNCFILE_THREAD
34
35#ifdef Q_OS_DARWIN
36
37#include <QtCore/qlist.h>
38#include <QtCore/qmutex.h>
39#include <QtCore/qset.h>
40#include <QtCore/qwaitcondition.h>
41
42#include <dispatch/dispatch.h>
43
44#endif // Q_OS_DARWIN
45
47
49{
50 Q_DECLARE_PUBLIC(QRandomAccessAsyncFile)
52public:
55
56 static QRandomAccessAsyncFilePrivate *get(QRandomAccessAsyncFile *file)
57 { return file->d_func(); }
58
59 void init();
60 void cancelAndWait(QIOOperation *op);
61
62 void close();
63 qint64 size() const;
64
65 [[nodiscard]] QIOOperation *open(const QString &path, QIODeviceBase::OpenMode mode);
67
68 [[nodiscard]] QIOReadOperation *read(qint64 offset, qint64 maxSize);
69 [[nodiscard]] QIOWriteOperation *write(qint64 offset, const QByteArray &data);
70 [[nodiscard]] QIOWriteOperation *write(qint64 offset, QByteArray &&data);
71
73 readInto(qint64 offset, QSpan<std::byte> buffer);
75 writeFrom(qint64 offset, QSpan<const std::byte> buffer);
76
78 readInto(qint64 offset, QSpan<const QSpan<std::byte>> buffers);
80 writeFrom(qint64 offset, QSpan<const QSpan<const std::byte>> buffers);
81
82private:
83 // common for all backends
84 enum class FileState : quint8
85 {
86 Closed,
87 OpenPending, // already got an open request
88 Opened,
89 };
90
91 QString m_filePath;
92 QIODeviceBase::OpenMode m_openMode;
93 FileState m_fileState = FileState::Closed;
94
95#ifdef QT_RANDOMACCESSASYNCFILE_THREAD
96public:
97 struct OperationResult
98 {
99 qint64 bytesProcessed; // either read or written
101 };
102
103private:
107
111
114 void processFlush();
115 void processOpen();
116 void operationComplete();
117#endif
118#ifdef Q_OS_DARWIN
119 using OperationId = quint64;
120 static constexpr OperationId kInvalidOperationId = 0;
122
123 struct OperationResult
124 {
126 qint64 result; // num bytes processed or file descriptor
127 int error;
128 };
129
130 enum class OpState : quint8
131 {
132 Pending,
133 Running,
134 };
135
136 struct OperationInfo
137 {
142
144 : opId(_id),
145 channel(nullptr),
146 operation(_op),
148 {}
149 };
150
151 // We need to maintain an actual queue of the operations, because
152 // certain operations (i.e. flush) should act like barriers. The docs
153 // for dispatch_io_barrier mention that it can synchronize between multiple
154 // channels handling the same file descriptor, but that DOES NOT work in
155 // practice. It works correctly only when there's a signle IO channel. But
156 // with a signle IO channel we're not able to cancel individual operations.
157 // As a result, we need to make sure that all previous operations are
158 // completed before starting a barrier operation. Similarly, we cannot start
159 // any other operation until a barrier operation is finished.
161 dispatch_io_t m_ioChannel = nullptr;
162 int m_fd = -1;
163
165 // the members below should only be accessed with the mutex
169
170 static OperationId getNextId();
171
172 template <typename Operation, typename ...Args>
174
180
182
188
195#endif
196};
197
198QT_END_NAMESPACE
199
200#endif // QRANDOMACCESSASYNCFILE_P_P_H
static QRandomAccessAsyncFilePrivate * get(QRandomAccessAsyncFile *file)
QIOOperation * open(const QString &path, QIODeviceBase::OpenMode mode)
QIOReadOperation * read(qint64 offset, qint64 maxSize)
QIOVectoredWriteOperation * writeFrom(qint64 offset, QSpan< const std::byte > buffer)
QIOWriteOperation * write(qint64 offset, QByteArray &&data)
QIOWriteOperation * write(qint64 offset, const QByteArray &data)
QIOVectoredReadOperation * readInto(qint64 offset, QSpan< std::byte > buffer)
Combined button and popup list for selecting options.