Guest User

Untitled

a guest
Oct 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. template <typename Token>
  2. auto async_meaning_of_life(bool success, Token&& token)
  3. {
  4. typename asio::handler_type<Token, void(error_code, int)>::type
  5. handler (std::forward<Token> (token));
  6.  
  7. asio::async_result<decltype (handler)> result (handler);
  8.  
  9. if (success)
  10. handler(42);
  11. else
  12. handler(asio::error::operation_aborted, 0);
  13.  
  14. return result.get ();
  15. }
  16.  
  17. #define BOOST_COROUTINES_NO_DEPRECATION_WARNING
  18. #include <iostream>
  19. #include <boost/asio.hpp>
  20. #include <boost/asio/spawn.hpp>
  21. #include <boost/asio/use_future.hpp>
  22.  
  23. using boost::system::error_code;
  24. namespace asio = boost::asio;
  25.  
  26. template <typename Token>
  27. auto async_meaning_of_life(bool success, Token&& token)
  28. {
  29. typename asio::handler_type<Token, void(error_code, int)>::type
  30. handler (std::forward<Token> (token));
  31.  
  32. asio::async_result<decltype (handler)> result (handler);
  33.  
  34. if (success)
  35. handler(error_code{}, 42);
  36. else
  37. handler(asio::error::operation_aborted, 0);
  38.  
  39. return result.get ();
  40. }
  41.  
  42. void using_yield_ec(asio::yield_context yield) {
  43. for (bool success : { true, false }) {
  44. boost::system::error_code ec;
  45. auto answer = async_meaning_of_life(success, yield[ec]);
  46. std::cout << __FUNCTION__ << ": Result: " << ec.message() << "n";
  47. std::cout << __FUNCTION__ << ": Answer: " << answer << "n";
  48. }
  49. }
  50.  
  51. void using_yield_catch(asio::yield_context yield) {
  52. for (bool success : { true, false }) try {
  53. auto answer = async_meaning_of_life(success, yield);
  54. std::cout << __FUNCTION__ << ": Answer: " << answer << "n";
  55. } catch(boost::system::system_error const& e) {
  56. std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "n";
  57. }
  58. }
  59.  
  60. void using_future() {
  61. for (bool success : { true, false }) try {
  62. auto answer = async_meaning_of_life(success, asio::use_future);
  63. std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "n";
  64. } catch(boost::system::system_error const& e) {
  65. std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "n";
  66. }
  67. }
  68.  
  69. void using_handler() {
  70. for (bool success : { true, false })
  71. async_meaning_of_life(success, [](error_code ec, int answer) {
  72. std::cout << "using_handler: Result: " << ec.message() << "n";
  73. std::cout << "using_handler: Answer: " << answer << "n";
  74. });
  75. }
  76.  
  77. int main() {
  78. asio::io_service svc;
  79.  
  80. spawn(svc, using_yield_ec);
  81. spawn(svc, using_yield_catch);
  82. std::thread work([] {
  83. using_future();
  84. using_handler();
  85. });
  86.  
  87. svc.run();
  88. work.join();
  89. }
  90.  
  91. using_yield_ec: Result: Success
  92. using_yield_ec: Answer: 42
  93. using_yield_ec: Result: Operation canceled
  94. using_yield_ec: Answer: 0
  95. using_future: Answer: 42
  96. using_yield_catch: Answer: 42
  97. using_yield_catch: Caught: Operation canceled
  98. using_future: Caught: Operation canceled
  99. using_handler: Result: Success
  100. using_handler: Answer: 42
  101. using_handler: Result: Operation canceled
  102. using_handler: Answer: 0
Add Comment
Please, Sign In to add comment