An open API service indexing awesome lists of open source software.

https://github.com/maikel/glib-senders

Senders and Receivers for the GNOME Glib library
https://github.com/maikel/glib-senders

coroutines cpp gtk stdexec

Last synced: 2 months ago
JSON representation

Senders and Receivers for the GNOME Glib library

Awesome Lists containing this project

README

        

![CMake Status](https://github.com/maikel/glib-senders/actions/workflows/cmake.yml/badge.svg)

Senders and Receivers for the GNOME Glib library
===============================================

This is a small C++ library that glues together the Glib event loop and the current executor proposal for C++.
It can be used to compose asynchronous tasks in a Glib event loop.

Since single-valued senders can be automatically converted to awaitable objects, the library also provides a way to use C++ coroutines in a Glib event loop.

The library requires C++20.

Example

```cpp
#include "glib-senders/glib_io_context.hpp"
#include "glib-senders/file_descriptor.hpp"

#include

using namespace gsenders;

exec::task write(file_descriptor fd, std::span buffer) {
while (!buffer.empty()) {
buffer = co_await async_write_some(fd, buffer);
}
}

exec::task echo(file_descriptor in, file_descriptor out) {
char buffer[1024];
int n = 0;
for (int n = 0; n < 10; ++n) {
std::span received = co_await async_read_some(in, buffer);
co_await write(out, received);
}
}

int main() {
glib_io_context ctx{};
file_descriptor in{STDIN_FILENO};
file_descriptor out{STDOUT_FILENO};
stdexec::start_detached(echo(in, out) | stdexec::then([&] { ctx.stop(); }));
ctx.run();
}
```