Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
ser_ct.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <type_traits>
5#include <utility>
6
7#include "gaia/core/utility.h"
8#include "gaia/ser/impl/ser_dispatch.h"
9#include "gaia/ser/ser_common.h"
10
11namespace gaia {
12 namespace ser {
17 namespace detail {
18 template <typename Writer, typename T>
19 void save_one(Writer& s, const T& arg) {
20 auto saveTrivial = [](auto& writer, const auto& value) {
21 writer.save(value);
22 };
23 save_dispatch(s, arg, saveTrivial);
24 }
25
26 template <typename Reader, typename T>
27 void load_one(Reader& s, T& arg) {
28 auto loadTrivial = [](auto& reader, auto& value) {
29 reader.load(value);
30 };
31 load_dispatch(s, arg, loadTrivial);
32 }
33
34#if GAIA_ASSERT_ENABLED
35 template <typename Writer, typename T>
36 void check_one(Writer& s, const T& arg) {
37 T tmp{};
38
39 // Make sure that we write just as many bytes as we read.
40 // If the positions are the same there is a good chance that save and load match.
41 const auto pos0 = s.tell();
42 save_one(s, arg);
43 const auto pos1 = s.tell();
44 s.seek(pos0);
45 load_one(s, tmp);
46 GAIA_ASSERT(s.tell() == pos1);
47
48 // Return back to the original position in the buffer.
49 s.seek(pos0);
50 }
51#endif
52
55 uint32_t m_pos = 0;
56
57 public:
58 template <typename T>
59 void save(const T&) {
60 m_pos += (uint32_t)sizeof(T);
61 }
62
63 void save_raw(const void*, uint32_t size, [[maybe_unused]] ser::serialization_type_id id) {
64 m_pos += size;
65 }
66
67 void seek(uint32_t pos) {
68 m_pos = pos;
69 }
70
71 GAIA_NODISCARD uint32_t tell() const {
72 return m_pos;
73 }
74 };
75 } // namespace detail
76
79 template <typename T>
80 GAIA_NODISCARD uint32_t bytes(const T& data) {
82 detail::save_one(counter, data);
83 return counter.tell();
84 }
85
92 template <typename Writer, typename T>
93 void save(Writer& writer, const T& data) {
94 detail::save_one(writer, data);
95 }
96
103 template <typename Reader, typename T>
104 void load(Reader& reader, T& data) {
105 detail::load_one(reader, data);
106 }
107
108#if GAIA_ASSERT_ENABLED
116 template <typename Writer, typename T>
117 void check(Writer& writer, const T& data) {
118 detail::check_one(writer, data);
119 }
120#endif
121 } // namespace ser
122} // namespace gaia
Minimal writer used by ser::bytes to count produced bytes without storing data.
Definition ser_ct.h:54
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9