• <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>

            清風(fēng)竹林

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

            datastream, C++數(shù)據(jù)序列化與反序列化容器

              其實本來不想自己寫這樣一個類的,但找來找去都沒有找到符合生活習(xí)慣的,于是在經(jīng)過反復(fù)的推敲設(shè)計后,嗚,我終于承認自己功力不夠---無法將內(nèi)存管理與迭代器完美地結(jié)合在一起,只好只好先用std::vector了,于是便有了下面的代碼。到目前為止還不能號稱功能完善,但,但應(yīng)該基本可用了,堪堪記在這里,也許能給以后的自己一些幫助,或可得到高手指點一二:
              類文件被保存為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();                            
            //清空數(shù)據(jù)(但不釋放ds的內(nèi)存)
                std::ifstream infile(strPath.c_str());
                
            if (infile)
                {
                    ds.assign(std::istreambuf_iterator
            <char>(infile), std::istreambuf_iterator<char>());    //別忘了std::vector有個assign函數(shù)
                    int a;
                    
            double b;
                    std::
            string c, d;
                    std::vector
            <std::string> v2;
                    ds
            >>a>>b>>c>>d>>v2;
                    datastream(ds).swap(ds);        
            //釋放datastream內(nèi)存
                    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-------------------------
            請按任意鍵繼續(xù). . .








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

            伊人久久综在合线亚洲2019| 久久久久99精品成人片三人毛片| 久久精品国产免费观看三人同眠| 日产精品久久久久久久性色| 久久精品男人影院| 久久久国产视频| 精品久久久久久国产91| 思思久久精品在热线热| 欧美精品一区二区精品久久| 久久亚洲精品国产亚洲老地址| 99久久人妻无码精品系列 | 午夜福利91久久福利| 无码专区久久综合久中文字幕| 国产午夜精品久久久久九九电影| 久久精品国产免费观看| 久久久久亚洲AV成人网人人软件| 无码精品久久久久久人妻中字 | 欧美精品国产综合久久| 国产毛片久久久久久国产毛片 | 国产精品99久久99久久久| 亚洲精品NV久久久久久久久久| 久久久久久久尹人综合网亚洲| 日本久久久久亚洲中字幕| AV无码久久久久不卡蜜桃| 久久久久久久久66精品片| 中文精品99久久国产| 久久亚洲AV永久无码精品| 伊人色综合久久天天| 97精品伊人久久久大香线蕉| 色噜噜狠狠先锋影音久久| 成人久久久观看免费毛片| 久久精品视频网| 99久久精品免费| 久久天天日天天操综合伊人av| 久久久久无码中| 国内精品久久久久影院老司| 国产69精品久久久久观看软件| 久久久久久久久波多野高潮| 精品熟女少妇AV免费久久| 色婷婷久久综合中文久久蜜桃av| 国内精品久久久久久久97牛牛 |