Ticket #5299: s11n_bug.cpp

File s11n_bug.cpp, 1.9 KB (added by Wang Peng <wangp.thu@…>, 12 years ago)

minimal example

Line 
1#include <fstream>
2
3#include <boost/archive/binary_iarchive.hpp>
4#include <boost/archive/binary_oarchive.hpp>
5#include <boost/serialization/vector.hpp>
6#include <boost/serialization/shared_ptr.hpp>
7#include <boost/serialization/nvp.hpp>
8
9typedef std::vector<int> B;
10
11class A;
12
13typedef boost::shared_ptr<A> a_ptr;
14typedef boost::shared_ptr<B> b_ptr;
15
16class A{
17public:
18 A(){}
19 A(b_ptr b) : b_(b){}
20 virtual ~A(){}
21 b_ptr b_;
22
23 friend class boost::serialization::access;
24 template<class Archive>
25 void serialize(Archive & ar, const unsigned int version){
26 ar & BOOST_SERIALIZATION_NVP(b_);
27 }
28};
29
30class DA : public A{
31public:
32 DA(){}
33 DA(b_ptr b) : A(b){}
34 template<class Archive>
35 void serialize(Archive & ar, const unsigned int version){
36 ar & boost::serialization::make_nvp("base", boost::serialization::base_object<A>(*this));
37 }
38};
39
40
41typedef std::vector<a_ptr> VA;
42typedef boost::shared_ptr<VA> va_ptr;
43
44typedef std::vector<va_ptr> VVA;
45typedef boost::shared_ptr<VVA> vva_ptr;
46
47template <typename Archive>
48void register_types(Archive& ar){
49 ar.template register_type<DA>();
50}
51
52b_ptr make_b(){
53 return b_ptr(new B);
54}
55
56a_ptr make_a(){
57 return a_ptr(new DA(make_b()));
58}
59
60va_ptr make_va(){
61 VA va;
62 va.push_back(make_a());
63 va.push_back(make_a());
64 return va_ptr(new VA(va));
65}
66
67vva_ptr make_vva(){
68 VVA vva;
69 vva.push_back(make_va());
70 vva.push_back(make_va());
71 return vva_ptr(new VVA(vva));
72}
73
74int main(){
75 std::string filename = "tmp";
76 std::vector<vva_ptr> thing;
77 thing.push_back(make_vva());
78 thing.push_back(make_vva());
79 {
80 std::ofstream ofs(filename.c_str());
81 boost::archive::binary_oarchive oa(ofs);
82 register_types(oa);
83 oa << BOOST_SERIALIZATION_NVP(thing);
84 }
85 {
86 std::ifstream ifs(filename.c_str());
87 boost::archive::binary_iarchive ia(ifs);
88 register_types(ia);
89 ia >> BOOST_SERIALIZATION_NVP(thing);
90 }
91 return 0;
92}