MCPcopy Create free account
hub / github.com/boostorg/asio / async_read_input

Function async_read_input

example/cpp14/operations/callback_wrapper.cpp:39–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37// The initiating function for the asynchronous operation.
38template <typename CompletionToken>
39auto 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

Callers 3

test_callbackFunction · 0.70
test_deferredFunction · 0.70
test_futureFunction · 0.70

Calls 6

get_associated_allocatorFunction · 0.85
stringFunction · 0.85
read_inputFunction · 0.70
dispatchFunction · 0.50
get_executorMethod · 0.45

Tested by 3

test_callbackFunction · 0.56
test_deferredFunction · 0.56
test_futureFunction · 0.56