网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> VC编程 >> 文章正文
  boost.any源码重列            【字体:
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 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     directx 图形接口指南(…
     win2k下的api函数的拦截
     用crypto  api  实现公钥…
     根据别人的md5源码封装的…
     vc中使用gdi+合并jpg图片
     document/view的交互 --…
     windows下的函数hook技术
     windows api函数大全一
     用vc 6.0实现串行通信的…
     vc++技术内幕(第四版)…
  • page、request、session、ap…

  • JSF 的性能远不及 JSP 或 St…

  • Struts2学习:在struts2中集…

  • 从头开始认识jboss

  • SPRING+STRUTS+HIBERNATE登录…

  • JSP标准模板库(JSTL)入门教…

  • Cookie又见Cookie-使用Html…

  • 搭建JSTL运行环境

  • struts多附件上传

  • jsf自定义toolbar组件

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David