Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert Plugin interface into a Factory class
Forum Updated to NodeBB v4.3 + New Features

Convert Plugin interface into a Factory class

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 677 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MarKSM Offline
    MarKSM Offline
    MarKS
    wrote on last edited by
    #1

    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 return Device* 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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      But your loader already returns an instance of SensorA when it loads the plugin for SensorA, or?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      MarKSM 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          But your loader already returns an instance of SensorA when it loads the plugin for SensorA, or?

          MarKSM Offline
          MarKSM Offline
          MarKS
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            MarKSM 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @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.

              MarKSM Offline
              MarKSM Offline
              MarKS
              wrote on last edited by
              #6

              @Christian-Ehrlicher Alright! Then how do you suggest i should implement to get my desired instance?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Enhance your interface, otherwise it's useless.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved