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 
6 #ifndef BOOST_PROCESS_WINDOWS_ON_EXIT_HPP_
7 #define BOOST_PROCESS_WINDOWS_ON_EXIT_HPP_
8 
9 #include <boost/process/async.hpp>
10 #include <boost/process/detail/config.hpp>
11 #include <boost/process/detail/handler_base.hpp>
12 #include <boost/process/detail/windows/async_handler.hpp>
13 #include <boost/asio/execution.hpp>
14 #include <system_error>
15 #include <functional>
16 
17 namespace boost { namespace process { namespace detail {
18 
19 template<typename Tuple>
20 inline asio::io_context& get_io_context(const Tuple & tup);
21 
22 namespace windows {
23 
24 struct on_exit_ : boost::process::detail::windows::async_handler
25 {
26     std::function<void(int, const std::error_code&)> handler;
on_exit_boost::process::detail::windows::on_exit_27     on_exit_(const std::function<void(int, const std::error_code&)> & handler) : handler(handler)
28     {
29 
30     }
31 
32     template<typename Executor>
on_exit_handlerboost::process::detail::windows::on_exit_33     std::function<void(int, const std::error_code&)> on_exit_handler(Executor& exec)
34     {
35         auto v = boost::asio::prefer(boost::process::detail::get_io_context(exec.seq).get_executor(),
36                                      boost::asio::execution::outstanding_work.tracked);
37         auto handler_ = this->handler;
38         return [v, handler_](int exit_code, const std::error_code & ec)
39                {
40                     handler_(static_cast<int>(exit_code), ec);
41                };
42 
43     }
44 };
45 
46 
47 }}}}
48 #endif /* INCLUDE_BOOST_PROCESS_WINDOWS_ON_EXIT_HPP_ */
49