pipe_in.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROCESS_WINDOWS_INITIALIZERS_PIPE_IN_HPP
  10. #define BOOST_PROCESS_WINDOWS_INITIALIZERS_PIPE_IN_HPP
  11. #include <boost/winapi/process.hpp>
  12. #include <boost/winapi/handles.hpp>
  13. #include <boost/process/v1/detail/used_handles.hpp>
  14. #include <boost/process/v1/detail/handler_base.hpp>
  15. #include <boost/system/error_code.hpp>
  16. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace windows {
  17. struct pipe_in : public ::boost::process::v1::detail::handler_base, ::boost::process::v1::detail::uses_handles
  18. {
  19. ::boost::winapi::HANDLE_ handle;
  20. ::boost::winapi::HANDLE_ get_used_handles() const { return handle; }
  21. pipe_in(::boost::winapi::HANDLE_ handle) : handle(handle) {}
  22. template<typename T> //async_pipe
  23. pipe_in(T & p) : handle(p.native_source())
  24. {
  25. p.assign_source(::boost::winapi::INVALID_HANDLE_VALUE_);
  26. }
  27. template <class WindowsExecutor>
  28. void on_setup(WindowsExecutor &e) const
  29. {
  30. boost::winapi::SetHandleInformation(handle,
  31. boost::winapi::HANDLE_FLAG_INHERIT_,
  32. boost::winapi::HANDLE_FLAG_INHERIT_);
  33. e.startup_info.hStdInput = handle;
  34. e.startup_info.dwFlags |= boost::winapi::STARTF_USESTDHANDLES_;
  35. e.inherit_handles = true;
  36. }
  37. template<typename WindowsExecutor>
  38. void on_error(WindowsExecutor &, const std::error_code &) const
  39. {
  40. ::boost::winapi::CloseHandle(handle);
  41. }
  42. template<typename WindowsExecutor>
  43. void on_success(WindowsExecutor &) const
  44. {
  45. ::boost::winapi::CloseHandle(handle);
  46. }
  47. };
  48. class async_pipe;
  49. struct async_pipe_in : public pipe_in
  50. {
  51. async_pipe &pipe;
  52. template<typename AsyncPipe>
  53. async_pipe_in(AsyncPipe & p) : pipe_in(p.native_source()), pipe(p)
  54. {
  55. }
  56. template<typename Pipe, typename Executor>
  57. static void close(Pipe & pipe, Executor &)
  58. {
  59. boost::system::error_code ec;
  60. std::move(pipe).source().close(ec);
  61. }
  62. template<typename Executor>
  63. void on_error(Executor & exec, const std::error_code &)
  64. {
  65. close(pipe, exec);
  66. }
  67. template<typename Executor>
  68. void on_success(Executor &exec)
  69. {
  70. close(pipe, exec);
  71. }
  72. };
  73. }}}}}
  74. #endif