1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// test_private_base.cpp
3
4// (C) Copyright 2009 Eric Moyer - 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// invoke header for a custom archive test.
12
13#include <fstream>
14#include <boost/config.hpp>
15#if defined(BOOST_NO_STDC_NAMESPACE)
16namespace std{
17 using ::remove;
18}
19#endif
20
21#include <boost/serialization/access.hpp>
22#include <boost/serialization/base_object.hpp>
23#include <boost/serialization/export.hpp>
24
25#include "test_tools.hpp"
26
27class Base {
28 friend class boost::serialization::access;
29 int m_i;
30 template<class Archive>
31 void serialize(Archive & ar, const unsigned int version){
32 ar & BOOST_SERIALIZATION_NVP(m_i);
33 }
34protected:
35 bool equals(const Base &rhs) const {
36 return m_i == rhs.m_i;
37 }
38 Base(int i = 0) :
39 m_i(i)
40 {}
41 virtual ~Base(){};
42public:
43 virtual bool operator==(const Base &rhs) const {
44 return false;
45 }// = 0;
46};
47
48class Derived : private Base {
49 friend class boost::serialization::access;
50public:
51 virtual bool operator==(const Derived &rhs) const {
52 return Base::equals(rhs: static_cast<const Base &>(rhs));
53 }
54 Derived(int i = 0) :
55 Base(i)
56 {}
57};
58
59//BOOST_CLASS_EXPORT(Derived)
60
61int
62test_main( int /* argc */, char* /* argv */[] )
63{
64 const char * testfile = boost::archive::tmpnam(NULL);
65 BOOST_REQUIRE(NULL != testfile);
66
67 Derived a(1), a1(2);
68 {
69 test_ostream os(testfile, TEST_STREAM_FLAGS);
70 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
71 oa << boost::serialization::make_nvp(n: "a", v&: a);
72 }
73 {
74 test_istream is(testfile, TEST_STREAM_FLAGS);
75 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
76 ia >> boost::serialization::make_nvp(n: "a", v&: a1);
77 }
78 BOOST_CHECK_EQUAL(a, a1);
79 std::remove(filename: testfile);
80
81 //Base *ta = static_cast<Base *>(&a);
82 //Base *ta1 = NULL;
83
84 Derived *ta = &a;
85 Derived *ta1 = NULL;
86
87 {
88 test_ostream os(testfile, TEST_STREAM_FLAGS);
89 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
90 oa << boost::serialization::make_nvp(n: "ta", v&: ta);
91 }
92 {
93 test_istream is(testfile, TEST_STREAM_FLAGS);
94 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
95 ia >> boost::serialization::make_nvp(n: "ta", v&: ta1);
96 }
97 BOOST_CHECK(ta != ta1);
98 BOOST_CHECK(*ta == *ta1);
99 //BOOST_CHECK(*static_cast<Derived *>(ta) == *static_cast<Derived *>(ta1));
100
101 std::remove(filename: testfile);
102
103 return 0;
104}
105

source code of boost/libs/serialization/test/test_private_base2.cpp