Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
ser_buffer_binary.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <type_traits>
5
6#include "gaia/cnt/darray.h"
7#include "gaia/cnt/darray_ext.h"
8
9namespace gaia {
10 namespace ser {
11 enum class serialization_type_id : uint8_t;
12
13 namespace detail {
14 static constexpr uint32_t SerializationBufferCapacityIncreaseSize = 128U;
15
16 template <typename DataContainer>
19 protected:
20 // Increase the capacity by multiples of CapacityIncreaseSize
21 static constexpr uint32_t CapacityIncreaseSize = SerializationBufferCapacityIncreaseSize;
22
24 DataContainer m_data;
26 uint32_t m_dataPos = 0;
27
28 public:
29 void reset() {
30 m_dataPos = 0;
31 m_data.clear();
32 }
33
35 GAIA_NODISCARD uint32_t bytes() const {
36 return (uint32_t)m_data.size();
37 }
38
40 GAIA_NODISCARD bool empty() const {
41 return m_data.empty();
42 }
43
45 GAIA_NODISCARD const auto* data() const {
46 return m_data.data();
47 }
48
51 void reserve(uint32_t size) {
52 const auto nextSize = m_dataPos + size;
53 if (nextSize <= bytes())
54 return;
55
56 // Make sure there is enough capacity to hold our data
57 const auto newSize = bytes() + size;
58 const auto newCapacity = ((newSize / CapacityIncreaseSize) * CapacityIncreaseSize) + CapacityIncreaseSize;
59 m_data.reserve(newCapacity);
60 }
61
64 void resize(uint32_t size) {
65 m_data.resize(size);
66 }
67
70 void seek(uint32_t pos) {
71 m_dataPos = pos;
72 }
73
76 void skip(uint32_t size) {
77 m_dataPos += size;
78 }
79
81 GAIA_NODISCARD uint32_t tell() const {
82 return m_dataPos;
83 }
84
87 template <typename T>
88 void save(T&& value) {
89 reserve((uint32_t)sizeof(T));
90
91 const auto cnt = m_dataPos + (uint32_t)sizeof(T);
92 if (cnt > m_data.size())
93 m_data.resize(cnt);
95 mem = GAIA_FWD(value);
96
97 m_dataPos += (uint32_t)sizeof(T);
98 }
99
104 void save_raw(const void* pSrc, uint32_t size, [[maybe_unused]] ser::serialization_type_id id) {
105 if (size == 0)
106 return;
107
108 reserve(size);
109
110 // Copy "size" bytes of raw data starting at pSrc
111 const auto cnt = m_dataPos + size;
112 if (cnt > m_data.size())
113 m_data.resize(cnt);
114 memcpy((void*)&m_data[m_dataPos], pSrc, size);
115
116 m_dataPos += size;
117 }
118
121 template <typename T>
122 void load(T& value) {
123 GAIA_ASSERT(m_dataPos + (uint32_t)sizeof(T) <= bytes());
124
125 const auto& cdata = std::as_const(m_data);
126 value = mem::unaligned_ref<T>((void*)&cdata[m_dataPos]);
127
128 m_dataPos += (uint32_t)sizeof(T);
129 }
130
135 void load_raw(void* pDst, uint32_t size, [[maybe_unused]] ser::serialization_type_id id) {
136 if (size == 0)
137 return;
138
139 GAIA_ASSERT(m_dataPos + size <= bytes());
140
141 const auto& cdata = std::as_const(m_data);
142 memmove(pDst, (const void*)&cdata[m_dataPos], size);
143
144 m_dataPos += size;
145 }
146 };
147 } // namespace detail
148
150 using ser_buffer_binary_storage_dyn = gaia::cnt::darray<uint8_t>;
151
155 class ser_buffer_binary: public detail::ser_buffer_binary_impl<ser_buffer_binary_storage> {};
157 class ser_buffer_binary_dyn: public detail::ser_buffer_binary_impl<ser_buffer_binary_storage_dyn> {};
158 } // namespace ser
159} // namespace gaia
Array of elements of type.
Definition darray_ext_impl.h:28
Array with variable size of elements of type.
Definition darray_impl.h:25
Pointer wrapper for writing memory in defined way (not causing undefined behavior)
Definition mem_alloc.h:260
In-memory binary read/write stream used by compile-time and runtime serializers.
Definition ser_buffer_binary.h:18
void seek(uint32_t pos)
Changes the current position in the buffer.
Definition ser_buffer_binary.h:70
GAIA_NODISCARD uint32_t tell() const
Returns the current position in the buffer.
Definition ser_buffer_binary.h:81
uint32_t m_dataPos
Current position in the buffer.
Definition ser_buffer_binary.h:26
GAIA_NODISCARD uint32_t bytes() const
Returns the number of bytes written in the buffer.
Definition ser_buffer_binary.h:35
GAIA_NODISCARD const auto * data() const
Returns the pointer to the data in the buffer.
Definition ser_buffer_binary.h:45
DataContainer m_data
Buffer holding raw data.
Definition ser_buffer_binary.h:24
void resize(uint32_t size)
Resizes the internal buffer to size bytes.
Definition ser_buffer_binary.h:64
void reserve(uint32_t size)
Makes sure there is enough capacity in our data container to hold another size bytes of data.
Definition ser_buffer_binary.h:51
void save(T &&value)
Writes value to the buffer.
Definition ser_buffer_binary.h:88
void load_raw(void *pDst, uint32_t size, ser::serialization_type_id id)
Loads size bytes of data from the buffer and writes it to the address pDst.
Definition ser_buffer_binary.h:135
void load(T &value)
Loads value from the buffer.
Definition ser_buffer_binary.h:122
void skip(uint32_t size)
Advances size bytes from the current buffer position.
Definition ser_buffer_binary.h:76
GAIA_NODISCARD bool empty() const
Returns true if there is no data written in the buffer.
Definition ser_buffer_binary.h:40
void save_raw(const void *pSrc, uint32_t size, ser::serialization_type_id id)
Writes size bytes of data starting at the address pSrc to the buffer.
Definition ser_buffer_binary.h:104
Same API as ser_buffer_binary, but backed by fully dynamic storage.
Definition ser_buffer_binary.h:157
Minimal in-memory binary serializer. It stores raw bytes only (no schema, versioning,...
Definition ser_buffer_binary.h:155
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9