Repository navigation
Expand file tree
/
Copy pathdata_buffer_serialiser.h
More file actions
114 lines (101 loc) · 2.45 KB
/
Copy pathdata_buffer_serialiser.h
File metadata and controls
114 lines (101 loc) · 2.45 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <cstdint>
#include <cstring>
#include <memory>
#include <type_traits>
#include "core/data_buffer.h"
#include "core/quaternion.h"
#include "core/vector3.h"
namespace iris
{
/**
* Class for serialising types to a DataBuffer.
*
* Note this assumes little-endian.
*/
class DataBufferSerialiser
{
public:
/**
* Get the serialised data.
*
* @returns
* DataBuffer of serialised data.
*/
DataBuffer data() const
{
return buffer_;
}
/**
* Serialise an integral type.
*
* @param value
* Value to serialise.
*/
template <class T, std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
void push(T value)
{
const auto size = sizeof(T);
buffer_.resize(buffer_.size() + size);
std::memcpy(std::addressof(*(std::end(buffer_) - size)), &value, size);
}
/**
* Serialise an enum.
*
* @param value
* Value to serialise.
*/
template <class T, std::enable_if_t<std::is_enum_v<T>> * = nullptr>
void push(T value)
{
using type = std::underlying_type_t<T>;
push<type>(static_cast<type>(value));
}
/**
* Serialise a Vector3.
*
* @param value
* Value to serialise.
*/
void push(const Vector3 &value)
{
push(value.x);
push(value.y);
push(value.z);
}
/**
* Serialise a Quaternion.
*
* @param value
* Value to serialise.
*/
void push(const Quaternion &value)
{
push(value.x);
push(value.y);
push(value.z);
push(value.w);
}
/**
* Serialise a DataBuffer.
*
* @param value
* Value to serialise.
*/
void push(const DataBuffer &value)
{
const auto size = value.size();
push(static_cast<std::uint32_t>(size));
buffer_.resize(buffer_.size() + size);
std::memcpy(std::addressof(*(std::end(buffer_) - size)), value.data(), size);
}
private:
/** Serialised data. */
DataBuffer buffer_;
};
}