async.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. //
  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)
  5. /** \file boost/process/async.hpp
  6. The header which provides the basic asynchronous features.
  7. It provides the on_exit property, which allows callbacks when the process exits.
  8. It also implements the necessary traits for passing an boost::asio::io_context,
  9. which is needed for asynchronous communication.
  10. It also pulls the [boost::asio::buffer](http://www.boost.org/doc/libs/release/doc/html/boost_asio/reference/buffer.html)
  11. into the boost::process namespace for convenience.
  12. \xmlonly
  13. <programlisting>
  14. namespace boost {
  15. namespace process {
  16. namespace v1 {
  17. <emphasis>unspecified</emphasis> <ulink url="http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/buffer.html">buffer</ulink>;
  18. <emphasis>unspecified</emphasis> <globalname alt="boost::process::v1::on_exit">on_exit</globalname>;
  19. }
  20. }
  21. }
  22. </programlisting>
  23. \endxmlonly
  24. */
  25. #ifndef BOOST_PROCESS_ASYNC_HPP_
  26. #define BOOST_PROCESS_ASYNC_HPP_
  27. #include <boost/process/v1/detail/traits.hpp>
  28. #include <boost/process/v1/detail/on_exit.hpp>
  29. #include <boost/asio/io_context.hpp>
  30. #include <boost/asio/streambuf.hpp>
  31. #include <boost/asio/buffer.hpp>
  32. #include <type_traits>
  33. #include <boost/fusion/iterator/deref.hpp>
  34. #if defined(BOOST_POSIX_API)
  35. #include <boost/process/v1/detail/posix/io_context_ref.hpp>
  36. #include <boost/process/v1/detail/posix/async_in.hpp>
  37. #include <boost/process/v1/detail/posix/async_out.hpp>
  38. #include <boost/process/v1/detail/posix/on_exit.hpp>
  39. #elif defined(BOOST_WINDOWS_API)
  40. #include <boost/process/v1/detail/windows/io_context_ref.hpp>
  41. #include <boost/process/v1/detail/windows/async_in.hpp>
  42. #include <boost/process/v1/detail/windows/async_out.hpp>
  43. #include <boost/process/v1/detail/windows/on_exit.hpp>
  44. #endif
  45. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail {
  46. struct async_tag;
  47. template<typename T>
  48. struct is_io_context : std::false_type {};
  49. template<>
  50. struct is_io_context<api::io_context_ref> : std::true_type {};
  51. template<typename Tuple>
  52. inline asio::io_context& get_io_context(const Tuple & tup)
  53. {
  54. auto& ref = *boost::fusion::find_if<is_io_context<boost::mpl::_>>(tup);
  55. return ref.get();
  56. }
  57. struct async_builder
  58. {
  59. boost::asio::io_context * ios;
  60. void operator()(boost::asio::io_context & ios_) {this->ios = &ios_;};
  61. typedef api::io_context_ref result_type;
  62. api::io_context_ref get_initializer() {return api::io_context_ref (*ios);};
  63. };
  64. template<>
  65. struct initializer_builder<async_tag>
  66. {
  67. typedef async_builder type;
  68. };
  69. }
  70. using ::boost::asio::buffer;
  71. #if defined(BOOST_PROCESS_DOXYGEN)
  72. /** When an io_context is passed, the on_exit property can be used, to be notified
  73. when the child process exits.
  74. The following syntax is valid
  75. \code{.cpp}
  76. on_exit=function;
  77. on_exit(function);
  78. \endcode
  79. with `function` being a callable object with the signature `(int, const std::error_code&)` or an
  80. `std::future<int>`.
  81. \par Example
  82. \code{.cpp}
  83. io_context ios;
  84. child c("ls", ios, on_exit=[](int exit, const std::error_code& ec_in){});
  85. std::future<int> exit_code;
  86. chlid c2("ls", ios, on_exit=exit_code);
  87. \endcode
  88. \note The handler is not invoked when the launch fails.
  89. \warning When used \ref ignore_error it might get invoked on error.
  90. \warning `on_exit` uses `boost::asio::signal_set` to listen for `SIGCHLD` on posix, and so has the
  91. same restrictions as that class (do not register a handler for `SIGCHLD` except by using
  92. `boost::asio::signal_set`).
  93. */
  94. constexpr static ::boost::process::v1::detail::on_exit_ on_exit{};
  95. #endif
  96. }}}
  97. #endif /* INCLUDE_BOOST_PROCESS_DETAIL_ASYNC_HPP_ */