-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchildprocess.hpp
64 lines (55 loc) · 1.87 KB
/
childprocess.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @brief Child Process Manager header file
* @version 1.0.0
* @author Wolfram Rösler
* @date 2016-10-20
* @copyright MIT license
*/
#pragma once
#include <future>
#include <functional>
#include <string>
#include <vector>
#include <sys/types.h>
/**
* Child process manager class.
*
* This class is used to run an executable in a child process, and have
* it terminated and waited for in the dtor. It encapsulates the Unix
* fork/exec/kill/wait procedures and provides a more C++-like API for
* specifying command line parameters.
*/
class ChildProcess {
public:
// I/O redirection options
enum Flags {
IN = 1<<0, ///< Write into standard input
OUT = 1<<1, ///< Read from standard output
ERR = 1<<2 ///< Read from standard error output
};
// Ctor/dtor
explicit ChildProcess(
const std::string& exe,
std::vector<std::string> const& args={},
int flags=0,
std::function<void()> init=[](){}
);
ChildProcess(ChildProcess &&) noexcept;
~ChildProcess();
// No copying
ChildProcess(const ChildProcess&) = delete;
void operator=(const ChildProcess&) = delete;
void operator=(const ChildProcess&&) = delete;
// Wait for process to terminate
int join();
// Piping
std::future<void> make_stdin(std::function<void(std::ostream&)>);
std::future<void> get_stdout(std::function<void(std::istream&)>);
std::future<void> get_stderr(std::function<void(std::istream&)>);
private:
pid_t pid_ = 0; // PID of the process we started (0=none)
int pipein_[2] = { -1, -1 }; // stdin pipe file descriptors
int pipeout_[2] = { -1, -1 }; // stdout pipe file descriptors
int pipeerr_[2] = { -1, -1 }; // stderr pipe file descriptors
int pipefd(Flags which) const;
};