C++ 九阴真经之任意值

any是一个可用于任何类型单个值的类型安全的容器,目前已收录到C++17标准库,但对于一些历史 项目,可能并没有使用最新编译器,那么可以自己简单的实现。

代码实现:

#include <assert.h>
#include <typeinfo>
#include <string>
class AnyVar
{
public:

	//保存真正数据的接口类   
	class placeholder
	{
	public:
		virtual ~placeholder()
		{
		}
	public:

		virtual const std::type_info & type() const = 0;
		virtual placeholder * clone() const = 0;
	};

	//真正保存和获取数据的类。   
	template<typename ValueType>
	class holder : public placeholder
	{
	public:
		holder(const ValueType & value) : held(value)
		{
		}

	public:

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

		virtual placeholder * clone() const
		{
			return new holder(held);//使用了原型模式   
		}

	public:

		//真正的数据,就保存在这里   
		ValueType held;
	};

public:

	AnyVar():content(nullptr)
	{
	}

	//模板构造函数,参数可以是任意类型,真正的数据保存在content中   
	template<typename ValueType>
	AnyVar(const ValueType & value) : content(new holder<ValueType>(value))
	{
	}

	//拷贝构造函数   
	AnyVar(const AnyVar & other)
		: content(other.content ? other.content->clone() : 0)
	{
	}

	AnyVar & operator=(const AnyVar& other)
	{
		if (this == &other)
		{
			return *this;
		}
		this->content = other.content ? other.content->clone() : 0;
		return *this;
	}

	//析构函数,删除保存数据的content对象   
	~AnyVar()
	{
		if (nullptr != content)
			delete content;
	}

private:
	//一个placeholde对象指针,指向其子类holder的一个实现   
	// 即content( new holder<ValueType>(value) )语句   
	placeholder* content;

	template<typename ValueType> friend ValueType any_cast(const AnyVar& operand);
public:

	//查询真实数据的类型。   
	const std::type_info & type() const
	{
		return content ? content->type() : typeid(void);
	}
};


//获取content->helder数据的方法。用来获取真正的数据   
template<typename ValueType>
ValueType any_cast(const AnyVar& operand)
{
	assert(operand.type() == typeid(ValueType));
	return static_cast<AnyVar::holder<ValueType> *>(operand.content)->held;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值