async.hpp 3.6 KB

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