]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/slice.h
Add an I/O class to access a restricted range of bytes from another object
[libs/core.git] / source / io / slice.h
diff --git a/source/io/slice.h b/source/io/slice.h
new file mode 100644 (file)
index 0000000..f611585
--- /dev/null
@@ -0,0 +1,50 @@
+#ifndef MSP_IO_SLICE_H_
+#define MSP_IO_SLICE_H_
+
+#include "seekable.h"
+
+namespace Msp {
+namespace IO {
+
+/**
+Presents a part of seekable I/O object as if it was a complete object.  This
+allows in-place access to embedded resources where the loader expects to
+receive a complete file.
+
+When a Slice is created, its read/write offset is placed at the beginning of
+its range.  If the offset of the underlying object is changed, the Slice will
+restore it before the next access.  This enables multiple Slices to be created
+on top of the same object.
+*/
+class Slice: public Seekable
+{
+private:
+       Seekable &below;
+       SeekOffset start_offset;
+       SeekOffset length;
+       SeekOffset position;
+       bool sync_position;
+
+public:
+       Slice(Seekable &, SeekOffset, SeekOffset);
+
+private:
+       void flush();
+
+       unsigned prepare_op(unsigned, Mode);
+protected:
+       virtual unsigned do_write(const char *, unsigned);
+       virtual unsigned do_read(char *, unsigned);
+
+public:
+       virtual unsigned put(char);
+       virtual int get();
+
+       virtual SeekOffset seek(SeekOffset, SeekType);
+       virtual SeekOffset tell() const { return position; }
+};
+
+} // namespace IO
+} // namespace Msp
+
+#endif