1. 程式人生 > >datastream, C++資料序列化與反序列化容器

datastream, C++資料序列化與反序列化容器

#pragma once
/********************************************************************
purpose:    used for data serialization and deserialization
********************************************************************
*/
#include <vector>
#include <cassert>
class datastream:public std::vector<char>
{
public
:
    datastream& operator<<(bool data){return push(data);}
    datastream& operator<<(char data){return push(data);}
    datastream& operator<<(wchar_t data){return push(data);}
    datastream& operator<<(short data){return push(data);}
    datastream& operator
<<(int data){return push(data);}
    datastream& operator<<(long data){return push(data);}
    datastream& operator<<(float data){return push(data);}
    datastream& operator<<(double data){return push(data);}
    datastream& operator<<(unsigned char data){return
 push(data);}    
    datastream& operator<<(unsigned short data){return push(data);}    
    datastream& operator<<(unsigned int data){return push(data);}
    datastream& operator<<(unsigned long data){return push(data);}

    datastream& operator>>(bool& data){return pop(data);}
    datastream& operator>>(char& data){return pop(data);}
    datastream& operator>>(wchar_t& data){return pop(data);}
    datastream& operator>>(short& data){return pop(data);}
    datastream& operator>>(int& data){return pop(data);}
    datastream& operator>>(long& data){return pop(data);}
    datastream& operator>>(float& data){return pop(data);}
    datastream& operator>>(double& data){return pop(data);}
    datastream& operator>>(unsigned char& data){return pop(data);}    
    datastream& operator>>(unsigned short& data){return pop(data);}    
    datastream& operator>>(unsigned int& data){return pop(data);}
    datastream& operator>>(unsigned long& data){return pop(data);}

    datastream& test(bool& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(char& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(short& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(int& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(long& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(float& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(double& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(unsigned char& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(unsigned short& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(unsigned int& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}
    datastream& test(unsigned long& data){memcpy(&data, &operator[](0), sizeof(data));return *this;}    

    //std::string處理    datastream& operator<<(const char* data){return push(strlen(data), (void*)data);}
    datastream& operator<<(const wchar_t* data){return push(wcslen(data)<<1, (void*)data);}
    datastream& operator<<(const std::string& data){return push(data.size(), (void*)data.c_str());}    
    datastream& operator<<(const std::wstring& data){return push(data.size()<<1, (void*)data.c_str());}
    datastream& operator>>(std::string& data)
    {
        size_t nSize;
        pop(nSize);
        std::vector<char>::iterator first=begin(), last=first+nSize;
        data.assign(first, last);
        erase(first, last);
        return *this;
    }
    datastream& operator>>(std::wstring& data)
    {
        size_t nSize;
        pop(nSize);
        wchar_t* a=(wchar_t*)&operator[](0), *b=a+(nSize>>1);
        data.assign(a, b);
        std::vector<char>::iterator first=begin();    
        erase(first, first+nSize);
        return *this;
    }
    datastream& test(std::string& data)
    {
        size_t nSize;
        test(nSize);
        std::vector<char>::iterator first=begin()+sizeof(nSize);
        data.assign(first, first+nSize);
        return *this;
    }
    datastream& test(std::wstring& data)
    {
        size_t nSize;
        test(nSize);
        wchar_t* first=(wchar_t*)&operator[](sizeof(nSize));
        data.assign(first, first+(nSize>>1));
        return *this;
    }
    //std::vector處理    template<typename C>
    datastream& operator<<(const std::vector<C>& data)
    {
        size_t nSize=data.size();
        push(nSize);
        for (int i=0; i<nSize; ++i)
        {
            operator<<(data[i]);
        }
        return *this;
    }
    template<typename C>
    datastream& operator>>(std::vector<C>& data)
    {
        size_t nSize;
        pop(nSize);
        C tmp;
        for (int i=0; i<nSize; ++i)
        {
            operator>>(tmp);
            data.push_back(tmp);
        }            
        return *this;
    }
private:
    char* inc_size(size_t delta_size)
    {
        size_t last_size=size();
        resize(last_size+delta_size);
        return &operator[](last_size);
    }
    template<typename C>
    datastream& push(C data)
    {
        memcpy(inc_size(sizeof(data)), &data, sizeof(data));
        return *this;
    }
    datastream& push(size_t size, void* data)
    {
        push(size);
        memcpy(inc_size(size), data, size);
        return *this;
    }
    template<typename C>
    datastream& pop(C& data)
    {
        memcpy(&data, &operator[](0), sizeof(data));
        erase(begin(), begin()+sizeof(data));
        return *this;
    }
};