]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp14/deferred/deferred_7.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / asio / example / cpp14 / deferred / deferred_7.cpp
1 //
2 // deferred_7.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 | deferred(
31 [](boost::system::error_code ec, boost::asio::steady_timer* timer)
32 {
33 std::cout << "first timer wait finished: " << ec.message() << "\n";
34 timer->expires_after(std::chrono::seconds(1));
35 return deferred.when(!ec)
36 .then(timer->async_wait(deferred))
37 .otherwise(deferred.values(ec));
38 }
39 )
40 | deferred(
41 [](boost::system::error_code ec)
42 {
43 std::cout << "second timer wait finished: " << ec.message() << "\n";
44 return deferred.when(!ec)
45 .then(deferred.values(42))
46 .otherwise(deferred.values(0));
47 }
48 )
49 | std::forward<CompletionToken>(token);
50 }
51
52 int main()
53 {
54 boost::asio::io_context ctx;
55
56 boost::asio::steady_timer timer(ctx);
57 timer.expires_after(std::chrono::seconds(1));
58
59 async_wait_twice(
60 timer,
61 [](int result)
62 {
63 std::cout << "result is " << result << "\n";
64 }
65 );
66
67 // Uncomment the following line to trigger an error in async_wait_twice.
68 //timer.cancel();
69
70 ctx.run();
71
72 return 0;
73 }