boost::any 学习

#include <iostream>
#include <typeinfo>
#include <list>
using namespace std;


class any
{
public:
    //interface
    class placeholder
    {
    public:
        //
        virtual ~placeholder(){}

        //
        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)
        {
        }

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

        //
        virtual placeholder * clone() const
        {
            return new holder(held);
        }

    public:
        // representation
        ValueType held;
    };

public:
    any(): content(0)
    {
    }

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

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

    // Move constructor
    any(any&& other)  : content(other.content)
    {
        other.content = 0;
    }

    // Perfect forwarding of ValueType
    template<typename ValueType>
    any(ValueType&& value): content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value)))
    {
    }

    //
    ~any()
    {
        delete content;
    }

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

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

    // move assignement
    any & operator=(any&& rhs)
    {
        rhs.swap(*this);
        any().swap(rhs);
        return *this;
    }

    // Perfect forwarding of ValueType
    template <class ValueType>
    any & operator=(ValueType&& rhs)
    {
        any(static_cast<ValueType&&>(rhs)).swap(*this);
        return *this;
    }

    //
    bool empty() const
    {
        return !content;
    }

    //
    void clear()
    {
        any().swap(*this);
    }

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




private: // representation

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

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

    placeholder * content;
};



inline void swap(any & lhs, any & rhs)
{
    lhs.swap(rhs);
}

class  bad_any_cast : public std::bad_cast
{
public:
//    virtual const char * what() const
//    {
//        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>
inline const ValueType * any_cast(const any * operand)
{
    return any_cast<ValueType>(const_cast<any *>(operand));
}

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

int main()
{
    typedef std::list<any> list_any;
    list_any list;
    list.push_back(10);
    list.push_back( std::string("std::string 类型") );
    const char* p = "const char* 字符串";
    list.push_back(p);

    for(const auto& elem:list)
    {
        if( elem.type() == typeid(int) )
            std::cout<<any_cast<int>(elem)<<std::endl;
        else if( elem.type() == typeid(std::string) )
            std::cout<<any_cast<std::string>(elem)<<std::endl;
        else if( elem.type() == typeid(const char*) )
            std::cout<<any_cast<const char*>(elem)<<std::endl;
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值