]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp14/deferred/deferred_6.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / asio / example / cpp14 / deferred / deferred_6.cpp
1 //
2 // deferred_6.cpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #include <boost/asio.hpp>
12 #include <boost/asio/experimental/append.hpp>
13 #include <boost/asio/experimental/deferred.hpp>
14 #include <iostream>
15
16 using boost::asio::experimental::append;
17 using boost::asio::experimental::deferred;
18
19 template <typename CompletionToken>
20 auto async_wait_twice(boost::asio::steady_timer& timer, CompletionToken&& token)
21 {
22 return deferred.values(&timer)(
23 deferred(
24 [](boost::asio::steady_timer* timer)
25 {
26 timer->expires_after(std::chrono::seconds(1));
27 return timer->async_wait(append(deferred, timer));
28 }
29 )
30 )(
31 deferred(
32 [](boost::system::error_code ec, boost::asio::steady_timer* timer)
33 {
34 std::cout << "first timer wait finished: " << ec.message() << "\n";
35 timer->expires_after(std::chrono::seconds(1));
36 return deferred.when(!ec)
37 .then(timer->async_wait(deferred))
38 .otherwise(deferred.values(ec));
39 }
40 )
41 )(
42 deferred(
43 [](boost::system::error_code ec)
44 {
45 std::cout << "second timer wait finished: " << ec.message() << "\n";
46 return deferred.when(!ec)
47 .then(deferred.values(42))
48 .otherwise(deferred.values(0));
49 }
50 )
51 )(
52 std::forward<CompletionToken>(token)
53 );
54 }
55
56 int main()
57 {
58 boost::asio::io_context ctx;
59
60 boost::asio::steady_timer timer(ctx);
61 timer.expires_after(std::chrono::seconds(1));
62
63 async_wait_twice(
64 timer,
65 [](int result)
66 {
67 std::cout << "result is " << result << "\n";
68 }
69 );
70
71 // Uncomment the following line to trigger an error in async_wait_twice.
72 //timer.cancel();
73
74 ctx.run();
75
76 return 0;
77 }