on_exit.hpp 1.6 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_WINDOWS_ON_EXIT_HPP_
  6. #define BOOST_PROCESS_WINDOWS_ON_EXIT_HPP_
  7. #include <boost/process/v1/async.hpp>
  8. #include <boost/process/v1/detail/config.hpp>
  9. #include <boost/process/v1/detail/handler_base.hpp>
  10. #include <boost/process/v1/detail/windows/async_handler.hpp>
  11. #include <boost/asio/execution.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 windows {
  18. struct on_exit_ : boost::process::v1::detail::windows::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 [v, handler_](int exit_code, const std::error_code & ec)
  31. {
  32. handler_(static_cast<int>(exit_code), ec);
  33. };
  34. }
  35. };
  36. }}}}}
  37. #endif /* INCLUDE_BOOST_PROCESS_WINDOWS_ON_EXIT_HPP_ */