#ifndef _ASYNCO_ #define _ASYNCO_ #include "engine.hpp" #include using namespace std; namespace marcelb { namespace asynco { /** * Run the function asynchronously */ template auto async_(F&& f, Args&&... args) -> future::type> { using return_type = typename result_of::type; future res = _asynco_engine.io_context.post(boost::asio::use_future(bind(forward(f), forward(args)...))); return res; } /** * Block until the asynchronous call completes */ template T await_(future& r) { return r.get(); } /** * Block until the asynchronous call completes */ template T await_(future&& r) { return move(r).get(); } /** * Block until the asynchronous call completes or time expired */ template T await_(future& 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 T await_(future&& 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