Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qiopipe.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 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
4#include "qiopipe_p.h"
5#include <QtCore/qmetaobject.h>
6#include <QDebug>
7
9
10/*
11 proxying means do *not* emit readyRead, and instead pump data
12 into child pipes directly in a zero-copy fashion.
13*/
15 : m_proxying(proxying), source(iodevice)
16{
17}
18
22
24{
25 const QIOPipe *parentPipe = qobject_cast<QIOPipe *>(source);
26 if (parentPipe && parentPipe->d_func()->m_proxying) // with proxying parent,
27 return; // don't do anything
28
29 // read available data, does not emit.
31 // connect readyRead to onReadyRead
33}
34
36 if (!source)
37 return false;
38 QByteArray ba = source->readAll();
39 if (ba.isEmpty())
40 return false;
41
42 pumpData(ba);
43 return true;
44}
45
47{
48 if (m_proxying) {
49 auto isNull = [](const auto &cp) { return cp == nullptr; };
50 childPipes.removeIf(isNull);
51 for (const auto &cp : std::as_const(childPipes))
52 cp->d_func()->pushData(ba);
53 } else {
54 for (auto &buffer : readBuffers)
55 buffer.append(ba);
56 }
57}
58
60{
61 Q_Q(QIOPipe);
62 if (ba.isEmpty())
63 return;
64
65 pumpData(ba);
66 if (!m_proxying)
67 emit q->readyRead();
68}
69
71{
72 Q_Q(QIOPipe);
74 emit q->readyRead();
75}
76
78{
79 if (childPipes.contains(childPipe))
80 return;
81 childPipes.append(childPipe);
82}
83
85{
86 childPipes.removeOne(childPipe);
87}
88
90 : QIODevice(*new QIOPipePrivate(parent, mode == ProxyPipe), parent)
91{
92 this->d_func()->initialize();
93 if (!parent->isOpen() && !parent->open(QIODevice::ReadOnly)) {
94 qWarning() << "QIOPipe: Failed to open " << parent;
95 return;
96 }
98}
99
101{
102
103}
104
105bool QIOPipe::open(QIODevice::OpenMode mode)
106{
107 if (isOpen())
108 return true;
109
110 static const OpenMode supportedOpenMode = ReadOnly; // Currently limit it to read only
111 if (!(mode & supportedOpenMode)) {
112 qFatal("Unsupported open mode");
113 return false;
114 }
115
116 return QIODevice::open(mode);
117}
118
120{
121 return true;
122}
123
125{
126 Q_D(QIOPipe);
127 d->setReadChannelCount(qMax(count, 1));
128}
129
131{
132 Q_D(QIOPipe);
133 d->addChildPipe(childPipe);
134}
135
147{
148 Q_UNUSED(data);
149 Q_UNUSED(maxlen);
150
151 // return 0 indicating there may be more data in the future
152 // Returning -1 means no more data in the future (end of stream).
153 return qint64(0);
154}
155
156qint64 QIOPipe::writeData(const char * /*data*/, qint64 /*len*/)
157{
158 qFatal("QIOPipe is a read-only device");
159 return qint64(0);
160}
161
163
164#include "moc_qiopipe_p.cpp"
\inmodule QtCore
Definition qbytearray.h:57
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
QVarLengthArray< QRingBuffer, 2 > readBuffers
\inmodule QtCore \reentrant
Definition qiodevice.h:34
virtual bool open(QIODeviceBase::OpenMode mode)
Opens the device and sets its OpenMode to mode.
void readyRead()
This signal is emitted once every time new data is available for reading from the device's current re...
bool isOpen() const
Returns true if the device is open; otherwise returns false.
void addChildPipe(QIOPipe *childPipe)
Definition qiopipe.cpp:77
void _q_onReadyRead()
Definition qiopipe.cpp:70
void removeChildPipe(QIOPipe *childPipe)
Definition qiopipe.cpp:84
QList< QPointer< QIOPipe > > childPipes
Definition qiopipe_p.h:73
~QIOPipePrivate() override
Definition qiopipe.cpp:19
QIOPipePrivate(QIODevice *iodevice, bool proxying)
Definition qiopipe.cpp:14
void pumpData(const QByteArray &ba)
Definition qiopipe.cpp:46
void pushData(const QByteArray &ba)
Definition qiopipe.cpp:59
bool readAvailableData()
Definition qiopipe.cpp:35
void initialize()
Definition qiopipe.cpp:23
QIOPipe(QIODevice *parent, Mode mode=EndPipe)
Definition qiopipe.cpp:89
qint64 readData(char *data, qint64 maxlen) override
\reimp
Definition qiopipe.cpp:146
~QIOPipe() override
Definition qiopipe.cpp:100
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
Definition qiopipe.cpp:119
void setReadChannelCount(int count)
Definition qiopipe.cpp:124
bool open(OpenMode openMode) override
Definition qiopipe.cpp:105
void addChildPipe(QIOPipe *childPipe)
Definition qiopipe.cpp:130
qint64 writeData(const char *data, qint64 len) override
Writes up to maxSize bytes from data to the device.
Definition qiopipe.cpp:156
bool removeOne(const AT &t)
Definition qlist.h:598
qsizetype removeIf(Predicate pred)
Definition qlist.h:604
void append(parameter_type t)
Definition qlist.h:458
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:299
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
#define qFatal
Definition qlogging.h:168
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLenum mode
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint buffer
GLsizei GLsizei GLchar * source
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define emit
#define Q_UNUSED(x)
long long qint64
Definition qtypes.h:60
QByteArray ba
[0]
bool contains(const AT &t) const noexcept
Definition qlist.h:45