pipe_out.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. // Copyright (c) 2016 Klemens D. Morgenstern
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_PROCESS_WINDOWS_PIPE_OUT_HPP
  11. #define BOOST_PROCESS_WINDOWS_PIPE_OUT_HPP
  12. #include <boost/winapi/process.hpp>
  13. #include <boost/winapi/handles.hpp>
  14. #include <boost/process/v1/detail/used_handles.hpp>
  15. #include <boost/process/v1/detail/handler_base.hpp>
  16. #include <boost/system/error_code.hpp>
  17. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace windows {
  18. template<int p1, int p2>
  19. struct pipe_out : public ::boost::process::v1::detail::handler_base, ::boost::process::v1::detail::uses_handles
  20. {
  21. ::boost::winapi::HANDLE_ handle;
  22. ::boost::winapi::HANDLE_ get_used_handles() const { return handle; }
  23. pipe_out(::boost::winapi::HANDLE_ handle) : handle(handle) {}
  24. template<typename T>
  25. pipe_out(T & p) : handle(p.native_sink())
  26. {
  27. p.assign_sink(::boost::winapi::INVALID_HANDLE_VALUE_);
  28. }
  29. template<typename WindowsExecutor>
  30. void on_setup(WindowsExecutor &e) const;
  31. template<typename WindowsExecutor>
  32. void on_error(WindowsExecutor &, const std::error_code &) const
  33. {
  34. ::boost::winapi::CloseHandle(handle);
  35. }
  36. template<typename WindowsExecutor>
  37. void on_success(WindowsExecutor &) const
  38. {
  39. ::boost::winapi::CloseHandle(handle);
  40. }
  41. };
  42. template<>
  43. template<typename WindowsExecutor>
  44. void pipe_out<1,-1>::on_setup(WindowsExecutor &e) const
  45. {
  46. boost::winapi::SetHandleInformation(handle,
  47. boost::winapi::HANDLE_FLAG_INHERIT_,
  48. boost::winapi::HANDLE_FLAG_INHERIT_);
  49. e.startup_info.hStdOutput = handle;
  50. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  51. e.inherit_handles = true;
  52. }
  53. template<>
  54. template<typename WindowsExecutor>
  55. void pipe_out<2,-1>::on_setup(WindowsExecutor &e) const
  56. {
  57. boost::winapi::SetHandleInformation(handle,
  58. boost::winapi::HANDLE_FLAG_INHERIT_,
  59. boost::winapi::HANDLE_FLAG_INHERIT_);
  60. e.startup_info.hStdError = handle;
  61. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  62. e.inherit_handles = true;
  63. }
  64. template<>
  65. template<typename WindowsExecutor>
  66. void pipe_out<1,2>::on_setup(WindowsExecutor &e) const
  67. {
  68. boost::winapi::SetHandleInformation(handle,
  69. boost::winapi::HANDLE_FLAG_INHERIT_,
  70. boost::winapi::HANDLE_FLAG_INHERIT_);
  71. e.startup_info.hStdOutput = handle;
  72. e.startup_info.hStdError = handle;
  73. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  74. e.inherit_handles = true;
  75. }
  76. template<int p1, int p2>
  77. struct async_pipe_out : public pipe_out<p1, p2>
  78. {
  79. async_pipe &pipe;
  80. template<typename AsyncPipe>
  81. async_pipe_out(AsyncPipe & p) : pipe_out<p1, p2>(p.native_sink()), pipe(p)
  82. {
  83. }
  84. template<typename Pipe, typename Executor>
  85. static void close(Pipe & pipe, Executor &)
  86. {
  87. boost::system::error_code ec;
  88. std::move(pipe).sink().close(ec);
  89. }
  90. template<typename Executor>
  91. void on_error(Executor & exec, const std::error_code &)
  92. {
  93. close(pipe, exec);
  94. }
  95. template<typename Executor>
  96. void on_success(Executor &exec)
  97. {
  98. close(pipe, exec);
  99. }
  100. };
  101. }}}}}
  102. #endif