|
Boost : |
Subject: Re: [boost] Any interest in static plugins?
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-11-29 21:52:59
On Sun, Nov 29, 2009 at 9:34 AM, Dave van Soest <dsoest_at_[hidden]> wrote:
> 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
> {
> public:
> Â Â 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));
> Â Â object->doSomething();
> Â Â 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.
Heh, I do the exact same thing for many of my things, especially in
the console to register new commands. If you can make it more simple
and/or efficient then I already have my code, I would use this, but so
far your code is slightly more verbose.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk