I am looking for a (working) example for external serialization of a cluster structure in a DLL. Currently, I cannot find examples for this. In the Boost documentation, there are only a few macros, forums, and newsgroups that simply discuss specific problems with their solutions.
So, I am asking an example for (from outside) serializing a class structure as follows. Along with the code class, I added some code for serialization (which does not work, see below for an error message).
class Foo { public: Foo() { number_ = 0; } virtual ~Foo() {} int getNumber() { return number_; } void setNumber( int var ) { number_ = var; } private: int number_; }; class Bar : public Foo { public: Bar() { doubleNumber_ = 0.0; } virtual ~Bar() {} double getDouble() { return doubleNumber_; } void setDouble( double var ) { doubleNumber_ = var; } private: double doubleNumber_; };
All I have so far is the code:
serializeFoo.h
#ifndef _SERIALIZE_FOO_H_ #define _SERIALIZE_FOO_H_ #include "Foo.h" #include <boost/serialization/split_free.hpp> #include <boost/serialization/version.hpp> namespace boost { namespace serialization { template <typename Archive> void save(Archive& ar, const Foo& object, const unsigned int version) { ar << object.getNumber(); } template <typename Archive> void load(Archive& ar, Foo& object, const unsigned int version) { int number; ar >> number; object.setNumber(number); } }} //namespace brackets BOOST_SERIALIZATION_SPLIT_FREE( Foo ) #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/export.hpp> BOOST_CLASS_EXPORT_KEY( Foo ) #endif //_SERIALIZE_FOO_H_
serializeFoo.cpp
#include "serializeFoo.h" BOOST_CLASS_EXPORT_IMPLEMENT( Foo )
serializeBar.h
#ifndef _SERIALIZE_BAR_H_ #define _SERIALIZE_BAR_H_ #include "Bar.h" #include <boost/serialization/split_free.hpp> #include <boost/serialization/version.hpp> namespace boost { namespace serialization { template <typename Archive> void save(Archive& ar, const Bar& object, const unsigned int version) { ar << base_object<Foo>(object); ar << object.getDouble(); } template <typename Archive> void load(Archive& ar, Bar& object, const unsigned int version) { double doubleNumber; ar >> doubleNumber; object.setDouble(doubleNumber); } }} //namespace brackets BOOST_SERIALIZATION_SPLIT_FREE( Bar ) #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/export.hpp> BOOST_CLASS_EXPORT_KEY( Bar ) #endif //_SERIALIZE_BAR_H_
serializeBar.cpp
#include "serializeBar.h" BOOST_CLASS_EXPORT_IMPLEMENT( Bar )
The serialization code goes into the DLL and should be used in another project using the Foo and Bar classes. Everything compiles fine, but at runtime I get a message
unregistered class - derived class not registered or exported
So did I use the wrong macros? Am I missing a macro? Is the above code correct or is there some kind of structural error? Perhaps this can be useful for many other people, I don’t think that including serialization of a class in a DLL is very exotic ...