1 // Copyright (c) 2016 Klemens D. Morgenstern
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 /** \file boost/process/async.hpp
8 The header which provides the basic asynchrounous features.
9 It provides the on_exit property, which allows callbacks when the process exits.
10 It also implements the necessary traits for passing an boost::asio::io_context,
11 which is needed for asynchronous communication.
13 It also pulls the [boost::asio::buffer](http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/buffer.html)
14 into the boost::process namespace for convenience.
20 <emphasis>unspecified</emphasis> <ulink url="http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/buffer.html">buffer</ulink>;
21 <emphasis>unspecified</emphasis> <globalname alt="boost::process::on_exit">on_exit</globalname>;
29 #ifndef BOOST_PROCESS_ASYNC_HPP_
30 #define BOOST_PROCESS_ASYNC_HPP_
32 #include <boost/process/detail/traits.hpp>
33 #include <boost/process/detail/on_exit.hpp>
35 #include <boost/asio/io_context.hpp>
36 #include <boost/asio/streambuf.hpp>
37 #include <boost/asio/buffer.hpp>
38 #include <type_traits>
39 #include <boost/fusion/iterator/deref.hpp>
41 #if defined(BOOST_POSIX_API)
42 #include <boost/process/detail/posix/io_context_ref.hpp>
43 #include <boost/process/detail/posix/async_in.hpp>
44 #include <boost/process/detail/posix/async_out.hpp>
45 #include <boost/process/detail/posix/on_exit.hpp>
47 #elif defined(BOOST_WINDOWS_API)
48 #include <boost/process/detail/windows/io_context_ref.hpp>
49 #include <boost/process/detail/windows/async_in.hpp>
50 #include <boost/process/detail/windows/async_out.hpp>
51 #include <boost/process/detail/windows/on_exit.hpp>
54 namespace boost { namespace process { namespace detail {
59 struct is_io_context : std::false_type {};
61 struct is_io_context<api::io_context_ref> : std::true_type {};
63 template<typename Tuple>
64 inline asio::io_context& get_io_context(const Tuple & tup)
66 auto& ref = *boost::fusion::find_if<is_io_context<boost::mpl::_>>(tup);
72 boost::asio::io_context * ios;
74 void operator()(boost::asio::io_context & ios_) {this->ios = &ios_;};
76 typedef api::io_context_ref result_type;
77 api::io_context_ref get_initializer() {return api::io_context_ref (*ios);};
82 struct initializer_builder<async_tag>
84 typedef async_builder type;
89 using ::boost::asio::buffer;
92 #if defined(BOOST_PROCESS_DOXYGEN)
93 /** When an io_context is passed, the on_exit property can be used, to be notified
94 when the child process exits.
97 The following syntax is valid
104 with `function` being a callable object with the signature `(int, const std::error_code&)` or an
112 child c("ls", on_exit=[](int exit, const std::error_code& ec_in){});
114 std::future<int> exit_code;
115 chlid c2("ls", on_exit=exit_code);
119 \note The handler is not invoked when the launch fails.
120 \warning When used \ref ignore_error it might get invoked on error.
121 \warning All `on_exit` use one signal(SIGCHLD) on posix, which is only guaranteed to work when all use the same `io_context`.
123 constexpr static ::boost::process::detail::on_exit_ on_exit{};
130 #endif /* INCLUDE_BOOST_PROCESS_DETAIL_ASYNC_HPP_ */