• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            清風竹林

            ぷ雪飄絳梅映殘紅
               ぷ花舞霜飛映蒼松
                 ----- Do more,suffer less

            datastream, C++數據序列化與反序列化容器

              其實本來不想自己寫這樣一個類的,但找來找去都沒有找到符合生活習慣的,于是在經過反復的推敲設計后,嗚,我終于承認自己功力不夠---無法將內存管理與迭代器完美地結合在一起,只好只好先用std::vector了,于是便有了下面的代碼。到目前為止還不能號稱功能完善,但,但應該基本可用了,堪堪記在這里,也許能給以后的自己一些幫助,或可得到高手指點一二:
              類文件被保存為datastream.hpp
            #pragma once
            /********************************************************************
            created:    2008-10-13
            author:        lixianmin

            purpose:    used for data serialization and deserialization
            Copyright (C) 2008 - All Rights Reserved
            ********************************************************************
            */
            #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;
                }
            };
              測試代碼:
            #include <iostream>
            #include 
            <fstream>
            #include 
            <iterator>
            #include 
            "datastream.hpp"

            template
            <typename T>
            void print_vector(const std::vector<T>& v)
            {
                std::cout
            <<"---------------------------Print Vector---------------------------"<<std::endl;
                std::copy(v.begin(), v.end(), std::ostream_iterator
            <T>(std::cout, "\n"));
                std::cout
            <<"-------------------------Print Vector End-------------------------"<<std::endl;
            }
            int main(int argc, char* argv[])
            {
                std::
            string strPath="c:\\abc.txt";

                std::vector
            <std::string> v;
                v.push_back(
            "hello world");
                v.push_back(
            "向量測試");
                
                datastream ds;
                ds
            <<318<<3.14159<<"hello world"<<std::string("中文測試")<<v;

                std::ofstream outfile(strPath.c_str());
                
            if (outfile)
                {
                    std::copy(ds.begin(), ds.end(), std::ostreambuf_iterator
            <char>(outfile));
                    outfile.close();
                    std::cout
            <<"write to file successfully!"<<std::endl;
                }
                
                ds.clear();                            
            //清空數據(但不釋放ds的內存)
                std::ifstream infile(strPath.c_str());
                
            if (infile)
                {
                    ds.assign(std::istreambuf_iterator
            <char>(infile), std::istreambuf_iterator<char>());    //別忘了std::vector有個assign函數
                    int a;
                    
            double b;
                    std::
            string c, d;
                    std::vector
            <std::string> v2;
                    ds
            >>a>>b>>c>>d>>v2;
                    datastream(ds).swap(ds);        
            //釋放datastream內存
                    std::cout<<"a="<<a<<", b="<<b<<", c="<<c<<", d="<<d<<std::endl;
                    print_vector(v2);
                }
                system(
            "pause");
                
            return 0;
            }
              輸出:
            write to file successfully!
            a=318, b=3.14159, c=hello world, d=中文測試
            ---------------------------Print Vector---------------------------
            hello world
            向量測試
            -------------------------Print Vector End-------------------------
            請按任意鍵繼續. . .








            posted on 2008-10-15 14:55 李現民 閱讀(1742) 評論(0)  編輯 收藏 引用 所屬分類: 語法試煉

            久久亚洲精品视频| 久久久国产精品亚洲一区| 久久久精品波多野结衣| 麻豆精品久久久久久久99蜜桃| 久久精品国产日本波多野结衣| 久久国产精品77777| 丰满少妇人妻久久久久久4| 久久综合成人网| 奇米综合四色77777久久| 国产精品九九久久精品女同亚洲欧美日韩综合区| 国产成人久久精品麻豆一区| 亚洲午夜久久久影院伊人| 久久伊人精品青青草原高清| 7777精品久久久大香线蕉| 国产福利电影一区二区三区久久久久成人精品综合 | 久久精品国产欧美日韩| 欧美牲交A欧牲交aⅴ久久| 久久久久久av无码免费看大片| 天天爽天天狠久久久综合麻豆| 精品久久久久久无码人妻蜜桃| 无码人妻精品一区二区三区久久久| 国产精久久一区二区三区| 久久天天躁狠狠躁夜夜96流白浆| 亚洲а∨天堂久久精品| 成人国内精品久久久久影院VR| 久久国产高潮流白浆免费观看| 久久久无码精品亚洲日韩蜜臀浪潮| 久久国产精品二国产精品| 99久久免费国产精精品| 日韩精品无码久久久久久| 亚洲精品高清一二区久久| 精品无码久久久久久久动漫| www亚洲欲色成人久久精品| 99热成人精品热久久669| 久久99国产综合精品女同| 麻豆亚洲AV永久无码精品久久| 久久精品久久久久观看99水蜜桃| 久久久www免费人成精品| 7777久久久国产精品消防器材| 中文字幕无码免费久久| 日产精品99久久久久久|