| 1 | #include <boost/archive/binary_oarchive.hpp>
|
|---|
| 2 | #include <boost/archive/binary_iarchive.hpp>
|
|---|
| 3 | #include <boost/serialization/serialization.hpp>
|
|---|
| 4 | #include <fstream>
|
|---|
| 5 |
|
|---|
| 6 | using namespace boost;
|
|---|
| 7 | using namespace archive;
|
|---|
| 8 | using namespace serialization;
|
|---|
| 9 |
|
|---|
| 10 | struct B{
|
|---|
| 11 | template<class Archive>
|
|---|
| 12 | void serialize(Archive &ar,unsigned int){
|
|---|
| 13 | ar & b;
|
|---|
| 14 | }
|
|---|
| 15 | int b;
|
|---|
| 16 | };
|
|---|
| 17 |
|
|---|
| 18 | struct A{
|
|---|
| 19 | template<class Archive>
|
|---|
| 20 | void serialize(Archive &ar,unsigned int){
|
|---|
| 21 | ar & bptr & b;
|
|---|
| 22 | //ar & b & bptr;
|
|---|
| 23 | }
|
|---|
| 24 | B *bptr;
|
|---|
| 25 | B b;
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | int main(){
|
|---|
| 29 | {
|
|---|
| 30 | A a; a.b.b=123; a.bptr=&a.b;
|
|---|
| 31 | std::ofstream os;
|
|---|
| 32 | os.open("test.bin",std::ios::binary);
|
|---|
| 33 | binary_oarchive oa(os);
|
|---|
| 34 | oa << a;
|
|---|
| 35 | }
|
|---|
| 36 | {
|
|---|
| 37 | A a;
|
|---|
| 38 | std::ifstream is;
|
|---|
| 39 | is.open("test.bin",std::ios::binary);
|
|---|
| 40 | binary_iarchive ia(is);
|
|---|
| 41 | ia >> a;
|
|---|
| 42 | assert(a.bptr == &a.b);
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|