SpaceCreator

serializer

Apr 6th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.78 KB | None | 0 0
  1. #include <string.h>
  2. #include<iostream>
  3. #include <sstream>
  4.  
  5. enum class Error
  6. {
  7.     NoError,
  8.     CorruptedArchive,
  9.     IncorrectType
  10. };
  11.  
  12.  
  13. struct Data
  14. {
  15.     uint64_t a;
  16.     bool b;
  17.     uint64_t c;
  18.  
  19.     template <class Serializer>
  20.     Error serialize(Serializer& serializer)
  21.     {
  22.         return serializer(a, b, c);
  23.     }
  24.  
  25.     template <class Deserializer>
  26.     Error deserialize(Deserializer& deserializer)
  27.     {
  28.         return deserializer(a, b, c);
  29.         }
  30.   // serializer.h
  31. //#pragma once
  32.  
  33. };
  34. template <class T1, class T2>
  35. struct IsSame
  36. {
  37.     static constexpr bool value = false;
  38. };
  39.  
  40. template <class T>
  41. struct IsSame<T, T>
  42. {
  43.     static constexpr bool value = true;
  44. };
  45.  
  46. class Serializer
  47. {
  48.     std::ostream &out_;
  49.     static constexpr char Separator = ' ';
  50.  
  51. public:
  52.     explicit Serializer(std::ostream& out)
  53.         : out_(out)
  54.     {
  55.     }
  56.  
  57.     template <class T>
  58.     Error save(T& object)
  59.     {
  60.         return object.serialize(*this);
  61.     }
  62.  
  63.     template <class... ArgsT>
  64.     Error operator()(ArgsT... args)
  65.     {
  66.         return process(args...);
  67.     }
  68.  
  69. private:
  70.  
  71.     template<class T>
  72.     Error process(T value){
  73.         if(IsSame<bool,decltype(value)>::value){
  74.             out_ << (value ? "true" : "false") << Separator;
  75.             return Error::NoError;
  76.         } else if(IsSame<uint64_t,decltype(value)>::value){
  77.             out_ << value << Separator;
  78.             return Error::NoError;
  79.         } else{
  80.             return Error::IncorrectType;
  81.         }
  82.     }
  83.  
  84.     template <class T, class... ArgsT>
  85.     Error process(T value, ArgsT... args){
  86.         if(IsSame<bool,decltype(value)>::value){
  87.             out_ << (value ? "true" : "false") << Separator;
  88.         } else if(IsSame<uint64_t,decltype(value)>::value){
  89.             out_ << value << Separator;
  90.         } else{
  91.             return Error::IncorrectType;
  92.         }
  93.         return process(args...);
  94.     }
  95.     // process использует variadic templates
  96. };
  97. /////////////////////////////////////////////////////////////////////////////////
  98. //-------------------------------------------------------------------------------
  99. /////////////////////////////////////////////////////////////////////////////////
  100. //-------------------------------------------------------------------------------
  101. /////////////////////////////////////////////////////////////////////////////////
  102.  
  103. class Deserializer
  104. {
  105.     std::istream &in_;
  106.     static constexpr char Separator = ' ';
  107.  
  108.     static bool isNumber(const std::string &str){
  109.         for(auto ch : str){
  110.             //std::cout<< ch;
  111.             if((ch < 48 )||(ch > 57)){
  112.                 return false;
  113.             }
  114.             return true;
  115.         }
  116.     }
  117.  
  118. public:
  119.     explicit Deserializer(std::istream& in)
  120.         : in_(in)
  121.     {
  122.     }
  123.  
  124.     template <class T>
  125.     Error load(T& object)
  126.     {
  127.         return object.deserialize(*this);
  128.     }
  129.  
  130.     template <class... ArgsT>
  131.     Error operator()(ArgsT... args)
  132.     {
  133.         return process(args...);
  134.     }
  135.  
  136. private:
  137.  
  138.     template<class T>
  139.     Error process(T &value){
  140.  
  141.         std::string str;
  142.         std::stringstream str_stream;
  143.         uint64_t temp;
  144.         if(IsSame<bool&,decltype(value)>::value){
  145.             in_>> str;
  146.             if(str == "true"){
  147.                 value =  true;
  148.                 return Error :: NoError;
  149.             } else if(str == "false"){
  150.                 value = false;
  151.                 return Error :: NoError;
  152.             } else{
  153.                 return Error::CorruptedArchive;
  154.             }
  155.         } else if(IsSame<uint64_t&,decltype(value)>::value){
  156.             std::cout<< "uint64_t"<<std::endl;
  157.             in_>> str;
  158.             std::cout<< "Str : "<<str<<std::endl;
  159.             if(isNumber(str)){
  160.                 std::cout << "True"<<std::endl;
  161.                 str_stream << str << Separator;
  162.                 str_stream >> temp;
  163.                 std::cout<< temp <<std::endl;
  164.                 value = temp;
  165.                 return Error :: NoError;
  166.             } else{
  167.                 return Error::CorruptedArchive;
  168.             }
  169.         } else{
  170.             return Error::IncorrectType;
  171.         }
  172.     }
  173.  
  174.     template <class T, class... ArgsT>
  175.     Error process(T &value, ArgsT&... args){
  176.         std::string str;
  177.         std::stringstream str_stream;
  178.         uint64_t temp ;
  179.         if(IsSame<bool&,decltype(value)>::value){
  180.             std::cout<< "Bool"<<std::endl;
  181.             in_>> str;
  182.             if(str == "true"){
  183.                 value =  true;
  184.                 return process(args...);
  185.             } else if(str == "false"){
  186.                 value = false;
  187.                 return process(args...);
  188.             } else{
  189.                 return Error::CorruptedArchive;
  190.             }
  191.         } else if(IsSame<uint64_t&,decltype(value)>::value){
  192.             std::cout<< "uint64_t"<<std::endl;
  193.             in_>> str;
  194.             std::cout<< "Str : "<<str<<std::endl;
  195.             if(isNumber(str)){
  196.                 std::cout << "True"<<std::endl;
  197.                 str_stream << str << Separator;
  198.                 str_stream >> temp;
  199.                 std::cout<< temp<<std::endl;
  200.                 value = temp;
  201.                 return process(args...);
  202.             } else{
  203.                 return Error::CorruptedArchive;
  204.             }
  205.         } else{
  206.             return Error::IncorrectType;
  207.         }
  208.         return process(args...);
  209.     }
  210.     // process использует variadic templates
  211. };
  212.  int main(int argc, char const *argv[]) {
  213.      int a  = 5;
  214.      Data x { 1, true, 2};
  215.      Data y {1, false, 4};
  216.      Error res;
  217.  
  218.      std::stringstream save_stream;
  219.      
  220.  
  221.      Serializer serializer(save_stream);
  222.      Deserializer deserializer(save_stream);
  223.      res = serializer.save(x);
  224.      switch (res) {
  225.          case Error::NoError:{
  226.              std::cout<< "NoError"<<std::endl;
  227.              break;
  228.          }
  229.          case Error::IncorrectType:{
  230.              std::cout<< "IncorrectType"<<std::endl;
  231.              break;
  232.          }
  233.          case Error::CorruptedArchive:{
  234.              std::cout<< "CorruptedArchive"<<std::endl;
  235.              break;
  236.          }
  237.      }
  238.      std::string result;
  239.      y.c = 5;
  240.      res = deserializer.load(y);
  241.      switch (res) {
  242.          case Error::NoError:{
  243.              std::cout<< "NoError"<<std::endl;
  244.              break;
  245.          }
  246.          case Error::IncorrectType:{
  247.              std::cout<< "IncorrectType"<<std::endl;
  248.              break;
  249.          }
  250.          case Error::CorruptedArchive:{
  251.              std::cout<< "CorruptedArchive"<<std::endl;
  252.              break;
  253.          }
  254.      }
  255.      std::cout<< "y.c = "<< y.c<<std::endl;
  256.      res = serializer.save(y);
  257.      while(save_stream >> result){
  258.         std::cout<< result +' ';
  259.      }
  260.  
  261.      return 0;
  262.  }
Advertisement
Add Comment
Please, Sign In to add comment