GithubHelp home page GithubHelp logo

hannahkiekens / qcoro Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danvratil/qcoro

0.0 1.0 0.0 1.79 MB

C++ Coroutines for Qt

Home Page: https://qcoro.dvratil.cz

CMake 6.97% C++ 92.52% C 0.51%

qcoro's Introduction

Build

QCoro - Coroutines for Qt

The QCoro library provides set of tools to make use of the C++20 coroutines in connection with certain asynchronous Qt actions.

Take a look at the example below to see what an amazing thing coroutines are:

QNetworkAccessManager networkAccessManager;
// co_await the reply - the coroutine is suspended until the QNetworkReply is finished.
// While the coroutine is suspended, *the Qt event loop runs as usual*.
const QNetworkReply *reply = co_await networkAccessManager.get(url);
// Once the reply is finished, your code resumes here as if nothing amazing has just happened ;-)
const auto data = reply->readAll();

This is a rather experimental library that helps me to understand coroutines in C++.

It requires compiler with support for the couroutines TS.

Documentation

๐Ÿ‘‰ ๐Ÿ“˜ Documentation

Supported Qt Types

QCoro supports using the co_await keyword with several Qt types:

QDBusPendingCall

QCoro can wait for an asynchronous D-Bus call to finish. There's no need to use QDBusPendingCallWatcher with QCoro - just co_await the result instead. While co_awaiting, the Qt event loop runs as usual.

QDBusInterface remoteServiceInterface{serviceName, objectPath, interface};
const QDBusReply<bool> isReady = co_await remoteServiceInterface.asyncCall(QStringLiteral("isReady"));

Full documentation here.

QFuture

QFuture represents a result of an asynchronous task. Normally you have to use QFutureWatcher to get notified when the future is ready. With QCoro, you can just co_await it!

const QFuture<int> task1 = QtConcurrent::run(....);
const QFuture<int> task2 = QtConcurrent::run(....);

const int a = co_await task1;
const int b = co_await task2;

co_return a + b;

Full documentation here.

QNetworkReply

Doing network requests with Qt can be tedious - the signal/slot approach breaks the flow of your code. Chaining requests and error handling quickly become mess and your code is broken into numerous functions. But not with QCoro, where you can simply co_await the QNetworkReply to finish:

QNetworkReply qnam;
QNetworkReply *reply = qnam.get(QStringLiteral("https://github.com/danvratil/qcoro"));
const auto contents = co_await reply;
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
    co_return handleError(reply);
}

const auto link = findLinkInReturnedHtmlCode(contents);
reply = qnam.get(link);
const auto data = co_await reply;
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
    co_return handleError(reply);
}
 ...

Full documentation here.

QTimer

Maybe you want to delay executing your code for a second, maybe you want to execute some code in repeated interval. This becomes super-trivial with co_await:

QTimer timer;
timer.setInterval(1s);
timer.start();

for (int i = 1; i <= 100; ++i) {
    co_await timer;
    qDebug() << "Waiting for " << i << " seconds...";
}

qDebug() << "Done!";

Full documentation here.

QIODevice

QIODevice is a base-class for many classes in Qt that allow data to be asynchronously written and read. How do you find out that there are data ready to be read? You could connect to QIODevice::readyRead() singal, or you could use QCoro and co_await the object:

socket->write("PING");
// Waiting for "pong"
const auto data = co_await socket;
co_return calculateLatency(data);

Full documentation here.

qcoro's People

Contributors

carlschwan avatar danvratil avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.