#include #include #include #include #include #include #include class Foo { public: explicit Foo(int n) { init(n); for(int i = 0; i < n; ++i) { buffer[i] = i; } } friend std::ostream& operator<<(std::ostream& os, const Foo& foo) { std::copy(foo.buffer, foo.buffer + foo.buflen, std::ostream_iterator(os, " ")); return(os); } private: int buflen; int* buffer; void deleteBuffer() { delete[] buffer; } void init(int n) { buflen = n; buffer = new int[n]; } // s11n stuff friend class boost::serialization::access; template void save(Archive & ar, const unsigned int version) const { // store length of buffer ar & buflen; // store data itself for (int j = 0; j < buflen; j++) ar & buffer[j]; } template void load(Archive & ar, const unsigned int version) { // get length of buffer ar & buflen; // create buffer with appropriate length deleteBuffer(); init(buflen); // reat data into buffer for (int j = 0; j < buflen; j++) ar & buffer[j]; } BOOST_SERIALIZATION_SPLIT_MEMBER() /* // just to try it with serialize alone: template void serialize(Archive &ar, const unsigned int version) { ar & buflen; } */ }; int main() { // check the save operation std::stringstream ssq (std::stringstream::in | std::stringstream::out); { const Foo rp(10); boost::archive::text_oarchive oa(ssq); oa << rp; } std::streambuf* s11nbuf = ssq.rdbuf(); // load { std::stringstream ss (std::stringstream::in| std::stringstream::out); ss << s11nbuf << std::endl; std::cout << ssq.str() << std::endl; Foo rp(1); boost::archive::text_iarchive ia(ss); // restore from the archive ia >> rp; std::cout << rp << std::endl; } }