| 1 | #include <fstream>
|
|---|
| 2 | #include <iostream>
|
|---|
| 3 | #include <boost/archive/xml_iarchive.hpp>
|
|---|
| 4 | #include <boost/archive/xml_oarchive.hpp>
|
|---|
| 5 | #include <boost/serialization/export.hpp>
|
|---|
| 6 |
|
|---|
| 7 | class A
|
|---|
| 8 | {
|
|---|
| 9 | friend class boost::serialization::access;
|
|---|
| 10 | int i;
|
|---|
| 11 | template<class Archive> void serialize (Archive& ar, const unsigned int version)
|
|---|
| 12 | {
|
|---|
| 13 | ar & BOOST_SERIALIZATION_NVP( i );
|
|---|
| 14 | }
|
|---|
| 15 | virtual void foo() {}
|
|---|
| 16 | };
|
|---|
| 17 |
|
|---|
| 18 | template<typename T> class B: public A
|
|---|
| 19 | {
|
|---|
| 20 | friend class boost::serialization::access;
|
|---|
| 21 | unsigned int a;
|
|---|
| 22 | template<class Archive> void serialize (Archive& ar, const unsigned int version)
|
|---|
| 23 | {
|
|---|
| 24 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( A );
|
|---|
| 25 | ar & BOOST_SERIALIZATION_NVP( a );
|
|---|
| 26 | }
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 | //typedef B<int> bint;
|
|---|
| 30 | BOOST_CLASS_EXPORT( B<int> )
|
|---|
| 31 | BOOST_CLASS_EXPORT( A )
|
|---|
| 32 |
|
|---|
| 33 | int main()
|
|---|
| 34 | {
|
|---|
| 35 | {
|
|---|
| 36 | std::ofstream fs( "test.xml" );
|
|---|
| 37 | BOOST_ASSERT( fs.good() );
|
|---|
| 38 | boost::archive::xml_oarchive archive( fs );
|
|---|
| 39 |
|
|---|
| 40 | A* b = new B<int>();
|
|---|
| 41 | archive & BOOST_SERIALIZATION_NVP( b );
|
|---|
| 42 | delete b;
|
|---|
| 43 | }
|
|---|
| 44 | std::cout << "finished writing" << std::endl;
|
|---|
| 45 | {
|
|---|
| 46 | std::ifstream fs( "test.xml" );
|
|---|
| 47 | BOOST_ASSERT( fs.good() );
|
|---|
| 48 | boost::archive::xml_iarchive archive( fs );
|
|---|
| 49 |
|
|---|
| 50 | A* b;
|
|---|
| 51 | archive & BOOST_SERIALIZATION_NVP( b );
|
|---|
| 52 | delete b;
|
|---|
| 53 | }
|
|---|
| 54 | std::cout << "finished reading" << std::endl;
|
|---|
| 55 | }
|
|---|