diff options
author | crupest <crupest@outlook.com> | 2024-05-12 00:19:16 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-05-17 23:55:37 +0800 |
commit | b05c7d76dcfef6d882bc01fea4663a4c2fcdcdf2 (patch) | |
tree | 7a52140660b2c9d177513c3dadb4a689a5c45b07 /include/cru/common/io/BufferStream.h | |
parent | d861d799d2f614a6b58a8f0c0bc6a8911ad0dcfd (diff) | |
download | cru-b05c7d76dcfef6d882bc01fea4663a4c2fcdcdf2.tar.gz cru-b05c7d76dcfef6d882bc01fea4663a4c2fcdcdf2.tar.bz2 cru-b05c7d76dcfef6d882bc01fea4663a4c2fcdcdf2.zip |
feat: completed BufferStream.
NEED TEST: BufferStream.
Diffstat (limited to 'include/cru/common/io/BufferStream.h')
-rw-r--r-- | include/cru/common/io/BufferStream.h | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/include/cru/common/io/BufferStream.h b/include/cru/common/io/BufferStream.h index a95b3487..b3a5e9d1 100644 --- a/include/cru/common/io/BufferStream.h +++ b/include/cru/common/io/BufferStream.h @@ -31,13 +31,18 @@ struct BufferStreamOptions { * @brief Total size limit of saved data in buffer. Use default value if < 0. * No limit if == 0. * - * The size will be ceil(total_size_limit / block_size). When the buffer is + * The size will be floor(total_size_limit / block_size). When the buffer is * filled, it will block and wait for user to read to get free space of buffer * to continue read. */ Index total_size_limit = 0; }; +/** + * @brief SPSC (Single Producer Single Consumer) buffer stream. + * + * If used by multiple producer or multiple consumer, the behavior is undefined. + */ class BufferStream : public Stream { public: /** @@ -50,7 +55,7 @@ class BufferStream : public Stream { * Actually I have no ideas about the best value for this. May change it later * when I get some ideas. */ - constexpr static Index kDefaultTotalSizeLimit = 1024; + constexpr static Index kDefaultTotalSizeLimit = 1024 * 1024; public: BufferStream(const BufferStreamOptions& options); @@ -63,19 +68,12 @@ class BufferStream : public Stream { bool CanRead() override; Index Read(std::byte* buffer, Index offset, Index size) override; - bool CanWrite() = 0; - Index Write(const std::byte* buffer, Index offset, Index size) = 0; - - virtual void Flush(); - - virtual void Close(); + bool CanWrite() override; + Index Write(const std::byte* buffer, Index offset, Index size) override; void SetEof(); private: - bool CheckClosed(); - - private: Index block_size_; Index total_size_limit_; Index block_count_limit_; |