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.