| 1 | #include <strstream>
|
|---|
| 2 | #define BOOST_TEST_MAIN
|
|---|
| 3 | #define BOOST_TEST_MODULE Serialization problem
|
|---|
| 4 | #include <boost/test/unit_test.hpp>
|
|---|
| 5 | #include <boost/archive/text_oarchive.hpp>
|
|---|
| 6 | #include <boost/archive/text_iarchive.hpp>
|
|---|
| 7 | #include <boost/serialization/base_object.hpp>
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | class A
|
|---|
| 11 | {
|
|---|
| 12 | friend class boost::serialization::access;
|
|---|
| 13 | template<class Archive>
|
|---|
| 14 | void serialize(Archive & ar, const unsigned int version) {}
|
|---|
| 15 | public:
|
|---|
| 16 | virtual ~A(){}
|
|---|
| 17 | };
|
|---|
| 18 | class RealBase : public A
|
|---|
| 19 | {
|
|---|
| 20 | friend class boost::serialization::access;
|
|---|
| 21 | template<class Archive>
|
|---|
| 22 | void serialize(Archive & ar, const unsigned int version)
|
|---|
| 23 | {
|
|---|
| 24 | ar & boost::serialization::base_object<A>(*this);
|
|---|
| 25 | }
|
|---|
| 26 | public:
|
|---|
| 27 | };
|
|---|
| 28 | class B : public RealBase
|
|---|
| 29 | {
|
|---|
| 30 | friend class boost::serialization::access;
|
|---|
| 31 | protected:
|
|---|
| 32 | RealBase * m_arg;
|
|---|
| 33 | template<class Archive>
|
|---|
| 34 | void serialize(Archive & ar, const unsigned int version)
|
|---|
| 35 | {
|
|---|
| 36 | ar & boost::serialization::base_object<RealBase>(*this);
|
|---|
| 37 | ar & m_arg;
|
|---|
| 38 | }
|
|---|
| 39 | public:
|
|---|
| 40 | void add_child(RealBase *c)
|
|---|
| 41 | {
|
|---|
| 42 | m_arg=c;
|
|---|
| 43 | };
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | class C : public RealBase
|
|---|
| 47 | {
|
|---|
| 48 | friend class boost::serialization::access;
|
|---|
| 49 | template<class Archive>
|
|---|
| 50 | void serialize(Archive & ar, const unsigned int version)
|
|---|
| 51 | {
|
|---|
| 52 | ar & boost::serialization::base_object<RealBase>(*this);
|
|---|
| 53 | }
|
|---|
| 54 | public:
|
|---|
| 55 | };
|
|---|
| 56 | BOOST_AUTO_TEST_CASE(test_serialization)
|
|---|
| 57 | {
|
|---|
| 58 |
|
|---|
| 59 | std::strstream ld;
|
|---|
| 60 | boost::archive::text_oarchive oa(ld);
|
|---|
| 61 |
|
|---|
| 62 | B * const za1=new B;
|
|---|
| 63 | C za2;
|
|---|
| 64 | za1->add_child(new C);
|
|---|
| 65 | oa << const_cast<const C &>(za2);
|
|---|
| 66 | oa << za1;
|
|---|
| 67 | delete za1;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|