Advertisement
Guest User

Untitled

a guest
Jul 1st, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include<vector>
  4. #include<string>
  5. #include<sstream>
  6. #include <algorithm>
  7. #include "Company.h"
  8.  
  9. byte* serializeToMemory(std::string const& input,
  10.                         size_t& bytesWritten) {
  11.   std::istringstream iss(input);
  12.   std::vector<Company> companies;
  13.  
  14.   Company parsedCompany;
  15.   while (iss >> parsedCompany) {
  16.     companies.push_back(parsedCompany);
  17.  
  18.   }
  19.  
  20.   std::vector<byte> bytes;
  21.  
  22.   bytes.push_back(companies.size());
  23.  
  24.   for (const auto& company : companies) {
  25.     bytes.push_back(company.getId());
  26.  
  27.     for (char c : company.getName()) {
  28.       bytes.push_back((byte) c);
  29.     }
  30.     bytes.push_back('\0');
  31.  
  32.     auto employees = company.getEmployees();
  33.     bytes.push_back(employees.size());
  34.  
  35.     for (auto e : employees) {
  36.       bytes.push_back((byte) e.first);
  37.       bytes.push_back((byte) e.second);
  38.     }
  39.   }
  40.  
  41.   bytesWritten = bytes.size();
  42.   byte* memory = new byte[bytes.size()];
  43.  
  44.   std::copy(std::begin(bytes), std::end(bytes), memory);
  45.  
  46.   return memory;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement