cprover
piped_process.h
Go to the documentation of this file.
1 
5 #ifndef CPROVER_UTIL_PIPED_PROCESS_H
6 #define CPROVER_UTIL_PIPED_PROCESS_H
7 
8 #ifdef _WIN32
9 # include <memory>
10 // The below are forward declarations for Windows APIs
11 struct _PROCESS_INFORMATION; // NOLINT
12 typedef struct _PROCESS_INFORMATION PROCESS_INFORMATION; // NOLINT
13 typedef void *HANDLE; // NOLINT
14 #endif
15 
16 #include "nodiscard.h"
17 #include "optional.h"
18 #include <vector>
19 
20 #define PIPED_PROCESS_INFINITE_TIMEOUT \
21  optionalt<std::size_t> \
22  { \
23  }
24 
26 {
27 public:
29  enum class statet
30  {
31  RUNNING,
32  ERRORED
33  };
34 
36  enum class send_responset
37  {
38  SUCCEEDED,
39  FAILED,
40  ERRORED
41  };
42 
46  NODISCARD send_responset send(const std::string &message);
49  std::string receive();
53  std::string wait_receive();
54 
58 
65  bool can_receive(optionalt<std::size_t> wait_time);
66 
70  bool can_receive();
71 
76  // of can_receive(0)
77  void wait_receivable(int wait_time);
78 
82  explicit piped_processt(const std::vector<std::string> &commandvec);
83 
84  // Deleted due to declaring an explicit destructor and not wanting copy
85  // constructors to be implemented.
86  piped_processt(const piped_processt &) = delete;
89 
90 protected:
91 #ifdef _WIN32
92  // Process information handle for Windows
93  std::unique_ptr<PROCESS_INFORMATION> proc_info;
94  // Handles for communication with child process
95  HANDLE child_std_IN_Rd;
96  HANDLE child_std_IN_Wr;
97  HANDLE child_std_OUT_Rd;
98  HANDLE child_std_OUT_Wr;
99 #else
100  // Child process ID.
103  // The member fields below are so named from the perspective of the
104  // parent -> child process. So `pipe_input` is where we are feeding
105  // commands to the child process, and `pipe_output` is where we read
106  // the results of execution from.
107  int pipe_input[2];
108  int pipe_output[2];
109 #endif
111 };
112 
113 #endif // endifndef CPROVER_UTIL_PIPED_PROCESS_H
pid_t child_process_id
piped_processt & operator=(const piped_processt &)=delete
statet process_state
bool can_receive()
See if this process can receive data from the other process.
piped_processt(const std::vector< std::string > &commandvec)
Initiate a new subprocess with pipes supporting communication between the parent (this process) and t...
void wait_receivable(int wait_time)
Wait for the pipe to be ready, waiting specified time between checks.
piped_processt(const piped_processt &)=delete
std::string receive()
Read a string from the child process' output.
send_responset send(const std::string &message)
Send a string message (command) to the child process.
FILE * command_stream
statet
Enumeration to keep track of child process state.
Definition: piped_process.h:30
statet get_status()
Get child process status.
send_responset
Enumeration for send response.
Definition: piped_process.h:37
std::string wait_receive()
Wait until a string is available and read a string from the child process' output.
#define NODISCARD
Definition: nodiscard.h:22
nonstd::optional< T > optionalt
Definition: optional.h:35
unsigned int statet