mirror of
https://github.com/OpenMW/openmw.git
synced 2025-03-03 03:39:42 +00:00
Add generic StreamWithBuffer owning the underlying buffer
This commit is contained in:
parent
9d6d0c6ffb
commit
c94d8be7bf
3 changed files with 25 additions and 14 deletions
|
@ -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;
|
||||
|
||||
|
|
23
components/files/streamwithbuffer.hpp
Normal file
23
components/files/streamwithbuffer.hpp
Normal file
|
@ -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…
Reference in a new issue