]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp14/operations/callback_wrapper.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / asio / example / cpp14 / operations / callback_wrapper.cpp
1 //
2 // callback_wrapper.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 <iostream>
13
14 //------------------------------------------------------------------------------
15
16 // This is a mock implementation of an API that uses a move-only function object
17 // for exposing a callback. The callback has the signature void(std::string).
18
19 template <typename Callback>
20 void read_input(const std::string& prompt, Callback cb)
21 {
22 std::thread(
23 [prompt, cb = std::move(cb)]() mutable
24 {
25 std::cout << prompt << ": ";
26 std::cout.flush();
27 std::string line;
28 std::getline(std::cin, line);
29 std::move(cb)(std::move(line));
30 }).detach();
31 }
32
33 //------------------------------------------------------------------------------
34
35 // This is an asynchronous operation that wraps the callback-based API.
36
37 // The initiating function for the asynchronous operation.
38 template <typename CompletionToken>
39 auto async_read_input(const std::string& prompt, CompletionToken&& token)
40 {
41 // Define a function object that contains the code to launch the asynchronous
42 // operation. This is passed the concrete completion handler, followed by any
43 // additional arguments that were passed through the call to async_initiate.
44 auto init = [](auto handler, const std::string& prompt)
45 {
46 // According to the rules for asynchronous operations, we need to track
47 // outstanding work against the handler's associated executor until the
48 // asynchronous operation is complete.
49 auto work = boost::asio::make_work_guard(handler);
50
51 // Launch the operation with a callback that will receive the result and
52 // pass it through to the asynchronous operation's completion handler.
53 read_input(prompt,
54 [
55 handler = std::move(handler),
56 work = std::move(work)
57 ](std::string result) mutable
58 {
59 // Get the handler's associated allocator. If the handler does not
60 // specify an allocator, use the recycling allocator as the default.
61 auto alloc = boost::asio::get_associated_allocator(
62 handler, boost::asio::recycling_allocator<void>());
63
64 // Dispatch the completion handler through the handler's associated
65 // executor, using the handler's associated allocator.
66 boost::asio::dispatch(work.get_executor(),
67 boost::asio::bind_allocator(alloc,
68 [
69 handler = std::move(handler),
70 result = std::string(result)
71 ]() mutable
72 {
73 std::move(handler)(result);
74 }));
75 });
76 };
77
78 // The async_initiate function is used to transform the supplied completion
79 // token to the completion handler. When calling this function we explicitly
80 // specify the completion signature of the operation. We must also return the
81 // result of the call since the completion token may produce a return value,
82 // such as a future.
83 return boost::asio::async_initiate<CompletionToken, void(std::string)>(
84 init, // First, pass the function object that launches the operation,
85 token, // then the completion token that will be transformed to a handler,
86 prompt); // and, finally, any additional arguments to the function object.
87 }
88
89 //------------------------------------------------------------------------------
90
91 void test_callback()
92 {
93 boost::asio::io_context io_context;
94
95 // Test our asynchronous operation using a lambda as a callback. We will use
96 // an io_context to specify an associated executor.
97 async_read_input("Enter your name",
98 boost::asio::bind_executor(io_context,
99 [](const std::string& result)
100 {
101 std::cout << "Hello " << result << "\n";
102 }));
103
104 io_context.run();
105 }
106
107 //------------------------------------------------------------------------------
108
109 void test_future()
110 {
111 // Test our asynchronous operation using the use_future completion token.
112 // This token causes the operation's initiating function to return a future,
113 // which may be used to synchronously wait for the result of the operation.
114 std::future<std::string> f =
115 async_read_input("Enter your name", boost::asio::use_future);
116
117 std::string result = f.get();
118 std::cout << "Hello " << result << "\n";
119 }
120
121 //------------------------------------------------------------------------------
122
123 int main()
124 {
125 test_callback();
126 test_future();
127 }