summaryrefslogtreecommitdiff
path: root/src/io.hh
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2017-09-26 20:09:31 +0200
committerJoel Klinghed <the_jk@yahoo.com>2017-09-26 20:09:31 +0200
commitc85b624d28564a6f785b25000e2b7825592a919d (patch)
tree647b756c824b470b35f1371eb869e9534ed6c1bb /src/io.hh
Initial commit
Diffstat (limited to 'src/io.hh')
-rw-r--r--src/io.hh87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/io.hh b/src/io.hh
new file mode 100644
index 0000000..abd91fa
--- /dev/null
+++ b/src/io.hh
@@ -0,0 +1,87 @@
+#ifndef IO_HH
+#define IO_HH
+
+#include <stddef.h>
+
+namespace io {
+
+class pipe {
+public:
+ pipe();
+ pipe(pipe&& pipe);
+ ~pipe();
+
+ pipe& operator=(pipe&& pipe) {
+ reset();
+ swap(pipe);
+ return *this;
+ }
+
+ bool open();
+ explicit operator bool() const {
+ return read() != -1;
+ }
+
+ void reset();
+
+ void swap(pipe& pipe);
+
+ int read() const {
+ return fd_[0];
+ }
+ int write() const {
+ return fd_[1];
+ }
+
+private:
+ pipe(pipe const&) = delete;
+ pipe& operator=(pipe const&) = delete;
+
+ int fd_[2];
+};
+
+class unique_fd {
+public:
+ unique_fd();
+ explicit unique_fd(int fd);
+ unique_fd(unique_fd&& fd);
+ ~unique_fd();
+
+ unique_fd& operator=(unique_fd&& fd) {
+ reset();
+ swap(fd);
+ return *this;
+ }
+
+ int get() const {
+ return fd_;
+ }
+
+ explicit operator bool() const {
+ return get() != -1;
+ }
+
+ void reset();
+ void reset(int fd);
+
+ void swap(unique_fd& fd);
+
+ int release();
+
+private:
+ unique_fd(unique_fd const& fd) = delete;
+ unique_fd& operator=(unique_fd const& fd) = delete;
+
+ int fd_;
+};
+
+bool read(pipe const& pipe, void* dst, size_t max, size_t* got = nullptr);
+bool write(pipe const& pipe, void const* src, size_t size,
+ size_t* wrote = nullptr);
+bool read(unique_fd const& fd, void* dst, size_t max, size_t* got = nullptr);
+bool write(unique_fd const& fd, void const* src, size_t size,
+ size_t* wrote = nullptr);
+
+} // namespace
+
+#endif // IO_HH