From c94d8be7bfc0e71f8ee81f8bd4cbf78e00e1bd56 Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 15 Apr 2022 01:55:14 +0200 Subject: [PATCH] Add generic StreamWithBuffer owning the underlying buffer --- components/files/constrainedfilestream.cpp | 6 ------ components/files/constrainedfilestream.hpp | 10 ++-------- components/files/streamwithbuffer.hpp | 23 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 components/files/streamwithbuffer.hpp diff --git a/components/files/constrainedfilestream.cpp b/components/files/constrainedfilestream.cpp index aafcacba74..22a85ce808 100644 --- a/components/files/constrainedfilestream.cpp +++ b/components/files/constrainedfilestream.cpp @@ -2,12 +2,6 @@ namespace Files { - ConstrainedFileStream::ConstrainedFileStream(std::unique_ptr buf) - : std::istream(buf.get()) - , mBuf(std::move(buf)) - { - } - IStreamPtr openConstrainedFileStream(const std::string& filename, std::size_t start, std::size_t length) { return std::make_shared(std::make_unique(filename, start, length)); diff --git a/components/files/constrainedfilestream.hpp b/components/files/constrainedfilestream.hpp index 4284705d17..3acb04d896 100644 --- a/components/files/constrainedfilestream.hpp +++ b/components/files/constrainedfilestream.hpp @@ -2,6 +2,7 @@ #define OPENMW_CONSTRAINEDFILESTREAM_H #include "constrainedfilestreambuf.hpp" +#include "streamwithbuffer.hpp" #include #include @@ -12,14 +13,7 @@ namespace Files { /// A file stream constrained to a specific region in the file, specified by the 'start' and 'length' parameters. -class ConstrainedFileStream final : public std::istream -{ -public: - explicit ConstrainedFileStream(std::unique_ptr buf); - -private: - std::unique_ptr mBuf; -}; +using ConstrainedFileStream = StreamWithBuffer; typedef std::shared_ptr IStreamPtr; diff --git a/components/files/streamwithbuffer.hpp b/components/files/streamwithbuffer.hpp new file mode 100644 index 0000000000..dfd1a04376 --- /dev/null +++ b/components/files/streamwithbuffer.hpp @@ -0,0 +1,23 @@ +#ifndef OPENMW_COMPONENTS_FILES_STREAMWITHBUFFER_H +#define OPENMW_COMPONENTS_FILES_STREAMWITHBUFFER_H + +#include +#include + +namespace Files +{ + template + class StreamWithBuffer final : public std::istream + { + public: + explicit StreamWithBuffer(std::unique_ptr&& buffer) + : std::istream(buffer.get()) + , mBuffer(std::move(buffer)) + {} + + private: + std::unique_ptr mBuffer; + }; +} + +#endif