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

            力為的技術博客

            聯系 聚合 管理
              154 Posts :: 1 Stories :: 561 Comments :: 0 Trackbacks

             

              1//////////////////////////////////////////////////////////////////////////
              2enum SerializeType
              3{
              4   ST_Read,
              5   ST_Write,
              6}
            ;
              7
              8//////////////////////////////////////////////////////////////////////////
              9// The basic template for both serialize and desierialze
             10template<SerializeType nType, class TStream,  class TDataType>
             11class Serialize
             12{
             13   typedef TDataType DataType;
             14public:
             15   Serialize(TStream& stream, DataType& data)
             16   {
             17      // Please specialize your own Serialize Class
             18      assert(false);
             19   }

             20}
            ;
             21
             22// Helper function to simplify the usage
             23template<SerializeType nType, class TStream,  class TDataType>
             24inline void SerializeFunc(TStream& stream, TDataType& data)
             25{
             26   typedef Serialize<nType, TStream, TDataType> Serializer;
             27   Serializer ss(stream, data);
             28}

             29
             30//////////////////////////////////////////////////////////////////////////
             31// string Serialize class
             32// To demonstrate the native data type 
             33// Read
             34template<class TStream>
             35class Serialize< ST_Read, TStream, std::string>
             36{   
             37   typedef std::string DataType;
             38public:
             39   Serialize(TStream& stream, DataType& data)
             40   {
             41      stream >> data;
             42   }

             43}
            ;
             44// Write
             45template<class TStream>
             46class Serialize<ST_Write, TStream, std::string>
             47{   
             48   typedef std::string DataType;
             49public:
             50   Serialize(TStream& stream, DataType& data)
             51   {
             52      stream << data;
             53   }

             54}
            ;
             55
             56//////////////////////////////////////////////////////////////////////////
             57// Test Class
             58class TestClass
             59{
             60public:
             61   TestClass()
             62      :m_Name("Name")
             63      ,m_Id("Id")
             64   {
             65
             66   }

             67
             68public:
             69   std::string m_Name;
             70   std::string m_Id;
             71}
            ;
             72
             73// Partial Specialize the Serialize Class
             74template<SerializeType nType,  class TStream>
             75class Serialize<nType, TStream, TestClass>
             76{
             77   typedef TestClass DataType;
             78public:
             79   Serialize(TStream& stream, DataType& data)
             80   {
             81      SerializeFunc<nType>(stream, data.m_Name);
             82      SerializeFunc<nType>(stream, data.m_Id);
             83   }

             84}
            ;
             85
             86//////////////////////////////////////////////////////////////////////////
             87// Customized stream
             88class CustomStream
             89{
             90public:
             91   template<class TDataType>
             92   CustomStream& operator >> (TDataType& data)
             93   {
             94      std::cin >> data;
             95      return *this;
             96   }

             97
             98   template<class TDataType>
             99   CustomStream& operator << (TDataType& data)
            100   {
            101      std::cout << "[" << data << "]" << std::endl;
            102      return *this;
            103   }

            104}
            ;
            105
            106//////////////////////////////////////////////////////////////////////////
            107
            108int _tmain(int argc, _TCHAR* argv[])
            109{
            110   CustomStream stream;
            111
            112   TestClass testObject;
            113   SerializeFunc<ST_Write>(std::cout, testObject);
            114   std::cout << std::flush;
            115
            116   SerializeFunc<ST_Read>(stream, testObject);
            117   SerializeFunc<ST_Write>(stream, testObject);
            118
            119   std::cout << "Please any key to exit!";
            120   char ch;
            121   std::cin >> ch;
            122    return 0;
            123}

            124
            posted on 2010-09-30 15:08 力為 閱讀(1357) 評論(0)  編輯 收藏 引用 所屬分類: 5. GP&STL&BOOST
            亚洲AV乱码久久精品蜜桃| 国内精品久久久久久久涩爱| 伊人热热久久原色播放www| 无码人妻久久一区二区三区蜜桃| 久久天天躁狠狠躁夜夜不卡| 久久精品国产亚洲AV麻豆网站 | 久久久久18| 久久天天躁狠狠躁夜夜不卡| 久久99毛片免费观看不卡| 久久久久久久久久久免费精品 | 精品久久久久久| 亚洲欧美成人久久综合中文网 | 久久99热狠狠色精品一区| 亚洲精品NV久久久久久久久久| 日本强好片久久久久久AAA| 99久久精品免费看国产一区二区三区| 久久亚洲精品无码aⅴ大香| 99久久婷婷国产综合精品草原| 国产亚洲精久久久久久无码77777| 99久久无码一区人妻| 日韩精品久久久肉伦网站| 久久人搡人人玩人妻精品首页| 狠狠色丁香久久婷婷综合五月| 欧美日韩精品久久久免费观看| 久久高清一级毛片| 亚洲国产精品久久| 精品精品国产自在久久高清 | 欧美日韩精品久久久免费观看| 久久国产高潮流白浆免费观看| 国内精品伊人久久久影院| 久久久WWW成人免费毛片| 久久99精品国产麻豆不卡| 99久久精品日本一区二区免费| 无码人妻少妇久久中文字幕蜜桃 | 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 国产精品一区二区久久精品无码| 国产亚洲欧美成人久久片| 精品久久一区二区三区| 97久久精品国产精品青草| 成人久久久观看免费毛片| www久久久天天com|