Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2007-08-09 17:00:05


See comments below

Jeshua Bratman wrote:
> I've been trying to get Boost serialization working for dynamically
> loaded shared libraries. Here's my basic structure simplified:
>
> 1. base.hpp - includes an abstract base class called base
> 2. derived.hpp - includes a derived class from base. includes
> base.hpp.
> 3. derived.cpp - includes code for derived class and for dynamically
> loading it
> 4. main.cpp - includes base.hpp but NOT derived.hpp. dynamically loads
> instantiates a base * which points to a derived object from derived.so
>
> Now in main I want to be able to serialize my pointer to the loaded
> derived class:
>
>
> base *obj = create_func(); //obj now points to a derived object
> obj->test(); //when test is run it correctly outputs the message from
> derived.cpp
>
> const base *to_serialize = obj;
// I would have prefered const base * const to_serialize = obj ;
// the above is OK

//it seems to want a const pointer in order to serialize
// see rationale section of documentation

> std::ofstream ofs("filename");
> boost::archive::text_oarchive oa(ofs);
> // oa << (*to_serialize); // instead of this
oa << to_serialize; // you want to serialize the pointer - NOT the
// the obect being pointed to. Once you apply *, you lose the
// character of the original object and just get the base class part.

> I can't use the BOOST_CLASS_EXPORT_GUID(derived,
> "derived") macro discussed on that page because main does not know
> what a derived object is (this is the whole point of having this
> plugin system).

In order to make this link and run correctly as it stands, you'll have to
use EXPORT. To make it work as desiired for plugins, include it
in the modules which implement the plug - in - not the main and
not the base class.

> Is there any way I can make boost serialization correctly serialize
> the derived class from the base pointer when the derived object is
> known only to a dynamically loaded library?

It can be done - but it requires more than a casual understanding of the way
the compiler and works. The really needs a "case study" in the
documentation.

> Thank you for your help,

You are welcome.

Robert Ramey

> Jeshua Bratman


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