Boost logo

Boost Users :

From: Guy Létourneau (Guy.Letourneau_at_[hidden])
Date: 2006-07-17 16:23:04


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(){};
        

    protected:

        virtual void Dummy(){};
        
        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