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