aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-12-25 22:16:13 +0800
committercrupest <crupest@outlook.com>2023-12-25 22:16:13 +0800
commit29bef137f5c33a599f2629fecc0b756611dd126b (patch)
tree486efa5ce66dc4b71949c7f845c0b50c4ea2ca04
parent8fa5b665ddb5a6b1d609b81b66b57dbce7473bc6 (diff)
downloadcru-29bef137f5c33a599f2629fecc0b756611dd126b.tar.gz
cru-29bef137f5c33a599f2629fecc0b756611dd126b.tar.bz2
cru-29bef137f5c33a599f2629fecc0b756611dd126b.zip
Add proxy stream (not completed).
-rw-r--r--include/cru/common/Base.h2
-rw-r--r--include/cru/common/io/ProxyStream.h51
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/io/ProxyStream.cpp18
4 files changed, 71 insertions, 1 deletions
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 <functional>
+
+namespace cru::io {
+struct ProxyStreamHandlers {
+ std::function<Index(Index offset, Stream::SeekOrigin origin)> seek;
+ std::function<Index(std::byte* buffer, Index offset, Index size)> read;
+ std::function<Index(const std::byte* buffer, Index offset, Index size)> write;
+ std::function<void()> flush;
+
+ /**
+ * @brief This method will be only called once when `Close` is called or the
+ * stream is destructed.
+ */
+ std::function<void()> 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
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 714a4636..45688deb 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -9,6 +9,7 @@ add_library(CruBase
SubProcess.cpp
io/CFileStream.cpp
io/Stream.cpp
+ io/ProxyStream.cpp
io/Resource.cpp
io/MemoryStream.cpp
log/Logger.cpp
diff --git a/src/common/io/ProxyStream.cpp b/src/common/io/ProxyStream.cpp
new file mode 100644
index 00000000..9fd6be76
--- /dev/null
+++ b/src/common/io/ProxyStream.cpp
@@ -0,0 +1,18 @@
+#include "cru/common/io/ProxyStream.h"
+#include "cru/common/io/Stream.h"
+
+namespace cru::io {
+ProxyStream::ProxyStream(ProxyStreamHandlers handlers)
+ : closed_(false), handlers_(std::move(handlers)) {}
+
+ProxyStream::~ProxyStream() { DoClose(); }
+
+void ProxyStream::DoClose() {
+ if (!closed_) {
+ if (handlers_.close) {
+ handlers_.close();
+ }
+ closed_ = true;
+ }
+}
+} // namespace cru::io