]> git.tdb.fi Git - libs/core.git/blob - source/io/slice.h
2965f9899a8252c8415b5c776e31dee8b6c490fd
[libs/core.git] / source / io / slice.h
1 #ifndef MSP_IO_SLICE_H_
2 #define MSP_IO_SLICE_H_
3
4 #include "seekable.h"
5
6 namespace Msp {
7 namespace IO {
8
9 /**
10 Presents a part of seekable I/O object as if it was a complete object.  This
11 allows in-place access to embedded resources where the loader expects to
12 receive a complete file.
13
14 When a Slice is created, its read/write offset is placed at the beginning of
15 its range.  If the offset of the underlying object is changed, the Slice will
16 restore it before the next access.  This enables multiple Slices to be created
17 on top of the same object.
18 */
19 class Slice: public Seekable, public sigc::trackable
20 {
21 private:
22         Seekable &below;
23         SeekOffset start_offset = 0;
24         SeekOffset length = 0;
25         SeekOffset position = 0;
26         bool sync_position = true;
27
28 public:
29         Slice(Seekable &, SeekOffset, SeekOffset);
30
31         void set_block(bool) override;
32         void set_inherit(bool) override;
33
34 private:
35         void flush();
36
37         unsigned prepare_op(unsigned, Mode);
38 protected:
39         std::size_t do_write(const char *, std::size_t) override;
40         std::size_t do_read(char *, std::size_t) override;
41
42 public:
43         std::size_t put(char) override;
44         int get() override;
45
46         const Handle &get_handle(Mode) override;
47
48         SeekOffset seek(SeekOffset, SeekType) override;
49         SeekOffset tell() const override { return position; }
50 };
51
52 } // namespace IO
53 } // namespace Msp
54
55 #endif