GeorgyFirsov / w32coro

C++14-compatible implementation of coroutines for Windows systems. Based of Win32 API fibers that are manually scheduled by this library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

w32coro

Small hand-written coroutines library. Written in C++14 with Win32 API (Fibers).

Examples

Receiving some information from server

#include "w32coro/w32coro.h"
#include "MyAwesomeProject.h"

int main()
{
    const auto ReceiveData = [](const std::wstring& wsServerName, DWORD dwPort) {
        IConnectionPtr spConn = Connect(wsServerName, dwPort);
        std::wstring wsData = spConn->Receive();
        
        w32coro::CoReturn(wsData);
    };
    
    std::wstring wsServerName = L"my.awesome.server";
    DWORD dwPort = 8080;
    
    const auto wsReceivedData = w32coro::CoAwait<std::wstring>(
        w32coro::Coroutine(ReceiveData, std::cref(wsServerName), dwPort));
        
    std::wcout << wsReceivedData << std::endl;
}

Suspending, resuming and returning various values

Actually this example can be extended to write real generator, that implements some methods such as begin and end to use it in range-based for loop.

#include "w32coro/w32coro.h"
#include "MyAwesomeProject.h"

int main()
{
    const auto Generate = [](size_t Initial) {
        while (true) {
            w32coro::CoYield(Initial++);
        }
    };
    
    w32coro::Coroutine gen(Generate, 0);
    
    std::vector<size_t> Numbers;
    for (size_t i = 0; i < 10; i++) {
        Numbers.emplace_back(gen.Get<size_t>());
        gen.Resume();
    }
    
    //
    // Will print 0 1 2 ...
    //
    for (size_t n : Numbers) {
        std::cout << n << ' ';
    }
}

About

C++14-compatible implementation of coroutines for Windows systems. Based of Win32 API fibers that are manually scheduled by this library.

License:GNU General Public License v3.0


Languages

Language:C++ 98.8%Language:C 1.2%