used_handles.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_USED_HANDLES_HPP_
  6. #define BOOST_PROCESS_DETAIL_USED_HANDLES_HPP_
  7. #include <type_traits>
  8. #include <boost/fusion/include/filter_if.hpp>
  9. #include <boost/fusion/include/for_each.hpp>
  10. #if defined(BOOST_POSIX_API)
  11. #include <boost/process/detail/posix/handles.hpp>
  12. #include <boost/process/detail/posix/asio_fwd.hpp>
  13. #else
  14. #include <boost/process/detail/windows/handles.hpp>
  15. #include <boost/process/detail/windows/asio_fwd.hpp>
  16. #endif
  17. namespace boost { namespace process { namespace detail {
  18. struct uses_handles
  19. {
  20. //If you get an error here, you must add a `get_handles` function that returns a range or a single handle value
  21. void get_used_handles() const;
  22. };
  23. template<typename T>
  24. struct does_use_handle: std::is_base_of<uses_handles, T> {};
  25. template<typename T>
  26. struct does_use_handle<T&> : std::is_base_of<uses_handles, T> {};
  27. template<typename T>
  28. struct does_use_handle<const T&> : std::is_base_of<uses_handles, T> {};
  29. template<typename Char, typename Sequence>
  30. class executor;
  31. template<typename Func>
  32. struct foreach_handle_invocator
  33. {
  34. Func & func;
  35. foreach_handle_invocator(Func & func) : func(func) {}
  36. template<typename Range>
  37. void invoke(const Range & range) const
  38. {
  39. for (auto handle_ : range)
  40. func(handle_);
  41. }
  42. void invoke(::boost::process::detail::api::native_handle_type handle) const {func(handle);};
  43. template<typename T>
  44. void operator()(T & val) const {invoke(val.get_used_handles());}
  45. };
  46. template<typename Executor, typename Function>
  47. void foreach_used_handle(Executor &exec, Function &&func)
  48. {
  49. boost::fusion::for_each(boost::fusion::filter_if<does_use_handle<boost::mpl::_>>(exec.seq),
  50. foreach_handle_invocator<Function>(func));
  51. }
  52. template<typename Executor>
  53. std::vector<::boost::process::detail::api::native_handle_type>
  54. get_used_handles(Executor &exec)
  55. {
  56. std::vector<::boost::process::detail::api::native_handle_type> res;
  57. foreach_used_handle(exec, [&](::boost::process::detail::api::native_handle_type handle){res.push_back(handle);});
  58. return res;
  59. }
  60. }}}
  61. #endif /* BOOST_PROCESS_DETAIL_USED_HANDLES_HPP_ */