序列化异构映射

时间:2012-09-18 08:59:47

标签: c++ map

我必须将地图序列化为文件。首先我将地图放到bytebuffer,然后将bytebuffer写入文件。在load方法中,首先读入整个文件的bytebuffer,然后从bytebuffer中填充map。目前我有这个代码(没有文件处理):

保存方法:

map<int, BaseClass*> myMap;
map<int, BaseClass*>::iterator it;
ByteBuffer bb;

bb.putUInt(myMap.size());
for (it = myMap.begin(); it!= myMap.end(); it++){
  bb.putUInt(it->first);
  it->second->save(bb); // the save method put the static type first, then the data
}

加载方法

...// get the map size from bb
for (int i=0; i<size; i++){
  int key = bb.getUInt() // in the real code there isn't signed/unsigned mismatch
  BaseClass* value = MyFactory.create(bb) // detailed later
  myMap.insert(key,value);
}

MyFactory.create:

BaseClass* MyFactory::create( ByteBuffer& bb){
  TypeEnum type = static_cast<TypeEnum>(bb.getUInt());
  BaseClass* value;

  switch (type){
    case DerivedClass1:
      value = new DerivedClass1()
      break;

    case DerivedClass2:
      value = new DerivedClass2()
      break;

    // etc...
  }

  value->load(bb);
  return value;
}

使用这个解决方案,我有一个很大的枚举TypeEnum,一个长开关,并且每个来自基类的新派生类我必须增加它们。有没有更好的方法呢?

2 个答案:

答案 0 :(得分:1)

我认为你的方法是正确的。但是,您想要考虑以下改进:

在factory :: create中使用智能指针。因为现在它不是例外安全:

    std::auto_ptr<BaseClass> MyFactory::create(ByteBuffer& bb)
    {
        TypeEnum type = static_cast<TypeEnum>(bb.getUInt());
        std::auto_ptr < BaseClass > value;
        switch (type)
        {
            case DerivedClass1:
                value.reset(new DerivedClass1());
                break;
            case DerivedClass2:
                value.reset(new DerivedClass2());
                break;
        }
        value->load(bb);
        return value;
    }

这样,如果加载失败,就不会有内存泄漏。

答案 1 :(得分:1)

在您的位置,我会考虑使用Boost.Serialization库。

相关问题