Choosing a Boost Plugin

I am looking to implement plugin architecture in our current application (Unix, C ++) and have been looking at options (pending approval) in Boost libraries.

Edit: I'm looking to dynamically add classes at runtime.

Boost.Plugin

Boost.reflection

Boost.extension

I was curious that any experience / opinion is in these and other implementations.

+5
source share
2 answers

E. We just used dlopenand dlsymtogether with a couple of functions extern "C" staticthat should be defined in the dll

extern "C" static plugin* create( arg_pack* );
extern "C" static errno_t destroy( plugin* );

Ask the plugin manager to search for the ".dll" or ".so" files and load them into the folder map<string, pair< plugin*(*)(arg_pack*), errno_t(*)(plugin*)> >

( ) "" ""

. : gmodule

+4

, ++ - , . ( ) .

, .

factory, . , factory factory . , :

// plugin.h start
#include <memory>

struct PlugIn // interface
{
    virtual ~PlugIn() = 0;
    virtual void doSomething() = 0;
};

extern "C" {

typedef std::auto_ptr<PlugIn> PlugInFactoryFn();

// A plugin .so must export this one factory function.
std::auto_ptr<PlugIn> createPlugIn();

}
// plugin.h end

// somewhere in you application
#include "plugin.h"
#include <assert.h>
#include <dlfcn.h>

std::auto_ptr<PlugIn> loadPlugIn(char const* filename)
{
    void* so = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
    assert(so);
    void* factory_function = dlsym(so, "createPlugIn");
    assert(factory_function);
    return reinterpret_cast<PlugInFactoryFn*>(factory_function)();
}

int main()
{
    std::auto_ptr<PlugIn> a(loadPlugIn("a.so"));
    std::auto_ptr<PlugIn> b(loadPlugIn("b.so"));
    a->doSomething();
    b->doSomething();
}
+1

All Articles