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