shushus

Спринт 15. Сериализация: бинарная и текстовая.

May 31st, 2023
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include "saveload.h"
  5.  
  6. #include <cassert>
  7. #include <map>
  8. #include <sstream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. void TestSaveLoad() {
  14.    
  15.     map<uint32_t, string> m = {{1, "hello"}, {2, "bye"},};
  16.     stringstream ss;
  17.     Serialize(m, ss);
  18.     auto s = ss.str();
  19.  
  20.     assert(s.size() == 40);
  21.  
  22.     assert(s[0] == 0x02);
  23.     assert(s[1] == 0x00);
  24.     assert(s[2] == 0x00);
  25.     assert(s[3] == 0x00);
  26.     assert(s[4] == 0x00);
  27.     assert(s[5] == 0x00);
  28.     assert(s[6] == 0x00);
  29.     assert(s[7] == 0x00);
  30.  
  31.     assert(s[8] == 0x01);
  32.     assert(s[9] == 0x00);
  33.     assert(s[10] == 0x00);
  34.     assert(s[11] == 0x00);
  35.  
  36.     assert(s[12] == 0x05);
  37.     assert(s[13] == 0x00);
  38.     assert(s[14] == 0x00);
  39.     assert(s[15] == 0x00);
  40.     assert(s[16] == 0x00);
  41.     assert(s[17] == 0x00);
  42.     assert(s[18] == 0x00);
  43.     assert(s[19] == 0x00);
  44.  
  45.     assert(s[20] == 0x68);  // 'h'
  46.     assert(s[21] == 0x65);  // 'e'
  47.     assert(s[22] == 0x6c);  // 'l'
  48.     assert(s[23] == 0x6c);  // 'l'
  49.     assert(s[24] == 0x6f);  // 'o'
  50.  
  51.     assert(s[25] == 0x02);
  52.     assert(s[26] == 0x00);
  53.     assert(s[27] == 0x00);
  54.     assert(s[28] == 0x00);
  55.  
  56.     assert(s[29] == 0x03);
  57.     assert(s[30] == 0x00);
  58.     assert(s[31] == 0x00);
  59.     assert(s[32] == 0x00);
  60.     assert(s[33] == 0x00);
  61.     assert(s[34] == 0x00);
  62.     assert(s[35] == 0x00);
  63.     assert(s[36] == 0x00);
  64.  
  65.     assert(s[37] == 0x62);  // 'b'
  66.     assert(s[38] == 0x79);  // 'y'
  67.     assert(s[39] == 0x65);  // 'e'
  68.  
  69.     map<uint32_t, string> m2;
  70.     Deserialize(ss, m2);
  71.  
  72.     assert(m == m2);
  73. }
  74.  
  75. int main() {
  76.     TestSaveLoad();
  77. }
  78. -----------------------------------------------------------------------
  79. saveload.h
  80. -----------------------------------------------------------------------
  81. #include <iostream>
  82. #include <map>
  83. #include <string>
  84. #include <vector>
  85.  
  86. template <typename T>
  87. void Serialize(T val, std::ostream& out) {
  88.     out.write(reinterpret_cast<char*>(&val), sizeof(val));
  89. }
  90.  
  91. inline void Serialize(const std::string& str, std::ostream& out) {    
  92.     Serialize(str.size(), out);
  93.    
  94.     out.write(reinterpret_cast<const char*>(str.data()), str.size());
  95. }
  96.  
  97. template <typename T>
  98. void Serialize(const std::vector<T>& val, std::ostream& out) {    
  99.     Serialize(val.size(), out);
  100.    
  101.     for (const T& item : val) {
  102.         Serialize(item, out);
  103.     }
  104. }
  105.  
  106. template <typename T1, typename T2>
  107. void Serialize(const std::map<T1, T2>& val, std::ostream& out) {
  108.     Serialize(val.size(), out);
  109.    
  110.     for (const auto& [key, value] : val) {
  111.         Serialize(key, out);
  112.         Serialize(value, out);
  113.     }
  114. }
  115.  
  116. template <typename T>
  117. void Deserialize(std::istream& in, T& val) {
  118.     in.read(reinterpret_cast<char*>(&val), sizeof(val));
  119. }
  120.  
  121. inline void Deserialize(std::istream& in, std::string& str) {  
  122.     size_t size_;
  123.     Deserialize(in, size_);
  124.    
  125.     str.resize(size_);          
  126.     in.read(reinterpret_cast<char*>(str.data()), str.size());
  127. }
  128.  
  129. template <typename T>
  130. void Deserialize(std::istream& in, std::vector<T>& val) {
  131.     size_t size_;
  132.     Deserialize(in, size_);    
  133.    
  134.     val.resize(size_);  
  135.     for (auto& item : val) {
  136.         Deserialize(in, item);
  137.     }
  138. }
  139.  
  140. template <typename T1, typename T2>
  141. void Deserialize(std::istream& in, std::map<T1, T2>& val) {
  142.     size_t size_;
  143.     Deserialize(in, size_);
  144.  
  145.     for (size_t i = 0; i < size_; ++i) {
  146.    
  147.         T1 key;
  148.         T2 value;
  149.        
  150.         Deserialize(in, key);
  151.         Deserialize(in, value);
  152.        
  153.         val.emplace(key, value);
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment