Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / process / detail / on_exit.hpp
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_DETAIL_ON_EXIT_HPP_
7 #define BOOST_PROCESS_DETAIL_ON_EXIT_HPP_
8
9 #include <boost/process/detail/config.hpp>
10
11 #if defined(BOOST_POSIX_API)
12 #include <boost/process/detail/posix/on_exit.hpp>
13 #elif defined(BOOST_WINDOWS_API)
14 #include <boost/process/detail/windows/on_exit.hpp>
15 #endif
16
17 #include <future>
18 #include <memory>
19
20 namespace boost { namespace process { namespace detail {
21
22 inline std::function<void(int, const std::error_code &)> on_exit_from_future(std::future<int> &f)
23 {
24     std::shared_ptr<std::promise<int>> promise = std::make_shared<std::promise<int>>();
25     f = promise->get_future();
26     return [promise](int code, const std::error_code & ec)
27             {
28                 if (ec)
29                     promise->set_exception(
30                         std::make_exception_ptr(process_error(ec, "on_exit failed with error"))
31                         );
32                 else
33                     promise->set_value(code);
34             };
35 }
36
37
38 struct on_exit_
39 {
40     api::on_exit_ operator= (const std::function<void(int, const std::error_code&)> & f) const {return f;}
41     api::on_exit_ operator()(const std::function<void(int, const std::error_code&)> & f) const {return f;}
42
43     api::on_exit_ operator= (std::future<int> &f) const {return on_exit_from_future(f);}
44     api::on_exit_ operator()(std::future<int> &f) const {return on_exit_from_future(f);}
45 };
46
47 }
48
49 constexpr static ::boost::process::detail::on_exit_ on_exit{};
50
51
52 }}
53 #endif /* INCLUDE_BOOST_PROCESS_WINDOWS_ON_EXIT_HPP_ */