]>
git.proxmox.com Git - ceph.git/blob - 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
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)
12 #include <boost/config.hpp>
13 #include <cstdio> // remove
14 #if defined(BOOST_NO_STDC_NAMESPACE)
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"
26 #include <boost/serialization/export.hpp>
27 #include <boost/serialization/nvp.hpp>
29 // This is a simple class. It contains a counter of the number
30 // of objects of this class which have been instantiated.
34 friend class boost::serialization::access
;
36 template<class Archive
>
37 void serialize(Archive
& ar
, const unsigned int /* file_version */){
38 ar
& BOOST_SERIALIZATION_NVP(x
);
42 A(){++count
;} // default constructor
43 virtual ~A(){--count
;} // default destructor
48 // B is a subclass of A
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
);
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
)
73 const char * testfile
= boost::archive::tmpnam(NULL
);
74 BOOST_REQUIRE(NULL
!= testfile
);
78 std::ofstream
os(testfile
);
80 oa
<< BOOST_SERIALIZATION_NVP(first
);
81 oa
<< BOOST_SERIALIZATION_NVP(second
);
84 // Clear the pointers, thereby destroying the objects they contain
90 std::ifstream
is(testfile
);
92 ia
>> BOOST_SERIALIZATION_NVP(first
);
93 ia
>> BOOST_SERIALIZATION_NVP(second
);
95 BOOST_CHECK(first
== second
);
96 std::remove(testfile
);
99 using namespace boost::archive
;
101 // This does the tests
102 int test_main(int /* argc */, char * /* argv */[])
104 // Try to save and load pointers to As, to a text archive
107 test_save_and_load
<text_oarchive
, text_iarchive
>(a
, a1
);
109 // Try to save and load pointers to Bs, to a text archive
112 test_save_and_load
<text_oarchive
, text_iarchive
>(b
, b1
);
114 // Try to save and load pointers to As, to an xml archive
115 test_save_and_load
<xml_oarchive
, xml_iarchive
>(a
, a1
);
117 // Try to save and load pointers to Bs, to an xml archive
118 test_save_and_load
<xml_oarchive
, xml_iarchive
>(b
, b1
);