青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

xiaoguozi's Blog
Pay it forword - 我并不覺的自豪,我所嘗試的事情都失敗了······習慣原本生活的人不容易改變,就算現狀很糟,他們也很難改變,在過程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛傳出去,很困難,也無法預料,人們需要更細心的觀察別人,要隨時注意才能保護別人,因為他們未必知道自己要什么·····
01.#include <iostream>  
02.#include <list>  
03.#include <boost/any.hpp>  
04. 
05.typedef std::list<boost::any> list_any;  
06. 
07.//關鍵部分:可以存放任意類型的對象  
08.void fill_list(list_any& la)  
09.{      
10.    la.push_back(10);//存放常數   
11.    la.push_back( std::string("dyunze") );//存放字符串對象;注意la.push_back(“dyunze”)錯誤,因為會被當錯字符串數組  
12.}  
13. 
14.//根據類型進行顯示  
15.void show_list(list_any& la)  
16.{  
17.    list_any::iterator it;  
18.    boost::any anyone;  
19. 
20.    for( it = la.begin(); it != la.end(); it++ )  
21.    {     
22.        anyone = *it;  
23. 
24.        if( anyone.type() == typeid(int) )  
25.            std::cout<<boost::any_cast<int>(*it)<<std::endl;  
26.        else if( anyone.type() == typeid(std::string) )  
27.            std::cout<<boost::any_cast<std::string>(*it).c_str()<<std::endl;  
28.    }  
29.}  
30. 
31.int main()  
32.{  
33.    list_any la;  
34.    fill_list(la);  
35.    show_list(la);  
36. 
37.    return 0;  
38.} 
#include <iostream>
#include <list>
#include <boost/any.hpp>

typedef std::list<boost::any> list_any;

//關鍵部分:可以存放任意類型的對象
void fill_list(list_any& la)
{   
    la.push_back(10);//存放常數
    la.push_back( std::string("dyunze") );//存放字符串對象;注意la.push_back(“dyunze”)錯誤,因為會被當錯字符串數組
}

//根據類型進行顯示
void show_list(list_any& la)
{
    list_any::iterator it;
    boost::any anyone;

    for( it = la.begin(); it != la.end(); it++ )
    {   
        anyone = *it;

        if( anyone.type() == typeid(int) )
            std::cout<<boost::any_cast<int>(*it)<<std::endl;
        else if( anyone.type() == typeid(std::string) )
            std::cout<<boost::any_cast<std::string>(*it).c_str()<<std::endl;
    }
}

int main()
{
    list_any la;
    fill_list(la);
    show_list(la);

    return 0;
}

boost::any的優點:

     對設計模式理解的朋友都會知道合成模式。因為多態只有在使用指針或引用的情況下才能顯現,所以std容器中只能存放指針或引用(但實際上只能存放指針,無法存放引用,這個好像是c++的不足吧)。如:

     std::list<BaseClass*> mylist;

這樣,我們就要對指針所指向內容的生存周期操心(可能需要程序員適時刪除申請的內存;但是由于存放指針,插入/刪除的效率高),而使用boost::any就可能避免這種情況,因為我們可以存放類型本身(當然存放指針也可以)。這是boost::any的優點之一。

    boost::any另一個優點是可以存放任何類型。而前面提到的mylist只能存放BaseClass類指針以及其繼承類的指針。

boost::any的缺點:

    由于boost::any可以存放任何類型,自然它用不了多態特性,沒有統一的接口,所以在獲取容器中的元素時需要實現判別元素的真正類型,這增加了程序員的負擔。與面向對象編程思想有些矛盾,但整個標準c++模板庫何嘗不是如此,用那些牛人的話來說,是“有益補充”。

   總之,有利必有弊,沒有十全十美的。

  

分析并模仿boost::any:

     讀了一下boost::any的源代碼,并模仿一下其實現(相當一部分時拷貝原代碼),下面是代碼(只包含必要功能)。

實現any的功能主要由三部分組成:
1)any類
2)真正保存數據的holder類及其基類placeholder
3)獲取真正數據的模板函數any_cast,類型轉換的功能。

