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>
18 protected:
19 // Increase the capacity by multiples of CapacityIncreaseSize
20 static constexpr uint32_t CapacityIncreaseSize = SerializationBufferCapacityIncreaseSize;
21
23 DataContainer m_data;
25 uint32_t m_dataPos = 0;
26
27 public:
28 void reset() {
29 m_dataPos = 0;
30 m_data.clear();
31 }
32
34 GAIA_NODISCARD uint32_t bytes() const {
35 return (uint32_t)m_data.size();
36 }
37
39 GAIA_NODISCARD bool empty() const {
40 return m_data.empty();
41 }
42
44 GAIA_NODISCARD const auto* data() const {
45 return m_data.data();
46 }
47
50 void reserve(uint32_t size) {
51 const auto nextSize = m_dataPos + size;
52 if (nextSize <= bytes())
53 return;
54
55 // Make sure there is enough capacity to hold our data
56 const auto newSize = bytes() + size;
57 const auto newCapacity = ((newSize / CapacityIncreaseSize) * CapacityIncreaseSize) + CapacityIncreaseSize;
58 m_data.reserve(newCapacity);
59 }
60
63 void resize(uint32_t size) {
64 m_data.resize(size);
65 }
66
69 void seek(uint32_t pos) {
70 m_dataPos = pos;
71 }
72
75 void skip(uint32_t size) {
76 m_dataPos += size;
77 }
78
80 GAIA_NODISCARD uint32_t tell() const {
81 return m_dataPos;
82 }
83
86 template <typename T>
87 void save(T&& value) {
88 reserve((uint32_t)sizeof(T));
89
90 const auto cnt = m_dataPos + (uint32_t)sizeof(T);
91 if (cnt > m_data.size())
92 m_data.resize(cnt);
94 mem = GAIA_FWD(value);
95
96 m_dataPos += (uint32_t)sizeof(T);
97 }
98
103 void save_raw(const void* pSrc, uint32_t size, [[maybe_unused]] ser::serialization_type_id id) {
104 if (size == 0)
105 return;
106
107 reserve(size);
108
109 // Copy "size" bytes of raw data starting at pSrc
110 const auto cnt = m_dataPos + size;
111 if (cnt > m_data.size())
112 m_data.resize(cnt);
113 memcpy((void*)&m_data[m_dataPos], pSrc, size);
114
115 m_dataPos += size;
116 }
117
120 template <typename T>
121 void load(T& value) {
122 GAIA_ASSERT(m_dataPos + (uint32_t)sizeof(T) <= bytes());
123
124 const auto& cdata = std::as_const(m_data);
125 value = mem::unaligned_ref<T>((void*)&cdata[m_dataPos]);
126
127 m_dataPos += (uint32_t)sizeof(T);
128 }
129
134 void load_raw(void* pDst, uint32_t size, [[maybe_unused]] ser::serialization_type_id id) {
135 if (size == 0)
136 return;
137
138 GAIA_ASSERT(m_dataPos + size <= bytes());
139
140 const auto& cdata = std::as_const(m_data);
141 memmove(pDst, (const void*)&cdata[m_dataPos], size);
142
143 m_dataPos += size;
144 }
145 };
146 } // namespace detail
147
149 using ser_buffer_binary_storage_dyn = gaia::cnt::darray<uint8_t>;
150
153 class ser_buffer_binary: public detail::ser_buffer_binary_impl<ser_buffer_binary_storage> {};
154 class ser_buffer_binary_dyn: public detail::ser_buffer_binary_impl<ser_buffer_binary_storage_dyn> {};
155 } // namespace ser
156} // 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
Definition ser_buffer_binary.h:17
void seek(uint32_t pos)
Changes the current position in the buffer.
Definition ser_buffer_binary.h:69
GAIA_NODISCARD uint32_t tell() const
Returns the current position in the buffer.
Definition ser_buffer_binary.h:80
uint32_t m_dataPos
Current position in the buffer.
Definition ser_buffer_binary.h:25
GAIA_NODISCARD uint32_t bytes() const
Returns the number of bytes written in the buffer.
Definition ser_buffer_binary.h:34
GAIA_NODISCARD const auto * data() const
Returns the pointer to the data in the buffer.
Definition ser_buffer_binary.h:44
DataContainer m_data
Buffer holding raw data.
Definition ser_buffer_binary.h:23
void resize(uint32_t size)
Resizes the internal buffer to size bytes.
Definition ser_buffer_binary.h:63
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:50
void save(T &&value)
Writes value to the buffer.
Definition ser_buffer_binary.h:87
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:134
void load(T &value)
Loads value from the buffer.
Definition ser_buffer_binary.h:121
void skip(uint32_t size)
Advances size bytes from the current buffer position.
Definition ser_buffer_binary.h:75
GAIA_NODISCARD bool empty() const
Returns true if there is no data written in the buffer.
Definition ser_buffer_binary.h:39
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:103
Definition ser_buffer_binary.h:154
Minimal binary serializer meant to runtime data. It does not offer any versioning,...
Definition ser_buffer_binary.h:153
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9