Hello,
I'm trying to serialize many boost::shared_ptr to the same object but through different base classes. When I look at the boost::shared_ptr for the base classes they are not properly set. I created a simple example that show my problem. The test program output is:
Running 1 test case...
In function ABC
In function A
In function B
In function C
In function ABC
In function A
In function A
In function A
I'm using boost 1.33.1 on Linux with gcc 3.4.5 (Red Hat 3.4.5-2).
Thanks for your help,
Michel :)
#include <sstream>
#include <iostream>
#include
#include
#include
#include
#include
#include
#include
#include
//----------------------------------------------------------------------------
//
class IA
{
public:
IA() :
a(1)
{
}
virtual ~IA()
{
}
virtual void fctA() = 0;
protected:
int a;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int i_version)
{
ar & BOOST_SERIALIZATION_NVP(a);
}
};
BOOST_IS_ABSTRACT(IA)
//----------------------------------------------------------------------------
//
class IB
{
public:
IB() :
b(2)
{
}
virtual ~IB()
{
}
virtual void fctB() = 0;
protected:
int b;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int i_version)
{
ar & BOOST_SERIALIZATION_NVP(b);
}
};
BOOST_IS_ABSTRACT(IB)
//----------------------------------------------------------------------------
//
class IC
{
public:
IC() :
c(3)
{
}
virtual ~IC()
{
}
virtual void fctC() = 0;
protected:
int c;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int i_version)
{
ar & BOOST_SERIALIZATION_NVP(c);
}
};
BOOST_IS_ABSTRACT(IC)
//----------------------------------------------------------------------------
//
class ABC : public IA, public IB, public IC
{
public:
ABC() :
abc(4)
{
}
virtual void fctABC()
{
std::cout << "In function ABC" << std::endl;
}
virtual void fctA()
{
std::cout << "In function A" << std::endl;
}
virtual void fctB()
{
std::cout << "In function B" << std::endl;
}
virtual void fctC()
{
std::cout << "In function C" << std::endl;
}
protected:
int abc;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int i_version)
{
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(IA);
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(IB);
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(IC);
ar & BOOST_SERIALIZATION_NVP(abc);
}
};
BOOST_CLASS_EXPORT(ABC)
BOOST_SERIALIZATION_SHARED_PTR(ABC)
BOOST_SERIALIZATION_SHARED_PTR(IA)
BOOST_SERIALIZATION_SHARED_PTR(IB)
BOOST_SERIALIZATION_SHARED_PTR(IC)
//----------------------------------------------------------------------------
//
class SharedPtrTest
{
public:
void testSharedPtr()
{
std::stringstream w_stream;
{
boost::shared_ptr<ABC> abc(new ABC());
boost::shared_ptr<IA> a(abc);
boost::shared_ptr<IB> b(abc);
boost::shared_ptr<IC> c(abc);
abc->fctABC();
a->fctA();
b->fctB();
c->fctC();
boost::archive::text_oarchive oa(w_stream);
oa << BOOST_SERIALIZATION_NVP(abc);
oa << BOOST_SERIALIZATION_NVP(a);
oa << BOOST_SERIALIZATION_NVP(b);
oa << BOOST_SERIALIZATION_NVP(c);
}
{
boost::shared_ptr<ABC> abc;
boost::shared_ptr<IA> a;
boost::shared_ptr<IB> b;
boost::shared_ptr<IC> c;
boost::archive::text_iarchive ia(w_stream);
ia >> BOOST_SERIALIZATION_NVP(abc);
ia >> BOOST_SERIALIZATION_NVP(a);
ia >> BOOST_SERIALIZATION_NVP(b);
ia >> BOOST_SERIALIZATION_NVP(c);
abc->fctABC();
a->fctA();
b->fctB();
c->fctC();
}
}
};
//----------------------------------------------------------------------------
//
class SharedPtrTestSuite : public boost::unit_test::test_suite
{
public:
SharedPtrTestSuite() :
test_suite("SharedPtr")
{
boost::shared_ptr<SharedPtrTest> w_instance(new SharedPtrTest());
add(BOOST_CLASS_TEST_CASE(&SharedPtrTest::testSharedPtr, w_instance));
}
};
//----------------------------------------------------------------------------
//
boost::unit_test::test_suite* init_unit_test_suite(int, char* [])
{
boost::unit_test::test_suite* test = BOOST_TEST_SUITE("Unit test");
test->add(new SharedPtrTestSuite());
return test;
}
Combattez les méchants pourriels... Le filtre SpamGuard vous aide à lutter efficacement contre les pourriels sur le Tout-nouveau Yahoo! Courriel
http://us.rd.yahoo.com/evt=40705/*http://mrd.mail.yahoo.com/try_beta?.intl=c...