суббота, 5 августа 2023 г.

Coroutines, Example, Futures, Promise, Release

https://habr.com/ru/companies/wunderfund/articles/582000/

https://github.com/feabhas/coroutines-blog/tree/main/src

https://github.com/feabhas/coroutines-blog datapoint_demo.cpp

https://github.com/feabhas/coroutines-blog/blob/main/src/char_demo.cpp

#include <coroutine>

#include <optional>

#include <iostream>

#include <cassert>

#include <exception>


class Future;


struct Promise

{

    using value_type = const char*;

    const char* value{};


    Promise() = default;

    std::suspend_always initial_suspend() { return {}; }

    std::suspend_always final_suspend() noexcept { return {}; }

    void unhandled_exception() { std::rethrow_exception(std::current_exception()); }


    std::suspend_always yield_value(const char* value) {

        this->value = std::move(value);

        return {};

    }


    // void return_value(const char* value) {

    //     this->value = std::move(value);

    // }


    void return_void() {

        this->value = nullptr;

    }


    Future get_return_object();

};


class Future

{

public:

    using promise_type = Promise;


    explicit Future(std::coroutine_handle<Promise> handle)

        : handle(handle)

    {}


    ~Future() {

        if (handle) { handle.destroy(); }

    }


    Promise::value_type next() {

        if (handle) {

            handle.resume();

            return handle.promise().value;

        }

        else {

            return {};

        }

    }


private:

    std::coroutine_handle<Promise> handle{};

};


Future Promise::get_return_object()

{

    return Future{ std::coroutine_handle<Promise>::from_promise(*this) };

}



// co-routine

Future Generator()

{

    co_yield "Hello ";

    co_yield "world";

    co_yield "!";

    //co_return;

}

int main()

{

     /*auto generator = Generator();

     std::cout << generator.next();

     std::cout << generator.next();

     std::cout << generator.next();

     std::cout << std::endl;*/


    auto generator = Generator();

    while (const char* item = generator.next()) {

        std::cout << item;

    }

    std::cout << std::endl;


    return 0;

}


Комментариев нет:

Отправить комментарий