pipe_in.hpp 2.5 KB

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