on_exit.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #ifndef BOOST_PROCESS_POSIX_ON_EXIT_HPP_
  6. #define BOOST_PROCESS_POSIX_ON_EXIT_HPP_
  7. #include <boost/asio/execution.hpp>
  8. #include <boost/process/v1/async.hpp>
  9. #include <boost/process/v1/detail/config.hpp>
  10. #include <boost/process/v1/detail/handler_base.hpp>
  11. #include <boost/process/v1/detail/posix/async_handler.hpp>
  12. #include <system_error>
  13. #include <functional>
  14. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail {
  15. template<typename Tuple>
  16. inline asio::io_context& get_io_context(const Tuple & tup);
  17. namespace posix {
  18. struct on_exit_ : boost::process::v1::detail::posix::async_handler
  19. {
  20. std::function<void(int, const std::error_code&)> handler;
  21. on_exit_(const std::function<void(int, const std::error_code&)> & handler) : handler(handler)
  22. {
  23. }
  24. template<typename Executor>
  25. std::function<void(int, const std::error_code&)> on_exit_handler(Executor& exec)
  26. {
  27. auto v = boost::asio::prefer(boost::process::v1::detail::get_io_context(exec.seq).get_executor(),
  28. boost::asio::execution::outstanding_work.tracked);
  29. auto handler_ = this->handler;
  30. return
  31. [handler_, v](int exit_code, const std::error_code & ec)
  32. {
  33. handler_(exit_code, ec);
  34. };
  35. }
  36. };
  37. }}}}}
  38. #endif /* BOOST_PROCESS_POSIX_ON_EXIT_HPP_ */