#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;
}
}
Boost.any的实现源码
最新推荐文章于 2022-08-28 12:04:36 发布