Results 1 to 8 of 8

Thread: Dynamic creation of Singleton

  1. #1
    Join Date
    Jan 2015
    Location
    Barquisimeto, Venezuela
    Posts
    24
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Dynamic creation of Singleton

    Hello everyone.

    I have a Singleton (which must be done to ensure that only has a single instance of that object) class, in fact Singleton is a set of classes that inherit from the same abstract class base.

    It is required that this set of Singleton classes are created dynamically, depending on the object to the request, these will be created, and if they already exist, assigned to that object if and when the active object.

    To create this set of Singleton classes, I am working with the factory pattern design method implemented with QMetaObject and QMetaMethod on demand.

    My problem is the following:

    By using QMetaMethod :: Invoke (...), I requested an object to run the Singleton :: instance () method;

    My question is: How can I do to run this method (Singleton :: instance ()), even if I do not have an instance of it?

    Now, is there another way to create an object and ensure that there is only a single instance of it? Rather work with the Singleton pattern because I require no further verification and / or validation for use.

    Additional, put them programming for dynamic creation of these classes Singleton:

    .h Code
    Qt Code:
    1. #include "ParseAbs.h"
    2. #include "ParseCpp.h"
    3. #include "ParseHtml.h"
    4. #include "ParseJavascript.h"
    5. #include "ParsePhp.h"
    6. #include "ParseQt.h"
    7. #include "ParseXml.h"
    8.  
    9. #include <QHash>
    10. #include <QMetaMethod>
    11. #include <QMetaObject>
    12.  
    13. class ParseFac {
    14.  
    15. public:
    16. static ParseAbs *create ( const QString &ParseType, QTextDocument *parent );
    17. };
    To copy to clipboard, switch view to plain text mode 

    .cpp Code
    Qt Code:
    1. #include <QDebug>
    2.  
    3. typedef QHash<QString, QMetaObject> T_Parses;
    4.  
    5. T_Parses parsesTypesList () {
    6.  
    7. T_Parses hash;
    8.  
    9. hash [ "ParseCpp" ] = ParseCpp::staticMetaObject;
    10. hash [ "ParseHtml" ] = ParseHtml::staticMetaObject;
    11. hash [ "ParseJavascript" ] = ParseJavascript::staticMetaObject;
    12. hash [ "ParsePhp" ] = ParsePhp::staticMetaObject;
    13. hash [ "ParseQt" ] = ParseQt::staticMetaObject;
    14. hash [ "ParseXml" ] = ParseXml::staticMetaObject;
    15.  
    16. return hash;
    17. }
    18.  
    19. ParseAbs *ParseFac::create ( const QString &parseType , QTextDocument *parent ) {
    20.  
    21. T_Parses parsesType = parsesTypesList ();
    22.  
    23. ParseAbs *parseCreated = 0;
    24.  
    25. if ( parsesType.contains ( parseType ) ) {
    26.  
    27. const QMetaObject& metaObjectClass = parsesType [ parseType ];
    28.  
    29. // [COLOR="#FF0000"]Here I try to get the method I want to run[/COLOR]
    30. //const QMetaMethod& metaMethodObjectClass = metaObjectClass.method ( metaObjectClass.indexOfMethod ( "getInstance" ) );
    31. //metaMethodObjectClass.invoke ( [COLOR="#FF0000"]"what kind of object place here?"[/COLOR] );
    32.  
    33. // [COLOR="#FF0000"]Here I try to create the object, but obviously I can not create[/COLOR]
    34. parseCreated = qobject_cast<ParseAbs *> ( metaObjectClass.newInstance ( Q_ARG ( QTextDocument*, parent ) ) );
    35.  
    36. if ( parseCreated == 0 ) {
    37.  
    38. qDebug () << "Error creating " << parseType;
    39. abort ();
    40. }
    41. } else {
    42.  
    43. qDebug () << "Error creating " << parseType;
    44. abort ();
    45. }
    46. return parseCreated;
    47. }
    To copy to clipboard, switch view to plain text mode 

    The following lines of code are those that want to run, but not as assigning the respective class:
    Qt Code:
    1. //const QMetaMethod& metaMethodObjectClass = metaObjectClass.method ( metaObjectClass.indexOfMethod ( "getInstance" ) );
    2. //metaMethodObjectClass.invoke ( [COLOR="#FF0000"]"what kind of object place here?"[/COLOR] );
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for your answers.

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

    Default Re: Dynamic creation of Singleton

    Make the factory the singleton and let it create normal objects on demand when they don't exist yet.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2015
    Location
    Barquisimeto, Venezuela
    Posts
    24
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Dynamic creation of Singleton

    Anda_skoa Hi, thanks for responding so quickly.

    This solution you offer me the thought too, but I have a problem with it, and then have to identify that or those created to allocate to other objects.

    I have to create a multi editor tabs, and those singleton to create, I must assign dynamically to the current tab, so I find it very convenient to create them as singleton rather than as normal objects, it would have to inspect the object stack to verify if it exists, if it does not create it, and so every time you change tab, I think it's not a good idea to do so much unnecessary work that way also consumes resources.

    Anyway, thank you very much for your contribution, I will continue investigating how to create these Singleton and when you have the answer I comment

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

    Default Re: Dynamic creation of Singleton

    I am not sure what you mean.

    Checking if you have created an object before would be a simple lookup, assuming you store the created obejcts in an associative container such as a hash.

    So first ParseFac::create() would check the hash with the existing objects and only continue with creation if it can't find an existing one.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2015
    Location
    Barquisimeto, Venezuela
    Posts
    24
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Dynamic creation of Singleton

    Hello everyone again.

    After investigating for several days, sadly it seems there is no way to invoke a static method without a previous instance of the class. It made me rethink the requested injunction.

    First of all, when working with a Singleton, I'm guaranteeing a single instance of the object to create (for here going well so far).

    On the other hand, the idea of ​​working with a factory Singletons, was simply to automate the creation of the same on demand, but this is where I presented the issue with the factory objects, for I have not got any way to invoke a static method without having a previous instance of the class.

    But as every editor knows that objects analyzers have to create, then it was here that I decided not to work with factory objects and create objects analyzers "manually" because when recreate a new editor for the required language, this with only "create" again objects analyzers requiring not he is actually creating, because, if it were created (because there is already another editor for the same language) then just get the instance of the parser object Singleton and assigned to this new editor.

    I hope this analysis will serve useful to someone else who has a similar problem.

    PD: I terminated and solved this thread, but I can not locate where to place fixed or modify the title to identify it as solved
    Last edited by remizero; 5th June 2016 at 16:57. Reason: updated contents

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

    Default Re: Dynamic creation of Singleton

    Quote Originally Posted by remizero View Post
    First of all, when working with a Singleton, I'm guaranteeing a single instance of the object to create (for here going well so far).
    Which of course would also be the case if the factory itself was a singleton.

    Quote Originally Posted by remizero View Post
    On the other hand, the idea of ​​working with a factory Singletons, was simply to automate the creation of the same on demand, but this is where I presented the issue with the factory objects, for I have not got any way to invoke a static method without having a previous instance of the class.
    A static member function can be stored as a function pointer, for example.

    Cheers,
    _

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,060
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Dynamic creation of Singleton

    sadly it seems there is no way to invoke a static method without a previous instance of the class.
    That's not true at all. Static methods do not require any class instance to invoke. You use the syntax:

    Qt Code:
    1. result = MyClass::myStaticMethod();
    To copy to clipboard, switch view to plain text mode 

    A common way to implement a Singleton class is to use a static class method along with a static pointer:

    Qt Code:
    1. // MyClass.h
    2. class MyClass
    3. {
    4. private:
    5. MyClass * pInstance;
    6.  
    7. MyClass();
    8. ~MyClass();
    9.  
    10. public:
    11. static MyClass * instance()
    12. {
    13. if ( !pInstance )
    14. pInstance = new MyClass();
    15.  
    16. return pInstance;
    17. }
    18.  
    19. };
    20.  
    21. // MyClass.cpp;
    22. MyClass::pInstance * instance = 0;
    23.  
    24. MyClass * pSingleton = MyClass::instance();
    To copy to clipboard, switch view to plain text mode 

    This leaves a dangling pInstance memory leak when the program exits, but you can implement code to clean that up if necessary.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Jan 2015
    Location
    Barquisimeto, Venezuela
    Posts
    24
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Dynamic creation of Singleton

    Hi d_stranz.

    I fully understand what you say, I know how to call a static method.

    I am referring to the invocation of a static method without instance through QMetaObject :: InvokeMethod (), work with this class makes it impossible to invoke a static method without instance: s.

    But thanks for your good intentions

Similar Threads

  1. Dynamic creation of Scale object
    By Mookie in forum Qt Quick
    Replies: 4
    Last Post: 28th March 2016, 15:13
  2. Dynamic creation of list<transform>
    By Mookie in forum Qt Quick
    Replies: 1
    Last Post: 27th March 2016, 15:26
  3. QMetaType dynamic object creation and initialization
    By d_stranz in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2014, 10:12
  4. Dynamic creation of widget with N images
    By davethomaspilot in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2014, 12:45
  5. Dynamic widget creation from an XML-like file
    By ttvo in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2009, 22:15

Tags for this Thread

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.