Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <type_traits>
  4. #include <thread>
  5. #include <cstdint>
  6. #include <tuple>
  7. #include <vector>
  8.  
  9. using buf_t = std::vector<unsigned char>;
  10.  
  11. template <typename T>
  12. std::size_t binary_serialize(buf_t& buf, std::size_t index, const T& variable)
  13. {
  14.     std::copy_n(reinterpret_cast<const unsigned char*>(&variable), sizeof(T), &buf[index]);
  15.     return index + sizeof(T);
  16. }
  17.  
  18. template <typename T>
  19. std::size_t binary_deserialize(const buf_t& buf, std::size_t index, T* variable)
  20. {
  21.     *variable = *reinterpret_cast<const T*>(&buf[index]);
  22.     return index + sizeof(T);
  23. }
  24.  
  25. class Message {
  26. public:
  27.     static buf_t serialize(const Message& msg)
  28.     {
  29.         buf_t buf;
  30.         buf.resize(sizeof(a_variable) + sizeof(b_variable));
  31.         std::size_t index = 0;
  32.  
  33.         index = binary_serialize(buf, index, msg.a_variable);
  34.         index = binary_serialize(buf, index, msg.b_variable);
  35.  
  36.         return buf;
  37.     }
  38.  
  39.     static Message deserialize(const buf_t& buf)
  40.     {
  41.         Message msg;
  42.         std::size_t index = 0;
  43.  
  44.         index = binary_deserialize(buf, index, &msg.a_variable);
  45.         index = binary_deserialize(buf, index, &msg.b_variable);
  46.  
  47.         return msg;
  48.     }
  49.  
  50.     int a_variable, b_variable;
  51. };
  52.  
  53. class LoginMessage : public Message {
  54. public:
  55.     static buf_t serialize(const LoginMessage& msg)
  56.     {
  57.         buf_t buf = Message::serialize(msg);
  58.         std::size_t index = buf.size();
  59.         buf.resize(buf.size() + sizeof(c_variable));
  60.  
  61.         index = binary_serialize(buf, index, msg.c_variable);
  62.  
  63.         return buf;
  64.     }
  65.  
  66.     static LoginMessage deserialize(const buf_t& buf)
  67.     {
  68.         LoginMessage msg;
  69.         static_cast<Message&>(msg) = Message::deserialize(buf);
  70.         std::size_t index = buf.size() - sizeof(c_variable);
  71.  
  72.         index = binary_deserialize(buf, index, &msg.c_variable);
  73.  
  74.         return msg;
  75.     }
  76.  
  77.     float c_variable;
  78. };
  79.  
  80. int main(int argc, char* argv[])
  81. {
  82.     (void)argc;
  83.     (void)argv;
  84.  
  85.     LoginMessage msg;
  86.     msg.a_variable = 1;
  87.     msg.b_variable = 2;
  88.     msg.c_variable = 3.1f;
  89.  
  90.     buf_t buf = LoginMessage::serialize(msg);
  91.     msg = LoginMessage::deserialize(buf);
  92.  
  93.     std::cout << msg.a_variable << " " << msg.b_variable << " " << msg.c_variable << std::endl;
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement