2012-04-03 45 views
1

我能够使用boost序列化将我的数据保存到磁盘上。但是,我无法取回数据。你能告诉我我做错了什么吗?无法使用boost升级到磁盘后加载数据

谢谢

下面是我的代码

void nDB::save_macros(string filename) { 
    std::ofstream ofs(filename.c_str(), std::ios::out | std::ios::binary); 
    //assert(ofs.good()); 
    boost::archive::binary_oarchive oa(ofs); 
    oa << this; 
} 

void nDB::load_macros(string filename) { 
    std::ifstream ifs(filename.c_str()); 
    //assert(ifs.good()); 
    boost::archive::binary_iarchive ia(ifs); 
    nDB *db = new nDB; 
    ia >> db; 
    *this = *db; 
} 

下面是我的序列化实例

template<class Archive> 
void nDB::serialize(Archive &ar, const unsigned int version) { 
    boost::unordered_map<string,macro*,myhash,cmp_str>::iterator M_IT; 
    boost::unordered_map<string,layer*,myhash,cmp_str>::iterator L_IT; 
    for (L_IT = _LAYERS.begin();L_IT != _LAYERS.end();L_IT++) { 
     string tmpstr = L_IT->first; 
     //ar & L_IT->first; 
     ar & tmpstr; 
     ar & *(L_IT->second); 
    } 
    for (M_IT = _MACROS.begin();M_IT != _MACROS.end();M_IT++) { 
     string tmpstr = M_IT->first; 
     //ar & M_IT->first; 
     ar & tmpstr; 
     ar & *(M_IT->second); 
    } 
} 

下面是我的运行结果是节省运行:

Insert macro mac1 into database OK!
Insert macro mac2 into database OK!
Insert macro mac3 into database OK!
Insert port P1 OK!
Insert port P2 OK!
Insert port P2 OK!
Insert port P3 OK!
Insert port P1 OK!
Insert port P3 OK!
Layer mac3 is found
Macro mac3 has these port:
Port P3 is found
Port P1 is found
Port P3 of macro mac3 has use CLOCK and dir INOUT
Port P1 of macro mac3 has use POWER and dir OUTPUT

以下是米y结果加载运行

ERROR:: Could not find macro mac3 in database

回答

1

我觉得你在nDB::serialize方法中做得太多了。

如果您只为macrolayer类提供serialize方法,Boost序列化将很高兴地为您映射您的地图。一旦你写了这些,你应该能够简化给定的方法是这样的:

template<class Archive> 
void nDB::serialize(Archive &ar, const unsigned int version) { 
    ar & _LAYERS; 
    ar & _MACROS; 
} 
+0

感谢Aldo,我实际上已经初始化了unordered_map序列化函数。我不知道你可以这样使用它。非常感谢您的建议。 – 2012-04-03 04:20:32

+0

http://stackoverflow.com/questions/4287299/boostserialization-of-boostunordered-map – Pablo 2012-04-03 20:44:00

1

我没有看它在细节,但我注意到,你打开输入和输出流不同...

save_macros

std::ofstream ofs(filename.c_str(), std::ios::out | std::ios::binary); 

load_macros

std::ifstream ifs(filename.c_str()); 

难道这就是问题吗?

+0

嗨阿尔多,我试过你的建议,但它没有解决它。我也试过保存和加载引用而不是指针。它也没有解决它。所以我想我一定是系统地做了一些错误的事情。 – 2012-04-03 04:15:31

相关问题