#include #include #include #include #include #include template struct Vector { Vector() {} Vector(int sz) : size(sz), data(new T[sz]) {} T& operator[](int i) { return data[i]; } template void save(Archive& ar, int) { } template void save(Archive& ar, int) const { ar & size; for (int i = size; i-- > 0; ar & data[i]); } template void load(Archive& ar, int) { ar & size; data = new T[size]; for (int i = size; i-- > 0; ar & data[i]); } template void serialize(Archive& ar, int file_version) { boost::serialization::split_member(ar, *this, file_version); } int size; T* data; }; struct Triggerable { public: virtual void trigger() = 0; template void serialize(Archive&, int) { } }; BOOST_IS_ABSTRACT(Triggerable); struct Event : Triggerable { Event() {} void trigger() {} template void serialize(Archive& ar, int) { ar & boost::serialization::base_object(*this); ar & a; } int a; }; struct Cell { Cell() : e(0) {} Event* e; template void serialize(Archive& ar, int) { ar & e; } }; struct EventQueue { EventQueue() {} EventQueue(int sz, bool full) : cells(sz), event(sz) { for (int i = sz/2; i-- > 0; cells[2*i+1].e = cells[2*i].e = &event[i]); } template void serialize(Archive& ar, int) { ar & cells & event ; // ar & event & cells; // works fine } Vector event; Vector cells; }; template void store(const T& v) { std::ofstream ofs("filename"); boost::archive::binary_oarchive oa(ofs); oa << v; } template void restore(T& v) { std::ifstream ifs("filename", std::ios::binary); boost::archive::binary_iarchive ia(ifs); ia >> v; } int main(int argc, char**) { EventQueue eq(5, argc > 1); for (int i = 5; i-- > 0; printf("%d=%p\n",i,eq.cells[i].e)); fflush(stdout); store(eq); EventQueue neq; restore(neq); for (int i = 5; i-- > 0; printf("%d=%p\n",i,neq.cells[i].e)); fflush(stdout); return 0; }