I was able to save my data onto disk using boost serialization. However, I was unable to retrieve back the data. Would you please show me what did I do wrong?
Thank you,
Below are my codes
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;
}
Below is my serialization instantiation
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);
}
}
Below are my run result saving run:
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
Below is my result loading run
ERROR:: Could not find macro mac3 in database
I think you’re doing too much in your
nDB::serializemethod.Boost Serialize will happily serialize your maps for you, if you just provide
serializemethods for yourmacroandlayerclasses. Once you’ve written those, you should be able to simplify the given method to something like this: