1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
2 | // test_null_ptr.cpp: test implementation level trait |
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> // NULL |
12 | #include <cstdio> // remove |
13 | #include <fstream> |
14 | |
15 | #include <boost/config.hpp> |
16 | #if defined(BOOST_NO_STDC_NAMESPACE) |
17 | namespace std{ |
18 | using ::remove; |
19 | } |
20 | #endif |
21 | |
22 | #include "test_tools.hpp" |
23 | #include <boost/serialization/base_object.hpp> |
24 | |
25 | class polymorphic_base |
26 | { |
27 | friend class boost::serialization::access; |
28 | template<class Archive> |
29 | void serialize(Archive & /* ar */, const unsigned int /* file_version */){ |
30 | } |
31 | public: |
32 | virtual ~polymorphic_base(){}; |
33 | }; |
34 | |
35 | class polymorphic_derived1 : public polymorphic_base |
36 | { |
37 | friend class boost::serialization::access; |
38 | template<class Archive> |
39 | void serialize(Archive &ar, const unsigned int /* file_version */){ |
40 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(polymorphic_base); |
41 | } |
42 | }; |
43 | |
44 | // save unregistered polymorphic classes |
45 | void save(const char *testfile) |
46 | { |
47 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
48 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
49 | |
50 | polymorphic_base *rb1 = NULL; |
51 | polymorphic_derived1 *rd1 = NULL; |
52 | |
53 | oa << BOOST_SERIALIZATION_NVP(rb1); |
54 | oa << BOOST_SERIALIZATION_NVP(rd1); |
55 | } |
56 | |
57 | // load unregistered polymorphic classes |
58 | void load(const char *testfile) |
59 | { |
60 | test_istream is(testfile, TEST_STREAM_FLAGS); |
61 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS); |
62 | |
63 | polymorphic_derived1 dummy; |
64 | |
65 | polymorphic_base *rb1 = & dummy; |
66 | polymorphic_derived1 *rd1 = & dummy; |
67 | |
68 | ia >> BOOST_SERIALIZATION_NVP(rb1); |
69 | BOOST_CHECK_MESSAGE(NULL == rb1, "Null pointer not restored" ); |
70 | |
71 | ia >> BOOST_SERIALIZATION_NVP(rd1); |
72 | BOOST_CHECK_MESSAGE(NULL == rd1, "Null pointer not restored" ); |
73 | |
74 | delete rb1; |
75 | delete rd1; |
76 | } |
77 | |
78 | int |
79 | test_main( int /* argc */, char* /* argv */[] ) |
80 | { |
81 | const char * testfile = boost::archive::tmpnam(NULL); |
82 | BOOST_REQUIRE(NULL != testfile); |
83 | save(testfile); |
84 | load(testfile); |
85 | std::remove(filename: testfile); |
86 | return EXIT_SUCCESS; |
87 | } |
88 | |
89 | // EOF |
90 | |