diff options
Diffstat (limited to 'include/cru/common')
-rw-r--r-- | include/cru/common/io/AutoReadStream.h | 44 | ||||
-rw-r--r-- | include/cru/common/io/BufferStream.h | 33 |
2 files changed, 34 insertions, 43 deletions
diff --git a/include/cru/common/io/AutoReadStream.h b/include/cru/common/io/AutoReadStream.h index e252bdff..4ba4066f 100644 --- a/include/cru/common/io/AutoReadStream.h +++ b/include/cru/common/io/AutoReadStream.h @@ -1,6 +1,7 @@ #pragma once #include "../Buffer.h" +#include "BufferStream.h" #include "Stream.h" #include <condition_variable> @@ -11,24 +12,21 @@ namespace cru::io { struct AutoReadStreamOptions { /** - * @brief The size of a single buffer allocated each time new space is needed. - * Use default value if <= 0. - * - * When current buffer is full and there is no space for following data, a new - * buffer will be allocated and appended to the buffer list. Note if sum size - * of all buffers reaches the total_buffer_size, no more buffer will be - * allocated but wait. + * @brief Will be passed to BufferStreamOptions::block_size. */ - int buffer_block_size = 0; + Index block_size = 0; /** - * @brief Total size limit of saved data in buffer. Use default value if < 0. - * No limit if == 0. - * - * When the buffer is filled, it will block and wait for user to read to get - * free space of buffer to continue read. + * @brief Will be passed to BufferStreamOptions::total_size_limit. */ - int total_buffer_size = 0; + Index total_size_limit = 0; + + BufferStreamOptions GetBufferStreamOptions() const { + BufferStreamOptions options; + options.block_size = block_size; + options.total_size_limit = total_size_limit; + return options; + } }; /** @@ -36,9 +34,6 @@ struct AutoReadStreamOptions { * background thread. */ class CRU_BASE_API AutoReadStream : public Stream { - private: - class BufferBlock {}; - public: /** * @brief Wrap a stream and auto read it in background. @@ -56,15 +51,15 @@ class CRU_BASE_API AutoReadStream : public Stream { bool CanSeek() override; Index Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) override; - bool CanRead() = 0; - virtual Index Read(std::byte* buffer, Index offset, Index size) = 0; + bool CanRead() override; + Index Read(std::byte* buffer, Index offset, Index size) override; bool CanWrite() override; Index Write(const std::byte* buffer, Index offset, Index size) override; void Flush() override; - void Close() = 0; + void Close() override; private: void BackgroundThreadRun(); @@ -73,12 +68,9 @@ class CRU_BASE_API AutoReadStream : public Stream { Stream* stream_; bool auto_delete_; - int buffer_block_size_; - int total_buffer_size_; - - std::mutex buffer_mutex_; - std::condition_variable buffer_condition_variable_; - std::list<Buffer> buffer_list_; + Index size_per_read_; + std::unique_ptr<BufferStream> buffer_stream_; + std::mutex buffer_stream_mutex_; std::thread background_thread_; }; 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_; |