From 29bef137f5c33a599f2629fecc0b756611dd126b Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 25 Dec 2023 22:16:13 +0800 Subject: Add proxy stream (not completed). --- include/cru/common/Base.h | 2 +- include/cru/common/io/ProxyStream.h | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 include/cru/common/io/ProxyStream.h (limited to 'include/cru') diff --git a/include/cru/common/Base.h b/include/cru/common/Base.h index 899cfc13..34faf779 100644 --- a/include/cru/common/Base.h +++ b/include/cru/common/Base.h @@ -1,5 +1,5 @@ #pragma once -#include "PreConfig.h" +#include "PreConfig.h" // IWYU pragma: keep #ifdef CRU_PLATFORM_WINDOWS #ifdef CRU_BASE_EXPORT_API diff --git a/include/cru/common/io/ProxyStream.h b/include/cru/common/io/ProxyStream.h new file mode 100644 index 00000000..688f714a --- /dev/null +++ b/include/cru/common/io/ProxyStream.h @@ -0,0 +1,51 @@ +#pragma once + +#include "Stream.h" + +#include + +namespace cru::io { +struct ProxyStreamHandlers { + std::function seek; + std::function read; + std::function write; + std::function flush; + + /** + * @brief This method will be only called once when `Close` is called or the + * stream is destructed. + */ + std::function close; +}; + +class ProxyStream : public Stream { + public: + explicit ProxyStream(ProxyStreamHandlers handlers); + + CRU_DELETE_COPY(ProxyStream) + CRU_DELETE_MOVE(ProxyStream) + + ~ProxyStream() override; + + public: + bool CanSeek() override; + Index Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) override; + + 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() override; + + private: + void DoClose(); + + private: + bool closed_; + ProxyStreamHandlers handlers_; +}; +} // namespace cru::io -- cgit v1.2.3