forked from albertz/planet_wars-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.h
45 lines (35 loc) · 942 Bytes
/
process.h
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
/*
* process.h
* PlanetWars
*
* Created by Albert Zeyer on 15.09.10.
* code under GPLv3
*
*/
#ifndef __PW__PROCESS_H__
#define __PW__PROCESS_H__
#include <string>
#include <unistd.h>
struct Process {
std::string cmd;
bool running;
int forkInputFd, forkOutputFd;
std::string inbuffer, outbuffer;
pid_t forkId;
Process(const std::string& __cmd = "")
: cmd(__cmd), running(false),
forkInputFd(0), forkOutputFd(0), forkId(0) {}
~Process() { destroyAndWait(); }
operator bool() const { return running; }
void destroyAndWait();
void destroy();
void waitForExit();
void run();
Process& operator<<(const std::string& s) { inbuffer += s; return *this; }
Process& operator<<(void (*func)(Process&)) { (*func)(*this); return *this; }
bool readLine(std::string& s, size_t timeout = 0);
void flush();
};
inline void flush(Process& p) { p.flush(); }
inline void endl(Process& p) { p << "\n"; p.flush(); }
#endif