]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/serialization/test/test_dll_exported.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / serialization / test / test_dll_exported.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_dll_exported.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 // should pass compilation and execution
10
11 // This is an overly complex test. The purpose of this test is to
12 // demostrate and test the ability to serialize a hiarchy of class
13 // through a base class pointer even though those class might be
14 // implemente in different dlls and use different extended type info
15 // systems.
16 //
17 // polymorphic_ base is locally declared and defined. It use the
18 // "no_rtti" extended type info system.
19
20 // polymorphic_derived1 is locally declared and defined. It uses
21 // the default "type_id" extended type info system
22
23 // polymorphic_derived2 is declared in polymorphic_derived.hpp
24 // and defined in dll_polymorphic_derived2. It uses the typeid
25 // system.
26
27 #include <cstddef> // NULL
28 #include <fstream>
29
30 #include <cstdio> // remove
31 #include <boost/config.hpp>
32 #if defined(BOOST_NO_STDC_NAMESPACE)
33 namespace std{
34 using ::remove;
35 }
36 #endif
37
38 // for now, only test with simple text archive
39 #define BOOST_ARCHIVE_TEST text_archive.hpp
40 #include "test_tools.hpp"
41
42 #include <boost/archive/archive_exception.hpp>
43
44 #include <boost/serialization/base_object.hpp>
45 #include <boost/serialization/export.hpp>
46 #include <boost/serialization/access.hpp>
47
48 #include "polymorphic_base.hpp"
49
50 class polymorphic_derived1 : public polymorphic_base
51 {
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(polymorphic_base);
56 }
57 virtual const char * get_key() const {
58 return "polymorphic_derived1";
59 }
60 public:
61 virtual ~polymorphic_derived1(){}
62 };
63
64 BOOST_CLASS_EXPORT_KEY(polymorphic_derived1)
65 BOOST_CLASS_EXPORT_IMPLEMENT(polymorphic_derived1)
66
67 // MWerks users can do this to make their code work
68 BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(polymorphic_base, polymorphic_derived1)
69
70 #define POLYMORPHIC_DERIVED2_IMPORT
71 #include "polymorphic_derived2.hpp"
72
73 // save exported polymorphic class
74 void save_exported(const char *testfile)
75 {
76 test_ostream os(testfile, TEST_STREAM_FLAGS);
77 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
78
79 polymorphic_base *rb1 = new polymorphic_derived1;
80 polymorphic_base *rb2 = new polymorphic_derived2;
81 polymorphic_derived2 *rd21 = new polymorphic_derived2;
82
83 // export will permit correct serialization
84 // through a pointer to a base class
85 oa << BOOST_SERIALIZATION_NVP(rb1);
86 oa << BOOST_SERIALIZATION_NVP(rb2);
87 oa << BOOST_SERIALIZATION_NVP(rd21);
88
89 delete rd21;
90 delete rb2;
91 delete rb1;
92 }
93
94 // save exported polymorphic class
95 void load_exported(const char *testfile)
96 {
97 test_istream is(testfile, TEST_STREAM_FLAGS);
98 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
99
100 polymorphic_base *rb1 = NULL;
101 polymorphic_base *rb2 = NULL;
102 polymorphic_derived2 *rd21 = NULL;
103
104 // export will permit correct serialization
105 // through a pointer to a base class
106 ia >> BOOST_SERIALIZATION_NVP(rb1);
107 BOOST_CHECK_MESSAGE(
108 boost::serialization::type_info_implementation<polymorphic_derived1>
109 ::type::get_const_instance()
110 ==
111 * boost::serialization::type_info_implementation<polymorphic_base>
112 ::type::get_const_instance().get_derived_extended_type_info(*rb1),
113 "restored pointer b1 not of correct type"
114 );
115 ia >> BOOST_SERIALIZATION_NVP(rb2);
116 BOOST_CHECK_MESSAGE(
117 boost::serialization::type_info_implementation<polymorphic_derived2>
118 ::type::get_const_instance()
119 ==
120 * boost::serialization::type_info_implementation<polymorphic_base>
121 ::type::get_const_instance().get_derived_extended_type_info(*rb2),
122 "restored pointer b2 not of correct type"
123 );
124 ia >> BOOST_SERIALIZATION_NVP(rd21);
125 BOOST_CHECK_MESSAGE(
126 boost::serialization::type_info_implementation<polymorphic_derived2>
127 ::type::get_const_instance()
128 ==
129 * boost::serialization::type_info_implementation<polymorphic_derived2>
130 ::type::get_const_instance().get_derived_extended_type_info(*rd21),
131 "restored pointer d2 not of correct type"
132 );
133 delete rd21;
134 delete rb2;
135 delete rb1;
136 }
137
138 int
139 test_main( int /* argc */, char* /* argv */[] )
140 {
141 const char * testfile = boost::archive::tmpnam(NULL);
142 BOOST_REQUIRE(NULL != testfile);
143
144 save_exported(testfile);
145 load_exported(testfile);
146 std::remove(testfile);
147 return EXIT_SUCCESS;
148 }
149
150 // EOF