diff options
author | crupest <crupest@outlook.com> | 2024-05-12 00:19:16 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-05-18 22:56:24 +0800 |
commit | c3cf29afa2b2dd3f2e972a25f35ef5840ad1e2f5 (patch) | |
tree | 6260a4143f254e7c974f66453be0be61f9b935f4 /include/cru/common/io/BufferStream.h | |
parent | b05c7d76dcfef6d882bc01fea4663a4c2fcdcdf2 (diff) | |
download | cru-c3cf29afa2b2dd3f2e972a25f35ef5840ad1e2f5.tar.gz cru-c3cf29afa2b2dd3f2e972a25f35ef5840ad1e2f5.tar.bz2 cru-c3cf29afa2b2dd3f2e972a25f35ef5840ad1e2f5.zip |
feat: completed AutoReadStream.
NEED TEST: BufferStream, AutoReadStream.
Diffstat (limited to 'include/cru/common/io/BufferStream.h')
-rw-r--r-- | include/cru/common/io/BufferStream.h | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/include/cru/common/io/BufferStream.h b/include/cru/common/io/BufferStream.h index b3a5e9d1..d5d165cb 100644 --- a/include/cru/common/io/BufferStream.h +++ b/include/cru/common/io/BufferStream.h @@ -17,6 +17,12 @@ class WriteAfterEofException : public Exception { struct BufferStreamOptions { /** + * Actually I have no ideas about the best value for this. May change it later + * when I get some ideas. + */ + constexpr static Index kDefaultBlockSize = 1024; + + /** * @brief The size of a single buffer allocated each time new space is needed. * Use default value if <= 0. * @@ -28,14 +34,19 @@ struct BufferStreamOptions { Index block_size = 0; /** - * @brief Total size limit of saved data in buffer. Use default value if < 0. - * No limit if == 0. + * @brief Total size limit of saved data in buffer. No limit if <= 0. * * 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; + + Index GetBlockSizeOrDefault() const { + return block_size <= 0 ? kDefaultBlockSize : block_size; + } + + Index GetMaxBlockCount() const { return total_size_limit / block_size; } }; /** @@ -45,19 +56,6 @@ struct BufferStreamOptions { */ class BufferStream : public Stream { public: - /** - * Actually I have no ideas about the best value for this. May change it later - * when I get some ideas. - */ - constexpr static Index kDefaultBlockSize = 1024; - - /** - * 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 * 1024; - - public: BufferStream(const BufferStreamOptions& options); ~BufferStream() override; @@ -67,16 +65,17 @@ class BufferStream : public Stream { bool CanRead() override; Index Read(std::byte* buffer, Index offset, Index size) override; + using Stream::Read; bool CanWrite() override; Index Write(const std::byte* buffer, Index offset, Index size) override; + using Stream::Write; void SetEof(); private: Index block_size_; - Index total_size_limit_; - Index block_count_limit_; + Index max_block_count_; std::list<Buffer> buffer_list_; bool eof_; |