diff options
author | crupest <crupest@outlook.com> | 2024-06-24 00:06:25 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-10-04 19:55:33 +0800 |
commit | f51eb955e188858272230a990565931e7403f23b (patch) | |
tree | 04de484bfcd056b6eea56c13c42cce83315c448f /include/cru/common/io/AutoReadStream.h | |
parent | 1b30150ab79ff1338f209a8ddb54b3dc60cfb599 (diff) | |
download | cru-f51eb955e188858272230a990565931e7403f23b.tar.gz cru-f51eb955e188858272230a990565931e7403f23b.tar.bz2 cru-f51eb955e188858272230a990565931e7403f23b.zip |
HALF WORK: Stream refactor.
TODO: Complete refactor of BufferStream and AutoReadStream.
NEED TEST: BufferStream, AutoReadStream, SubProcess.
Diffstat (limited to 'include/cru/common/io/AutoReadStream.h')
-rw-r--r-- | include/cru/common/io/AutoReadStream.h | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/include/cru/common/io/AutoReadStream.h b/include/cru/common/io/AutoReadStream.h index b416d050..759d5026 100644 --- a/include/cru/common/io/AutoReadStream.h +++ b/include/cru/common/io/AutoReadStream.h @@ -1,10 +1,10 @@ #pragma once -#include "../SelfResolvable.h" #include "BufferStream.h" #include "Stream.h" #include <mutex> +#include <thread> namespace cru::io { struct AutoReadStreamOptions { @@ -30,8 +30,7 @@ struct AutoReadStreamOptions { * @brief A stream that wraps another stream and auto read it into a buffer in a * background thread. */ -class CRU_BASE_API AutoReadStream : public Stream, - public SelfResolvable<AutoReadStream> { +class CRU_BASE_API AutoReadStream : public Stream { public: /** * @brief Wrap a stream and auto read it in background. @@ -46,20 +45,18 @@ class CRU_BASE_API AutoReadStream : public Stream, ~AutoReadStream() override; public: - bool CanSeek() override; - Index Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) override; + CRU_STREAM_IMPLEMENT_CLOSE_BY_DO_CLOSE - bool CanRead() override; - Index Read(std::byte* buffer, Index offset, Index size) override; + void BeginToDrop(bool auto_delete = true); - bool CanWrite() override; - Index Write(const std::byte* buffer, Index offset, Index size) override; - - void Flush() override; - - void Close() override; + protected: + Index DoRead(std::byte* buffer, Index offset, Index size) override; + Index DoWrite(const std::byte* buffer, Index offset, Index size) override; + void DoFlush() override; private: + void DoClose(); + void BackgroundThreadRun(); private: @@ -69,5 +66,7 @@ class CRU_BASE_API AutoReadStream : public Stream, Index size_per_read_; std::unique_ptr<BufferStream> buffer_stream_; std::mutex buffer_stream_mutex_; + + std::thread background_thread_; }; } // namespace cru::io |