Boost logo

Boost :

Subject: Re: [boost] Any interest in static plugins?
From: Dave van Soest (dsoest_at_[hidden])
Date: 2009-11-29 11:34:06

Hi Christian, Darryl, and anyone else interested,

>> You may get more response if you posted or linked to some motivational
>> examples.
> +1 on the examples. So far I'm merely intrigued to understand what the
> library is/does.

Suppose that your program provides this simplified plugin interface
class 'ObjectBase' in the header 'ObjectBase.hpp':

---- Start of code sample 1 ----
class ObjectBase
     typedef std::string IdType;
     ObjectBase() {}
     virtual ~ObjectBase() {}
     virtual void doSomething() = 0;
---- End of code sample 1 ----

Then the static plugin library will allow you to extend the
functionality of this program merely by adding a separate source file
containing the following code:

---- Start of code sample 2 ----
#include <static_plugin.hpp>
#include "ObjectBase.hpp"

class SomeObject : ObjectBase
     void doSomething() { std::cout << "Doing something" << std::endl; }
     static const IdType id;

const SomeObject::IdType SomeObject::id = "SomeObject_ID";
static const static_plugin::Creator<SomeObject, ObjectBase> myPlugin;
---- End of code sample 2 ----

The above code causes a factory for the 'SomeObject' class to be created
automatically and registered with the factory manager for classes
implementing the 'ObjectBase' interface. This all happens when the
program is just started (before it enters 'main').

The factory manager for 'ObjectBase' derivatives, which follows the
singleton pattern, can be used from your program as follows (note: again
this code is simplified):

---- Start of code sample 3 ----
#include <static_plugin.hpp>
#include "ObjectBase.hpp"

typedef FactoryManager<ObjectBase> ObjectBaseFM;

int main(void) {
     const ObjectBaseFM& fm(getFactoryManager<ObjectBase>());
     assert(fm.factoryCount() > 0);
     ObjectBase::IdType id(fm.factoryIdList().at(0));
     std::auto_ptr<ObjectBase> object(fm.create(id));
     return 0;
---- End of code sample 3 ----

The only requirements are that ObjectBase defines the IdType type and
SomeObject contains a static member of type IdType called 'id'. The
declaration of the static_plugin::Creator<...> causes the SomeObject
plugin to be registered.

Sidenote: I just tested this functionality in a dynamically loaded
library and that works now too. This means that any plugin in your
dynamic library is automatically registered without the need to call
some 'initLibrary' function in the library.

Boost list run by bdawes at, gregod at, cpdaniel at, john at