Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / serialization / test / test_exported.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_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 #include <cstddef>
12 #include <fstream>
13
14 #include <boost/config.hpp>
15 #include <cstdio> // remove
16 #if defined(BOOST_NO_STDC_NAMESPACE)
17 namespace std{ 
18     using ::remove;
19 }
20 #endif
21
22 #include <boost/serialization/type_info_implementation.hpp>
23 #include <boost/serialization/export.hpp>
24 #include <boost/serialization/base_object.hpp>
25
26 #include "test_tools.hpp"
27
28 #include <boost/archive/polymorphic_oarchive.hpp>
29 #include <boost/archive/polymorphic_iarchive.hpp>
30
31 #include "polymorphic_derived1.hpp"
32 #include "polymorphic_derived2.hpp"
33
34 // save exported polymorphic class
35 void save_exported(const char *testfile)
36 {
37     test_ostream os(testfile, TEST_STREAM_FLAGS);
38     test_oarchive oa_implementation(os, TEST_ARCHIVE_FLAGS);
39     boost::archive::polymorphic_oarchive & oa_interface = oa_implementation;
40
41     const polymorphic_base *rb1 = new polymorphic_derived1;
42     const polymorphic_base *rb2 = new polymorphic_derived2;
43
44     // export will permit correct serialization
45     // through a pointer to a base class
46     std::cout << "saving polymorphic_derived1 (no_rtti)\n";
47     oa_interface << BOOST_SERIALIZATION_NVP(rb1);
48
49     std::cout << "saving polymorphic_derived2\n";
50     oa_interface << BOOST_SERIALIZATION_NVP(rb2);
51
52     delete rb1;
53     delete rb2;
54 }
55
56 // save exported polymorphic class
57 void load_exported(const char *testfile)
58 {
59     test_istream is(testfile, TEST_STREAM_FLAGS);
60     test_iarchive  ia_implementation(is, TEST_ARCHIVE_FLAGS);
61     boost::archive::polymorphic_iarchive & ia_interface = ia_implementation;
62
63     polymorphic_base *rb1 = NULL;
64     polymorphic_base *rb2 = NULL;
65
66     // export will permit correct serialization
67     // through a pointer to a base class
68     ia_interface >> BOOST_SERIALIZATION_NVP(rb1);
69     BOOST_CHECK_MESSAGE(
70         boost::serialization::type_info_implementation<polymorphic_derived1>
71             ::type::get_const_instance()
72         == 
73         * boost::serialization::type_info_implementation<polymorphic_base>
74             ::type::get_const_instance().get_derived_extended_type_info(*rb1),
75         "restored pointer b1 not of correct type"
76     );
77
78     ia_interface >> BOOST_SERIALIZATION_NVP(rb2);
79     BOOST_CHECK_MESSAGE(
80         boost::serialization::type_info_implementation<polymorphic_derived2>
81             ::type::get_const_instance()
82         == 
83         * boost::serialization::type_info_implementation<polymorphic_base>
84             ::type::get_const_instance().get_derived_extended_type_info(*rb2),
85         "restored pointer b2 not of correct type"
86     );
87
88     delete rb1;
89     delete rb2;
90 }
91
92 int
93 test_main( int /* argc */, char* /* argv */[] )
94 {
95     const char * testfile = boost::archive::tmpnam(NULL);
96     BOOST_REQUIRE(NULL != testfile);
97
98     save_exported(testfile);
99     load_exported(testfile);
100     std::remove(testfile);
101     return EXIT_SUCCESS;
102 }
103
104 // EOF