subprocess 0.5.0
Modern subprocess library for c++
Loading...
Searching...
No Matches
pipe.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "basic_types.hpp"
4
5namespace subprocess {
6 /* You might be wondering why the C-like API. I played around with a more
7 C++ like API but it gets hairy quite fast trying to support all the
8 possible use-cases. My opinion is to simply roll a RAII class for
9 when you need it that is specific to your needs. 1, or a set of RAII
10 classes is too complex.
11 */
12 struct PipePair {
16 // No copy, move only
17 PipePair (const PipePair&)=delete;
18 PipePair& operator= (const PipePair&)=delete;
19 PipePair (PipePair&& other) { *this = std::move(other); }
21 /* we make it const as outside of code shouldn't modify these.
22 this might not be a good design as users may assume it's truly const.
23 disown, close*, and move semantics overwrite these values.
24 */
27
29 void disown() {
30 const_cast<PipeHandle&>(input) = const_cast<PipeHandle&>(output) = kBadPipeValue;
31 }
33 void disown_input() {
34 const_cast<PipeHandle&>(input) = kBadPipeValue;
35 }
38 const_cast<PipeHandle&>(output) = kBadPipeValue;
39 }
40
41 void close();
44 explicit operator bool() const noexcept {
45 return input != output;
46 }
47 };
48
51
57
79 PipePair pipe_create(bool inheritable = false);
80
87 void pipe_set_inheritable(PipeHandle handle, bool inheritable);
88
93 ssize_t pipe_read(PipeHandle, void* buffer, size_t size);
98 ssize_t pipe_write(PipeHandle, const void* buffer, size_t size);
99
109 bool pipe_set_blocking(PipeHandle, bool should_block);
110
115
124
138 double seconds
139 );
140
142 ssize_t pipe_read_some(PipeHandle, void* buffer, size_t size);
143
160 PipeHandle pipe_file(const char* filename, const char* mode);
161
162 #if 0
163 /* Obviously this is possible. The problem is the API around it, I really
164 want to stop calling pipe_close. To be fair in python's subprocess
165 library you also have to close things either manually or use `with`.
166 Maybe it's just not possible.
167 */
168 class UniquePipeHandle {
169 public:
170 UniquePipeHandle(){}
171 ~UniquePipeHandle(){
172 pipe_close(handle);
173 handle = kBadPipeValue;
174 }
175 explicit UniquePipeHandle(PipeHandle handle): handle(handle){}
176 UniquePipeHandle(UniquePipeHandle&& other) {
177 pipe_close(handle);
178 handle = other.handle;
179 other.handle = kBadPipeValue;
180 }
181 UniquePipeHandle& operator=(UniquePipeHandle&& other) {
182 pipe_close(handle);
183 handle = other.handle;
184 other.handle = kBadPipeValue;
185 return *this;
186 }
187 UniquePipeHandle(const UniquePipeHandle&)=delete;
188 UniquePipeHandle operator=(const UniquePipeHandle&)=delete;
189
190 PipeHandle get() { return handle; }
191 private:
193 };
194 #endif
195}
Definition basic_types.hpp:22
ssize_t pipe_peak_bytes(PipeHandle pipe)
ssize_t pipe_read(PipeHandle, void *buffer, size_t size)
PipeHandle pipe_file(const char *filename, const char *mode)
bool pipe_close(PipeHandle handle)
int PipeHandle
Definition basic_types.hpp:77
@ pipe
Redirects to a new handle created for you.
int pipe_wait_for_read(PipeHandle pipe, double seconds)
intptr_t ssize_t
Definition basic_types.hpp:24
bool pipe_set_blocking(PipeHandle, bool should_block)
const PipeHandle kBadPipeValue
Definition basic_types.hpp:84
ssize_t pipe_read_some(PipeHandle, void *buffer, size_t size)
PipePair pipe_create(bool inheritable=false)
void pipe_ignore_and_close(PipeHandle handle)
void pipe_set_inheritable(PipeHandle handle, bool inheritable)
ssize_t pipe_write(PipeHandle, const void *buffer, size_t size)
std::string pipe_read_all(PipeHandle handle)
Definition pipe.hpp:12
const PipeHandle output
Definition pipe.hpp:26
PipePair(PipePair &&other)
Definition pipe.hpp:19
PipePair()
Definition pipe.hpp:13
void disown()
Definition pipe.hpp:29
const PipeHandle input
Definition pipe.hpp:25
void disown_input()
Disowns the input end.
Definition pipe.hpp:33
PipePair(PipeHandle input, PipeHandle output)
Definition pipe.hpp:14
void disown_output()
Disowns the output end.
Definition pipe.hpp:37
PipePair & operator=(const PipePair &)=delete
~PipePair()
Definition pipe.hpp:15
PipePair(const PipePair &)=delete