//内部就一个std::unordered_map
class vtkInformationInternals
{
struct HashFun
{
size_t operator()(vtkInformationKey* key) const
{
return reinterpret_cast<int>(key) / sizeof(vtkInformationKey);
}
};
std::unordered_map<vtkInformationKey*, vtkObjectBase*, HashFun> Map;
};
//1、vtkInformation其实就是对map的封装,具体实现是通过内部类vtkInformationInternals
//2、vtkInformation的一些实现要依赖vtkInformationKey和其子类
//3、vtkInformationKey有很多子类,例如vtkInformationIntegerKey,vtkInformationRequestKey等
class vtkInformation : public vtkObject
{
protected:
vtkInformationInternals* Internal;
private:
vtkInformationRequestKey* Request;
pubilc:
vtkInformation::vtkInformation()
{
this->Internal = new vtkInformationInternals;
this->Request = nullptr;
}
//添加一个key-value
void vtkInformation::Set(vtkInformationIntegerKey* key, Intege value)
{
//内部是通过key进行添加,
key->Set(this, value);
}
//通过key获取value
vtkObjectBase* vtkInformation::GetAsObjectBase(vtkInformationKey* key)
{
if (key)
{
typedef vtkInformationInternals::MapType MapType;
MapType::const_iterator i = this->Internal->Map.find(key);
if (i != this->Internal->Map.end())
{
return i->second;
}
}
return nullptr;
}
void vtkInformation::SetAsObjectBase(vtkInformationKey* key, vtkObjectBase* newvalue)
{
typedef vtkInformationInternals::MapType MapType;
MapType::iterator i = this->Internal->Map.find(key);
if (i != this->Internal->Map.end())
{
vtkObjectBase* oldvalue = i->second;
if (newvalue)
{
i->second = newvalue;
newvalue->Register(nullptr);
}
else
{
this->Internal->Map.erase(i);
}
oldvalue->UnRegister(nullptr);
}
else if (newvalue)
{
MapType::value_type entry(key, newvalue);
this->Internal->Map.insert(entry);
newvalue->Register(nullptr);
}
this->Modified(key);
}
vtkObjectBase* vtkInformation::Get(const vtkInformationKey* key)
{
if (key)
{
typedef vtkInformationInternals::MapType MapType;
MapType::const_iterator i = this->Internal->Map.find(const_cast<vtkInformationKey*>(key));
if (i != this->Internal->Map.end())
{
return i->second;
}
}
return nullptr;
}
};
//
class vtkInformationIntegerKey : public vtkInformationKey
{
//调用vtkInformation::SetAsObjectBase
void vtkInformationIntegerKey::Set(vtkInformation* info, int value)
{
if (vtkInformationIntegerValue* oldv = static_cast<vtkInformationIntegerValue*>(this->GetAsObjectBase(info)))
{
if (oldv->Value != value)
{
oldv->Value = value;
}
}
else
{
vtkInformationIntegerValue* v = new vtkInformationIntegerValue;
v->Value = value;
this->SetAsObjectBase(info, v);
}
}
//调用vtkInformation::GetAsObjectBase
int vtkInformationIntegerKey::Get(vtkInformation* info)
{
vtkInformationIntegerValue* v =static_cast<vtkInformationIntegerValue*>(this->GetAsObjectBase(info));
return v ? v->Value : 0;
}
};
//基本用法
vtkInformation* pInfo = vtkInformation::New();
vtkInformationIntegerKey* key1 = vtkInformationIntegerKey::MakeKey("mykey1", "location");
pInfo->Set(key1, 1);
vtkInformationIntegerKey* key2 = vtkInformationIntegerKey::MakeKey("mykey2", "location");
pInfo->Set(key2, 2);
int val1 = pInfo->Get(key1);
int val2 = pInfo->Get(key2);
//下面演示pInfo->Set(key1, 1)的过程
//step1
void vtkInformation::Set(vtkInformationIntegerKey* key, Intege value)
{
key->Set(this, value);
}
//step2
void vtkInformationIntegerKey::Set(vtkInformation* info, int value)
{
vtkInformationIntegerValue* v = new vtkInformationIntegerValue;
v->Value = value;
this->SetAsObjectBase(info, v);
}
//step3
void vtkInformation::SetAsObjectBase(vtkInformationKey* key, vtkObjectBase* newvalue)
{
MapType::value_type entry(key, newvalue);
this->Internal->Map.insert(entry);
}
06-09
1337
03-29
336