group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
RegistryArchive.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <cstddef>
12#include <cstdint>
13#include <cstring>
14#include <stdexcept>
15#include <type_traits>
16#include <vector>
17
20{
21public:
22 std::vector<uint8_t> buffer;
23
25 void appendRaw(const uint8_t* data, std::size_t size)
26 {
27 if (size == 0)
28 return;
29 const std::size_t offset = buffer.size();
30 buffer.resize(offset + size);
31 std::memcpy(buffer.data() + offset, data, size);
32 }
33
37 template <typename T>
38 void operator()(const T& value)
39 {
40 static_assert(std::is_trivially_copyable_v<T>, "Component type must be trivially copyable for serialization");
41
42 appendRaw(reinterpret_cast<const uint8_t*>(&value), sizeof(T));
43 }
44};
45
48{
49public:
53 InputArchive(const uint8_t* d, size_t s) : data(d), size(s) {}
54
58 template <typename T>
59 void operator()(T& value)
60 {
61 static_assert(std::is_trivially_copyable_v<T>, "Component type must be trivially copyable for deserialization");
62
63 if (offset + sizeof(T) > size) {
64 throw std::runtime_error("InputArchive: out of data");
65 }
66
67 std::memcpy(&value, data + offset, sizeof(T));
68 offset += sizeof(T);
69 }
70
71private:
72 const uint8_t* data;
73 size_t size;
74 size_t offset = 0;
75};
void operator()(T &value)
Read the next value from the buffer and advance the offset.
Definition RegistryArchive.hpp:59
size_t size
Definition RegistryArchive.hpp:73
const uint8_t * data
Definition RegistryArchive.hpp:72
size_t offset
Definition RegistryArchive.hpp:74
InputArchive(const uint8_t *d, size_t s)
Construct an InputArchive over an existing data buffer.
Definition RegistryArchive.hpp:53
Archive that serializes trivially-copyable values into a byte buffer.
Definition RegistryArchive.hpp:20
void operator()(const T &value)
Append value to the internal buffer as raw bytes.
Definition RegistryArchive.hpp:38
void appendRaw(const uint8_t *data, std::size_t size)
Append a raw byte range with a single resize + memcpy.
Definition RegistryArchive.hpp:25
std::vector< uint8_t > buffer
Definition RegistryArchive.hpp:22