I'm able to serialize an object through an abstract base using boost::serialization. However, when I try to add deserialization, I get compile errors about the abstract base. Here's my serialization code that works:
/*
g++ -Iinclude/ -Llib -lboost_serialization ~/Desktop/ser_ex.cpp -o stest
*/
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <boost/serialization/export.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <memory>
namespace bser = boost::serialization;
using namespace std;
namespace foo {
class AA
{
public:
virtual void foo() = 0;
virtual ~AA() {}
std::string name;
template<class Archive>
void serialize(Archive & ar, unsigned int )
{
ar & name;
}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT( AA );
class BB : public AA
{
public:
virtual void foo() = 0;
virtual void bar() = 0;
virtual ~BB () {}
int thing;
template<class Archive>
void serialize(Archive & ar, unsigned int )
{
ar.template register_type< AA >();
ar & boost::serialization::base_object<AA>(*this);
ar & thing;
}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT( BB );
class CC : public BB
{
public:
virtual void foo() {}
virtual void bar() {}
virtual ~CC() {}
int otherThing;
template<class Archive>
void serialize(Archive & ar, unsigned int )
{
ar.template register_type< BB >();
ar & boost::serialization::base_object<BB>(*this);
ar & otherThing;
}
};
}
BOOST_CLASS_EXPORT_KEY(foo::CC)
BOOST_CLASS_EXPORT_IMPLEMENT(foo::CC)
int main (int , char const **)
{
std::vector< shared_ptr<foo::AA> > vv;
vv.push_back( make_shared<foo::CC>() );
std::ostringstream oss;
boost::archive::binary_oarchive out_archive( oss );
out_archive << vv;
std::cout << oss.str() << std::endl;
}
Just add this line right below the include for binary_oarchive.hpp causes the code not to compile.
#include <boost/archive/binary_iarchive.hpp>
dawilcox-macOS:debug dawilcox$ make foo
dependencies: foo_main.o
building: foo_main.o
In file included from /Users/dawilcox/src/axle/foo_main.cpp:5:
In file included from /usr/local/include/boost/serialization/serialization.hpp:43:
/usr/local/include/boost/serialization/access.hpp:130:17: error: allocating an object of abstract class type 'foo::AA'
::new(t)T;
Boost is complaining that it can't instantiate my base type. That's kind of the point -- it's an abstract base type.
What's the proper way to deserialize an abstract base?