Boost logo

Boost Users :

From: Guy Létourneau (Guy.Letourneau_at_[hidden])
Date: 2006-07-18 10:27:09


Robert,
I've tried to include your changes but it doesn't solve the problem. I had to add the following line: BOOST_IS_ABSTRACT(NonVolatileData) for the program to compile. I agree with you, to have an abstract class is what is needed in that case as the NonVolatileData class is not intended to do something useful except for allowing my data storage layer to be generic. However, abstract or not, I still have the same problem: when serializing my SomeData object through a pointer to the NonVolatileData, only the data of the parent class gets serialized. Anything else you want me to try?

Thanks,
Guy

>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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net