Debian Codegraph @

src/mpt/io_read/filedata_base.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_IO_READ_FILEDATA_BASE_HPP
4 #define MPT_IO_READ_FILEDATA_BASE_HPP
5
6
7
8 #include "mpt/base/memory.hpp"
9 #include "mpt/base/namespace.hpp"
10 #include "mpt/io_read/filedata.hpp"
11
12 #include <algorithm>
13 #include <memory>
14
15 #include <cstddef>
16
17
18
19 namespace mpt {
20 inline namespace MPT_INLINE_NS {
21
22
23
24 namespace IO {
25
26
27
28 class FileDataDummy : public IFileData {
29 public:
30 FileDataDummy() { }
31
32 public:
33 bool IsValid() const override {
34 return false;
35 }
36
37 bool HasFastGetLength() const override {
38 return true;
39 }
40
41 bool HasPinnedView() const override {
42 return true;
43 }
44
45 const std::byte * GetRawData() const override {
46 return nullptr;
47 }
48
49 pos_type GetLength() const override {
50 return 0;
51 }
52 mpt::byte_span Read(pos_type /* pos */, mpt::byte_span dst) const override {
53 return dst.first(0);
54 }
55 };
56
57
58 class FileDataWindow : public IFileData {
59 private:
60 std::shared_ptr<const IFileData> data;
61 const pos_type dataOffset;
62 const pos_type dataLength;
63
64 public:
65 FileDataWindow(std::shared_ptr<const IFileData> src, pos_type off, pos_type len)
66 : data(src)
67 , dataOffset(off)
68 , dataLength(len) { }
69
70 bool IsValid() const override {
71 return data->IsValid();
72 }
73 bool HasFastGetLength() const override {
74 return true;
75 }
76 bool HasPinnedView() const override {
77 return data->HasPinnedView();
78 }
79 const std::byte * GetRawData() const override {
80 return data->GetRawData() + dataOffset;
81 }
82 pos_type GetLength() const override {
83 return dataLength;
84 }
85 mpt::byte_span Read(pos_type pos, mpt::byte_span dst) const override {
86 if (pos >= dataLength) {
87 return dst.first(0);
88 }
89 return data->Read(dataOffset + pos, dst.first(static_cast<std::size_t>(std::min(static_cast<pos_type>(dst.size()), dataLength - pos))));
90 }
91 bool CanRead(pos_type pos, pos_type length) const override {
92 if ((pos == dataLength) && (length == 0)) {
93 return true;
94 }
95 if (pos >= dataLength) {
96 return false;
97 }
98 return (length <= dataLength - pos);
99 }
100 pos_type GetReadableLength(pos_type pos, pos_type length) const override {
101 if (pos >= dataLength) {
102 return 0;
103 }
104 return std::min(length, dataLength - pos);
105 }
106 };
107
108
109
110 } // namespace IO
111
112
113
114 } // namespace MPT_INLINE_NS
115 } // namespace mpt
116
117
118
119 #endif // MPT_IO_READ_FILEDATA_BASE_HPP