36 #include <type_traits>
40 #include <glm/glm.hpp>
41 #include "eng3d/utils.hpp"
42 #include "eng3d/string.hpp"
43 #include "eng3d/rectangle.hpp"
44 #include "eng3d/freelist.hpp"
57 virtual const char*
what() const noexcept {
58 return buffer.c_str();
67 void to_file(const ::std::string& path);
68 void from_file(const ::std::string& path);
85 return static_cast<const void*
>(&
buffer[0]);
101 template<
bool is_const,
typename T>
112 template<
bool is_const>
114 #ifdef DEBUG_SERIALIZER
115 template<
bool is_serialize = true>
120 template<
bool is_serialize = false>
130 template<
bool is_serialize,
typename T>
133 template<
bool is_serialize = true,
typename T>
138 template<
bool is_serialize = false,
typename T>
163 template<
bool is_const>
166 template<
bool is_serialize>
168 if constexpr(is_serialize) ar.
copy_from(&obj,
sizeof(T));
169 else ar.
copy_to(&obj,
sizeof(T));
175 template<SerializerScalar T>
177 constexpr
static auto scaling = 1000.f;
179 template<
bool is_const>
182 template<
bool is_serialize>
184 if constexpr(std::is_floating_point_v<T>) {
185 auto tmp =
static_cast<int32_t
>(obj * scaling);
186 Eng3D::Deser::deser_dynamic<is_serialize>(ar, tmp);
187 if constexpr(!is_serialize)
188 obj =
static_cast<T
>(tmp) * (1.f / scaling);
191 obj = ::std::byteswap<T>(obj);
194 obj = ::std::byteswap<T>(obj);
201 #if defined(__GNUC__) && !defined(__clang__) && !defined(__llvm__)
202 requires std::destructible<typename T::value_type>;
203 requires std::forward_iterator<typename T::iterator>;
205 { a.begin() } -> std::same_as<typename T::iterator>;
206 { a.end() } -> std::same_as<typename T::iterator>;
210 template<SerializerContainer T>
211 struct Serializer<T> {
212 static constexpr
auto max_elements = 163550 * 32;
214 template<
bool is_const>
217 template<
bool is_serialize>
219 uint32_t len = obj_group.size();
220 Eng3D::Deser::deser_dynamic<is_serialize>(ar, len);
223 if(len >= max_elements)
226 if constexpr(is_serialize) {
227 for(
auto& obj : obj_group)
228 Eng3D::Deser::deser_dynamic<true>(ar, obj);
230 if constexpr(requires(T a) { a.clear(); })
234 constexpr
bool has_insert = requires(T a,
typename T::value_type tp) { a.insert(tp); };
235 constexpr
bool has_resize = requires(T a,
size_t n) { a.resize(n); };
236 if constexpr(!has_insert && !has_resize) {
237 for(decltype(len) i = 0; i < len; i++)
238 Eng3D::Deser::deser_dynamic<false>(ar, obj_group[i]);
240 if constexpr(has_resize) {
241 obj_group.resize(len);
242 for(decltype(len) i = 0; i < len; i++)
243 Eng3D::Deser::deser_dynamic<false>(ar, obj_group[i]);
245 for(decltype(len) i = 0; i < len; i++) {
246 typename T::value_type obj{};
247 Eng3D::Deser::deser_dynamic<false>(ar, obj);
248 constexpr
bool has_push_back = requires(T a,
typename T::value_type tp) { a.push_back(tp); };
249 if constexpr(has_push_back)
250 obj_group.push_back(obj);
251 else if constexpr(has_insert)
252 obj_group.insert(obj);
264 template<
bool is_const>
267 template<
bool is_serialize>
269 auto tmp =
static_cast<char>(obj);
270 Eng3D::Deser::deser_dynamic<is_serialize>(ar, tmp);
271 if constexpr(!is_serialize) {
278 template<
typename T,
typename U>
280 template<
bool is_const>
283 template<
bool is_serialize>
285 using tp_1 = ::std::remove_const_t<T>;
286 using tp_2 = ::std::remove_const_t<U>;
287 Eng3D::Deser::deser_dynamic<is_serialize>(ar,
const_cast<tp_1&
>(obj.first));
288 Eng3D::Deser::deser_dynamic<is_serialize>(ar,
const_cast<tp_2&
>(obj.second));
292 template<
typename T,
int N>
294 template<
bool is_const>
297 template<
bool is_serialize>
299 unsigned long num = obj_group.to_ulong();
300 if constexpr(is_serialize) {
301 Eng3D::Deser::deser_dynamic<is_serialize>(ar, num);
303 Eng3D::Deser::deser_dynamic<is_serialize>(ar, num);
308 template<
size_t bits>
313 template<
bool is_const>
316 template<
bool is_serialize>
318 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.id);
324 template<
bool is_const>
327 template<
bool is_serialize>
329 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.left);
330 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.right);
331 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.top);
332 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.bottom);
338 template<
bool is_const>
341 template<
bool is_serialize>
343 if constexpr(is_serialize) {
344 auto has_value = obj.has_value();
345 Eng3D::Deser::deser_dynamic<is_serialize>(ar, has_value);
347 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.value());
349 bool has_value =
false;
350 Eng3D::Deser::deser_dynamic<is_serialize>(ar, has_value);
353 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.value());
361 template<
bool is_const>
364 template<
bool is_serialize>
366 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.data);
367 Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.slots);
The purpouse of the serializer is to serialize objects onto a byte stream that can be transfered onto...
Exception(const ::std::string &msg)
virtual const char * what() const noexcept
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj_group)
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
typename CondConstType< is_const, T >::type type
void deser_dynamic(Eng3D::Deser::Archive &ar, T &obj)
Template generic (de)-serializer.
concept SerializerContainer
void deserialize(Eng3D::Deser::Archive &ar, T &obj)
void serialize(Eng3D::Deser::Archive &ar, const T &obj)
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...
::std::vector< uint8_t > buffer
void set_buffer(const void *buf, size_t size)
void to_file(const ::std::string &path)
void from_file(const ::std::string &path)
void expand(size_t amount)
const void * get_buffer()
void copy_from(const void *ptr, size_t size)
void copy_to(void *ptr, size_t size)
::std::remove_reference_t< T > type
const ::std::remove_reference_t< T > type
typename CondConstType< is_const, Eng3D::Freelist< T > >::type type
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
typename CondConstType< is_const, Eng3D::Rectangle >::type type
typename CondConstType< is_const, Eng3D::StringRef >::type type
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
typename CondConstType< is_const, bool >::type type
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
typename CondConstType< is_const, std::optional< T > >::type type
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
typename CondConstType< is_const, ::std::pair< T, U > >::type type
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj_group)
typename CondConstType< is_const, T >::type type
A serializer (base class) which can be used to serialize objects and create per-object optimized clas...
typename CondConstType< is_const, T >::type type
A serializer optimized to memcpy directly the element into the byte stream use only when the object c...
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
typename CondConstType< is_const, T >::type type
A reference to a string on the global string pool.
#define CXX_THROW(class,...)