Created
March 4, 2016 10:29
-
-
Save 0xlitf/7c27452d12cbcdd8a2be to your computer and use it in GitHub Desktop.
ItemFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "ItemFactory.h" | |
| #include <QDebug> | |
| ItemFactory *ItemFactory::instance = NULL; | |
| ItemFactory::ItemFactory(void) | |
| { | |
| } | |
| ItemFactory &ItemFactory::getInstance() | |
| { | |
| if(!instance) | |
| { | |
| instance = new ItemFactory; | |
| } | |
| return *instance; | |
| } | |
| HMIItemBase *ItemFactory::createItem(const QString &className) | |
| { | |
| if(!m_factorys.contains(className)) | |
| { | |
| qDebug()<<"cann't find class in ItemFactory:"<<className; | |
| return NULL; | |
| } | |
| return m_factorys[className]->createItem(); | |
| } | |
| void ItemFactory::registerFactory(const QString &className, ItemVirtualFactory *fa) | |
| { | |
| if(m_factorys.contains(className)) | |
| { | |
| qDebug()<<"重复注册类:"<<className; | |
| return; | |
| } | |
| m_factorys[className]=fa; | |
| } | |
| QStringList ItemFactory::itemTypeList() const | |
| { | |
| return m_factorys.keys(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef ItemFactory_h__ | |
| #define ItemFactory_h__ | |
| #include <HMICore/hmicore_global.h> | |
| #include <QString> | |
| #include <QStringList> | |
| #include <QMap> | |
| class HMIItemBase; | |
| class HMICORE_EXPORT ItemVirtualFactory | |
| { | |
| public: | |
| virtual HMIItemBase *createItem(){return NULL;} | |
| }; | |
| #define RegisterItemFactory(typename) class typename##Factory : public ItemVirtualFactory\ | |
| {\ | |
| public:\ | |
| typename##Factory()\ | |
| {\ | |
| ItemFactory::getInstance().registerFactory(#typename,this);\ | |
| }\ | |
| HMIItemBase *createItem()\ | |
| {\ | |
| return new typename;\ | |
| }\ | |
| };\ | |
| typename##Factory _##typename##_factory; | |
| class HMICORE_EXPORT ItemFactory | |
| { | |
| public: | |
| static ItemFactory &getInstance(); | |
| HMIItemBase *createItem(const QString &className); | |
| void registerFactory(const QString &className, ItemVirtualFactory *fa); | |
| QStringList itemTypeList() const; | |
| protected: | |
| ItemFactory(void); | |
| private: | |
| static ItemFactory *instance; | |
| QMap<QString,ItemVirtualFactory *> m_factorys; | |
| }; | |
| #endif // ItemFactory_h__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment