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

source code of boost/libs/serialization/test/test_mult_archive_types.cpp