Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/test/test_cyclic_ptrs.cpp @ 33

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

updated boost from 1_33_1 to 1_34_1

File size: 4.2 KB
Line 
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// test_cyclic_ptrs.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
22#include "test_tools.hpp"
23#include <boost/preprocessor/stringize.hpp>
24#include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
25#include <boost/detail/no_exceptions_support.hpp>
26
27#include <boost/serialization/nvp.hpp>
28#include <boost/serialization/version.hpp>
29#include <boost/serialization/base_object.hpp>
30
31#include "A.hpp"
32
33///////////////////////////////////////////////////////
34// class with a member which refers to itself
35class J : public A
36{
37private:
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(A);
42        ar & BOOST_SERIALIZATION_NVP(j);
43    }
44public:
45    bool operator==(const J &rhs) const;
46    J *j;
47    J(J *_j) : j(_j) {}
48    J() : j(NULL){}
49};
50
51BOOST_CLASS_VERSION(J, 6)
52
53bool J::operator==(const J &rhs) const
54{
55    return static_cast<const A &>(*this) == static_cast<const A &>(rhs);
56}
57
58///////////////////////////////////////////////////////
59// class with members that refer to each other
60// this is an example of a class that, as written, cannot
61// be serialized with this system.  The problem is that the
62// serialization of the first member - j1 , provokes serialization
63// of those objects which it points to either directly or indirectly.
64// When those objects are subsequently serialized, it is discovered
65// that have already been serialized through pointers.  This is
66// detected by the system and an exception - pointer_conflict -
67// is thrown.  Permiting this to go undetected would result in the
68// creation of multiple equal objects rather than the original
69// structure. 
70class K
71{
72    J j1;
73    J j2;
74    J j3;
75    friend class boost::serialization::access;
76    template<class Archive>
77    void serialize(
78        Archive &ar,
79        const unsigned int /* file_version */
80    ){
81        ar & BOOST_SERIALIZATION_NVP(j1);
82        ar & BOOST_SERIALIZATION_NVP(j2);
83        ar & BOOST_SERIALIZATION_NVP(j3);
84    }
85public:
86    bool operator==(const K &rhs) const;
87    K();
88};
89
90K::K()
91: j1(&j2), j2(&j3), j3(&j1)
92{
93}
94
95bool K::operator==(const K &rhs) const
96{
97    return
98        j1.j == & j2
99        && j2.j == & j3
100        && j3.j == & j1
101        && j1 == rhs.j1
102        && j2 == rhs.j2
103        && j3 == rhs.j3
104    ;
105}
106
107int test_main( int /* argc */, char* /* argv */[] )
108{
109    const char * testfile = boost::archive::tmpnam(NULL);
110    BOOST_REQUIRE(NULL != testfile);
111
112    K k;
113    boost::archive::archive_exception exception(
114        boost::archive::archive_exception::no_exception
115    );
116    {   
117        test_ostream os(testfile, TEST_STREAM_FLAGS);
118        test_oarchive oa(os);
119        BOOST_TRY {
120            oa << BOOST_SERIALIZATION_NVP(k);
121        }
122        BOOST_CATCH (boost::archive::archive_exception ae){
123            exception = ae;
124        }
125        BOOST_CATCH_END
126        BOOST_CHECK(
127            exception.code == boost::archive::archive_exception::pointer_conflict
128        );
129    }
130    // if exception wasn't invoked
131    if(exception.code == boost::archive::archive_exception::no_exception){
132        // try to read the archive
133        test_istream is(testfile, TEST_STREAM_FLAGS);
134        test_iarchive ia(is);
135        exception = boost::archive::archive_exception(
136            boost::archive::archive_exception::no_exception
137        );
138        BOOST_TRY {
139            ia >> BOOST_SERIALIZATION_NVP(k);
140        }
141        BOOST_CATCH (boost::archive::archive_exception ae){
142            exception = ae;
143        }
144        BOOST_CATCH_END
145        BOOST_CHECK(
146            exception.code == boost::archive::archive_exception::pointer_conflict
147        );
148    }
149    std::remove(testfile);
150    return EXIT_SUCCESS;
151}
152
153// EOF
Note: See TracBrowser for help on using the repository browser.