aboutsummaryrefslogtreecommitdiff
path: root/include/cru/base/io/ProxyStream.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/base/io/ProxyStream.h')
-rw-r--r--include/cru/base/io/ProxyStream.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/include/cru/base/io/ProxyStream.h b/include/cru/base/io/ProxyStream.h
new file mode 100644
index 00000000..42ec9dfd
--- /dev/null
+++ b/include/cru/base/io/ProxyStream.h
@@ -0,0 +1,42 @@
+#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);
+
+ ~ProxyStream() override;
+
+ public:
+ CRU_STREAM_IMPLEMENT_CLOSE_BY_DO_CLOSE
+
+ protected:
+ Index DoSeek(Index offset, SeekOrigin origin) override;
+ 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();
+
+ private:
+ ProxyStreamHandlers handlers_;
+};
+} // namespace cru::io