#include #include #include #include #include #include #include #include #include using namespace std; class Protocol { public: Protocol(const string &name) : _name(name) {} const string& name() const { return _name; } private: string _name; friend class boost::serialization::access; template void serialize(Archive &/*ar*/, const unsigned int /*version*/) {} }; class MyTable { public: typedef map Table; //typedef map Table; MyTable() { _table = new Table(); } ~MyTable() { delete _table; } void add_protocol(const Protocol * protocol) //void add_protocol(Protocol * protocol) { (*_table)[protocol->name()] = protocol; } private: Table* _table; friend class boost::serialization::access; template void serialize(Archive &ar, const unsigned int /*version*/) { ar & _table; } }; namespace boost { namespace serialization { template inline void save_construct_data(Archive & ar, const Protocol * t, const unsigned int /*version*/) { cout << "save_construct_data(Protocol)" << endl; // save data required to construct instance ar << t->name(); } template inline void load_construct_data(Archive & ar, Protocol * t, const unsigned int /*version*/) { // retrieve data from archive required to construct new instance string name; ar >> name; // invoke inplace constructor to initialize instance of my_class ::new(t)Protocol(name); } }} // namespace ... int main() { std::ofstream ofs("snapshot"); Protocol proto("foobar"); MyTable table; table.add_protocol(&proto); { boost::archive::text_oarchive oa(ofs); MyTable * const ptable = &table; oa << ptable; } MyTable * loaded_table; { std::ifstream ifs("snapshot", std::ios::binary); boost::archive::text_iarchive ia(ifs); ia >> loaded_table; } return 0; }