pipe_in.hpp 2.4 KB

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