Advertisement
Guest User

BinarySerializer.h

a guest
Jun 19th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #ifndef _MSL_H_
  2. #define _MSL_H_
  3.  
  4. #include <cassert>
  5. #include <fstream>
  6. #include <vector>
  7.  
  8. class ISerializable;
  9.  
  10. class BinarySerializer
  11. {
  12. public:
  13.     BinarySerializer(const std::string& filename) : mode(0), filename(filename) { }
  14.     ~BinarySerializer();
  15.    
  16.     void closeFiles();
  17.     void serialize();
  18.     void deserialize();
  19.  
  20.     unsigned getMode() { return mode; }
  21.     void addData(const char* ptr, unsigned size);
  22.     void addData(const ISerializable& ptr);
  23.     void readData(void* ptr, unsigned size);
  24.  
  25.     // operators
  26.     // <<
  27.     template <class T>
  28.     BinarySerializer& operator<<(const std::vector<T*>& vec)
  29.     {
  30.         std::vector<ISerializable*> cvec;
  31.         cvec.assign(vec.begin(), vec.end());
  32.  
  33.         *this << vec.size();
  34.  
  35.         for (std::vector<ISerializable*>::const_iterator itr = cvec.begin(); itr != cvec.end(); itr++)
  36.             (*itr)->Serialize(this);
  37.        
  38.         return *this;
  39.     }
  40.  
  41.     BinarySerializer& operator<<(const std::vector<ISerializable*>& vec);
  42.     BinarySerializer& operator<<(const ISerializable& ptr);
  43.     BinarySerializer& operator<<(const std::string& str);
  44.     BinarySerializer& operator<<(const int& i);
  45.     BinarySerializer& operator<<(const unsigned int& ui);
  46.     BinarySerializer& operator<<(const short int& si);
  47.     BinarySerializer& operator<<(const unsigned short& us);
  48.     BinarySerializer& operator<<(const long long& ll);
  49.     BinarySerializer& operator<<(const float& f);
  50.     BinarySerializer& operator<<(const double& d);
  51.  
  52.     // >>
  53.     template <class T>
  54.     BinarySerializer& operator>>(std::vector<T*>& vec)
  55.     {
  56.         unsigned size;
  57.         T* obj;
  58.  
  59.         *this >> size;
  60.  
  61.         for (unsigned i = 0; i < size; i++)
  62.         {
  63.             obj = new T();
  64.             *this >> *obj;
  65.  
  66.             vec.push_back(obj);
  67.         }
  68.  
  69.         return *this;
  70.     }
  71.  
  72.     template <class T>
  73.     BinarySerializer& operator>>(T** ptr)
  74.     {
  75.         T* obj = new T();
  76.         *this >> *obj;
  77.  
  78.         *ptr = obj;
  79.  
  80.         return *this;
  81.     }
  82.  
  83.     BinarySerializer& operator>>(const ISerializable& ptr);
  84.     BinarySerializer& operator>>(std::string& str);
  85.     BinarySerializer& operator>>(int& i);
  86.     BinarySerializer& operator>>(unsigned int& ui);
  87.     BinarySerializer& operator>>(short int& si);
  88.     BinarySerializer& operator>>(unsigned short& us);
  89.     BinarySerializer& operator>>(long long& ll);
  90.     BinarySerializer& operator>>(float& f);
  91.     BinarySerializer& operator>>(double& d);
  92.  
  93. protected:
  94.     // 0 = inactive
  95.     // 1 = serialize
  96.     // 2 = deserialize
  97.     unsigned mode;
  98.  
  99.     std::string filename;
  100.     std::ofstream ofs;
  101.     std::ifstream ifs;
  102. };
  103.  
  104. class ISerializable
  105. {
  106. protected:
  107.     friend class BinarySerializer;
  108.  
  109.     virtual ~ISerializable() {}
  110.     virtual void Serialize(BinarySerializer* sds)=0;
  111.     virtual void Deserialize(BinarySerializer* sds)=0;
  112.  
  113. protected:
  114.     ISerializable() { }
  115. };
  116.  
  117. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement