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 /src/common/io/ProxyStream.cpp | |
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 'src/common/io/ProxyStream.cpp')
-rw-r--r-- | src/common/io/ProxyStream.cpp | 49 |
1 files changed, 11 insertions, 38 deletions
diff --git a/src/common/io/ProxyStream.cpp b/src/common/io/ProxyStream.cpp index df8e7dcc..c2e64056 100644 --- a/src/common/io/ProxyStream.cpp +++ b/src/common/io/ProxyStream.cpp @@ -3,62 +3,35 @@ namespace cru::io { ProxyStream::ProxyStream(ProxyStreamHandlers handlers) - : closed_(false), handlers_(std::move(handlers)) {} + : Stream(static_cast<bool>(handlers.seek), static_cast<bool>(handlers.read), + static_cast<bool>(handlers.write)), + handlers_(std::move(handlers)) {} ProxyStream::~ProxyStream() { DoClose(); } -bool ProxyStream::CanSeek() { - CheckClosed(); - return static_cast<bool>(handlers_.seek); -} - -Index ProxyStream::Seek(Index offset, SeekOrigin origin) { - CheckClosed(); - StreamOperationNotSupportedException::CheckSeek(CanSeek()); +Index ProxyStream::DoSeek(Index offset, SeekOrigin origin) { return handlers_.seek(offset, origin); } -bool ProxyStream::CanRead() { - CheckClosed(); - return static_cast<bool>(handlers_.read); -} - -Index ProxyStream::Read(std::byte* buffer, Index offset, Index size) { - CheckClosed(); - StreamOperationNotSupportedException::CheckRead(CanRead()); +Index ProxyStream::DoRead(std::byte* buffer, Index offset, Index size) { return handlers_.read(buffer, offset, size); } -bool ProxyStream::CanWrite() { - CheckClosed(); - return static_cast<bool>(handlers_.write); -} - -Index ProxyStream::Write(const std::byte* buffer, Index offset, Index size) { - CheckClosed(); - StreamOperationNotSupportedException::CheckWrite(CanWrite()); +Index ProxyStream::DoWrite(const std::byte* buffer, Index offset, Index size) { return handlers_.write(buffer, offset, size); } -void ProxyStream::Flush() { - CheckClosed(); +void ProxyStream::DoFlush() { if (handlers_.flush) { handlers_.flush(); } } -void ProxyStream::Close() { DoClose(); } - -void ProxyStream::CheckClosed() { - StreamAlreadyClosedException::Check(closed_); -} - void ProxyStream::DoClose() { - if (!closed_) { - if (handlers_.close) { - handlers_.close(); - } - closed_ = true; + CRU_STREAM_BEGIN_CLOSE + if (handlers_.close) { + handlers_.close(); } + handlers_ = {}; } } // namespace cru::io |