R-Type
Serialization.hpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** Serialization.hpp
4 ** File description:
5 ** Serialization
6 */
7 
8 #ifndef SERIALIZATION_HPP_
9 #define SERIALIZATION_HPP_
10 
11 #include <asio.hpp>
12 #include <vector>
13 #include <cstddef>
14 #include <cstring>
15 
16 namespace Serialization
17 {
22  template <typename Data>
23  std::vector<std::byte> serializeData(Data data, std::size_t size)
24  {
25  std::vector<std::byte> byteArray(size);
26 
27  byteArray.reserve(size);
28  std::memmove(byteArray.data(), &data, size);
29  return byteArray;
30  }
36  template <typename Data>
37  Data deserializeData(asio::streambuf &buffer, std::size_t size)
38  {
39  Data data;
40 
41  std::memcpy(&data, buffer.data().data(), size);
42  return data;
43  }
48  inline std::vector<uint8_t> deserializeData(asio::streambuf &buffer, std::size_t size)
49  {
50  std::vector<uint8_t> byteArray(size);
51 
52  byteArray.reserve(size);
53  std::memcpy(byteArray.data(), buffer.data().data(), size);
54  return byteArray;
55  }
56 }; // namespace Serialization
57 
58 #endif /* !SERIALIZATION_HPP_ */
Definition: Serialization.hpp:17
Data deserializeData(asio::streambuf &buffer, std::size_t size)
Template to deserialize the data.
Definition: Serialization.hpp:37
std::vector< std::byte > serializeData(Data data, std::size_t size)
Template to serialize the data.
Definition: Serialization.hpp:23