• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <boost/asio/associated_executor.hpp>
2 #include <boost/asio/bind_executor.hpp>
3 #include <boost/asio/execution.hpp>
4 #include <boost/asio/static_thread_pool.hpp>
5 #include <iostream>
6 #include <string>
7 
8 using boost::asio::bind_executor;
9 using boost::asio::get_associated_executor;
10 using boost::asio::static_thread_pool;
11 namespace execution = boost::asio::execution;
12 
13 // A function to asynchronously read a single line from an input stream.
14 template <class IoExecutor, class Handler>
async_getline(IoExecutor io_ex,std::istream & is,Handler handler)15 void async_getline(IoExecutor io_ex, std::istream& is, Handler handler)
16 {
17   // Track work for the handler's associated executor.
18   auto work_ex = boost::asio::prefer(
19       get_associated_executor(handler, io_ex),
20       execution::outstanding_work.tracked);
21 
22   // Post a function object to do the work asynchronously.
23   execution::execute(
24       boost::asio::require(io_ex, execution::blocking.never),
25       [&is, work_ex, handler=std::move(handler)]() mutable
26       {
27         std::string line;
28         std::getline(is, line);
29 
30         // Pass the result to the handler, via the associated executor.
31         execution::execute(
32             boost::asio::prefer(work_ex, execution::blocking.possibly),
33             [line=std::move(line), handler=std::move(handler)]() mutable
34             {
35               handler(std::move(line));
36             });
37       });
38 }
39 
40 // A function to asynchronously read multiple lines from an input stream.
41 template <class IoExecutor, class Handler>
async_getlines(IoExecutor io_ex,std::istream & is,std::string init,Handler handler)42 void async_getlines(IoExecutor io_ex, std::istream& is, std::string init, Handler handler)
43 {
44   // Track work for the I/O executor.
45   auto io_work_ex = boost::asio::prefer(io_ex,
46       execution::outstanding_work.tracked);
47 
48   // Track work for the handler's associated executor.
49   auto handler_work_ex = boost::asio::prefer(
50       get_associated_executor(handler, io_ex),
51       execution::outstanding_work.tracked);
52 
53   // Use the associated executor for each operation in the composition.
54   async_getline(io_work_ex, is,
55       bind_executor(handler_work_ex,
56         [io_work_ex, &is, lines=std::move(init), handler=std::move(handler)]
57         (std::string line) mutable
58         {
59           if (line.empty())
60             handler(lines);
61           else
62             async_getlines(io_work_ex, is, lines + line + "\n", std::move(handler));
63         }));
64 }
65 
main()66 int main()
67 {
68   static_thread_pool io_pool(1);
69   static_thread_pool completion_pool(1);
70 
71   std::cout << "Enter text, terminating with a blank line:\n";
72 
73   async_getlines(io_pool.executor(), std::cin, "",
74       bind_executor(completion_pool.executor(), [](std::string lines)
75         {
76           std::cout << "Lines:\n" << lines << "\n";
77         }));
78 
79   io_pool.wait();
80   completion_pool.wait();
81 }
82