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;
29 while(!pending_writes.empty()) {
30 auto relocation_address = data_.data() + pending_writes.back().first;
32 std::memcpy(relocation_address, &new_address,
sizeof(
uint32_t));
34 pending_writes.back().second(*
this);
36 pending_writes.pop_back();
40 auto sz_address = data_.size();
43 open_sections.emplace_back(sz_address);
46 assert(!open_sections.empty());
48 auto size_address = data_.data() + open_sections.back();
50 std::memcpy(size_address, &bytes_in_section,
sizeof(
uint32_t));
52 open_sections.pop_back();
56 while(open_sections.empty() ==
false) {
63 auto start_size = data_.size();
64 data_.resize(start_size +
sizeof(T), 0);
65 std::memcpy(data_.data() + start_size, &d,
sizeof(T));
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);
80 auto reloc_address = data_.size();
83 pending_writes.emplace_back(reloc_address, std::move(f));
86 pending_writes.emplace_back(reloc_address, std::move(f));
91 void write(std::string_view sv) {
94 void write(std::string
const& s) {
97 void write(std::wstring_view sv) {
109 size_t read_position = 0;
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) {
115 operator bool() const noexcept {
116 return data && read_position < size;
126 return read_position;
132 if(read_position +
sizeof(T) <= size) {
133 std::memcpy(&temp, data + read_position,
sizeof(T));
134 read_position +=
sizeof(T);
141 if(read_position +
sizeof(T) <= size) {
142 std::memcpy(&out, data + read_position,
sizeof(T));
143 read_position +=
sizeof(T);
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);
155 auto count = read<uint32_t>();
156 return read_fixed<T>(
size_t(count));
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);
169 std::string_view read<std::string_view>() {
170 auto s = read_variable<char>();
171 return std::string_view(s.data(), s.size());
174 std::wstring_view read<std::wstring_view>() {
175 auto s = read_variable<wchar_t>();
176 return std::wstring_view(s.data(), s.size());
179 auto s = read_variable<char>();
180 out = std::string(s.data(), s.size());
183 auto s = read_variable<wchar_t>();
184 out = std::wstring(s.data(), s.size());
std::span< T const > read_variable()
size_t view_read_position() const
void read(std::wstring &out)
std::span< T const > read_fixed(size_t count)
in_buffer(char const *data, size_t size, size_t read_position)
void read(std::string &out)
char const * view_data() const
in_buffer(char const *data, size_t size)
in_buffer read_relocation()
void write(std::wstring_view sv)
void write_fixed(T const *d, size_t count)
void write_relocation(std::function< void(out_buffer &)> &&f)
size_t get_data_position() const
void write(std::wstring const &s)
void write(std::string const &s)
void write(std::string_view sv)
void write_variable(T const *d, size_t count)
void write_relocation(size_t reloc_address, std::function< void(out_buffer &)> &&f)
char const * data() const
#define assert(condition)