Results 1 to 10 of 10

Thread: automatic loading plugin factory

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default automatic loading plugin factory

    Hi,

    I have a base class for a base plugin factory (BaseFactory), and many derived factories (DerivedFactory). In each DerivedFactory, there is a static singleton DerivedFactory::instance() as in the following code

    Qt Code:
    1. class BaseFactory : public QObject
    2. {
    3. };
    4.  
    5. class DerivedFactory : public BaseFactory
    6. {
    7. public:
    8.  
    9. virtual ~DerivedFactory();
    10. static const DerivedFactory& instance();
    11.  
    12. private:
    13.  
    14. DerivedFactory();
    15. };
    To copy to clipboard, switch view to plain text mode 

    Each DerivedFactory::instance() needed to be called immediately after the QApplication is created so the factory is ready to produce object base on class name.

    The problem is I have quite a few of those factories, so I hope to force programmers to register the derived factory into the base by some macros, so the base factory will call all DerivedFactory::instance() in one go. This will also avoid some derived factory not be initialized, causing program to crash when restoring previously saved session (because factory has not loaded the plugin yet).

    I try following way, and obviously, the function prototype does not match. How can I achieve this goal? Many thanks!

    Qt Code:
    1. class BaseFactory : public QObject
    2. {
    3. typedef const BaseFactory& (FactoryReg)();
    4.  
    5. /// register the factory to be called by loadAllFactories
    6. static int registerFactory( const QString& key, FactoryReg* );
    7.  
    8. static void loadAllFactories();
    9.  
    10. };
    11.  
    12. #define REGISTER_FACTORY( Key, Func ) \
    13. static const BaseFactory& instance = BaseFactory::registerFactory( #Key, Func );
    14.  
    15. class DerivedFactory : public BaseFactory
    16. {
    17. public:
    18.  
    19. virtual ~DerivedFactory();
    20. static const DerivedFactory& instance();
    21.  
    22. private:
    23.  
    24. DerivedFactory();
    25. };
    26.  
    27. // in DerivedFactory C++ file, add this line, but fail to compile of course.
    28. REGISTER_FACTORY( DerivedFactory, DerivedFactory::instance );
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: automatic loading plugin factory

    Can you show how the factories are being used?
    Are you calling a static method of the base class which then iterates over all derived class objects?

    Cheers,
    _

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: automatic loading plugin factory

    Yes, I try to register all derived classes static instance (DerivedClass::instance()) into a based class static function pointer array.

    Then I can iterates through the function pointer array and call them at appropriate time.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: automatic loading plugin factory


  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: automatic loading plugin factory

    Hi,

    My instance doesn't return void, it s like

    static MyFactor& MyFactory::instance();

    Is there is solution for this?

    Of course I can write another static like

    static void init() {
    MyFactory::instance();
    }

    But this would required adding a new static init function...

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: automatic loading plugin factory

    What do you need the instance() function with return value for?
    You are registering all derived factories with the base class and calling them through there, no?

    Cheers,
    _

  7. #7
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: automatic loading plugin factory

    Quote Originally Posted by anda_skoa View Post
    What do you need the instance() function with return value for?
    You are registering all derived factories with the base class and calling them through there, no?

    Cheers,
    _
    The returned value (the factory itself) is needed, for instance, an algorithm factory will give all available plugins, so in gui, I can give a choice for users to choose which plugin to use...

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: automatic loading plugin factory

    But how do you call a static method on a class that you don't know?

    Wouldn't you just have a pointer to the base?

    Cheers,
    _

  9. #9
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: automatic loading plugin factory

    Quote Originally Posted by anda_skoa View Post
    I am using Qt4.8, is there a similar macro for Q_COREAPP_STARTUP_FUNCTION in Qt4? Thanks

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: automatic loading plugin factory

    No, I think that was added in Qt5.

    Cheers,
    _

Similar Threads

  1. Trouble loading UI (with images) from plugin (.so)
    By dcrespo in forum Qt Programming
    Replies: 6
    Last Post: 25th February 2011, 15:42
  2. Loading QWidgets as a Plugin
    By aylek in forum Qt Programming
    Replies: 2
    Last Post: 25th May 2010, 23:34
  3. Loading SQLite plugin. Again!
    By miwarre in forum Newbie
    Replies: 5
    Last Post: 12th November 2009, 11:23
  4. How to debug plugin loading?
    By yosefm in forum Qt Tools
    Replies: 1
    Last Post: 1st November 2007, 13:02
  5. plugin loading problem
    By naresh in forum Qt Programming
    Replies: 6
    Last Post: 9th June 2007, 20:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.