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
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)
10 #ifndef BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_OUT_HPP
11 #define BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_OUT_HPP
13 #include <boost/winapi/process.hpp>
14 #include <boost/winapi/handles.hpp>
15 #include <boost/winapi/handle_info.hpp>
16 #include <boost/winapi/error_codes.hpp>
17 #include <boost/asio/read.hpp>
18 #include <boost/process/detail/handler_base.hpp>
19 #include <boost/process/detail/windows/asio_fwd.hpp>
27 namespace boost { namespace process { namespace detail { namespace windows {
30 template <typename Executor>
31 inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 1>, std::integral_constant<int, -1>)
33 boost::winapi::SetHandleInformation(handle,
34 boost::winapi::HANDLE_FLAG_INHERIT_,
35 boost::winapi::HANDLE_FLAG_INHERIT_);
37 e.startup_info.hStdOutput = handle;
38 e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
39 e.inherit_handles = true;
42 template <typename Executor>
43 inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 2>, std::integral_constant<int, -1>)
45 boost::winapi::SetHandleInformation(handle,
46 boost::winapi::HANDLE_FLAG_INHERIT_,
47 boost::winapi::HANDLE_FLAG_INHERIT_);
50 e.startup_info.hStdError = handle;
51 e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
52 e.inherit_handles = true;
55 template <typename Executor>
56 inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 1>, std::integral_constant<int, 2>)
58 boost::winapi::SetHandleInformation(handle,
59 boost::winapi::HANDLE_FLAG_INHERIT_,
60 boost::winapi::HANDLE_FLAG_INHERIT_);
62 e.startup_info.hStdOutput = handle;
63 e.startup_info.hStdError = handle;
64 e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
65 e.inherit_handles = true;
68 template<int p1, int p2, typename Buffer>
69 struct async_out_buffer : ::boost::process::detail::windows::handler_base_ext,
70 ::boost::process::detail::windows::require_io_context
74 std::shared_ptr<boost::process::async_pipe> pipe;
77 async_out_buffer(Buffer & buf) : buf(buf)
80 template <typename Executor>
81 inline void on_success(Executor&)
83 auto pipe = this->pipe;
84 boost::asio::async_read(*pipe, buf,
85 [pipe](const boost::system::error_code&, std::size_t){});
86 std::move(*pipe).sink().close();
91 template<typename Executor>
92 void on_error(Executor &, const std::error_code &) const
94 std::move(*pipe).sink().close();
97 template <typename WindowsExecutor>
98 void on_setup(WindowsExecutor &exec)
101 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
102 apply_out_handles(exec, std::move(*pipe).sink().native_handle(),
103 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
109 template<int p1, int p2, typename Type>
110 struct async_out_future : ::boost::process::detail::windows::handler_base_ext,
111 ::boost::process::detail::windows::require_io_context
113 std::shared_ptr<boost::process::async_pipe> pipe;
114 std::shared_ptr<std::promise<Type>> promise = std::make_shared<std::promise<Type>>();
115 std::shared_ptr<boost::asio::streambuf> buffer = std::make_shared<boost::asio::streambuf>();
118 async_out_future(std::future<Type> & fut)
120 fut = promise->get_future();
122 template <typename Executor>
123 inline void on_success(Executor&)
125 auto pipe = this->pipe;
126 auto buffer = this->buffer;
127 auto promise = this->promise;
128 std::move(*pipe).sink().close();
129 boost::asio::async_read(*pipe, *buffer,
130 [pipe, buffer, promise](const boost::system::error_code& ec, std::size_t)
132 if (ec && (ec.value() != ::boost::winapi::ERROR_BROKEN_PIPE_))
134 std::error_code e(ec.value(), std::system_category());
135 promise->set_exception(std::make_exception_ptr(process_error(e)));
139 std::istream is (buffer.get());
141 if (buffer->size() > 0)
143 arg.resize(buffer->size());
144 is.read(&*arg.begin(), buffer->size());
147 promise->set_value(std::move(arg));
152 this->pipe = nullptr;
153 this->buffer = nullptr;
154 this->promise = nullptr;
159 template<typename Executor>
160 void on_error(Executor &, const std::error_code &) const
162 std::move(*pipe).sink().close();
165 template <typename WindowsExecutor>
166 void on_setup(WindowsExecutor &exec)
169 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
171 apply_out_handles(exec, std::move(*pipe).sink().native_handle(),
172 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());