https://github.com/voltra/async-tools
A tiny C++11 library providing tools to ease the use of asynchrony
https://github.com/voltra/async-tools
asynchrony c-plus-plus c-plus-plus-11 c-plus-plus-library stream
Last synced: 3 months ago
JSON representation
A tiny C++11 library providing tools to ease the use of asynchrony
- Host: GitHub
- URL: https://github.com/voltra/async-tools
- Owner: Voltra
- Created: 2018-09-08T16:01:51.000Z (over 6 years ago)
- Default Branch: dev
- Last Pushed: 2018-09-15T12:09:55.000Z (over 6 years ago)
- Last Synced: 2025-03-02T01:34:38.907Z (3 months ago)
- Topics: asynchrony, c-plus-plus, c-plus-plus-11, c-plus-plus-library, stream
- Language: C++
- Size: 3.9 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# async-tools
## What is it?
async-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.
It provides a lot of classes and helper functions under the namespace `async`
## Tools
### stream
`async::stream` 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.
It 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` should be used to have a uniform way to asynchronously manipulate a (supposedly) non-finite one-way data flow.
Obviously you could use `async::stream` 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)).
Like Java's `Stream`, `async::stream` provides a way to manipulate the data flow easily (eg. `async::stream::forEach`, `async::stream::filter`, `async::stream::mapTo`).
### task
At 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`, it is a rather simple wrapper around `std::thread` that has a `async::stream` ready for you to use. You can then `async::task::run` (and `async::task::stop`) the task (eg. reading a file line by line and processing each line individually).
## Example
```c++
#include
#include
#include
#include
#include
#includeusing namespace std;
using namespace std::placeholders;
#define ASYNC_TASK_DEBUGbool is_whitespace(const string& str){
static regex emptystr{"^\\s*$"};
return regex_search(str, emptystr);
}bool is_not_whitespace(const std::string& str){
return !is_whitespace(str);
}template
void streamFile(const char* path, async::task& task, async::stream& stream){
ifstream file{path};
if(!file.is_open())
task->stop("Could not open file");
while(file.good()){
Str buffer;
std::getline(file, buffer);
stream << buffer; //only a line at a time is in memory
}
if(!file.eof())
task->stop("Stopped reading file before EOF");
}template
void print_it(const T& t){
cout << "$> " << t << " <$";
}int main(){
const/*expr*/ char* PATH = "kittens.txt";async::task task{bind(
streamFile<>,
PATH, _1, _2
)};task->stream()
->filter(is_not_whitespace)
->forEach(print_it<>);task->run()->wait();
}
```