ResManage增加防重复添加功能

This commit is contained in:
hyzboy 2019-06-11 23:15:45 +08:00
parent a33d76da09
commit 25cbb2d44d
2 changed files with 49 additions and 0 deletions

View File

@ -87,6 +87,43 @@ namespace hgl
return(nullptr);
}
/**
*
*/
template<typename F,typename T>
bool ResManage<F,T>::ValueExist(T *value)
{
return(items.FindByValue(value)!=-1);
}
/**
* Key和引用计数
* @param value
* @param key Key存放地址
* @param ref_count
* @param
*/
template<typename F,typename T>
bool ResManage<F,T>::GetKeyByValue(T *value,F *key,uint *ref_count,bool inc_ref_count)
{
int index=items.FindByValue(value);
if(index==-1)return(false);
ResItem *obj=items.GetItem(index);
if(inc_ref_count)
++obj->count;
if(key)
*key=obj->left;
if(ref_count)
*key=obj->count;
return(true);
}
template<typename F,typename T>
void ResManage<F,T>::ReleaseBySerial(int index,bool zero_clear)
{

View File

@ -46,6 +46,9 @@ namespace hgl
virtual T * Find(const F &); ///<查找一个数据
virtual T * Get(const F &); ///<取得一个数据
virtual bool ValueExist(T *); ///<确认这个数据是否存在
virtual bool GetKeyByValue(T *,F *,uint *,bool inc_ref_count=false); ///<取得一个数据的Key和引用次数
virtual void Release(const F &,bool zero_clear=false); ///<释放一个数据
virtual void Release(T *,bool zero_clear=false); ///<释放一个数据
};//template<typename F,typename T> class ResManage
@ -65,6 +68,15 @@ namespace hgl
virtual F Add(T *value)
{
if(!value)return(-1);
{
F key;
uint count;
if(ResManage<F,T>::GetKeyByValue(value,&key,&count,true))
return key;
}
if(!ResManage<F,T>::Add(id_count,value))
return(-1);