In Qt its possible by using Qt's meta object system.
Here is my sample implementation that uses QMetaType to achieve the same. In sample code Parser is base class of all parser.
class Parser { public: virtual void parse() = 0; virtual ~Parser() {}; };Now I am creating two dummy parser which are derived from Parser.
class Parser1 : public Parser { public: Parser1() { qDebug() <<"Parser1::Parser1()"; } void parse() { qDebug() << "Parser1::parse()"; } ~Parser1() { qDebug() <<"Parser1::~Parser1()"; } }; Q_DECLARE_METATYPE(Parser1) class Parser2 : public Parser { public: Parser2() { qDebug() <<"Parser2::Parser2()"; } void parse() { qDebug() << "Parser2::parse()"; } ~Parser2() { qDebug() <<"Parser2::~Parser2()"; } }; Q_DECLARE_METATYPE(Parser2)Now in parse function I am trying to create meta type from its name.
void parse( const char* parserName ) { int id = QMetaType::type( parserName ); if (id != -1) { Parser *parser = static_cast< Parser* > ( QMetaType::construct( id ) ); parser->parse(); delete parser; } }Some sample code to test above function.
int main ( int argc, char* argv[] ) { qRegisterMetaTypeand finally output.("Parser1"); qRegisterMetaType ("Parser2"); qDebug() << "###### Trying create Parser1"; parse("Parser1"); qDebug() << "###### Trying create Parser2"; parse("Parser2"); }
###### Trying create Parser1 Parser1::Parser1() Parser1::parse() Parser1::~Parser1() ###### Trying create Parser2 Parser2::Parser2() Parser2::parse() Parser2::~Parser2()so that was all, but remember to use Q_DECLARE_METATYPE, qRegisterMetaType macros for registering your custom type with Qt meta object system.
No comments:
Post a Comment