vtkInformation、vtkInformationKey

//内部就一个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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值