[serialization] a pattern for serialization and plugins

Hi all, I'm trying to use the serialization library for the first time and I'm wondering if there is an extant solution for the problem I'm trying to solve. I have an application which has dynamicly loaded plugin modules. The application structure is something like: main app: ---- class base { ... template <class Archive> void serialize(Archive& pAr, unsigned int pVer) { ... } ... }; class baseFactory { shared_ptr<base> create(...) = 0; }; ---- plugin: ---- class myPluginN : public base { ... template <class Archive> void serialize(Archive& pAr, unsigned int pVer) { ... } }; class myPluginNFactory : public baseFactory { shared_ptr<base> create(...); }; extern "C" { void* makeFactory(); } ---- Now the problem is that by my reading of the serialization library documentation, the registration of derived classes is going to present difficulties. If I understand things correctly, derived classes need to be registered, and the archive contains a "tag" for each derived class. Now in this instance the set of derived classes is dynamic, and I don't want to require the set of derived classes to be the same in order to be able to load a structure. Of course, all the derived classes that are used in the structure must be loaded (of course, I'd like to serialization code to attempt to load any that are required but not already loaded), but I don't want the archive to depend on any derived classes that are not used. So the question really is: is this a solved problem? And if so, what is the solution? Also, the structures contain pointers to members of other structures (e.g. class Foo { .... }; class Bar { Foo foo; }; class Baz { Foo* fooPtr; }; ... shared_ptr<Bar> bar(new Bar); shared_ptr<Baz> baz(new Baz); baz->fooPtr = &(bar->foo); ) Will the obvious serialization code Bar::serialize(...) { ar & foo; } Baz::serialize(...) { ar & fooPtr; } do the right thing? thanks to anyone who can shed some light, Tom

Thomas Conway wrote:
Hi all,
I'm trying to use the serialization library for the first time and I'm wondering if there is an extant solution for the problem I'm trying to solve.
I have an application which has dynamicly loaded plugin modules. The application structure is something like:
main app: ---- class base { ... template <class Archive> void serialize(Archive& pAr, unsigned int pVer)
};
plugin: ----
class myPluginN : public base { ... template <class Archive> void serialize(Archive& pAr, unsigned int pVer)
};
In order for this to work, you need to add BOOST_CLASS_EXPORT(myPluginN); to the source of your plugin. In the same source file you need to include headers for several of archive headers. As a result, this derived class will be registered with that archives and you can use those archives in your main applications to load and save instances of the derived class. I'd recomment #including headers for polymorphic_iarchive and polymorphic_oarchive, and using those archive in your application. This will reduce code bloat and make sure your plugins will work with any existing or future archive types (they all can be wrapped in polymorphic_{i,o}archive. - Volodya
participants (2)
-
Thomas Conway
-
Vladimir Prus