5 #ifndef CHI_SUBPROCESS_HPP 6 #define CHI_SUBPROCESS_HPP 10 #include <boost/filesystem/operations.hpp> 11 #include <boost/filesystem/path.hpp> 12 #include <boost/optional.hpp> 54 using pipeHandler = std::function<void(const char* data, size_t size)>;
58 Subprocess(
const boost::filesystem::path& pathToExecutable);
74 template <
typename ForwardIterator>
75 void setArguments(
const ForwardIterator& begin,
const ForwardIterator& end) {
78 std::is_convertible<
typename std::iterator_traits<ForwardIterator>::value_type,
80 "Cannot run setArguments with iterators not convertable to std::string");
82 assert(!
started() &&
"Cannot set arguments after start() has been called");
84 mArguments = std::vector<std::string>(begin, end);
90 template <
typename Range>
92 setArguments(std::forward<Range>(range).begin(), std::forward<Range>(range).end());
104 const std::vector<std::string>&
arguments()
const {
return mArguments; }
113 "Cannot attach a differnt function to stdout after start() has been called");
114 mStdOutHandler = stdOutHandler;
124 "Cannot attach a differnt function to stderr after start() has been called");
125 mStdErrHandler = stdErrHandler;
136 attachToStdOut([&str](
const char* data,
size_t size) { str.append(data, size); });
147 attachToStdErr([&str](
const char* data,
size_t size) { str.append(data, size); });
212 struct Implementation;
213 std::unique_ptr<Implementation> mPimpl;
215 std::vector<std::string> mArguments;
220 boost::filesystem::path mExePath;
222 boost::filesystem::path mWorkingDir = boost::filesystem::current_path();
224 bool mStarted =
false;
225 bool mStdInClosed =
false;
227 boost::optional<int> mExitCode;
234 #endif // CHI_SUBPROCESS_HPP void attachStringToStdOut(std::string &str)
Attach a string to stdout.
~Subprocess()
Wait for the process to exit and close all open handles.
void wait()
Wait for the process to complete.
void attachToStdOut(pipeHandler stdOutHandler)
Attach a function handler to the child stdout.
int exitCode()
Wait and gets the exit code.
void attachStringToStdErr(std::string &str)
Attach a string to stderr.
const std::vector< std::string > & arguments() const
Get the currently set arguments for the program.
void attachToStdErr(pipeHandler stdErrHandler)
Attach a function handler to the child stderr.
Result closeStdIn()
Close the STDIN stream (send an EOF)
Subprocess(const boost::filesystem::path &pathToExecutable)
Construct a Subprocess with a path to an executable.
bool isStdInClosed() const
Checks if the stdin is closed.
Result pushToStdIn(const char *data, size_t size)
Pushes data to the stdin stream of the child process.
Provides an platform-independent abstraction for creating subprocesses.
bool running()
Check if the process is still running.
Result start()
Start the process.
void setArguments(const ForwardIterator &begin, const ForwardIterator &end)
Set the arguments for the program with a begin and end iterator.
void kill()
Kill the child process On POSIX, this sends SIGINT.
void setArguments(Range &&range)
Set the arguments for the program with a range.
void setWorkingDirectory(boost::filesystem::path newWd)
Set the working directory of the process.
bool started() const
Check if the child has started (start() has been called)
The namespace where chigraph lives.
void setArguments(std::initializer_list< const char *> init)
Set the arguments for the program with an initializer_list.
The result object, used for identifiying errors with good diagnostics.
std::function< void(const char *data, size_t size)> pipeHandler
The function type for recieving data from pipes.