Project Alice
Loading...
Searching...
No Matches
stools.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <functional>
4#include <cstring>
5#include <stdint.h>
6#include <string>
7#include <string_view>
8#include <array>
9#include <span>
10#include <bit>
11#include <assert.h>
12
13namespace serialization {
14
15
17private:
18 std::vector<char> data_;
19 std::vector<std::pair<size_t, std::function<void(out_buffer&)>>> pending_writes;
20 std::vector<size_t> open_sections;
21public:
22 char const* data() const {
23 return data_.data();
24 }
25 size_t size() const {
26 return data_.size();
27 }
29 while(!pending_writes.empty()) {
30 auto relocation_address = data_.data() + pending_writes.back().first;
31 uint32_t new_address = uint32_t(data_.size());
32 std::memcpy(relocation_address, &new_address, sizeof(uint32_t));
33
34 pending_writes.back().second(*this);
35
36 pending_writes.pop_back();
37 }
38 }
40 auto sz_address = data_.size();
41 uint32_t placeholder = 0;
42 write(placeholder);
43 open_sections.emplace_back(sz_address);
44 }
46 assert(!open_sections.empty());
47
48 auto size_address = data_.data() + open_sections.back();
49 uint32_t bytes_in_section = uint32_t(data_.size() - open_sections.back());
50 std::memcpy(size_address, &bytes_in_section, sizeof(uint32_t));
51
52 open_sections.pop_back();
53 }
54 void finalize() {
56 while(open_sections.empty() == false) {
58 }
59 }
60
61 template<typename T>
62 void write(T const& d) {
63 auto start_size = data_.size();
64 data_.resize(start_size + sizeof(T), 0);
65 std::memcpy(data_.data() + start_size, &d, sizeof(T));
66 }
67 template<typename T>
68 void write_fixed(T const* d, size_t count) {
69 auto start_size = data_.size();
70 data_.resize(start_size + sizeof(T) * count, 0);
71 std::memcpy(data_.data() + start_size, d, sizeof(T) * count);
72 }
73 template<typename T>
74 void write_variable(T const* d, size_t count) {
75 uint32_t c = uint32_t(count);
76 write(c);
77 write_fixed(d, count);
78 }
79 void write_relocation(std::function<void(out_buffer&)>&& f) {
80 auto reloc_address = data_.size();
81 uint32_t placeholder = 0;
82 write(placeholder);
83 pending_writes.emplace_back(reloc_address, std::move(f));
84 }
85 void write_relocation(size_t reloc_address, std::function<void(out_buffer&)>&& f) {
86 pending_writes.emplace_back(reloc_address, std::move(f));
87 }
88 size_t get_data_position() const {
89 return data_.size();
90 }
91 void write(std::string_view sv) {
92 write_variable(sv.data(), sv.length());
93 }
94 void write(std::string const& s) {
95 write_variable(s.data(), s.length());
96 }
97 void write(std::wstring_view sv) {
98 write_variable(sv.data(), sv.length());
99 }
100 void write(std::wstring const& s) {
101 write_variable(s.data(), s.length());
102 }
103};
104
106private:
107 char const* data;
108 size_t size;
109 size_t read_position = 0;
110public:
111 in_buffer(char const* data, size_t size) : data(data), size(size) { }
112 in_buffer(char const* data, size_t size, size_t read_position) : data(data), size(size), read_position(read_position) {
113 }
114
115 operator bool() const noexcept {
116 return data && read_position < size;
117 }
118
119 char const* view_data() const {
120 return data;
121 }
122 size_t view_size() const {
123 return size;
124 }
125 size_t view_read_position() const {
126 return read_position;
127 }
128
129 template<typename T>
130 T read() {
131 T temp = T{ };
132 if(read_position + sizeof(T) <= size) {
133 std::memcpy(&temp, data + read_position, sizeof(T));
134 read_position += sizeof(T);
135 }
136 return temp;
137 }
138 template<typename T>
139 void read(T& out) {
140 out = T{ };
141 if(read_position + sizeof(T) <= size) {
142 std::memcpy(&out, data + read_position, sizeof(T));
143 read_position += sizeof(T);
144 }
145 }
146 template<typename T>
147 std::span<T const> read_fixed(size_t count) {
148 auto len = std::min(count, (size - read_position) / sizeof(T));
149 auto start = (T const*)(data + read_position);
150 read_position += count * sizeof(T);
151 return std::span<T const>(start, start + len);
152 }
153 template<typename T>
154 std::span<T const> read_variable() {
155 auto count = read<uint32_t>();
156 return read_fixed<T>(size_t(count));
157 }
159 uint32_t offset = read<uint32_t>();
160 return in_buffer(data, size, offset);
161 }
163 uint32_t section_size = read<uint32_t>();
164 auto start_postion = read_position;
165 read_position += (section_size - 4);
166 return in_buffer(data, std::min(size_t(start_postion + section_size - 4), size), start_postion);
167 }
168 template<>
169 std::string_view read<std::string_view>() {
170 auto s = read_variable<char>();
171 return std::string_view(s.data(), s.size());
172 }
173 template<>
174 std::wstring_view read<std::wstring_view>() {
175 auto s = read_variable<wchar_t>();
176 return std::wstring_view(s.data(), s.size());
177 }
178 void read(std::string& out) {
179 auto s = read_variable<char>();
180 out = std::string(s.data(), s.size());
181 }
182 void read(std::wstring& out) {
183 auto s = read_variable<wchar_t>();
184 out = std::wstring(s.data(), s.size());
185 }
186};
187
188}
in_buffer read_section()
Definition: stools.hpp:162
size_t view_size() const
Definition: stools.hpp:122
std::span< T const > read_variable()
Definition: stools.hpp:154
size_t view_read_position() const
Definition: stools.hpp:125
void read(std::wstring &out)
Definition: stools.hpp:182
std::span< T const > read_fixed(size_t count)
Definition: stools.hpp:147
in_buffer(char const *data, size_t size, size_t read_position)
Definition: stools.hpp:112
void read(std::string &out)
Definition: stools.hpp:178
char const * view_data() const
Definition: stools.hpp:119
in_buffer(char const *data, size_t size)
Definition: stools.hpp:111
in_buffer read_relocation()
Definition: stools.hpp:158
void write(std::wstring_view sv)
Definition: stools.hpp:97
void write_fixed(T const *d, size_t count)
Definition: stools.hpp:68
void write_relocation(std::function< void(out_buffer &)> &&f)
Definition: stools.hpp:79
size_t get_data_position() const
Definition: stools.hpp:88
void write(std::wstring const &s)
Definition: stools.hpp:100
void write(T const &d)
Definition: stools.hpp:62
void write(std::string const &s)
Definition: stools.hpp:94
void write(std::string_view sv)
Definition: stools.hpp:91
void write_variable(T const *d, size_t count)
Definition: stools.hpp:74
void write_relocation(size_t reloc_address, std::function< void(out_buffer &)> &&f)
Definition: stools.hpp:85
size_t size() const
Definition: stools.hpp:25
char const * data() const
Definition: stools.hpp:22
#define assert(condition)
Definition: debug.h:74
uint uint32_t