123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #pragma once
- //#include "FileManager.h"
- //#include "StringConvert.h"
- #include <QtCore/QLibrary>
- #include <QtCore/QFile>
- #include <QtCore/QDebug>
- class LibraryLoader
- {
- private:
- LibraryLoader()
- {
- }
-
- ~LibraryLoader()
- {
- }
- public:
- template <typename T,typename D>
- static D* to(T* t)
- {
- return dynamic_cast<D*>(t);
- }
- template <typename T>
- static T* load(std::string szPlugin)
- {
- QString szAddin = szPlugin.c_str();
- return load<T>(szAddin);
- }
- template <typename T>
- static T* load(QString szPlugin)
- {
- #ifndef _Andriod
- QFile file;
- if( file.exists(szPlugin.toStdString().c_str()) == false )
- //if( FileManager::ifFileExist(szPlugin.toStdString()) == false )
- {
- // qCritical() << LOG_HEADER << szPlugin << " not exist.";
- return NULL;
- }
- #endif
- T* pPlugin = NULL;
- QLibrary* pLoader = new QLibrary(szPlugin);
- if( pLoader->load() )
- {
- typedef T* (*funInterface)();
- funInterface fun = (funInterface)pLoader->resolve("instance");
- if( fun != nullptr )
- {
- pPlugin = fun();
- }
- }
- if( pPlugin != nullptr)
- {
- pPlugin->setLoader(pLoader);
- }
- else
- {
- QString szError = pLoader->errorString();
- // qCritical() << LOG_HEADER << " load " << szPlugin.toStdString().c_str() << " : " << szError.toStdString().c_str();
- pLoader->unload();
- pLoader->deleteLater();
- }
- return pPlugin;
- }
- // 释放对象
- // 卸载插件
- template <typename T>
- static bool unload(T* pPlugin )
- {
- if( pPlugin == nullptr )
- {
- return false;
- }
- QLibrary* pLoader = pPlugin->getLoader();
- if( pLoader != nullptr )
- {
- typedef void (*funDestroy)(T*);
- funDestroy fun = (funDestroy)pLoader->resolve("destroy");
- if( fun != nullptr )
- {
- fun(pPlugin);
- }
- pLoader->unload();
- }
- return true;
- }
- };
|