]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/process/detail/windows/async_out.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / process / detail / windows / async_out.hpp
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_ASYNC_OUT_HPP
11 #define BOOST_PROCESS_WINDOWS_INITIALIZERS_ASYNC_OUT_HPP
12
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>
20
21 #include <istream>
22 #include <memory>
23 #include <exception>
24 #include <future>
25
26
27 namespace boost { namespace process { namespace detail { namespace windows {
28
29
30 template <typename Executor>
31 inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 1>, std::integral_constant<int, -1>)
32 {
33 boost::winapi::SetHandleInformation(handle,
34 boost::winapi::HANDLE_FLAG_INHERIT_,
35 boost::winapi::HANDLE_FLAG_INHERIT_);
36
37 e.startup_info.hStdOutput = handle;
38 e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
39 e.inherit_handles = true;
40 }
41
42 template <typename Executor>
43 inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 2>, std::integral_constant<int, -1>)
44 {
45 boost::winapi::SetHandleInformation(handle,
46 boost::winapi::HANDLE_FLAG_INHERIT_,
47 boost::winapi::HANDLE_FLAG_INHERIT_);
48
49
50 e.startup_info.hStdError = handle;
51 e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
52 e.inherit_handles = true;
53 }
54
55 template <typename Executor>
56 inline void apply_out_handles(Executor &e, void* handle, std::integral_constant<int, 1>, std::integral_constant<int, 2>)
57 {
58 boost::winapi::SetHandleInformation(handle,
59 boost::winapi::HANDLE_FLAG_INHERIT_,
60 boost::winapi::HANDLE_FLAG_INHERIT_);
61
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;
66 }
67
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
71 {
72 Buffer & buf;
73
74 std::shared_ptr<boost::process::async_pipe> pipe;
75
76
77 async_out_buffer(Buffer & buf) : buf(buf)
78 {
79 }
80 template <typename Executor>
81 inline void on_success(Executor&)
82 {
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();
87 this->pipe = nullptr;
88
89 }
90
91 template<typename Executor>
92 void on_error(Executor &, const std::error_code &) const
93 {
94 std::move(*pipe).sink().close();
95 }
96
97 template <typename WindowsExecutor>
98 void on_setup(WindowsExecutor &exec)
99 {
100 if (!pipe)
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>());
104 }
105 };
106
107
108
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
112 {
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>();
116
117
118 async_out_future(std::future<Type> & fut)
119 {
120 fut = promise->get_future();
121 }
122 template <typename Executor>
123 inline void on_success(Executor&)
124 {
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)
131 {
132 if (ec && (ec.value() != ::boost::winapi::ERROR_BROKEN_PIPE_))
133 {
134 std::error_code e(ec.value(), std::system_category());
135 promise->set_exception(std::make_exception_ptr(process_error(e)));
136 }
137 else
138 {
139 std::istream is (buffer.get());
140 Type arg;
141 if (buffer->size() > 0)
142 {
143 arg.resize(buffer->size());
144 is.read(&*arg.begin(), buffer->size());
145 }
146
147 promise->set_value(std::move(arg));
148
149
150 }
151 });
152 this->pipe = nullptr;
153 this->buffer = nullptr;
154 this->promise = nullptr;
155
156
157 }
158
159 template<typename Executor>
160 void on_error(Executor &, const std::error_code &) const
161 {
162 std::move(*pipe).sink().close();
163 }
164
165 template <typename WindowsExecutor>
166 void on_setup(WindowsExecutor &exec)
167 {
168 if (!pipe)
169 pipe = std::make_shared<boost::process::async_pipe>(get_io_context(exec.seq));
170
171 apply_out_handles(exec, std::move(*pipe).sink().native_handle(),
172 std::integral_constant<int, p1>(), std::integral_constant<int, p2>());
173 }
174 };
175
176
177 }}}}
178
179 #endif