Debian Codegraph @

src/mpt/io/io_span.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_IO_IO_SPAN_HPP
4 #define MPT_IO_IO_SPAN_HPP
5
6
7
8 #include "mpt/base/macros.hpp"
9 #include "mpt/base/memory.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/base/saturate_cast.hpp"
12 #include "mpt/base/span.hpp"
13 #include "mpt/io/base.hpp"
14
15 #include <algorithm>
16 #include <utility>
17
18 #include <cstddef>
19
20
21
22 namespace mpt {
23 inline namespace MPT_INLINE_NS {
24
25
26
27 namespace IO {
28
29
30
31 template <typename Tbyte>
32 struct FileOperations<std::pair<mpt::span<Tbyte>, IO::Offset>> {
33
34 private:
35 std::pair<mpt::span<Tbyte>, IO::Offset> & f;
36
37 public:
38 FileOperations(std::pair<mpt::span<Tbyte>, IO::Offset> & f_)
39 : f(f_) {
40 return;
41 }
42
43 public:
44 // cppcheck false-positive
45 // cppcheck-suppress duplInheritedMember
46 inline bool IsValid() {
47 return (f.second >= 0);
48 }
49
50 // cppcheck false-positive
51 // cppcheck-suppress duplInheritedMember
52 inline bool IsReadSeekable() {
53 MPT_UNUSED(f);
54 return true;
55 }
56
57 // cppcheck false-positive
58 // cppcheck-suppress duplInheritedMember
59 inline bool IsWriteSeekable() {
60 MPT_UNUSED(f);
61 return true;
62 }
63
64 // cppcheck false-positive
65 // cppcheck-suppress duplInheritedMember
66 inline IO::Offset TellRead() {
67 return f.second;
68 }
69
70 // cppcheck false-positive
71 // cppcheck-suppress duplInheritedMember
72 inline IO::Offset TellWrite() {
73 return f.second;
74 }
75
76 // cppcheck false-positive
77 // cppcheck-suppress duplInheritedMember
78 inline bool SeekBegin() {
79 f.second = 0;
80 return true;
81 }
82
83 // cppcheck false-positive
84 // cppcheck-suppress duplInheritedMember
85 inline bool SeekEnd() {
86 f.second = f.first.size();
87 return true;
88 }
89
90 // cppcheck false-positive
91 // cppcheck-suppress duplInheritedMember
92 inline bool SeekAbsolute(IO::Offset pos) {
93 f.second = pos;
94 return true;
95 }
96
97 // cppcheck false-positive
98 // cppcheck-suppress duplInheritedMember
99 inline bool SeekRelative(IO::Offset off) {
100 if (f.second < 0) {
101 return false;
102 }
103 f.second += off;
104 return true;
105 }
106
107 // cppcheck false-positive
108 // cppcheck-suppress duplInheritedMember
109 inline mpt::byte_span ReadRawImpl(mpt::byte_span data) {
110 if (f.second < 0) {
111 return data.first(0);
112 }
113 if (f.second >= static_cast<IO::Offset>(f.first.size())) {
114 return data.first(0);
115 }
116 std::size_t num = mpt::saturate_cast<std::size_t>(std::min(static_cast<IO::Offset>(f.first.size()) - f.second, static_cast<IO::Offset>(data.size())));
117 std::copy(mpt::byte_cast<const std::byte *>(f.first.data() + f.second), mpt::byte_cast<const std::byte *>(f.first.data() + f.second + num), data.data());
118 f.second += num;
119 return data.first(num);
120 }
121
122 // cppcheck false-positive
123 // cppcheck-suppress duplInheritedMember
124 inline bool WriteRawImpl(mpt::const_byte_span data) {
125 if (f.second < 0) {
126 return false;
127 }
128 if (f.second > static_cast<IO::Offset>(f.first.size())) {
129 return false;
130 }
131 std::size_t num = mpt::saturate_cast<std::size_t>(std::min(static_cast<IO::Offset>(f.first.size()) - f.second, static_cast<IO::Offset>(data.size())));
132 if (num != data.size()) {
133 return false;
134 }
135 std::copy(data.data(), data.data() + num, mpt::byte_cast<std::byte *>(f.first.data() + f.second));
136 f.second += num;
137 return true;
138 }
139
140 // cppcheck false-positive
141 // cppcheck-suppress duplInheritedMember
142 inline bool IsEof() {
143 return (f.second >= static_cast<IO::Offset>(f.first.size()));
144 }
145
146 // cppcheck false-positive
147 // cppcheck-suppress duplInheritedMember
148 inline bool Flush() {
149 MPT_UNUSED(f);
150 return true;
151 }
152 };
153
154
155
156
157 } // namespace IO
158
159
160
161 } // namespace MPT_INLINE_NS
162 } // namespace mpt
163
164
165
166 #endif // MPT_IO_IO_SPAN_HPP