| 339 | }; |
| 340 | |
| 341 | class Writer |
| 342 | { |
| 343 | public: |
| 344 | // Returns false if the data could not be written because |
| 345 | // either end of the pipe was already closed. Note that an |
| 346 | // empty write has no effect. |
| 347 | bool write(std::string s); |
| 348 | |
| 349 | // Closing the write-end of the pipe will send end-of-file |
| 350 | // to the reader. Returns false if the write-end of the pipe |
| 351 | // was already closed or failed. |
| 352 | bool close(); |
| 353 | |
| 354 | // Closes the write-end of the pipe but sends a failure |
| 355 | // to the reader rather than end-of-file. Returns false |
| 356 | // if the write-end of the pipe was already closed or failed. |
| 357 | bool fail(const std::string& message); |
| 358 | |
| 359 | // Returns Nothing when the read-end of the pipe is closed |
| 360 | // before the write-end is closed, which means the reader |
| 361 | // was unable to continue reading! |
| 362 | Future<Nothing> readerClosed() const; |
| 363 | |
| 364 | // Comparison operators useful for checking connection equality. |
| 365 | bool operator==(const Writer& other) const { return data == other.data; } |
| 366 | bool operator!=(const Writer& other) const { return !(*this == other); } |
| 367 | private: |
| 368 | friend class Pipe; |
| 369 | |
| 370 | enum State |
| 371 | { |
| 372 | OPEN, |
| 373 | CLOSED, |
| 374 | FAILED, |
| 375 | }; |
| 376 | |
| 377 | explicit Writer(const std::shared_ptr<Data>& _data) |
| 378 | : data(_data) {} |
| 379 | |
| 380 | std::shared_ptr<Data> data; |
| 381 | }; |
| 382 | |
| 383 | Pipe() |
| 384 | : data(new Data()) {} |