view plaincopy to clipboardprint?
01.#include <iostream>  
02.#include <list>  
03.#include <cassert>  
04. 
05.//自定義的any類  
06.class any  
07.{  
08.public:  
09.      
10.    //保存真正數據的接口類  
11.    class placeholder  
12.    {  
13.    public:       
14.        virtual ~placeholder()  
15.        {  
16.        }  
17.    public:   
18. 
19.        virtual const std::type_info & type() const = 0;  
20.        virtual placeholder * clone() const = 0;      
21.    };  
22. 
23.    //真正保存和獲取數據的類。  
24.    template<typename ValueType>  
25.    class holder : public placeholder  
26.    {  
27.    public:           
28.        holder(const ValueType & value): held(value)  
29.        {  
30.        }  
31. 
32.    public:   
33. 
34.        virtual const std::type_info & type() const 
35.        {  
36.            return typeid(ValueType);  
37.        }  
38. 
39.        virtual placeholder * clone() const 
40.        {  
41.            return new holder(held);//使用了原型模式  
42.        }  
43. 
44.    public:   
45. 
46.        //真正的數據,就保存在這里  
47.        ValueType held;  
48.    };  
49. 
50.public:  
51. 
52.    any(): content(NULL)     
53.    {         
54.    }  
55. 
56.    //模板構造函數,參數可以是任意類型,真正的數據保存在content中  
57.    template<typename ValueType>  
58.    any(const ValueType & value): content(new holder<ValueType>(value))  
59.    {  
60.    }    
61. 
62.    //拷貝構造函數  
63.    any(const any & other)  
64.        : content(other.content ? other.content->clone() : 0)  
65.    {  
66.    }  
67. 
68.    //析構函數,刪除保存數據的content對象  
69.    ~any()  
70.    {  
71.        if(NULL != content)  
72.            delete content;  
73.    }  
74. 
75.private:  
76.    //一個placeholde對象指針,指向其子類folder的一個實現  
77.    // 即content( new holder<ValueType>(value) )語句  
78.    placeholder* content;  
79. 
80.    template<typename ValueType> friend ValueType any_cast(const any& operand);  
81.public:   
82. 
83.    //查詢真實數據的類型。  
84.    const std::type_info & type() const 
85.    {  
86.        return content ? content->type() : typeid(void);  
87.    }  
88.};  
89. 
90. 
91.//獲取content->helder數據的方法。用來獲取真正的數據  
92.template<typename ValueType>  
93.ValueType any_cast(const any& operand)  
94.{  
95.    assert( operand.type() == typeid(ValueType) );  
96.    return static_cast<any::holder<ValueType> *>(operand.content)->held;  
97.}  
98. 
99.//下代碼是使用示例  
100. 
101.typedef std::list<any> list_any;  
102. 
103.void fill_list(list_any& la)  
104.{      
105.    la.push_back(10);//存放常數;調用了any的模板構造函數,下同  
106.    la.push_back( std::string("我是string") );//存放字符串對象;注意la.push_back(“dyunze”)錯誤,因為會被當錯字符串數組  
107. 
108.    char* p = "我是常量區字符串abc";  
109.    la.push_back(p);//可以存放指針,但要注意指針的失效問題  
110.}  
111. 
112.//根據類型進行顯示  
113.void show_list(list_any& la)  
114.{  
115.    list_any::iterator it;  
116. 
117.    for( it = la.begin(); it != la.end(); it++ )  
118.    {     
119. 
120.        if( (*it).type() == typeid(int) )  
121.            std::cout<<any_cast<int>(*it)<<std::endl;  
122.        else if( (*it).type() == typeid(std::string) )  
123.            std::cout<<any_cast<std::string>(*it).c_str()<<std::endl;  
124.        else if( (*it).type() == typeid(char*) )  
125.            std::cout<<any_cast<char*>(*it)<<std::endl;  
126.    }  
127.}  
128. 
129.int main()  
130.{  
131.    list_any la;  
132.    fill_list(la);  
133.    show_list(la);  
134. 
135.    return 0;  
136.} 

 boost::any是一個很有趣的類,剛剛開始我還以為其就是一個variant類型,
