#pragma once //#include "FileManager.h" //#include "StringConvert.h" #include #include #include class LibraryLoader { private: LibraryLoader() { } ~LibraryLoader() { } public: template static D* to(T* t) { return dynamic_cast(t); } template static T* load(std::string szPlugin) { QString szAddin = szPlugin.c_str(); return load(szAddin); } template 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 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; } };