| 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)
|
|---|
| 16 | namespace 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 |
|
|---|
| 27 | class 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 | }
|
|---|
| 34 | protected:
|
|---|
| 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(){};
|
|---|
| 42 | public:
|
|---|
| 43 | virtual bool operator==(const Base &rhs) const {
|
|---|
| 44 | return false;
|
|---|
| 45 | }// = 0;
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | class Derived : private Base {
|
|---|
| 49 | friend class boost::serialization::access;
|
|---|
| 50 | private:
|
|---|
| 51 | Base & base_cast(){
|
|---|
| 52 | return static_cast<Base &>(*this);
|
|---|
| 53 | }
|
|---|
| 54 | template<class Archive>
|
|---|
| 55 | void serialize(Archive & ar, const unsigned int version){
|
|---|
| 56 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
|
|---|
| 57 | ar & base_cast();
|
|---|
| 58 | }
|
|---|
| 59 | public:
|
|---|
| 60 | virtual bool operator==(const Derived &rhs) const {
|
|---|
| 61 | return Base::equals(static_cast<const Base &>(rhs));
|
|---|
| 62 | }
|
|---|
| 63 | Derived(int i = 0) :
|
|---|
| 64 | Base(i)
|
|---|
| 65 | {}
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | BOOST_CLASS_EXPORT(Derived)
|
|---|
| 69 |
|
|---|
| 70 | int
|
|---|
| 71 | test_main( int /* argc */, char* /* argv */[] )
|
|---|
| 72 | {
|
|---|
| 73 | const char * testfile = boost::archive::tmpnam(NULL);
|
|---|
| 74 | BOOST_REQUIRE(NULL != testfile);
|
|---|
| 75 |
|
|---|
| 76 | Derived a(1), a1(2);
|
|---|
| 77 | {
|
|---|
| 78 | test_ostream os(testfile, TEST_STREAM_FLAGS);
|
|---|
| 79 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
|
|---|
| 80 | oa << boost::serialization::make_nvp("a", a);
|
|---|
| 81 | }
|
|---|
| 82 | {
|
|---|
| 83 | test_istream is(testfile, TEST_STREAM_FLAGS);
|
|---|
| 84 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
|
|---|
| 85 | ia >> boost::serialization::make_nvp("a", a1);
|
|---|
| 86 | }
|
|---|
| 87 | BOOST_CHECK_EQUAL(a, a1);
|
|---|
| 88 | std::remove(testfile);
|
|---|
| 89 |
|
|---|
| 90 | Base *ta = static_cast<Base *>(&a);
|
|---|
| 91 | Base *ta1 = NULL;
|
|---|
| 92 |
|
|---|
| 93 | {
|
|---|
| 94 | test_ostream os(testfile, TEST_STREAM_FLAGS);
|
|---|
| 95 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
|
|---|
| 96 | oa << boost::serialization::make_nvp("ta", ta);
|
|---|
| 97 | }
|
|---|
| 98 | {
|
|---|
| 99 | test_istream is(testfile, TEST_STREAM_FLAGS);
|
|---|
| 100 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
|
|---|
| 101 | ia >> boost::serialization::make_nvp("ta", ta1);
|
|---|
| 102 | }
|
|---|
| 103 | BOOST_CHECK(ta != ta1);
|
|---|
| 104 | BOOST_CHECK(*ta == *ta1);
|
|---|
| 105 | //BOOST_CHECK(*static_cast<Derived *>(ta) == *static_cast<Derived *>(ta1));
|
|---|
| 106 |
|
|---|
| 107 | std::remove(testfile);
|
|---|
| 108 |
|
|---|
| 109 | return 0;
|
|---|
| 110 | }
|
|---|