1

我必须将映射序列化为文件。首先,我将映射放入字节缓冲区,然后将字节缓冲区写入文件。在 load 方法中,首先将整个文件读入 bytebuffer,然后从 bytebuffer 中填充映射。目前我有这个代码(没有文件处理):

保存方法:

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、一个长开关,并且对于基类的每个新派生类,我都必须增加它们。有没有更好的方法来做到这一点?

4

2 回答 2

1

I think your approach is correct. However you migth want to consider following improvement:

Use smart pointer inside the factory::create. Because now it is not exception safe:

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

This way if it fails on loading you will not have memory leak.

于 2012-09-18T09:19:57.060 回答
1

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

于 2012-09-18T09:25:52.550 回答