asynco/lib/asynco.hpp

83 lines
1.9 KiB
C++

#ifndef _ASYNCO_
#define _ASYNCO_
#include "engine.hpp"
#include <iostream>
using namespace std;
namespace marcelb {
namespace asynco {
/**
* Run the function asynchronously
*/
template<class F, class... Args>
auto async_(F&& f, Args&&... args) -> future<typename result_of<F(Args...)>::type> {
using return_type = typename result_of<F(Args...)>::type;
future<return_type> res = _asynco_engine.io_context.post(boost::asio::use_future(bind(forward<F>(f), forward<Args>(args)...)));
return res;
}
/**
* Block until the asynchronous call completes
*/
template<typename T>
T await_(future<T>& r) {
return r.get();
}
/**
* Block until the asynchronous call completes
*/
template<typename T>
T await_(future<T>&& r) {
return move(r).get();
}
/**
* Block until the multiple asynchronous call completes
* Use only on no-void calls
*/
template<typename... F>
auto await_(F&&... f) -> std::tuple<typename std::decay<decltype(f.get())>::type...> {
return std::make_tuple(move(f).get()...);
}
/**
* Block until the multiple asynchronous call completes
* Use only on no-void calls
*/
template<typename... F>
auto await_(F&... f) -> std::tuple<typename std::decay<decltype(f.get())>::type...> {
return std::make_tuple(f.get()...);
}
/**
* Block until the asynchronous call completes or time expired
*/
template<typename T>
T await_(future<T>& r, uint64_t time) {
if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) {
throw runtime_error("Asynchronous execution timed out");
}
return r.get();
}
/**
* Block until the asynchronous call completes or time expired
*/
template<typename T>
T await_(future<T>&& r, uint64_t time) {
if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) {
throw runtime_error("Asynchronous execution timed out");
}
return move(r).get();
}
}
}
#endif