Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/test/test_derived.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.3 KB
Line 
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// test_derived.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 <fstream>
12
13#include <cstdio> // remove
14#include <boost/config.hpp>
15#if defined(BOOST_NO_STDC_NAMESPACE)
16namespace std{ 
17    using ::remove;
18}
19#endif
20
21#include <boost/archive/archive_exception.hpp>
22#include "test_tools.hpp"
23#include <boost/preprocessor/stringize.hpp>
24#include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
25
26#include <boost/serialization/base_object.hpp>
27
28class base
29{
30    friend class boost::serialization::access;
31    template<class Archive>
32    void serialize(Archive & /* ar */, const unsigned int /* file_version */){
33    }
34};
35
36class derived1 : public base
37{
38    friend class boost::serialization::access;
39    template<class Archive>
40    void serialize(Archive &ar, const unsigned int /* file_version */){
41        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base);
42    }
43};
44
45class derived2 : public base
46{
47    friend class boost::serialization::access;
48    template<class Archive>
49    void serialize(Archive &ar, const unsigned int /* file_version */){
50        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(base);
51    }
52};
53
54// save non-polymorphic classes through a base class pointer
55void save_derived(const char *testfile)
56{
57    test_ostream os(testfile, TEST_STREAM_FLAGS);
58    test_oarchive oa(os);
59
60    // registration not necessary when serializing the most derived pointer
61    derived1 *d1 = new derived1;
62    derived2 *d2 = new derived2;
63    oa << BOOST_SERIALIZATION_NVP(d1) << BOOST_SERIALIZATION_NVP(d2);
64
65    // upcasting non-polymorphic pointers may not lead to the expected
66    // result. In the current type id system
67    base *b1 = d1;
68    base *b2 = d2;
69   
70    // Warning, the current type id system does not yield true
71    // type id for non-polymorphic types
72    const boost::serialization::extended_type_info & this_type
73        = * boost::serialization::type_info_implementation<base>::type
74            ::get_instance();
75    // retrieve the true type of the object pointed to
76    const boost::serialization::extended_type_info & true_type
77        = * boost::serialization::type_info_implementation<base>::type
78            ::get_derived_extended_type_info(*b1);
79
80    BOOST_WARN_MESSAGE(
81        (this_type != true_type), 
82        "current type id system does not support non-polymorphic types"
83    );
84
85    oa << BOOST_SERIALIZATION_NVP(b1);
86    oa << BOOST_SERIALIZATION_NVP(b2);
87
88    delete d1;
89    delete d2;
90}
91
92// save non-polymorphic classes through a base class pointer
93void load_derived(const char *testfile)
94{
95    test_istream is(testfile, TEST_STREAM_FLAGS);
96    test_iarchive ia(is);
97
98    // registration not necessary when serializing the most derived pointer
99    derived1 *d1 = NULL;
100    derived2 *d2 = NULL;
101    ia >> BOOST_SERIALIZATION_NVP(d1) >> BOOST_SERIALIZATION_NVP(d2);
102
103    // upcasting non-polymorphic pointers may not lead to the expected
104    // result. In the current type id system
105    base *b1 = NULL;
106    base *b2 = NULL;
107   
108    // Warning, the current type id system does not yield true
109    // type id for non-polymorphic types
110    const boost::serialization::extended_type_info & this_type
111        = * boost::serialization::type_info_implementation<base>::type
112            ::get_instance();
113    // retrieve the true type of the object pointed to
114    const boost::serialization::extended_type_info & true_type
115        = * boost::serialization::type_info_implementation<base>::type
116            ::get_derived_extended_type_info(*b1);
117           
118    BOOST_WARN_MESSAGE(
119        this_type != true_type, 
120        "current type id system does fails for non-polymorphic types"
121    );
122
123    // note: this will produce incorrect results for non-polymorphic classes
124    ia >> BOOST_SERIALIZATION_NVP(b1);
125    ia >> BOOST_SERIALIZATION_NVP(b2);
126
127    delete d1;
128    delete d2;
129}
130
131int
132test_main( int /* argc */, char* /* argv */[] )
133{
134    const char * testfile = boost::archive::tmpnam(NULL);
135    BOOST_REQUIRE(NULL != testfile);
136
137    save_derived(testfile);
138    load_derived(testfile);
139    std::remove(testfile);
140    return EXIT_SUCCESS;
141}
142
143// EOF
Note: See TracBrowser for help on using the repository browser.