1 #ifndef SILICIUM_RUN_PROCESS_HPP
2 #define SILICIUM_RUN_PROCESS_HPP
6 #include <silicium/sink/multi_sink.hpp>
7 #include <boost/range/algorithm/transform.hpp>
16 destination = std::forward<T>(*source);
23 #define SILICIUM_HAS_RUN_PROCESS SILICIUM_HAS_EXCEPTIONS
25 #if SILICIUM_HAS_RUN_PROCESS
30 inline int run_process(process_parameters
const ¶meters)
32 async_process_parameters async_parameters;
35 throw std::invalid_argument(
"a process can only be started with an absolute path to the executable");
39 throw std::invalid_argument(
"a process can only be started with an absolute path as the working directory");
41 boost::range::transform(parameters.arguments, std::back_inserter(async_parameters.arguments), [](std::string
const &argument)
43 return to_os_string(argument);
45 auto input = make_pipe().move_value();
46 auto std_output = make_pipe().move_value();
47 auto std_error = make_pipe().move_value();
48 async_process process =
51 boost::asio::io_service io;
53 auto std_output_consumer = make_multi_sink<char, success>([¶meters]()
57 experimental::read_from_anonymous_pipe(io, std_output_consumer,
std::move(std_output.read));
59 auto std_error_consumer = make_multi_sink<char, success>([¶meters]()
63 experimental::read_from_anonymous_pipe(io, std_error_consumer,
std::move(std_error.read));
66 std_output.write.close();
67 std_error.write.close();
69 auto copy_input = std::async(std::launch::async, [&input, ¶meters]()
77 optional<char>
const c =
Si::get(*parameters.in);
83 if (written.is_error())
88 assert(written.get() == 1);
96 return process.wait_for_exit().get();
99 inline int run_process(
100 boost::filesystem::path executable,
101 std::vector<std::string> arguments,
102 boost::filesystem::path current_path,
103 Si::sink<char, success> &output)
109 parameters.
out = &output;
110 return Si::run_process(parameters);
std::remove_reference< T >::type && move(T &&ref)
Definition: move.hpp:10
std::vector< std::string > arguments
the values for the child's argv[1...]
Definition: process_parameters.hpp:18
bool extract(T &destination, optional< T > &&source)
Definition: run_process.hpp:12
boost::filesystem::path executable
Definition: process_parameters.hpp:15
sink< char, success > * out
stdout of the child process will be written to this sink. When nullptr, output is discarded...
Definition: process_parameters.hpp:24
Definition: absolute_path.hpp:19
Definition: optional.hpp:39
Definition: process_parameters.hpp:13
boost::filesystem::path current_path
must be an existing path, otherwise the child cannot launch properly
Definition: process_parameters.hpp:21
static SILICIUM_USE_RESULT optional< absolute_path > create(boost::filesystem::path const &maybe_absolute)
Definition: absolute_path.hpp:103
Value get(error_or< Value, Error > &&value)
Definition: error_or.hpp:479
SILICIUM_USE_RESULT error_or< std::size_t > write(native_file_descriptor file, memory_range data)
Definition: write.hpp:13
BOOST_CONSTEXPR auto make_iterator_range(Iterator1 &&begin, Iterator2 &&end) -> iterator_range< typename std::decay< Iterator1 >::type >
Definition: iterator_range.hpp:142
auto make_memory_range(Byte *begin, Byte *end) -> iterator_range< DestType * >
Definition: memory_range.hpp:27