Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
ser_binary.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include "gaia/mem/mem_alloc.h"
5#include "gaia/ser/ser_buffer_binary.h"
6#include "gaia/ser/ser_rt.h"
7
8namespace gaia {
9 namespace ser {
12 class bin_stream {
13 ser_buffer_binary m_buffer;
14
16 void align(uint32_t size, serialization_type_id id) {
17 const auto pos = m_buffer.tell();
18 const auto posAligned = mem::align(pos, serialization_type_size(id, size));
19 const auto offset = posAligned - pos;
20 m_buffer.reserve(offset + size);
21 m_buffer.skip(offset);
22 }
23
24 public:
26 void save_raw(const void* src, uint32_t size, serialization_type_id id) {
27 align(size, id);
28 m_buffer.save_raw((const char*)src, size, id);
29 }
30
32 void load_raw(void* src, uint32_t size, serialization_type_id id) {
33 align(size, id);
34 m_buffer.load_raw((char*)src, size, id);
35 }
36
38 const char* data() const {
39 return (const char*)m_buffer.data();
40 }
41
43 void reset() {
44 m_buffer.reset();
45 }
46
48 uint32_t tell() const {
49 return m_buffer.tell();
50 }
51
53 uint32_t bytes() const {
54 return m_buffer.bytes();
55 }
56
58 void seek(uint32_t pos) {
59 m_buffer.seek(pos);
60 }
61
63 template <typename T>
64 void save(const T& data) {
65 auto s = make_serializer(*this);
66 s.save(data);
67 }
68
70 template <typename T>
71 void load(T& data) {
72 auto s = make_serializer(*this);
73 s.load(data);
74 }
75 };
76 } // namespace ser
77} // namespace gaia
Default in-memory binary backend used by ECS world/runtime serialization. Provides aligned raw read/w...
Definition ser_binary.h:12
void load_raw(void *src, uint32_t size, serialization_type_id id)
Reads raw bytes with type-aware alignment.
Definition ser_binary.h:32
void load(T &data)
Convenience typed load routed through serializer traversal.
Definition ser_binary.h:71
uint32_t tell() const
Returns current stream cursor position in bytes.
Definition ser_binary.h:48
void reset()
Clears buffered data and resets stream position.
Definition ser_binary.h:43
void save(const T &data)
Convenience typed save routed through serializer traversal.
Definition ser_binary.h:64
void save_raw(const void *src, uint32_t size, serialization_type_id id)
Writes raw bytes with type-aware alignment.
Definition ser_binary.h:26
uint32_t bytes() const
Returns total buffered byte count.
Definition ser_binary.h:53
void seek(uint32_t pos)
Moves stream cursor to an absolute byte position.
Definition ser_binary.h:58
const char * data() const
Returns pointer to serialized bytes.
Definition ser_binary.h:38
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
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
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 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 skip(uint32_t size)
Advances size bytes from the current buffer position.
Definition ser_buffer_binary.h:76
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
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