classByteStream { protected: uint64_t capacity_; // Please add any additional state to the ByteStream here, and not to the Writer and Reader interfaces. bool is_closed_ { false }; bool has_error_ { false };
// Helper functions (provided) to access the ByteStream's Reader and Writer interfaces Reader& reader(); const Reader& reader()const; Writer& writer(); const Writer& writer()const; };
classWriter :public ByteStream { public: voidpush( std::string data ); // Push data to stream, but only as much as available capacity allows.
voidclose(); // Signal that the stream has reached its ending. Nothing more will be written. voidset_error(); // Signal that the stream suffered an error.
boolis_closed()const; // Has the stream been closed? uint64_tavailable_capacity()const; // How many bytes can be pushed to the stream right now? uint64_tbytes_pushed()const; // Total number of bytes cumulatively pushed to the stream };
classReader :public ByteStream { public: std::string_view peek()const; // Peek at the next bytes in the buffer voidpop( uint64_t len ); // Remove `len` bytes from the buffer
boolis_finished()const; // Is the stream finished (closed and fully popped)? boolhas_error()const; // Has the stream had an error?
uint64_tbytes_buffered()const; // Number of bytes currently buffered (pushed and not popped) uint64_tbytes_popped()const; // Total number of bytes cumulatively popped from stream };
/* * read: A (provided) helper function thats peeks and pops up to `len` bytes * from a ByteStream Reader into a string; */ voidread( Reader& reader, uint64_t len, std::string& out );