summaryrefslogtreecommitdiff
path: root/src/unique_pipe.hh
blob: 7020cdf62cb10f173d48ee7faed241fc1ae295b5 (plain)
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
#ifndef UNIQUE_PIPE_HH
#define UNIQUE_PIPE_HH

#include <cstddef>

#include "unique_fd.hh"

class unique_pipe {
public:
  explicit unique_pipe(bool non_blocking = false)
    : unique_pipe(non_blocking, non_blocking) {}
  unique_pipe(bool non_blocking_reader,
              bool non_blocking_writer);
  unique_pipe(unique_pipe&& fd) noexcept;
  unique_pipe(std::nullptr_t) noexcept
    : fd_{nullptr, nullptr} {}

  ~unique_pipe() = default;

  unique_pipe& operator=(unique_pipe&& fd) noexcept {
    fd_[0].reset(fd.fd_[0].release());
    fd_[1].reset(fd.fd_[1].release());
    return *this;
  }

  unique_pipe& operator=(std::nullptr_t) noexcept {
    reset();
    return *this;
  }

  int reader() const noexcept { return fd_[0].get(); }
  int writer() const noexcept { return fd_[1].get(); }

  explicit operator bool() const noexcept {
    return fd_[0] || fd_[1];
  }

  void reset();

  unique_fd release_reader();
  unique_fd release_writer();

private:
  unique_pipe(unique_pipe const&) = delete;
  unique_pipe& operator=(unique_pipe const&) = delete;

  unique_fd fd_[2];
};


#endif  // UNIQUE_PIPE_HH