{"id":20494374,"url":"https://github.com/voltra/async-tools","last_synced_at":"2025-03-05T17:46:40.166Z","repository":{"id":133008428,"uuid":"147948358","full_name":"Voltra/async-tools","owner":"Voltra","description":"A tiny C++11 library providing tools to ease the use of asynchrony","archived":false,"fork":false,"pushed_at":"2018-09-15T12:09:55.000Z","size":4092,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"dev","last_synced_at":"2025-03-02T01:34:38.907Z","etag":null,"topics":["asynchrony","c-plus-plus","c-plus-plus-11","c-plus-plus-library","stream"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Voltra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-08T16:01:51.000Z","updated_at":"2018-11-11T15:17:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"cf9c3cae-9e16-453a-9609-bb23f2d164a6","html_url":"https://github.com/Voltra/async-tools","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Voltra%2Fasync-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Voltra%2Fasync-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Voltra%2Fasync-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Voltra%2Fasync-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Voltra","download_url":"https://codeload.github.com/Voltra/async-tools/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242076029,"owners_count":20068231,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asynchrony","c-plus-plus","c-plus-plus-11","c-plus-plus-library","stream"],"created_at":"2024-11-15T17:39:19.339Z","updated_at":"2025-03-05T17:46:40.135Z","avatar_url":"https://github.com/Voltra.png","language":"C++","readme":"# async-tools\n\n## What is it?\n\nasync-tools is a tiny C++11 library that aims toward making asynchrony easy in C++. It provides a variety of tools that will help you achieve that.\n\n\n\nIt provides a lot of classes and helper functions under the namespace `async`\n\n## Tools\n\n### stream\n\n`async::stream\u003cT\u003e` is a one-way non-owning data flow : It takes the data and simply passes it around without keeping it. The design is based around the principle of sensors next to a river, the sensor grabs the newest data and makes it available for processing but doesn't hold it or block it : the data just passes in front of it.\n\n\n\nIt shares similar traits with [Java8's Stream API](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html) even though it serves a totally different purpose : Java's Stream API is mainly used to ease collection manipulation operations while `async::stream\u003cT\u003e` should be used to have a uniform way to asynchronously manipulate a (supposedly) non-finite one-way data flow.\n\nObviously you could use `async::stream\u003cT\u003e` to manipulate a container of `T`s but that would probably be a bit more expensive than using a library that is specifically made for this single purpose (such as [range-v3](https://github.com/ericniebler/range-v3)).\n\n\n\nLike Java's `Stream`, `async::stream\u003cT\u003e` provides a way to manipulate the data flow easily (eg. `async::stream\u003cT\u003e::forEach`, `async::stream\u003cT\u003e::filter`, `async::stream\u003cT\u003e::mapTo\u003cU\u003e`).\n\n\n\n### task\n\nAt first sight, streams might seem tedious to use since you need to setup listeners before the work actually starts (if you don't want to miss anything). This is why I provide the `async::task\u003cT\u003e`, it is a rather simple wrapper around `std::thread` that has a `async::stream\u003cT\u003e` ready for you to use. You can then `async::task\u003cT\u003e::run` (and `async::task\u003cT\u003e::stop`) the task (eg. reading a file line by line and processing each line individually).\n\n\n\n## Example\n\n```c++\n#include \u003ciostream\u003e\n#include \u003cfstream\u003e\n#include \u003cstring\u003e\n#include \u003cregex\u003e\n#include \u003cfunctional\u003e\n#include \u003casync/async.hpp\u003e\n\nusing namespace std;\nusing namespace std::placeholders;\n#define ASYNC_TASK_DEBUG\n\nbool is_whitespace(const string\u0026 str){\n    static regex emptystr{\"^\\\\s*$\"};\n    return regex_search(str, emptystr);\n}\n\nbool is_not_whitespace(const std::string\u0026 str){\n    return !is_whitespace(str);\n}\n\ntemplate \u003cclass Str=string\u003e\nvoid streamFile(const char* path, async::task\u003cStr\u003e\u0026 task, async::stream\u003cStr\u003e\u0026 stream){\n\tifstream file{path};\n\t\n\tif(!file.is_open())\n\t\ttask-\u003estop(\"Could not open file\");\n\t\t\n        while(file.good()){\n        \tStr buffer;\n        \tstd::getline(file, buffer);\n        \tstream \u003c\u003c buffer; //only a line at a time is in memory\n        }\n        \n        if(!file.eof())\n        \ttask-\u003estop(\"Stopped reading file before EOF\");\n}\n\ntemplate \u003cclass T=string\u003e\nvoid print_it(const T\u0026 t){\n    cout \u003c\u003c \"$\u003e \" \u003c\u003c t \u003c\u003c \" \u003c$\";\n}\n\nint main(){\n\tconst/*expr*/ char* PATH = \"kittens.txt\";\n\n\tasync::task\u003cstring\u003e task{bind(\n\t\tstreamFile\u003c\u003e,\n\t\tPATH, _1, _2\n\t)};\n\n\ttask-\u003estream()\n\t-\u003efilter(is_not_whitespace)\n\t-\u003eforEach(print_it\u003c\u003e);\n\n\ttask-\u003erun()-\u003ewait();\n}\n```\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoltra%2Fasync-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvoltra%2Fasync-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoltra%2Fasync-tools/lists"}