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 
10 #ifndef BOOST_PROCESS_WINDOWS_INITIALIZERS_PIPE_IN_HPP
11 #define BOOST_PROCESS_WINDOWS_INITIALIZERS_PIPE_IN_HPP
12 
13 #include <boost/winapi/process.hpp>
14 #include <boost/winapi/handles.hpp>
15 #include <boost/process/detail/used_handles.hpp>
16 #include <boost/process/detail/handler_base.hpp>
17 
18 namespace boost { namespace process { namespace detail { namespace windows {
19 
20 struct pipe_in : public ::boost::process::detail::handler_base, ::boost::process::detail::uses_handles
21 {
22     ::boost::winapi::HANDLE_ handle;
23 
get_used_handlesboost::process::detail::windows::pipe_in24     ::boost::winapi::HANDLE_ get_used_handles() const { return handle; }
25 
pipe_inboost::process::detail::windows::pipe_in26     pipe_in(::boost::winapi::HANDLE_ handle) : handle(handle) {}
27 
28     template<typename T> //async_pipe
pipe_inboost::process::detail::windows::pipe_in29     pipe_in(T & p) : handle(p.native_source())
30     {
31         p.assign_source(::boost::winapi::INVALID_HANDLE_VALUE_);
32     }
33 
34     template <class WindowsExecutor>
on_setupboost::process::detail::windows::pipe_in35     void on_setup(WindowsExecutor &e) const
36     {
37         boost::winapi::SetHandleInformation(handle,
38                 boost::winapi::HANDLE_FLAG_INHERIT_,
39                 boost::winapi::HANDLE_FLAG_INHERIT_);
40 
41         e.startup_info.hStdInput = handle;
42         e.startup_info.dwFlags  |= boost::winapi::STARTF_USESTDHANDLES_;
43         e.inherit_handles = true;
44     }
45     template<typename WindowsExecutor>
on_errorboost::process::detail::windows::pipe_in46     void on_error(WindowsExecutor &, const std::error_code &) const
47     {
48         ::boost::winapi::CloseHandle(handle);
49     }
50 
51     template<typename WindowsExecutor>
on_successboost::process::detail::windows::pipe_in52     void on_success(WindowsExecutor &) const
53     {
54         ::boost::winapi::CloseHandle(handle);
55     }
56 };
57 
58 class async_pipe;
59 
60 struct async_pipe_in : public pipe_in
61 {
62     async_pipe &pipe;
63 
64     template<typename AsyncPipe>
async_pipe_inboost::process::detail::windows::async_pipe_in65     async_pipe_in(AsyncPipe & p) : pipe_in(p.native_source()), pipe(p)
66     {
67     }
68 
69     template<typename Pipe, typename Executor>
closeboost::process::detail::windows::async_pipe_in70     static void close(Pipe & pipe, Executor &)
71     {
72         boost::system::error_code ec;
73         std::move(pipe).source().close(ec);
74     }
75 
76     template<typename Executor>
on_errorboost::process::detail::windows::async_pipe_in77     void on_error(Executor & exec, const std::error_code &)
78     {
79         close(pipe, exec);
80     }
81 
82     template<typename Executor>
on_successboost::process::detail::windows::async_pipe_in83     void on_success(Executor &exec)
84     {
85         close(pipe, exec);
86     }
87 };
88 
89 
90 }}}}
91 
92 #endif
93