Try making the base class polymorphic - that it define at least one abstract function. See my changes below. This will have the beneficial effect of inhibiting anyone from creating an instance of your base class directly. Robert Ramey Guy Létourneau wrote:
Hello,
I need to develop a generic data storage layer. Its job is to handle the serialization of all objects that need to be persistent in my system and handle management of archives, backups and restores and so forth. The most important point is that it must stay generic, i.e. have no knowledge of the type of objects being passed by other modules in the system. Because of this, I define a base class NonVolatileData from which all data classes in the system will derive:
/////////////////////////////////////////////////////////////////////// // Base class class NonVolatileData { friend class boost::serialization::access;
public:
NonVolatileData(){ i = 1; }; virtual ~NonVolatileData() = 0; // Note - pure virtual function make class abstract-
protected:
virtual void Dummy() =0; // Note - pure virtual function make class abstract- ;
int i; template <class Archive> void serialize( Archive& ar, const unsigned int version ) { ar & i; } }; BOOST_CLASS_TRACKING(NonVolatileData, boost::serialization::track_never)
class SomeData: public NonVolatileData { friend class boost::serialization::access;
public:
SomeData () { };
~SomeData () { };
void SetValue( std::string& Address ) { this->Address = Address; }
void Dummy() { }
private:
template <class Archive> void serialize( Archive& ar, const unsigned int version ) { ar &
boost::serialization::base_object<NonVolatileData>(*this); ar& Address; }
std::string Address; }; BOOST_CLASS_TRACKING(SomeData, boost::serialization::track_never) /////////////////////////////////////////////////////////////////
My data storage interface will take a ptr to the base class i.e. Save(NonVolatileData* data) so the serialization will be performed through a ptr to the base class. I've read the documentation multiple times and I'm still unable to make that work correctly. When a do a save, only the base class is serialized even though the base class is polymorphic. I've tried using the BOOST_CLASS_EXPORT_GUID macro the force a registration of my derived class without success. I've also checked the example from demo.cpp. It uses the direct call to register_type() but in my case, I don't want to change the code everytime a new class is defined in the system so I went with option 2 (the macro). There's probably a subtle detail I do not do correctly (polite way of saying I'm missing something :-)).
Thanks for your help,
Guy Letourneau