Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/nall/stream/stream.hpp
2 views
1
#ifndef NALL_STREAM_STREAM_HPP
2
#define NALL_STREAM_STREAM_HPP
3
4
namespace nall {
5
6
struct stream {
7
virtual bool seekable() const = 0;
8
virtual bool readable() const = 0;
9
virtual bool writable() const = 0;
10
virtual bool randomaccess() const = 0;
11
12
virtual unsigned size() const = 0;
13
virtual unsigned offset() const = 0;
14
virtual void seek(unsigned offset) const = 0;
15
16
virtual uint8_t read() const = 0;
17
virtual void write(uint8_t data) const = 0;
18
19
inline virtual uint8_t read(unsigned) const { return 0; }
20
inline virtual void write(unsigned, uint8_t) const {}
21
22
inline bool end() const {
23
return offset() >= size();
24
}
25
26
inline void copy(uint8_t *&data, unsigned &length) const {
27
seek(0);
28
length = size();
29
data = new uint8_t[length];
30
for(unsigned n = 0; n < length; n++) data[n] = read();
31
}
32
33
inline uintmax_t readl(unsigned length = 1) const {
34
uintmax_t data = 0, shift = 0;
35
while(length--) { data |= read() << shift; shift += 8; }
36
return data;
37
}
38
39
inline uintmax_t readm(unsigned length = 1) const {
40
uintmax_t data = 0;
41
while(length--) data = (data << 8) | read();
42
return data;
43
}
44
45
inline void read(uint8_t *data, unsigned length) const {
46
while(length--) *data++ = read();
47
}
48
49
inline void writel(uintmax_t data, unsigned length = 1) const {
50
while(length--) {
51
write(data);
52
data >>= 8;
53
}
54
}
55
56
inline void writem(uintmax_t data, unsigned length = 1) const {
57
uintmax_t shift = 8 * length;
58
while(length--) {
59
shift -= 8;
60
write(data >> shift);
61
}
62
}
63
64
inline void write(const uint8_t *data, unsigned length) const {
65
while(length--) write(*data++);
66
}
67
68
struct byte {
69
inline operator uint8_t() const { return s.read(offset); }
70
inline byte& operator=(uint8_t data) { s.write(offset, data); }
71
inline byte(const stream &s, unsigned offset) : s(s), offset(offset) {}
72
73
private:
74
const stream &s;
75
const unsigned offset;
76
};
77
78
inline byte operator[](unsigned offset) const {
79
return byte(*this, offset);
80
}
81
82
inline stream() {}
83
inline virtual ~stream() {}
84
stream(const stream&) = delete;
85
stream& operator=(const stream&) = delete;
86
};
87
88
}
89
90
#endif
91
92