Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
ser_common.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstdint>
5#include <type_traits>
6
7#include "gaia/core/utility.h"
8
9namespace gaia {
10 namespace ser {
12 enum class serialization_type_id : uint8_t {
14 ignore = 0,
15
17 s8 = 1,
19 u8 = 2,
21 s16 = 3,
23 u16 = 4,
25 s32 = 5,
27 u32 = 6,
29 s64 = 7,
31 u64 = 8,
32
34 b = 9,
35
37 c8 = 10,
39 c16 = 11,
41 c32 = 12,
42
44 f8 = 13,
46 f16 = 14,
48 f32 = 15,
50 f64 = 16,
51
53 special_begin = 17,
55 trivial_wrapper = special_begin,
57 data_and_size = 18,
58
60 Last = data_and_size,
61 };
62
67 inline uint32_t serialization_type_size(serialization_type_id id, uint32_t size) {
68 static constexpr uint32_t sizes[] = {
69 // Dummy
70 0, // ignore
71
72 // Integer types
73 1, // s8
74 1, // u8
75 2, // s16
76 2, // u16
77 4, // s32
78 4, // u32
79 8, // s64
80 8, // u64
81
82 // Boolean
83 1, // b
84
85 // Character types
86 1, // c8
87 2, // c16
88 4, // c32
89
90 // Floating point types
91 1, // f8
92 2, // f16
93 4, // f32
94 8, // f64
95
96 // Special
97 (uint32_t)-1, // trivial_wrapper, resolved from the size argument
98 sizeof(uintptr_t), // data_and_size, assume natural alignment
99 };
100
101 const auto s = id == serialization_type_id::trivial_wrapper ? size : sizes[(uint32_t)id];
102 GAIA_ASSERT(s != (uint32_t)-1);
103 return s;
104 }
105
108 template <typename T>
110 private:
111 static constexpr bool update() {
112 return std::is_enum_v<T> || std::is_fundamental_v<T> || std::is_trivially_copyable_v<T>;
113 }
114
115 public:
117 static constexpr bool value = update();
118 };
119
120 template <typename T>
122 std::disjunction<
123 std::is_same<T, char>, std::is_same<T, unsigned char>, //
124 std::is_same<T, int8_t>, std::is_same<T, uint8_t>, //
125 std::is_same<T, int16_t>, std::is_same<T, uint16_t>, //
126 std::is_same<T, int32_t>, std::is_same<T, uint32_t>, //
127 std::is_same<T, int64_t>, std::is_same<T, uint64_t>, //
128 std::is_same<T, size_t>, std::is_same<T, bool>> {};
129
130 template <typename T>
132 std::disjunction<
133 // std::is_same<T, float8_t>, //
134 // std::is_same<T, float16_t>, //
135 std::is_same<T, float>, //
136 std::is_same<T, double>, //
137 std::is_same<T, long double>> {};
138
142 template <typename T>
143 GAIA_NODISCARD constexpr serialization_type_id int_kind_id() {
144 static_assert(is_int_kind_id<T>::value, "Unsupported integral type");
145
146 if constexpr (std::is_same_v<char, T>) {
147 return serialization_type_id::s8;
148 } else if constexpr (std::is_same_v<unsigned char, T>) {
149 return serialization_type_id::u8;
150 } else if constexpr (std::is_same_v<int8_t, T>) {
151 return serialization_type_id::s8;
152 } else if constexpr (std::is_same_v<uint8_t, T>) {
153 return serialization_type_id::u8;
154 } else if constexpr (std::is_same_v<int16_t, T>) {
155 return serialization_type_id::s16;
156 } else if constexpr (std::is_same_v<uint16_t, T>) {
157 return serialization_type_id::u16;
158 } else if constexpr (std::is_same_v<int32_t, T>) {
159 return serialization_type_id::s32;
160 } else if constexpr (std::is_same_v<uint32_t, T>) {
161 return serialization_type_id::u32;
162 } else if constexpr (std::is_same_v<int64_t, T>) {
163 return serialization_type_id::s64;
164 } else if constexpr (std::is_same_v<uint64_t, T>) {
165 return serialization_type_id::u64;
166 } else if constexpr (std::is_same_v<size_t, T>) {
167 return serialization_type_id::u64;
168 } else { // if constexpr (std::is_same_v<bool, T>) {
169 return serialization_type_id::b;
170 }
171 }
172
176 template <typename T>
177 GAIA_NODISCARD constexpr serialization_type_id flt_type_id() {
178 static_assert(is_flt_kind_id<T>::value, "Unsupported floating type");
179
180 // if constexpr (std::is_same_v<float8_t, T>) {
181 // return serialization_type_id::f8;
182 // } else if constexpr (std::is_same_v<float16_t, T>) {
183 // return serialization_type_id::f16;
184 // } else
185 if constexpr (std::is_same_v<float, T>) {
186 return serialization_type_id::f32;
187 } else { // if constexpr (std::is_same_v<double, T>) {
188 return serialization_type_id::f64;
189 }
190 }
191
195 template <typename T>
196 GAIA_NODISCARD constexpr serialization_type_id type_id() {
197 if constexpr (std::is_enum_v<T>)
198 return int_kind_id<std::underlying_type_t<T>>();
199 else if constexpr (std::is_integral_v<T>)
200 return int_kind_id<T>();
201 else if constexpr (std::is_floating_point_v<T>)
202 return flt_type_id<T>();
203 else if constexpr (std::is_pointer_v<T>)
204 return serialization_type_id::data_and_size;
205 else if constexpr (core::has_size_begin_end<T>::value)
206 return serialization_type_id::data_and_size;
207 else if constexpr (std::is_class_v<T>)
208 return serialization_type_id::trivial_wrapper;
209 else
210 return serialization_type_id::ignore;
211 }
212
214
215 GAIA_DEFINE_HAS_MEMBER_FUNC(save);
216 GAIA_DEFINE_HAS_MEMBER_FUNC(load);
217 GAIA_DEFINE_HAS_MEMBER_FUNC(resize);
218
219 // --------------------
220 // Customization tags
221 // --------------------
222
223 struct save_tag {};
224 struct load_tag {};
225 inline constexpr save_tag save_v{};
226 inline constexpr load_tag load_v{};
227
228 // --------------------
229 // Detection traits
230 // --------------------
231
232 template <typename S, typename T>
233 auto has_tag_save_impl(int)
234 -> decltype(tag_invoke(save_v, std::declval<S&>(), std::declval<const T&>()), std::true_type{});
235 template <typename, typename>
236 std::false_type has_tag_save_impl(...);
237 template <typename S, typename T>
238 using has_tag_save = decltype(has_tag_save_impl<S, T>(0));
239
240 template <typename S, typename T>
241 auto has_tag_load_impl(int)
242 -> decltype(tag_invoke(load_v, std::declval<S&>(), std::declval<T&>()), std::true_type{});
243 template <typename, typename>
244 std::false_type has_tag_load_impl(...);
245 template <typename S, typename T>
246 using has_tag_load = decltype(has_tag_load_impl<S, T>(0));
248 } // namespace ser
249} // namespace gaia
Definition ser_common.h:137
Definition ser_common.h:128
Reports whether a type may be serialized by copying its representation.
Definition ser_common.h:109
static constexpr bool value
True when the type can be serialized trivially.
Definition ser_common.h:117