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