on_exit.hpp 1.7 KB

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