Split input and output streams

This commit is contained in:
topjohnwu
2023-05-19 04:19:43 -07:00
parent 582cad1b8d
commit 57bd450798
6 changed files with 104 additions and 124 deletions
+44 -40
View File
@@ -6,50 +6,39 @@
#include "../files.hpp"
class stream {
public:
virtual ssize_t read(void *buf, size_t len);
virtual ssize_t readFully(void *buf, size_t len);
virtual ssize_t readv(const iovec *iov, int iovcnt);
virtual bool write(const void *buf, size_t len);
struct out_stream {
virtual bool write(const void *buf, size_t len) = 0;
virtual ssize_t writev(const iovec *iov, int iovcnt);
virtual off_t seek(off_t off, int whence);
virtual ~stream() = default;
virtual ~out_stream() = default;
};
using stream_ptr = std::unique_ptr<stream>;
using out_strm_ptr = std::unique_ptr<out_stream>;
// Delegates all operations to base stream
class filter_stream : public stream {
class filter_out_stream : public out_stream {
public:
filter_stream(stream_ptr &&base) : base(std::move(base)) {}
filter_out_stream(out_strm_ptr &&base) : base(std::move(base)) {}
ssize_t read(void *buf, size_t len) override;
bool write(const void *buf, size_t len) override;
virtual bool write(const void *buf, size_t len, bool final);
// Seeking while filtering does not make sense
off_t seek(off_t off, int whence) final { return stream::seek(off, whence); }
protected:
stream_ptr base;
out_strm_ptr base;
};
using filter_strm_ptr = std::unique_ptr<filter_stream>;
using filter_strm_ptr = std::unique_ptr<filter_out_stream>;
// Buffered output stream, writing in chunks
class chunk_out_stream : public filter_stream {
class chunk_out_stream : public filter_out_stream {
public:
chunk_out_stream(stream_ptr &&base, size_t buf_sz, size_t chunk_sz)
: filter_stream(std::move(base)), chunk_sz(chunk_sz), buf_sz(buf_sz) {}
chunk_out_stream(out_strm_ptr &&base, size_t buf_sz, size_t chunk_sz)
: filter_out_stream(std::move(base)), chunk_sz(chunk_sz), buf_sz(buf_sz) {}
chunk_out_stream(stream_ptr &&base, size_t buf_sz = 4096)
chunk_out_stream(out_strm_ptr &&base, size_t buf_sz = 4096)
: chunk_out_stream(std::move(base), buf_sz, buf_sz) {}
~chunk_out_stream() override { delete[] _buf; }
// Reading does not make sense
ssize_t read(void *buf, size_t len) final { return stream::read(buf, len); }
bool write(const void *buf, size_t len) final;
bool write(const void *buf, size_t len, bool final) final;
@@ -66,12 +55,27 @@ private:
uint8_t *_buf = nullptr;
};
// Byte stream that dynamically allocates memory
class byte_stream : public stream {
struct in_stream {
virtual ssize_t read(void *buf, size_t len) = 0;
virtual ssize_t readFully(void *buf, size_t len);
virtual ssize_t readv(const iovec *iov, int iovcnt);
virtual ~in_stream() = default;
};
// A channel is something that is writable, readable, and seekable
struct channel : public in_stream, public out_stream {
virtual off_t seek(off_t off, int whence) = 0;
virtual ~channel() = default;
};
using channel_ptr = std::unique_ptr<channel>;
// Byte channel that dynamically allocates memory
class byte_channel : public channel {
public:
byte_stream(uint8_t *&buf, size_t &len);
byte_channel(uint8_t *&buf, size_t &len);
template <class Byte>
byte_stream(Byte *&buf, size_t &len) : byte_stream(reinterpret_cast<uint8_t *&>(buf), len) {}
byte_channel(Byte *&buf, size_t &len) : byte_channel(reinterpret_cast<uint8_t *&>(buf), len) {}
ssize_t read(void *buf, size_t len) override;
bool write(const void *buf, size_t len) override;
@@ -86,17 +90,17 @@ private:
void resize(size_t new_pos, bool zero = false);
};
class file_stream : public stream {
class file_channel : public channel {
public:
bool write(const void *buf, size_t len) final;
protected:
virtual ssize_t do_write(const void *buf, size_t len) = 0;
};
// File stream but does not close the file descriptor at any time
class fd_stream : public file_stream {
// File channel but does not close the file descriptor at any time
class fd_channel : public file_channel {
public:
fd_stream(int fd) : fd(fd) {}
fd_channel(int fd) : fd(fd) {}
ssize_t read(void *buf, size_t len) override;
ssize_t readv(const iovec *iov, int iovcnt) override;
ssize_t writev(const iovec *iov, int iovcnt) override;
@@ -108,14 +112,14 @@ private:
};
/* ****************************************
* Bridge between stream class and C stdio
* Bridge between channel class and C stdio
* ****************************************/
// sFILE -> stream_ptr
class fp_stream final : public file_stream {
// sFILE -> channel_ptr
class fp_channel final : public file_channel {
public:
fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {}
fp_stream(sFILE &&fp) : fp(std::move(fp)) {}
fp_channel(FILE *fp = nullptr) : fp(fp, fclose) {}
fp_channel(sFILE &&fp) : fp(std::move(fp)) {}
ssize_t read(void *buf, size_t len) override;
off_t seek(off_t off, int whence) override;
protected:
@@ -124,10 +128,10 @@ private:
sFILE fp;
};
// stream_ptr -> sFILE
sFILE make_stream_fp(stream_ptr &&strm);
// channel_ptr -> sFILE
sFILE make_channel_fp(channel_ptr &&strm);
template <class T, class... Args>
sFILE make_stream_fp(Args &&... args) {
return make_stream_fp(stream_ptr(new T(std::forward<Args>(args)...)));
sFILE make_channel_fp(Args &&... args) {
return make_channel_fp(channel_ptr(new T(std::forward<Args>(args)...)));
}