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 <cstdint>
12#include <cstring>
13#include <stdexcept>
14#include <type_traits>
15#include <vector>
16
19{
20public:
21 std::vector<uint8_t> buffer;
22
26 template <typename T>
27 void operator()(const T& value)
28 {
29 static_assert(std::is_trivially_copyable_v<T>, "Component type must be trivially copyable for serialization");
30
31 const auto* p = reinterpret_cast<const uint8_t*>(&value);
32 buffer.insert(buffer.end(), p, p + sizeof(T));
33 }
34};
35
38{
39public:
43 InputArchive(const uint8_t* d, size_t s) : data(d), size(s) {}
44
48 template <typename T>
49 void operator()(T& value)
50 {
51 static_assert(std::is_trivially_copyable_v<T>, "Component type must be trivially copyable for deserialization");
52
53 if (offset + sizeof(T) > size) {
54 throw std::runtime_error("InputArchive: out of data");
55 }
56
57 std::memcpy(&value, data + offset, sizeof(T));
58 offset += sizeof(T);
59 }
60
61private:
62 const uint8_t* data;
63 size_t size;
64 size_t offset = 0;
65};
void operator()(T &value)
Read the next value from the buffer and advance the offset.
Definition RegistryArchive.hpp:49
size_t size
Definition RegistryArchive.hpp:63
const uint8_t * data
Definition RegistryArchive.hpp:62
size_t offset
Definition RegistryArchive.hpp:64
InputArchive(const uint8_t *d, size_t s)
Construct an InputArchive over an existing data buffer.
Definition RegistryArchive.hpp:43
Archive that serializes trivially-copyable values into a byte buffer.
Definition RegistryArchive.hpp:19
void operator()(const T &value)
Append value to the internal buffer as raw bytes.
Definition RegistryArchive.hpp:27
std::vector< uint8_t > buffer
Definition RegistryArchive.hpp:21