1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
2 | // test_mult_archive_types.cpp |
---|
3 | |
---|
4 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
---|
5 | // Use, modification and distribution is subject to the Boost Software |
---|
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | |
---|
9 | #include <fstream> |
---|
10 | |
---|
11 | #include <cstdio> // remove |
---|
12 | #include <boost/config.hpp> |
---|
13 | #if defined(BOOST_NO_STDC_NAMESPACE) |
---|
14 | namespace std{ |
---|
15 | using ::remove; |
---|
16 | } |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <boost/archive/text_oarchive.hpp> |
---|
20 | #include <boost/archive/text_iarchive.hpp> |
---|
21 | #include <boost/archive/xml_oarchive.hpp> |
---|
22 | #include <boost/archive/xml_iarchive.hpp> |
---|
23 | #include "test_tools.hpp" |
---|
24 | #include <boost/preprocessor/stringize.hpp> |
---|
25 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
---|
26 | |
---|
27 | #include <boost/serialization/export.hpp> |
---|
28 | #include <boost/serialization/nvp.hpp> |
---|
29 | |
---|
30 | // This is a simple class. It contains a counter of the number |
---|
31 | // of objects of this class which have been instantiated. |
---|
32 | class A |
---|
33 | { |
---|
34 | private: |
---|
35 | friend class boost::serialization::access; |
---|
36 | int x; |
---|
37 | template<class Archive> |
---|
38 | void serialize(Archive & ar, const unsigned int /* file_version */){ |
---|
39 | ar & BOOST_SERIALIZATION_NVP(x); |
---|
40 | } |
---|
41 | public: |
---|
42 | static int count; |
---|
43 | A(){++count;} // default constructor |
---|
44 | virtual ~A(){--count;} // default destructor |
---|
45 | }; |
---|
46 | |
---|
47 | |
---|
48 | // B is a subclass of A |
---|
49 | class B : public A |
---|
50 | { |
---|
51 | private: |
---|
52 | friend class boost::serialization::access; |
---|
53 | int y; |
---|
54 | template<class Archive> |
---|
55 | void serialize(Archive & ar, const unsigned int /* file_version */){ |
---|
56 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A); |
---|
57 | } |
---|
58 | public: |
---|
59 | static int count; |
---|
60 | B() : A() {}; |
---|
61 | virtual ~B() {}; |
---|
62 | }; |
---|
63 | |
---|
64 | BOOST_CLASS_EXPORT(A) |
---|
65 | BOOST_CLASS_EXPORT(B) |
---|
66 | |
---|
67 | int A::count = 0; |
---|
68 | |
---|
69 | // Run tests by serializing two shared_ptrs into an archive of type |
---|
70 | // OARCH, clearing them (deleting the objects) and then reloading the |
---|
71 | // objects back from an archive of type OARCH. |
---|
72 | template<class OA, class IA> |
---|
73 | void test_save_and_load(A * first, A * second) |
---|
74 | { |
---|
75 | const char * testfile = boost::archive::tmpnam(NULL); |
---|
76 | BOOST_REQUIRE(NULL != testfile); |
---|
77 | |
---|
78 | // Save |
---|
79 | { |
---|
80 | std::ofstream os(testfile); |
---|
81 | OA oa(os); |
---|
82 | oa << BOOST_SERIALIZATION_NVP(first); |
---|
83 | oa << BOOST_SERIALIZATION_NVP(second); |
---|
84 | } |
---|
85 | |
---|
86 | // Clear the pointers, thereby destroying the objects they contain |
---|
87 | first = NULL; |
---|
88 | second = NULL; |
---|
89 | |
---|
90 | // Load |
---|
91 | { |
---|
92 | std::ifstream is(testfile); |
---|
93 | IA ia(is); |
---|
94 | ia >> BOOST_SERIALIZATION_NVP(first); |
---|
95 | ia >> BOOST_SERIALIZATION_NVP(second); |
---|
96 | } |
---|
97 | BOOST_CHECK(first == second); |
---|
98 | std::remove(testfile); |
---|
99 | } |
---|
100 | |
---|
101 | using namespace boost::archive; |
---|
102 | |
---|
103 | // This does the tests |
---|
104 | int test_main(int /* argc */, char * /* argv */[]) |
---|
105 | { |
---|
106 | // Try to save and load pointers to As, to a text archive |
---|
107 | A * a = new A; |
---|
108 | A * a1 = a; |
---|
109 | test_save_and_load<text_oarchive, text_iarchive>(a, a1); |
---|
110 | |
---|
111 | // Try to save and load pointers to Bs, to a text archive |
---|
112 | B * b = new B; |
---|
113 | B * b1 = b; |
---|
114 | test_save_and_load<text_oarchive, text_iarchive>(b, b1); |
---|
115 | |
---|
116 | // Try to save and load pointers to As, to an xml archive |
---|
117 | test_save_and_load<xml_oarchive, xml_iarchive>(a, a1); |
---|
118 | |
---|
119 | // Try to save and load pointers to Bs, to an xml archive |
---|
120 | test_save_and_load<xml_oarchive, xml_iarchive>(b, b1); |
---|
121 | |
---|
122 | return EXIT_SUCCESS; |
---|
123 | } |
---|