增加PlugInManage模板代码
This commit is contained in:
parent
0786f4c611
commit
fb81037fa3
@ -3,29 +3,41 @@
|
||||
|
||||
#include<hgl/plugin/ExternalPlugIn.h>
|
||||
#include<hgl/type/ResManage.h>
|
||||
#include<hgl/type/StringList.h>
|
||||
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:
|
||||
|
||||
PlugInManage(const OSString &n)
|
||||
PlugInManage(const UTF16String &n)
|
||||
{
|
||||
name=n;
|
||||
}
|
||||
|
||||
virtual ~PlugInManager()=default;
|
||||
};//template<typename T> class PlugInManage
|
||||
virtual ~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;
|
||||
|
||||
@ -33,7 +45,7 @@ namespace hgl
|
||||
|
||||
RegistryPlugInProxy()
|
||||
{
|
||||
plugin=new CN;
|
||||
plugin=new T;
|
||||
}
|
||||
|
||||
virtual ~RegistryPlugInProxy()
|
||||
@ -45,13 +57,8 @@ namespace hgl
|
||||
};//template<typename T> class RegistryPlugInProxy
|
||||
|
||||
/*
|
||||
内部插件:
|
||||
如Log一类必须存在的插件,直接在代码中一块儿编译链接
|
||||
|
||||
外部插件:
|
||||
如音频音频图片解码编码、压缩解压缩等根据需要加载释放,以独立的.dll/.so/.dylib文件形式存在
|
||||
|
||||
除log console/log file外,其它所有插件可以是内部插件也可以是外部插件
|
||||
如Log插件中的Console,File插件,是直接在代码中的,属于内部插件。
|
||||
但Log输出至MySQL的插件,是以独立.dll/.so/dylib形式存在的,属于外部插件。
|
||||
*/
|
||||
|
||||
#ifndef __MAKE_PLUGIN__ //内部插件
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include<hgl/PlugIn.h>
|
||||
#include<hgl/Logger.h>
|
||||
#include<hgl/plugin/PlugIn.h>
|
||||
#include<hgl/log/Logger.h>
|
||||
#include<hgl/type/DateTime.h>
|
||||
#include<hgl/type/List.h>
|
||||
#include<hgl/thread/RWLock.h>
|
||||
|
||||
namespace hgl
|
||||
@ -130,7 +131,7 @@ namespace hgl
|
||||
/**
|
||||
* 日志插件,一个模拟的接口
|
||||
*/
|
||||
class LogPlugIn:public PlugIn ///日志插件
|
||||
class LogPlugIn:public PlugIn ///日志插件
|
||||
{
|
||||
public:
|
||||
|
||||
|
@ -56,6 +56,9 @@ namespace hgl
|
||||
return(true);
|
||||
}
|
||||
}
|
||||
|
||||
delete pi_module;
|
||||
pi_module=nullptr;
|
||||
}
|
||||
|
||||
status=PlugInStatus::LOAD_FAILED;
|
||||
|
103
src/PlugIn/PlugInManage.cpp
Normal file
103
src/PlugIn/PlugInManage.cpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user