/* demo.cpp */ #include #include #include #include #include #include #include #include #include using namespace std; using namespace __gnu_cxx;
#include #include
class subdemo { private: friend class boost::serialization::access; friend ostream& operator << (ostream &os, const subdemo &sd) { os << sd.x << " " << sd.s; return os; } template void serialize(Archive & ar, const unsigned int version) { ar & x & s; } int x; string s; public: subdemo(){} subdemo(int i, string c) : x(i), s(c){} };
class basedemo { protected: friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & x & s; } int x; string s; public: basedemo(){} basedemo(int i, string c) : x(i), s(c){} };
class demo : public basedemo { private: friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::base_object(*this); ar & i; ar & str; ar & vec; ar & hm; ar & sd; } int i; string str; vector vec; hash_map *hm; subdemo sd; public: demo() { hm = new hash_map; } demo(int a): basedemo(4321, "Perfect"), i(a), str("hello"), vec(2,4), sd(1234, "world") { hm = new hash_map; hm->insert(pair(11,"aaa")); hm->insert(pair(22,"bbb")); } void print() { cout << "test int:" << endl; cout << "-------------------" << endl; cout << i << endl << endl;
cout << "test string:" << endl; cout << "-------------------" << endl; cout << str << endl << endl;
cout << "test vector:" << endl; cout << "-------------------" << endl; copy(vec.begin(), vec.end(), ostream_iterator(cout," ")); cout << endl << endl;
cout << "test hash_map pointer:" << endl; cout << "-------------------" << endl; for (hash_map::iterator iter=hm->begin(); iter!=hm->end(); ++iter) cout << iter->first << " " << iter->second << endl; cout << endl;
cout << "test member object:" << endl; cout << "-------------------" << endl; cout << sd << endl << endl;
cout << "test derived class:" << endl; cout << "-------------------" << endl; cout << x << " " << s << endl << endl; } ~demo() { delete hm; } };
int main() { ofstream ofs("filename"); /* stringstream ss; */
demo d(1); boost::archive::text_oarchive oa(ofs); /* boost::archive::text_oarchive oa(cout); */ /* boost::archive::text_oarchive oa(ss); */ oa << d; ofs.close();
demo newd; ifstream ifs("filename"); boost::archive::text_iarchive ia(ifs); /* boost::archive::text_iarchive ia(ss); */ ia >> newd; newd.print(); ifs.close();
return 0; }
|