Boost.any的实现源码

#include<iostream>
#include<iomanip>
using namespace std;

class any
{
public:
    //Representation and basic construction of a generalized union type.
    any(): content(NULL){cout << "构造函数" << endl;}
    ~any(){cout << "析构函数" << endl;if(NULL != content)        delete content;}
    
    //获得类型信息
    const type_info &type_info() const {return content ? content->type_info() : typeid(void);}

    //INWARD CONVERSIONS
    any(const any &other) : content(other.content ? other.content->clone() : 0){cout << "拷贝构造函数" << endl;}
    template<typename ValueType>
    any(const ValueType &value) : content(new holder<ValueType>(value)){cout << "拷贝构造函数" << endl;}//函数模板
    
    any &swap(any &rhs){std::swap(content, rhs.content); return *this;}// 只是简单地将两个指针的值互换

    //利用拷贝构造函数创建局部变量作为参数传给swap,交换值之后,局部变量自动销毁,而rhs值不变;
    any &operator=(const any &rhs){cout << "赋值函数" << endl;return swap(any(rhs)); }
    template<typename ValueType> //函数模板
    any &operator=(const ValueType &rhs){cout << "赋值函数" << endl;return swap(any(rhs));}
    
    //OUTWARD CONVERSIONS
    operator const void *() const{return content;}

    template<typename ValueType>
    bool copy_to(ValueType &value) const
    {
        const ValueType *copyable = to_ptr<ValueType>();
        if(copyable)        value = *copyable;
        return copyable;
    }

    template<typename ValueType>
    const ValueType *to_ptr() const
    {
        return type_info() == typeid(ValueType)? /*判断类型是否相同*/&static_cast<holder<ValueType> *>(content)->held : 0;
    }

private:
    class placeholder
    {
    public:
        virtual ~placeholder(){}
        virtual const std::type_info & type_info() const = 0; //获得类型
        virtual placeholder *clone() const = 0; //获得值拷贝
    };

    template<typename ValueType>
    class holder : public placeholder
    {
    public:
        holder(const ValueType &value) : held(value){}
        virtual const std::type_info &type_info() const {return typeid(ValueType);}
        virtual placeholder *clone() const{return new holder(held);}
        const ValueType held;//存储值
    };

    placeholder *content;
};

template<typename ValueType>
ValueType any_cast(const any &operand)
{
    const ValueType *result = operand.to_ptr<ValueType>();
    //return result ? *result : throw std::bad_cast(); //有异常,原因未知,将来再分析
    if (result)        return *result;
    throw std::bad_cast();
}

int AnyTst()
{
    any i_a = 3;
    int i_b = any_cast<int>(i_a);    
    cout << "i_a = " << i_a << ", i_b = " << i_b << endl;

    i_a = 4;
    i_b = any_cast<int>(i_a);    
    cout << "i_a = " << i_a << ", i_b = " << i_b << endl;

    int i_c = 5;
    if(i_a.copy_to<int>(i_c)){
        i_b = any_cast<int>(i_a);    
        cout << "i_a = " << i_a << ", i_b = " << i_b << ", i_c = " << i_c << endl;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值