Convert Plugin interface into a Factory class
-
I have a situation wherein i have a plugin interface as shown below:
class Device { public: /** * @brief Virtual destructor */ virtual ~Device() = default; /** * @brief Start the device */ virtual void start() = 0; /** * @brief Stop the device */ virtual void stop() = 0; }; #define iIterface_IID "com.company.Device/1.0" Q_DECLARE_INTERFACE(Device, iIterface_IID)
And 2 more implementing plugins SensorA and SensorB which look almost similar:
class SensorA : public QObject, public Device { Q_OBJECT Q_PLUGIN_METADATA(IID "com.company.SensorA") Q_INTERFACES(Device) public: /** * @brief Constructor */ SensorA(); /** * @brief Destructor */ ~SensorA(); /** * @brief Establishes a connection to the sensor. * * @return True if the connect was successfully established. */ bool connect(); /** * @brief Start the sensor */ virtual void start() override; /** * @brief Stop the sensor */ virtual void stop() override; };
I have a pluginloader class which spits out a Device* instance using:
Device* PluginLoader::load() { // initiate loading QPluginLoader* pluginLoader = new QPluginLoader(this); pluginLoader->setFileName("SensorA"); // omitting .dll suffix as per documentation if (!pluginLoader->load()) { qWarning() << "Plugin loading failed..." << pluginLoader->errorString(); return nullptr; } qInfo() << "Plugin loaded successfully!!!"; // get plugin instance Device* device = dynamic_cast<Device*>(pluginLoader->instance()); // here is the issue. Returns the root component object of the plugin return device; }
I would like my
load()
to returnDevice*
based on classType of my choice. As i am trying to implement lazy loading of the .dlls, this is how i want it to be:Action1: User chooses to connect to SensorA
Action2: Load the plugin SensorA using QPluginLoader
Action3: load() must return something like:Device* device = new SensorA();
Or more conventionally, i want to make my plugin class a factory for the class type i wish to use. Any workarounds or ideas?
-
But your loader already returns an instance of SensorA when it loads the plugin for SensorA, or?
-
Hi,
The usual way is to pass the information to the factory method so it can load the appropriate class.
You can also check the plug and paint example for another way to do that.
-
But your loader already returns an instance of SensorA when it loads the plugin for SensorA, or?
@Christian-Ehrlicher Does it? How does it know which instance to return? Maybe i misunderstood the documentation "Returns the root component object of the plugin" part.
-
@MarKS said in Convert Plugin interface into a Factory class:
How does it know which instance to return?
It returns a Device* pointer which you can cast to your specific instance although this completely contradicts the use of an interface.
-
@MarKS said in Convert Plugin interface into a Factory class:
How does it know which instance to return?
It returns a Device* pointer which you can cast to your specific instance although this completely contradicts the use of an interface.
@Christian-Ehrlicher Alright! Then how do you suggest i should implement to get my desired instance?
-
Enhance your interface, otherwise it's useless.