![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
|||||
| boost.any源码重列 | |||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-9-12 ![]() |
|||||
|
正在装载数据…… #include <algorithm> #include <typeinfo> #include "boost/config.hpp" #include <boost/type_traits/remove_reference.hpp> #include <boost/type_traits/is_reference.hpp> #include <boost/throw_exception.hpp> #include <boost/static_assert.hpp> namespace kimi_boost { class any { public: // constructors 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); } template<typename ValueType> friend ValueType * any_cast(any *); private://inner class class placeholder { public: virtual ~placeholder(){} virtual const std::type_info & type() const = 0; virtual placeholder * clone() const = 0; };//class placeholder template<typename ValueType> class holder : public placeholder { public: holder(const ValueType & value): held(value){} virtual const std::type_info & type() const { return typeid(ValueType); } virtual /*holder*/placeholder * clone() const//注意返回值类型 { return new holder(held); } ValueType held; };//class holder private://member data placeholder * content; };//class any //异常类 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";} }; //两个指针版本的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)); } //两个引用版本的any_cast失败时会抛出异常 template<typename ValueType> ValueType any_cast(const any & operand) { typedef typename remove_reference<ValueType>::type nonref; const nonref * result = any_cast<nonref>(&operand); if(!result) boost::throw_exception(bad_any_cast()); return *result; } template<typename ValueType> ValueType any_cast(any & operand) { typedef typename remove_reference<ValueType>::type nonref; nonref * result = any_cast<nonref>(&operand); if(!result) boost::throw_exception(bad_any_cast()); return *result; } } 本文来源:http://blog.csdn.net/laibach0304/archive/2007/08/24/1758077.aspx
|
|||||
| 文章录入:admin 责任编辑:admin | |||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | |||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|