pragma once include cstddef include vector include stdexcept include r

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <cstddef>
#include <vector>
#include <stdexcept>
#include "runtime_check.h"
namespace bastion {
template<typename Byte>
class basic_byte_view : public span<Byte> {
public:
using span<Byte>::span;
using span<Byte>::data;
using span<Byte>::size;
constexpr basic_byte_view<Byte> take_view(std::size_t taken_size) {
runtime_check(size() >= taken_size);
auto ptr = data();
span<Byte>::data_ += taken_size;
span<Byte>::size_ -= taken_size;
return {ptr, taken_size};
}
template <typename T>
constexpr T* as_record() const {
runtime_check(size() >= sizeof(T));
auto ptr = reinterpret_cast<T *>(data());
return ptr;
}
template <typename T>
constexpr span<T> as_span() const {
runtime_check(size() % sizeof(T) == 0);
T *ptr = reinterpret_cast<T *>(data());
return {ptr, size() / sizeof(T)};
}
template <typename T>
constexpr T *take_record() {
return take_view(sizeof(T)).template as_record<T>();
}
template <typename T>
constexpr T read_record() {
return *take_record<T>();
}
template <typename T>
constexpr T pread_record(std::size_t offset) {
return *sub_view(offset, sizeof(T)).template as_record<T>();
}
constexpr basic_byte_view<Byte> sub_view(std::size_t start) const {
runtime_check(size() >= start);
return {data() + start, size() - start};
}
constexpr basic_byte_view<Byte> sub_view(std::size_t start, std::size_t size) const {
runtime_check(this->size() >= start);
runtime_check(this->size() - start >= size);
return {data() + start, size};
}
private:
using span<Byte>::subspan;
};
using byte_view = basic_byte_view<std::byte>;
using cbyte_view = basic_byte_view<std::byte const>;
}