Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialize] Serialize just the base class ofpolymorphic pointers
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-04-07 17:09:56


since your BASE isn't abstract - (no = 0) you can do what you want in the
following way

save
    Plugin *p
    ar << static_cast<BASE *>(p)
load
    BASE *b
    ar >> b

This will get you what you want.

Robert Ramey

Brandon Aubie wrote:
> I have a base class BASE that stores data. It has a virtual function
> (empty in base class) that determines how this data is calculated, but
> there's no data stored in the derived classes. In my program, I'll
> load a pluging that is based on BASE and implements the function to
> calculate the data. Right now I have BOOST_EXPORT_CLASS in the plugin
> header files and I serialize
> boost::serialization::base_object<BASE>(*this) in the derived class.
> This lets me save the serialization to file just fine.
>
> When I load it back in though, my application isn't aware of the
> plugins (I'm just analyzing the data). But I don't need the plugins,
> since I just need the data. So when I try to load in my
> serialization, I get an "unregistered class" exception. Here's how it
> works
>
> /*************** CODE ******************/
>
> class BASE {
> public:
> vector<double> data;
> virtual calculate() {};
>
> private:
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive &ar, const unsigned int version) {
> ar & data;
> }
> }
>
> class Plugin: public BASE {
> public:
> virtual calculate() { /* do stuff */ }
>
> private:
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive &ar, const unsigned int version) {
> ar & boost::serialization::base_object<BASE>(*this);
> }
> }
>
> BOOST_CLASS_EXPORT(Plugin)
>
> /*************** CODE ******************/
>
>
> Class Plugin is loaded dynamically at runtime via dlopen() on linux.
> So I have a BASE* that points to an instance of A. When I load in the
> serialized data though, I don't really care about A and would rather
> just have a BASE* that points to an instance of BASE. Since there's
> no data described in A.
>
> Can I do this somehow?
>
> Thanks


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