C++ 九阴真经之任意值

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-07 09:53:36

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;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!