An interprocess or interthread pipe for sending tokens (one-byte values) * over. */
| 73 | * over. |
| 74 | */ |
| 75 | class TokenPipe |
| 76 | { |
| 77 | private: |
| 78 | int m_fds[2] = {-1, -1}; |
| 79 | |
| 80 | TokenPipe(int fds[2]) : m_fds{fds[0], fds[1]} {} |
| 81 | |
| 82 | public: |
| 83 | ~TokenPipe(); |
| 84 | |
| 85 | /** Create a new pipe. |
| 86 | * @returns The created TokenPipe, or an empty std::nullopt in case of error. |
| 87 | */ |
| 88 | static std::optional<TokenPipe> Make(); |
| 89 | |
| 90 | /** Take the read end of this pipe. This can only be called once, |
| 91 | * as the object will be moved out. |
| 92 | */ |
| 93 | TokenPipeEnd TakeReadEnd(); |
| 94 | |
| 95 | /** Take the write end of this pipe. This should only be called once, |
| 96 | * as the object will be moved out. |
| 97 | */ |
| 98 | TokenPipeEnd TakeWriteEnd(); |
| 99 | |
| 100 | /** Close and end of the pipe that hasn't been moved out. |
| 101 | */ |
| 102 | void Close(); |
| 103 | |
| 104 | // Move-only class. |
| 105 | TokenPipe(TokenPipe&& other) |
| 106 | { |
| 107 | for (int i = 0; i < 2; ++i) { |
| 108 | m_fds[i] = other.m_fds[i]; |
| 109 | other.m_fds[i] = -1; |
| 110 | } |
| 111 | } |
| 112 | TokenPipe& operator=(TokenPipe&& other) |
| 113 | { |
| 114 | Close(); |
| 115 | for (int i = 0; i < 2; ++i) { |
| 116 | m_fds[i] = other.m_fds[i]; |
| 117 | other.m_fds[i] = -1; |
| 118 | } |
| 119 | return *this; |
| 120 | } |
| 121 | TokenPipe(const TokenPipe&) = delete; |
| 122 | TokenPipe& operator=(const TokenPipe&) = delete; |
| 123 | }; |
| 124 | |
| 125 | #endif // WIN32 |
| 126 |