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

#include "unique_fd.hh"

#include <cstddef>

class unique_pipe {
 public:
  unique_pipe();
  unique_pipe(unique_pipe&& fd);
  unique_pipe(unique_pipe const&) = delete;
  unique_pipe& operator=(unique_pipe const&) = delete;

  ~unique_pipe() = default;

  unique_pipe& operator=(unique_pipe&& fd);

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

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

  void reset();

  unique_fd release_reader();
  unique_fd release_writer();

 private:
  unique_fd fd_[2];
};

#endif  // UNIQUE_PIPE_HH