Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename Token>
- auto async_meaning_of_life(bool success, Token&& token)
- {
- typename asio::handler_type<Token, void(error_code, int)>::type
- handler (std::forward<Token> (token));
- asio::async_result<decltype (handler)> result (handler);
- if (success)
- handler(42);
- else
- handler(asio::error::operation_aborted, 0);
- return result.get ();
- }
- #define BOOST_COROUTINES_NO_DEPRECATION_WARNING
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/asio/spawn.hpp>
- #include <boost/asio/use_future.hpp>
- using boost::system::error_code;
- namespace asio = boost::asio;
- template <typename Token>
- auto async_meaning_of_life(bool success, Token&& token)
- {
- typename asio::handler_type<Token, void(error_code, int)>::type
- handler (std::forward<Token> (token));
- asio::async_result<decltype (handler)> result (handler);
- if (success)
- handler(error_code{}, 42);
- else
- handler(asio::error::operation_aborted, 0);
- return result.get ();
- }
- void using_yield_ec(asio::yield_context yield) {
- for (bool success : { true, false }) {
- boost::system::error_code ec;
- auto answer = async_meaning_of_life(success, yield[ec]);
- std::cout << __FUNCTION__ << ": Result: " << ec.message() << "n";
- std::cout << __FUNCTION__ << ": Answer: " << answer << "n";
- }
- }
- void using_yield_catch(asio::yield_context yield) {
- for (bool success : { true, false }) try {
- auto answer = async_meaning_of_life(success, yield);
- std::cout << __FUNCTION__ << ": Answer: " << answer << "n";
- } catch(boost::system::system_error const& e) {
- std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "n";
- }
- }
- void using_future() {
- for (bool success : { true, false }) try {
- auto answer = async_meaning_of_life(success, asio::use_future);
- std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "n";
- } catch(boost::system::system_error const& e) {
- std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "n";
- }
- }
- void using_handler() {
- for (bool success : { true, false })
- async_meaning_of_life(success, [](error_code ec, int answer) {
- std::cout << "using_handler: Result: " << ec.message() << "n";
- std::cout << "using_handler: Answer: " << answer << "n";
- });
- }
- int main() {
- asio::io_service svc;
- spawn(svc, using_yield_ec);
- spawn(svc, using_yield_catch);
- std::thread work([] {
- using_future();
- using_handler();
- });
- svc.run();
- work.join();
- }
- using_yield_ec: Result: Success
- using_yield_ec: Answer: 42
- using_yield_ec: Result: Operation canceled
- using_yield_ec: Answer: 0
- using_future: Answer: 42
- using_yield_catch: Answer: 42
- using_yield_catch: Caught: Operation canceled
- using_future: Caught: Operation canceled
- using_handler: Result: Success
- using_handler: Answer: 42
- using_handler: Result: Operation canceled
- using_handler: Answer: 0
Add Comment
Please, Sign In to add comment