pipe_out.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/detail/handler_base.hpp>
  15. namespace boost { namespace process { namespace detail { namespace windows {
  16. template<int p1, int p2>
  17. struct pipe_out : public ::boost::process::detail::handler_base
  18. {
  19. ::boost::winapi::HANDLE_ handle;
  20. pipe_out(::boost::winapi::HANDLE_ handle) : handle(handle) {}
  21. template<typename T>
  22. pipe_out(T & p) : handle(p.native_sink())
  23. {
  24. p.assign_sink(::boost::winapi::INVALID_HANDLE_VALUE_);
  25. }
  26. template<typename WindowsExecutor>
  27. void on_setup(WindowsExecutor &e) const;
  28. template<typename WindowsExecutor>
  29. void on_error(WindowsExecutor &, const std::error_code &) const
  30. {
  31. ::boost::winapi::CloseHandle(handle);
  32. }
  33. template<typename WindowsExecutor>
  34. void on_success(WindowsExecutor &) const
  35. {
  36. ::boost::winapi::CloseHandle(handle);
  37. }
  38. };
  39. template<>
  40. template<typename WindowsExecutor>
  41. void pipe_out<1,-1>::on_setup(WindowsExecutor &e) const
  42. {
  43. boost::winapi::SetHandleInformation(handle,
  44. boost::winapi::HANDLE_FLAG_INHERIT_,
  45. boost::winapi::HANDLE_FLAG_INHERIT_);
  46. e.startup_info.hStdOutput = handle;
  47. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  48. e.inherit_handles = true;
  49. }
  50. template<>
  51. template<typename WindowsExecutor>
  52. void pipe_out<2,-1>::on_setup(WindowsExecutor &e) const
  53. {
  54. boost::winapi::SetHandleInformation(handle,
  55. boost::winapi::HANDLE_FLAG_INHERIT_,
  56. boost::winapi::HANDLE_FLAG_INHERIT_);
  57. e.startup_info.hStdError = handle;
  58. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  59. e.inherit_handles = true;
  60. }
  61. template<>
  62. template<typename WindowsExecutor>
  63. void pipe_out<1,2>::on_setup(WindowsExecutor &e) const
  64. {
  65. boost::winapi::SetHandleInformation(handle,
  66. boost::winapi::HANDLE_FLAG_INHERIT_,
  67. boost::winapi::HANDLE_FLAG_INHERIT_);
  68. e.startup_info.hStdOutput = handle;
  69. e.startup_info.hStdError = handle;
  70. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  71. e.inherit_handles = true;
  72. }
  73. template<int p1, int p2>
  74. struct async_pipe_out : public pipe_out<p1, p2>
  75. {
  76. async_pipe &pipe;
  77. template<typename AsyncPipe>
  78. async_pipe_out(AsyncPipe & p) : pipe_out<p1, p2>(p.native_sink()), pipe(p)
  79. {
  80. }
  81. template<typename Pipe, typename Executor>
  82. static void close(Pipe & pipe, Executor &)
  83. {
  84. boost::system::error_code ec;
  85. std::move(pipe).sink().close(ec);
  86. }
  87. template<typename Executor>
  88. void on_error(Executor & exec, const std::error_code &)
  89. {
  90. close(pipe, exec);
  91. }
  92. template<typename Executor>
  93. void on_success(Executor &exec)
  94. {
  95. close(pipe, exec);
  96. }
  97. };
  98. }}}}
  99. #endif