boost any

boost::any是一个能代表任何对象类型的对象,正如COM库的Variant变量类型,以及JAVA中的Object。不同的是,Variant的做法是包含所有可能类型的一个成员实现,浪费空间,而则boost::any借助于模板,没有空间浪费。

 

Variant的大致实现是:

ClassCvariant

{

int iData;

longlData;

….

int type;

}


而boost::any则使用模板,依靠两个内部类来封装实际数据(PlaceFolder和Folder),并对外暴露一个叫做Type()的函数暴露实际数据的类型。

为了方便分析其代码,现展示一个简单的测试代码:

<!--@page { margin: 2cm }P { margin-bottom: 0.21cm }-->

#include"stdafx.h"

#include<iostream>

#include<list>

#include"boost/any.hpp"

 

typedefstd::list<boost::any> list_any;

//关键部分:可以存放任意类型的对象

voidfill_list(list_any& la)

{

//存放常数

la.push_back(10);

//存放字符串对象

la.push_back(std::string("dyunze"));

//注意la.push_back(“dyunze”)错误,因为会被当错字符串数组

}

//根据类型进行显示:

voidshow_list(list_any& la)

{

list_any::iteratorit;

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;

elseif(anyone.type() == typeid(std::string))

std::cout<<boost::any_cast<std::string>(*it).c_str()<<std::endl;

}

}

//主程序部分:

intmain(intargc, char*argv[])

{

list_anyla;

fill_list(la);

 

show_list(la);

return0;

}


以下是我整理了后的boost::any的关键代码,( 只是为了说明,可能无法直接运行,如需要完整代码,请到www.boost.org下载boost库。)如下所示:
  1. class any
  2. {
  3. public:
  4.     //模板构造函数,参数可以是任意类型,真正的数据保存在content中
  5.     template<typename ValueType>
  6.         any(const ValueType & value): content(new holder<ValueType>(value))
  7.     {
  8.     }
  9.     //析构函数,删除保存数据的content对象
  10.     ~any()
  11.     {
  12.         delete content;
  13.     }
  14.     //一个placeholde对象指针,只想其子类folder的一个实现
  15.     // 即content( new holder<ValueType>(value) )语句
  16.     placeholder * content;
  17. public:
  18.  
  19.     //查询真实数据的类型,拆葙时有用。
  20.     const std::type_info & type() const
  21.     {
  22.         return content ? content->type() : typeid(void);
  23.     }
  24.     /**一个稻草人,存在好处是没有模板参数,可以直接申明,
  25.      *如:       placeholder * content;
  26.      *如果使用子类folder类,则这能用older<Type>
  27.      *content,而申明时Type还不确定
  28.      */
  29.     class placeholder
  30.     {
  31.     public:   
  32.         virtual ~placeholder()
  33.         {
  34.         }
  35.     public:
  36.         virtual const std::type_info & type() const = 0;
  37.         virtual placeholder * clone() const = 0;  
  38.     };
  39.     //真正保存和获取数据的类。
  40.     template<typename ValueType>
  41.         class holder : public placeholder
  42.     {
  43.     public:
  44.         holder(const ValueType & value)
  45.             : held(value)
  46.         {
  47.         }
  48.     public:
  49.         virtual const std::type_info & type() const
  50.         {
  51.             return typeid(ValueType);
  52.         }
  53.  
  54.         virtual placeholder * clone() const
  55.         {
  56.             return new holder(held);
  57.         }
  58.  
  59.     public:
  60.         //真正的数据,就保存在这里
  61.         ValueType held;
  62.     };
  63. };
  64. /**
  65.  *获取content->helder数据的方法。
  66.  *
  67.  */
  68.     template<typename ValueType>
  69. ValueType * any_cast(any * operand)
  70. {
  71.     return operand && operand->type() == typeid(ValueType) ? &static_cast<any::holder<ValueType> *>(operand->content)->held : 0;
  72. }


以上就是boost::any源代码的关键部分,其实很短小,但是,功能上非常强大,特别是在配合容器使用时。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值