能夠將任意類型值保存進去,能夠以任意類型值讀出來,不過我錯了 :(
  boost::any的作者認為,所謂generic type有三個層面的解釋方法:
  1.類似variant類型那樣任意進行類型轉換,可以保存一個(int)5進去,
    讀一個(string)"5"出來。Win下面的VARIANT類型是以一個巨大的
    union實現的類似功能,使用靈活但效率較低
  2.區別對待包含值的類型,保存一個(int)5進去,不會被隱式轉換為
    (string)'5'或者(double)5.0,這樣效率較高且類型安全,
    不必擔心ambiguous conversions
  3.對包含值類型不加區別,例如把所有保存的值強制轉換為void *保存
    讀取時再有程序員判斷其類型。這樣效率雖最高但無法保證類型安全
  boost::any就選擇了第二層面的設計思路,它允許用戶將任意類型值保存
進一個any類型變量,但內部并不改變值的類型,并提供方法讓用戶在使用時
主動/被動進行類型判斷。

  在實現方面,boost::any使用兩層內部類placeholder和holder保存
實際類型的值。類placeholder只是一個接口,模板類holder是特定類型
的實現。其中type()方法獲取實際值類型,即typeid(ValueType);
clone()方法獲取值的拷貝return new holder(held);
  virtual const std::type_info & type() const
  virtual placeholder * clone() const
  其值的類型信息不象Win的VARIANT那樣以專門的字段保存,
而是通過模板參數形式靜態保存。這樣效率更高(僅在編譯期),

通用性更強(任何類型都可以,真正any)但靈活性不如VARIANT
  在進行拷貝構造/賦值/swap時,都直接將整個placeholder換掉,
這樣可以保證值類型的延續性。

  在使用方面,提供了主動/被動進行類型檢測的方法。
  可以使用any::type()方法主動檢測值類型
bool is_int(const boost::any & operand)
{
    return operand.type() == typeid(int);
}
  也可以通過any_cast函數被動進行檢測。此函數與C++中的*_cast
系列關鍵字有相同的語法規范,嘗試進行類型轉換,如類型不匹配則對
指針轉換返回NULL,對引用轉換拋出boost::bad_any_cast異常
  boost::any str = string("12345");
  try
  {
    cout << boost::any_cast<int>(str) << endl;
  }
  catch(boost::bad_any_cast e)
  {
    cerr << e.what() << endl;
  }


  在應用方面,any類型適合于類型不同但使用相關的值。如C++的...
形式的函數參數本事不是類型安全的,可以通過vector<any>改造之
然后在使用時檢測類型是否匹配,如可改造printf為
  void safe_printf(const char *format, const vector<any>& params)
  {
    int index = 0;
    for(const char *pch = format; *pch; pch++)
    {
      switch(*pch)
      {
        case '%':
        {
          switch(*++pch)
          {
            case 'i':
            case 'd':
            {
              if(params[index].type() == typeid(int) ||
                 params[index].type() == typeid(short))
              {
                ...
              }
              else
                throw ...
            }
          }
        }
        case '\':
        {
          ...
        }
        default:
        {
          putchar(*pch);
        }
      }
    }
  }

附:boost::any.hpp
#ifndef BOOST_ANY_INCLUDED
#define BOOST_ANY_INCLUDED

// what:  variant type boost::any
// who:   contributed by Kevlin Henney,
//        with features contributed and bugs found by
//        Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
// when:  July 2001
// where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95

#include <algorithm>
#include <typeinfo>

#include "boost/config.hpp"

namespace boost
{
    class any
    {
    public: // structors

        any()
          : content(0)
        {
        }

        template<typename ValueType>
        any(const ValueType & value)
          : content(new holder<ValueType>(value))
        {
        }

        any(const any & other)
          : content(other.content ? other.content->clone() : 0)
        {
        }

        ~any()
        {
            delete content;
        }

    public: // modifiers

        any & swap(any & rhs)
        {
            std::swap(content, rhs.content);
            return *this;
        }

        template<typename ValueType>
        any & operator=(const ValueType & rhs)
        {
            any(rhs).swap(*this);
            return *this;
        }

        any & operator=(const any & rhs)
        {
            any(rhs).swap(*this);
            return *this;
        }

    public: // queries

        bool empty() const
        {
            return !content;
        }

        const std::type_info & type() const
        {
            return content ? content->type() : typeid(void);
        }

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
    private: // types
#else
    public: // types (public so any_cast can be non-friend)
#endif


        class placeholder
        {
        public: // structors
    
            virtual ~placeholder()
            {
            }

        public: // queries

            virtual const std::type_info & type() const = 0;

            virtual placeholder * clone() const = 0;
    
        };

        template<typename ValueType>
        class holder : public placeholder
        {
        public: // structors

            holder(const ValueType & value)
              : held(value)
            {
            }

        public: // queries

            virtual const std::type_info & type() const
            {
                return typeid(ValueType);
            }

            virtual placeholder * clone() const
            {

                return new holder(held);
            }

        public: // representation

            ValueType held;

        };

#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS

    private: // representation

        template<typename ValueType>
        friend ValueType * any_cast(any *);

#else

    public: // representation (public so any_cast can be non-friend)

#endif

        placeholder * content;

    };

    class bad_any_cast : public std::bad_cast
    {
    public:
        virtual const char * what() const throw()
        {
            return "boost::bad_any_cast: "
                   "failed conversion using boost::any_cast";
        }
    };

    template<typename ValueType>
    ValueType * any_cast(any * operand)
    {
        return operand && operand->type() == typeid(ValueType)
                    ? &static_cast<any::holder<ValueType> *>(operand->content)->held
                    : 0;
    }

    template<typename ValueType>
    const ValueType * any_cast(const any * operand)
    {
        return any_cast<ValueType>(const_cast<any *>(operand));
    }

    template<typename ValueType>
    ValueType any_cast(const any & operand)
    {
        const ValueType * result = any_cast<ValueType>(&operand);
        if(!result)
            throw bad_any_cast();
        return *result;
    }

}

// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
//
// Permission to use, copy, modify, and distribute this software for any
// purpose is hereby granted without fee, provided that this copyright and
// permissions notice appear in all copies and derivatives.
//
// This software is provided "as is" without express or implied warranty.

#endif

posted on 2011-06-16 18:32 小果子 閱讀(875) 評論(0)  編輯 收藏 引用 所屬分類: C++
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲第一网站免费视频| 六月婷婷一区| 一本色道久久综合一区| 欧美日韩国产综合一区二区| 一区二区国产在线观看| 一本久道久久久| 国产欧美在线播放| 蜜桃av噜噜一区| 欧美激情亚洲另类| 亚洲欧美国产va在线影院| 校园春色国产精品| 91久久久国产精品| 在线亚洲+欧美+日本专区| 国产偷国产偷精品高清尤物| 欧美成在线观看| 欧美调教视频| 欧美不卡视频| 欧美三区视频| 免费欧美高清视频| 欧美性理论片在线观看片免费| 欧美一区=区| 欧美国产精品中文字幕| 香蕉久久夜色精品国产使用方法| 欧美主播一区二区三区| 在线视频你懂得一区| 午夜在线不卡| 亚洲少妇自拍| 乱人伦精品视频在线观看| 亚洲你懂的在线视频| 久久亚洲综合| 欧美一区二区在线观看| 欧美日韩国产区| 欧美1区3d| 国产亚洲欧洲997久久综合| 国产精品高清在线观看| 欧美不卡在线| 国产午夜精品全部视频播放| 日韩视频中文| 亚洲美女91| 久久久视频精品| 欧美在线免费| 国产精品久久久一区二区三区| 亚洲国产精品久久久| 狠狠干综合网| 欧美一区二区三区免费在线看| 亚洲午夜三级在线| 欧美韩日一区二区| 亚洲大胆人体视频| 136国产福利精品导航网址| 午夜精品久久久久久久蜜桃app| 国产精品99久久久久久久久久久久| 久热re这里精品视频在线6| 久久久久久电影| 国产欧美日韩一区二区三区| 亚洲特级毛片| 午夜欧美理论片| 国产精品免费在线| 一区二区三区四区五区视频| 中文国产成人精品| 欧美深夜影院| av不卡免费看| 亚洲欧美视频| 国产美女精品视频免费观看| 午夜国产一区| 久久久精品国产免费观看同学| 国产欧美精品日韩精品| 翔田千里一区二区| 乱中年女人伦av一区二区| 伊人夜夜躁av伊人久久| 蜜桃久久av| 亚洲精品女人| 一本久久a久久免费精品不卡| 欧美日韩精品一区二区三区四区 | 久久久免费精品视频| 国产裸体写真av一区二区| 欧美在线中文字幕| 美女91精品| 亚洲精选一区二区| 欧美日韩一区二区三区在线看 | 欧美国产一区二区| 亚洲精品一区二区网址| 亚洲欧美在线看| 国产精品一区二区三区久久| 久久精品日韩欧美| 亚洲第一中文字幕在线观看| 一区二区三区精品在线| 国产麻豆午夜三级精品| 葵司免费一区二区三区四区五区| 91久久久亚洲精品| 亚洲在线一区二区| 很黄很黄激情成人| 欧美久久久久久久久久| 午夜视频在线观看一区| 国产精品视频免费一区| 久久久久久999| 亚洲免费激情| 老司机午夜精品视频| 9色精品在线| 国自产拍偷拍福利精品免费一| 免费成人高清视频| 亚洲一区日韩在线| 欧美激情视频免费观看| 亚洲综合色网站| 亚洲欧洲一区二区三区| 国产区亚洲区欧美区| 欧美国产另类| 久久久精品国产一区二区三区 | 99热在线精品观看| 蜜臀av国产精品久久久久| 亚洲主播在线播放| 亚洲人成欧美中文字幕| 国产午夜精品麻豆| 国产精品wwwwww| 欧美成人精精品一区二区频| 欧美一区二区三区视频免费播放| 亚洲精品国产视频| 欧美www在线| 乱人伦精品视频在线观看| 午夜视频精品| 亚洲一级黄色av| 亚洲美女在线看| 亚洲国产精品久久久久婷婷老年| 国产人成精品一区二区三| 欧美午夜精品久久久久免费视| 男人的天堂成人在线| 久久久久九九九九| 久久精品视频va| 欧美在线看片| 欧美一区二区精品久久911| 亚洲天堂免费在线观看视频| 99精品国产在热久久下载| 亚洲欧洲日韩综合二区| 亚洲第一页在线| 亚洲国产天堂久久综合| 亚洲电影免费观看高清完整版在线观看 | 日韩一级大片| 亚洲精品国产欧美| 亚洲精品看片| 日韩午夜免费视频| 99re8这里有精品热视频免费 | 欧美激情日韩| 欧美精品在线一区二区| 欧美人与性禽动交情品| 欧美日韩免费网站| 欧美日韩精品国产| 欧美三区在线观看| 国产精品免费区二区三区观看| 国产精品久久久91| 国产深夜精品福利| 狠狠入ady亚洲精品| 在线播放亚洲| 日韩一本二本av| 久久五月天婷婷| 欧美福利专区| 欧美视频手机在线| 国产欧美综合一区二区三区| 狠狠色狠狠色综合日日小说| 1769国产精品| 99在线视频精品| 欧美一区二区高清| 欧美mv日韩mv国产网站app| 亚洲高清在线观看| 一区二区三区高清在线| 午夜影院日韩| 欧美紧缚bdsm在线视频| 国产精品国产亚洲精品看不卡15| 国产日韩欧美一区在线| 亚洲人成在线观看| 亚洲伊人伊色伊影伊综合网 | 中国成人在线视频| 久久成年人视频| 亚洲高清在线精品| 午夜精品在线| 欧美二区乱c少妇| 国产日韩精品久久久| 亚洲人永久免费| 久久丁香综合五月国产三级网站| 欧美激情国产高清| 亚洲欧美一区二区三区极速播放| 美女91精品| 国产一区二区三区在线观看视频| 亚洲精品国产拍免费91在线| 欧美有码视频| 99精品欧美| 欧美不卡在线视频| 国产午夜精品视频免费不卡69堂| 99国产精品久久久久久久| 久久夜色撩人精品| 亚洲一二三区在线| 欧美理论电影网| 在线欧美日韩| 久久精品欧美| 这里只有精品在线播放| 欧美成人午夜77777| 国产亚洲欧美一区二区| 亚洲男人天堂2024| 99天天综合性| 欧美精品一区三区| 亚洲国产精品一区二区久| 久久久精品午夜少妇|