jeudi 11 janvier 2018

How can a Plugin call Other Plugins with C++?

This is a plugin system which is based on the factory pattern. Each plugin extends the Plugin class and implements a factory that returns the extended class to the kernel.

Plugins are compiled as shared libraries. Kernel checks the plugins/ directory and loads all libraries with dlopen at runtime. When the red button is clicked, the kernel calls call_on_red_click() on every loaded plugin.

// Plugin.h
class Plugin {
public:
    virtual void call_on_blue_click() = 0;
    virtual void call_on_red_click() = 0;
}

// MyPlugin.h
class MyPlugin : public Plugin {
public:
    void call_on_blue_click() {/*[...]*/}
    void call_on_red_click() {
        // Does something
        // Optionaly, activate teleporter
    }
}

Plugin *plug_factory() { return new MyPlugin; }

// MyOtherPlugin.h
class MyOtherPlugin : public Plugin {
public:
    void call_on_blue_click() {/*[...]*/}
    void call_on_red_click() {
        // Does something else
        // Optionaly, activate teleporter
    }
}

Plugin *plug_factory() { return new MyOtherPlugin; }

The teleporter is compiled separately and may not be present in every installation (It's shiped by itself). Lets say that MyPlugin and MyOtherPlugin want to activate the teleporter if it is present.

Questions:

  1. Should the Teleporter be a kernel Plugin or something else?
  2. Is there a design pattern for this?

Aucun commentaire:

Enregistrer un commentaire