boost之any源码解析
来源:互联网 发布:三戒大师知乎 编辑:程序博客网 时间:2024/05/21 21:39
any是一个特殊的容器,它只能容纳一个元素,但元素类型是任意的
使用也比较简单
内部是通过typeinfo的概念实现的。
void CAnyTest::Test1()
{
CTest1 t1;
any any1(t1);
try
{
CTest1 a1 = any_cast<CTest1>(any1); // any_cast内部就是用了typeinfo的概念实现的
}
catch (boost::bad_any_cast&)
{
}
try
{
CTest2 a2 = any_cast<CTest2>(any1);
}
catch (boost::bad_any_cast&)
{
}
}
当然,我们也可以把try..catch封装在一个接口中让调用看着行数少一点。或则自己使用typeinfo的概念先判断一下能否转换,然后使用assert,减少throw异常
源码实现也比较简单,从最底层的基类看起吧
class placeholder
{
public: // structors
virtual ~placeholder()
{
}
public: // queries
virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT = 0;
virtual placeholder * clone() const = 0;
};
里面只是一个简单的基类接口,然后是真正存放数据的结构
template<typename ValueType>
class holder : public placeholder
{
public: // structors
holder(const ValueType & value)
: held(value)
{
}
// 返回了type_info信息
virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
{
return boost::typeindex::type_id<ValueType>().type_info();
}
public: // representation
ValueType held; // 真正数据存储的地方
}
然后再看any的构造函数,其实就是把值new一个,然后放到holder中
template<typename ValueType>
any(const ValueType & value)
: content(new holder<
BOOST_DEDUCED_TYPENAME remove_cv<BOOST_DEDUCED_TYPENAME decay<const ValueType>::type>::type
>(value))
{
}
还有一个重要函数,any_cast的实现
template<typename ValueType>
ValueType any_cast(any & operand)
{
typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
nonref * result = any_cast<nonref>(&operand);
if(!result)
boost::throw_exception(bad_any_cast()); // 转换不了就抛出异常
。。。
return static_cast<ref_type>(*result); //能转换就进行转换
}
判断能否转换的函数
template<typename ValueType>
ValueType * any_cast(any * operand) BOOST_NOEXCEPT
{
return operand && operand->type() == boost::typeindex::type_id<ValueType>()
? &static_cast<any::holder<BOOST_DEDUCED_TYPENAME remove_cv<ValueType>::type> *>(operand->content)->held
: 0;
}
就是使用了type_id进行判断
基本上就是这些了
我们也可以这些了
- boost之any源码解析
- Boost.Any 源码解析
- boost源码剖析之any
- 关于boost::any源码的额外解析
- Boost使用指南之Boost.Any
- boost学习之-any
- 浅尝BOOST之ANY
- Boost源码剖析之:泛型指针类any
- boost之pool源码解析
- Boost学习笔记之any
- boost.any源码整理和使用说明
- boost源码剖析之:泛型指针类any之海纳百川(rev#2)
- [转载]Boost源码剖析之:泛型指针类any之海纳百川
- Boost源码剖析之:泛型指针类any之海纳百川
- boost源码剖析之:泛型指针类any之海纳百川(rev#2)
- boost源码剖析之:泛型指针类any之海纳百川(rev#2)
- boost源码剖析之:泛型指针类any之海纳百川(rev#2)
- boost源码剖析之:泛型指针类any之海纳百川(rev#2)
- 一分耕耘一分收获,精诚所至金石为开
- css属性:改变鼠标形状
- win7下安装MySQL
- 折半查找
- 验证相关
- boost之any源码解析
- Scala中的var val/def
- IDEA 扩大tomcat内存
- 【模拟】zoj3359
- Binary Tree Traversal (二)
- [leetcode-210]course scheduleII(java)
- HDU 1260------简单DP
- php和js中数组中分别循环找出最大的数。
- HDU 1398 Square Coins