]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_mult_archive_types.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / serialization / test / test_mult_archive_types.cpp
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)
15 namespace 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.
31 class A
32 {
33 private:
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 }
40 public:
41 static int count;
42 A(){++count;} // default constructor
43 virtual ~A(){--count;} // default destructor
44 };
45
46 BOOST_CLASS_EXPORT(A)
47
48 // B is a subclass of A
49 class B : public A
50 {
51 private:
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 }
57 public:
58 static int count;
59 B() : A() {};
60 virtual ~B() {};
61 };
62
63 BOOST_CLASS_EXPORT(B)
64
65 int 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.
70 template<class OA, class IA>
71 void 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(testfile);
97 }
98
99 using namespace boost::archive;
100
101 // This does the tests
102 int 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>(a, 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>(b, b1);
113
114 // Try to save and load pointers to As, to an xml archive
115 test_save_and_load<xml_oarchive, xml_iarchive>(a, a1);
116
117 // Try to save and load pointers to Bs, to an xml archive
118 test_save_and_load<xml_oarchive, xml_iarchive>(b, b1);
119
120 return EXIT_SUCCESS;
121 }