Add generic StreamWithBuffer owning the underlying buffer

pull/3226/head
elsid 3 years ago
parent 9d6d0c6ffb
commit c94d8be7bf
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40

@ -2,12 +2,6 @@
namespace Files
{
ConstrainedFileStream::ConstrainedFileStream(std::unique_ptr<ConstrainedFileStreamBuf> 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<ConstrainedFileStream>(std::make_unique<ConstrainedFileStreamBuf>(filename, start, length));

@ -2,6 +2,7 @@
#define OPENMW_CONSTRAINEDFILESTREAM_H
#include "constrainedfilestreambuf.hpp"
#include "streamwithbuffer.hpp"
#include <istream>
#include <memory>
@ -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<ConstrainedFileStreamBuf> buf);
private:
std::unique_ptr<ConstrainedFileStreamBuf> mBuf;
};
using ConstrainedFileStream = StreamWithBuffer<ConstrainedFileStreamBuf>;
typedef std::shared_ptr<std::istream> IStreamPtr;

@ -0,0 +1,23 @@
#ifndef OPENMW_COMPONENTS_FILES_STREAMWITHBUFFER_H
#define OPENMW_COMPONENTS_FILES_STREAMWITHBUFFER_H
#include <istream>
#include <memory>
namespace Files
{
template <class Buffer>
class StreamWithBuffer final : public std::istream
{
public:
explicit StreamWithBuffer(std::unique_ptr<Buffer>&& buffer)
: std::istream(buffer.get())
, mBuffer(std::move(buffer))
{}
private:
std::unique_ptr<Buffer> mBuffer;
};
}
#endif
Loading…
Cancel
Save