Skip to content

Commit

Permalink
add qAwait() function
Browse files Browse the repository at this point in the history
  • Loading branch information
hgoldfish committed May 29, 2018
1 parent cea8a48 commit eac5c93
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
26 changes: 26 additions & 0 deletions examples/using_iodevice/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <QApplication>
#include <QPlainTextEdit>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include "../../qtnetworkng.h"

using namespace qtng;

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QSharedPointer<QPlainTextEdit> text(new QPlainTextEdit);
text->show();
CoroutineGroup operations;
operations.spawn([text] {
QNetworkAccessManager manager;
QUrl url("http://download.qt.io/online/qt5/linux/x64/online_repository/Updates.xml");
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
qAwait(reply, &QNetworkReply::finished);
text->setPlainText(reply->readAll());
reply->deleteLater();
});
return startQtLoop();
}
10 changes: 10 additions & 0 deletions examples/using_iodevice/using_iodevice.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
######################################################################
# Automatically generated by qmake (3.1) Tue May 29 08:36:34 2018
######################################################################

TEMPLATE = app
QT+= widgets network
TARGET = using_iodevice
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp
include(../../qtnetworkng.pri)
14 changes: 10 additions & 4 deletions include/coroutine_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,20 @@ void runLocalLoop(EventLoop *loop)
}


template<typename Func1>
void waitSignal(const QObject *obj, Func1 signal, const QObject *context, Qt::ConnectionType type = Qt::DirectConnection)
template <typename Func>
void qAwait(const typename QtPrivate::FunctionPointer<Func>::Object *obj, Func signal)
{
QSharedPointer<Event> event(new Event);
QObject::connect(obj, signal, [event] {
const auto connection = QObject::connect(obj, signal, [event] {
event->set();
});
event->wait();
try {
event->wait();
QObject::disconnect(connection);
} catch (...) {
QObject::disconnect(connection);
throw;
}
}


Expand Down

0 comments on commit eac5c93

Please sign in to comment.