remizero
2nd June 2016, 19:15
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
#include "ParseAbs.h"
#include "ParseCpp.h"
#include "ParseHtml.h"
#include "ParseJavascript.h"
#include "ParsePhp.h"
#include "ParseQt.h"
#include "ParseXml.h"
#include <QHash>
#include <QMetaMethod>
#include <QMetaObject>
class ParseFac {
public:
static ParseAbs *create ( const QString &ParseType, QTextDocument *parent );
};
.cpp Code
#include <QDebug>
typedef QHash<QString, QMetaObject> T_Parses;
T_Parses parsesTypesList () {
T_Parses hash;
hash [ "ParseCpp" ] = ParseCpp::staticMetaObject;
hash [ "ParseHtml" ] = ParseHtml::staticMetaObject;
hash [ "ParseJavascript" ] = ParseJavascript::staticMetaObject;
hash [ "ParsePhp" ] = ParsePhp::staticMetaObject;
hash [ "ParseQt" ] = ParseQt::staticMetaObject;
hash [ "ParseXml" ] = ParseXml::staticMetaObject;
return hash;
}
ParseAbs *ParseFac::create ( const QString &parseType , QTextDocument *parent ) {
T_Parses parsesType = parsesTypesList ();
ParseAbs *parseCreated = 0;
if ( parsesType.contains ( parseType ) ) {
const QMetaObject& metaObjectClass = parsesType [ parseType ];
// Here I try to get the method I want to run
//const QMetaMethod& metaMethodObjectClass = metaObjectClass.method ( metaObjectClass.indexOfMethod ( "getInstance" ) );
//metaMethodObjectClass.invoke ( "what kind of object place here?" );
// Here I try to create the object, but obviously I can not create
parseCreated = qobject_cast<ParseAbs *> ( metaObjectClass.newInstance ( Q_ARG ( QTextDocument*, parent ) ) );
if ( parseCreated == 0 ) {
qDebug () << "Error creating " << parseType;
abort ();
}
} else {
qDebug () << "Error creating " << parseType;
abort ();
}
return parseCreated;
}
The following lines of code are those that want to run, but not as assigning the respective class:
//const QMetaMethod& metaMethodObjectClass = metaObjectClass.method ( metaObjectClass.indexOfMethod ( "getInstance" ) );
//metaMethodObjectClass.invoke ( "what kind of object place here?" );
Thanks in advance for your answers.
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
#include "ParseAbs.h"
#include "ParseCpp.h"
#include "ParseHtml.h"
#include "ParseJavascript.h"
#include "ParsePhp.h"
#include "ParseQt.h"
#include "ParseXml.h"
#include <QHash>
#include <QMetaMethod>
#include <QMetaObject>
class ParseFac {
public:
static ParseAbs *create ( const QString &ParseType, QTextDocument *parent );
};
.cpp Code
#include <QDebug>
typedef QHash<QString, QMetaObject> T_Parses;
T_Parses parsesTypesList () {
T_Parses hash;
hash [ "ParseCpp" ] = ParseCpp::staticMetaObject;
hash [ "ParseHtml" ] = ParseHtml::staticMetaObject;
hash [ "ParseJavascript" ] = ParseJavascript::staticMetaObject;
hash [ "ParsePhp" ] = ParsePhp::staticMetaObject;
hash [ "ParseQt" ] = ParseQt::staticMetaObject;
hash [ "ParseXml" ] = ParseXml::staticMetaObject;
return hash;
}
ParseAbs *ParseFac::create ( const QString &parseType , QTextDocument *parent ) {
T_Parses parsesType = parsesTypesList ();
ParseAbs *parseCreated = 0;
if ( parsesType.contains ( parseType ) ) {
const QMetaObject& metaObjectClass = parsesType [ parseType ];
// Here I try to get the method I want to run
//const QMetaMethod& metaMethodObjectClass = metaObjectClass.method ( metaObjectClass.indexOfMethod ( "getInstance" ) );
//metaMethodObjectClass.invoke ( "what kind of object place here?" );
// Here I try to create the object, but obviously I can not create
parseCreated = qobject_cast<ParseAbs *> ( metaObjectClass.newInstance ( Q_ARG ( QTextDocument*, parent ) ) );
if ( parseCreated == 0 ) {
qDebug () << "Error creating " << parseType;
abort ();
}
} else {
qDebug () << "Error creating " << parseType;
abort ();
}
return parseCreated;
}
The following lines of code are those that want to run, but not as assigning the respective class:
//const QMetaMethod& metaMethodObjectClass = metaObjectClass.method ( metaObjectClass.indexOfMethod ( "getInstance" ) );
//metaMethodObjectClass.invoke ( "what kind of object place here?" );
Thanks in advance for your answers.