增加PlugInManage模板代码

This commit is contained in:
hyzboy 2019-08-26 21:19:13 +08:00
parent 0786f4c611
commit fb81037fa3
4 changed files with 131 additions and 17 deletions

View File

@ -3,29 +3,41 @@
#include<hgl/plugin/ExternalPlugIn.h> #include<hgl/plugin/ExternalPlugIn.h>
#include<hgl/type/ResManage.h> #include<hgl/type/ResManage.h>
#include<hgl/type/StringList.h>
namespace hgl namespace hgl
{ {
/** /**
* *
*/ */
template<typename T,typename C> class PlugInManage:public ResManage<C,PlugIn> class PlugInManage:public ResManage<UTF16String,PlugIn>
{ {
OSString name; ///<插件类目名称(必须符合代码名称规则) UTF16String name; ///<插件类目名称(必须符合代码名称规则)
OSStringList findpath; ///<插件查找目录
public: public:
PlugInManage(const OSString &n) PlugInManage(const UTF16String &n)
{ {
name=n; name=n;
} }
virtual ~PlugInManager()=default; virtual ~PlugInManage();
};//template<typename T> class PlugInManage
bool RegistryPlugin(PlugIn *); ///<注册一个内置插件
uint UnregistryPlugin(const UTF16String &); ///<释放一个内置插件
bool AddFindPath (const OSString &path); ///<添加一个插件查找目录
PlugIn *LoadPlugin (const UTF16String &,const OSString &); ///<加载一个外部插件,明确指定全路径文件名
PlugIn *LoadPlugin (const UTF16String &); ///<加载一个外部插件,自行查找
bool UnloadPlugin(const UTF16String &); ///<释放一个外部插件
};//class PlugInManage:public ResManage<UTF16String,PlugIn>
/** /**
* *
*/ */
template<typename T,typename CN> class RegistryPlugInProxy template<typename T> class RegistryPlugInProxy
{ {
T *plugin; T *plugin;
@ -33,7 +45,7 @@ namespace hgl
RegistryPlugInProxy() RegistryPlugInProxy()
{ {
plugin=new CN; plugin=new T;
} }
virtual ~RegistryPlugInProxy() virtual ~RegistryPlugInProxy()
@ -45,13 +57,8 @@ namespace hgl
};//template<typename T> class RegistryPlugInProxy };//template<typename T> class RegistryPlugInProxy
/* /*
: Log插件中的Console,File插件
Log一类必须存在的插件 Log输出至MySQL的插件.dll/.so/dylib形式存在的
:
.dll/.so/.dylib文件形式存在
log console/log file外
*/ */
#ifndef __MAKE_PLUGIN__ //内部插件 #ifndef __MAKE_PLUGIN__ //内部插件

View File

@ -1,6 +1,7 @@
#include<hgl/PlugIn.h> #include<hgl/plugin/PlugIn.h>
#include<hgl/Logger.h> #include<hgl/log/Logger.h>
#include<hgl/type/DateTime.h> #include<hgl/type/DateTime.h>
#include<hgl/type/List.h>
#include<hgl/thread/RWLock.h> #include<hgl/thread/RWLock.h>
namespace hgl namespace hgl
@ -130,7 +131,7 @@ namespace hgl
/** /**
* , * ,
*/ */
class LogPlugIn:public PlugIn ///日志插件 class LogPlugIn:public PlugIn ///日志插件
{ {
public: public:

View File

@ -56,6 +56,9 @@ namespace hgl
return(true); return(true);
} }
} }
delete pi_module;
pi_module=nullptr;
} }
status=PlugInStatus::LOAD_FAILED; status=PlugInStatus::LOAD_FAILED;

103
src/PlugIn/PlugInManage.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<hgl/plugin/PlugInManage.h>
#include<hgl/filesystem/FileSystem.h>
namespace hgl
{
using namespace filesystem;
bool PlugInManage::RegistryPlugin(PlugIn *pi)
{
if(!pi)return(false);
const UTF16String &pi_name=pi->GetName();
if(this->Find(pi_name))
return(false);
return this->Add(pi_name,pi);
}
uint PlugInManage::UnregistryPlugin(const UTF16String &pi_name)
{
PlugIn *pi=this->Find(pi_name);
if(!pi)return(0);
return this->Release(pi);
}
bool PlugInManage::AddFindPath(const OSString &path)
{
if(path.IsEmpty())return(false);
if(!IsDirectory(path))return(false);
#if HGL_OS == HGL_OS_Windows
if(findpath.CaseFind(path)!=-1) //Windows平台无视大小写
#else
if(findpath.Find(path)!=-1)
#endif//
return(false);
findpath.Add(path);
return(true);
}
PlugIn *PlugInManage::LoadPlugin(const UTF16String &pi_name,const OSString &filename)
{
if(pi_name.IsEmpty())return(nullptr);
if(filename.IsEmpty())return(nullptr);
PlugIn *pi=this->Get(pi_name);
if(pi)return pi;
if(!FileExist(filename))return(false);
ExternalPlugIn *epi=new ExternalPlugIn;
epi->Load(pi_name,filename);
if(this->Add(pi_name,epi))
return epi;
delete epi;
return(nullptr);
}
PlugIn *PlugInManage::LoadPlugin(const UTF16String &pi_name)
{
if(pi_name.IsEmpty())return(nullptr);
PlugIn *pi=this->Get(pi_name);
if(pi)return(pi);
const uint fp_count=findpath.GetCount();
if(fp_count<=0)return(nullptr);
#if HGL_OS == HGL_OS_Windows
OSString pi_filename=name+L'.'+pi_name+HGL_PLUGIN_EXTNAME;
#else
OSString pi_filename=ToOSString(name)+'.'+ToOString(pi_name)+HGL_PLUGIN_EXTNAME;
#endif//HGL_OS == HGL_OS_Windows
OSString pi_fullfilename;
ExternalPlugIn *epi=new ExternalPlugIn;
for(uint i=0;i<fp_count;i++)
{
pi_fullfilename=MergeFilename(findpath[i],pi_filename);
if(!FileExist(pi_fullfilename))continue;
if(epi->Load(pi_name,pi_fullfilename))
{
if(this->Add(pi_name,epi))
return epi;
}
epi->Free();
}
}
}//namespace hgl