update names and types in ResManage
This commit is contained in:
parent
5f20d34fa4
commit
9179fe8e17
@ -4,14 +4,14 @@
|
|||||||
#include<hgl/type/ResManage.h>
|
#include<hgl/type/ResManage.h>
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
ResManage<F,T>::~ResManage()
|
ResManage<K,V>::~ResManage()
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
void ResManage<F,T>::Clear()
|
void ResManage<K,V>::Clear()
|
||||||
{
|
{
|
||||||
int n=items.GetCount();
|
int n=items.GetCount();
|
||||||
|
|
||||||
@ -25,8 +25,8 @@ namespace hgl
|
|||||||
items.Clear();
|
items.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
void ResManage<F,T>::ClearFree()
|
void ResManage<K,V>::ClearFree()
|
||||||
{
|
{
|
||||||
int n=items.GetCount();
|
int n=items.GetCount();
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ namespace hgl
|
|||||||
{
|
{
|
||||||
ResItem *obj=items.GetItem(n);
|
ResItem *obj=items.GetItem(n);
|
||||||
|
|
||||||
if(obj->count<=0)
|
if(obj->ref_count<=0)
|
||||||
{
|
{
|
||||||
Clear(obj->right);
|
Clear(obj->right);
|
||||||
items.DeleteBySerial(n);
|
items.DeleteBySerial(n);
|
||||||
@ -42,8 +42,8 @@ namespace hgl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
bool ResManage<F,T>::Add(const F &flag,T *obj)
|
bool ResManage<K,V>::Add(const K &flag,V *obj)
|
||||||
{
|
{
|
||||||
if(!obj)return(false);
|
if(!obj)return(false);
|
||||||
|
|
||||||
@ -54,8 +54,8 @@ namespace hgl
|
|||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
T *ResManage<F,T>::Find(const F &flag)
|
V *ResManage<K,V>::Find(const K &flag)
|
||||||
{
|
{
|
||||||
int index=items.Find(flag);
|
int index=items.Find(flag);
|
||||||
|
|
||||||
@ -67,8 +67,8 @@ namespace hgl
|
|||||||
return obj->right;
|
return obj->right;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
T *ResManage<F,T>::Get(const F &flag)
|
V *ResManage<K,V>::Get(const K &flag)
|
||||||
{
|
{
|
||||||
int index=items.Find(flag);
|
int index=items.Find(flag);
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ namespace hgl
|
|||||||
{
|
{
|
||||||
ResItem *obj=items.GetItem(index);
|
ResItem *obj=items.GetItem(index);
|
||||||
|
|
||||||
++obj->count;
|
++obj->ref_count;
|
||||||
|
|
||||||
return obj->right;
|
return obj->right;
|
||||||
}
|
}
|
||||||
@ -87,8 +87,8 @@ namespace hgl
|
|||||||
/**
|
/**
|
||||||
* 确认指定数据是否存在
|
* 确认指定数据是否存在
|
||||||
*/
|
*/
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
bool ResManage<F,T>::ValueExist(T *value)
|
bool ResManage<K,V>::ValueExist(V *value)
|
||||||
{
|
{
|
||||||
return(items.FindByValue(value)!=-1);
|
return(items.FindByValue(value)!=-1);
|
||||||
}
|
}
|
||||||
@ -100,8 +100,8 @@ namespace hgl
|
|||||||
* @param ref_count 引用计数存放地址
|
* @param ref_count 引用计数存放地址
|
||||||
* @param 是否增加引用计数
|
* @param 是否增加引用计数
|
||||||
*/
|
*/
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
bool ResManage<F,T>::GetKeyByValue(T *value,F *key,uint *ref_count,bool inc_ref_count)
|
bool ResManage<K,V>::GetKeyByValue(V *value,K *key,uint *ref_count,bool inc_ref_count)
|
||||||
{
|
{
|
||||||
int index=items.FindByValue(value);
|
int index=items.FindByValue(value);
|
||||||
|
|
||||||
@ -110,32 +110,32 @@ namespace hgl
|
|||||||
ResItem *obj=items.GetItem(index);
|
ResItem *obj=items.GetItem(index);
|
||||||
|
|
||||||
if(inc_ref_count)
|
if(inc_ref_count)
|
||||||
++obj->count;
|
++obj->ref_count;
|
||||||
|
|
||||||
if(key)
|
if(key)
|
||||||
*key=obj->left;
|
*key=obj->left;
|
||||||
|
|
||||||
if(ref_count)
|
if(ref_count)
|
||||||
*ref_count=obj->count;
|
*ref_count=obj->ref_count;
|
||||||
|
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
uint ResManage<F,T>::ReleaseBySerial(int index,bool zero_clear)
|
int ResManage<K,V>::ReleaseBySerial(int index,bool zero_clear)
|
||||||
{
|
{
|
||||||
if(index==-1)
|
if(index==-1)
|
||||||
{
|
{
|
||||||
// ErrorHint(u"所释放的资源不存在");
|
// ErrorHint(u"所释放的资源不存在");
|
||||||
return(0);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
ResItem *obj=items.GetItem(index);
|
ResItem *obj=items.GetItem(index);
|
||||||
|
|
||||||
--obj->count;
|
--obj->ref_count;
|
||||||
|
|
||||||
if(obj->count>0)
|
if(obj->ref_count>0)
|
||||||
return obj->count;
|
return obj->ref_count;
|
||||||
|
|
||||||
if(zero_clear)
|
if(zero_clear)
|
||||||
{
|
{
|
||||||
@ -147,14 +147,14 @@ namespace hgl
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
uint ResManage<F,T>::Release(const F &flag,bool zero_clear)
|
int ResManage<K,V>::Release(const K &flag,bool zero_clear)
|
||||||
{
|
{
|
||||||
return ReleaseBySerial(items.Find(flag),zero_clear);
|
return ReleaseBySerial(items.Find(flag),zero_clear);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F,typename T>
|
template<typename K,typename V>
|
||||||
uint ResManage<F,T>::Release(T *td,bool zero_clear)
|
int ResManage<K,V>::Release(V *td,bool zero_clear)
|
||||||
{
|
{
|
||||||
return ReleaseBySerial(items.FindByValue(td),zero_clear);
|
return ReleaseBySerial(items.FindByValue(td),zero_clear);
|
||||||
}
|
}
|
||||||
|
@ -4,34 +4,34 @@
|
|||||||
#include<hgl/type/Map.h>
|
#include<hgl/type/Map.h>
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
template<typename F,typename T> struct RefFlagData:public Pair<F,T *>
|
template<typename K,typename V> struct RefKeyValue:public Pair<K,V *> ///<带引用计数的Key/value数据结构
|
||||||
{
|
{
|
||||||
uint count;
|
int ref_count; ///<引用计数
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
RefFlagData():Pair<F,T *>()
|
RefKeyValue():Pair<K,V *>()
|
||||||
{
|
{
|
||||||
count=1;
|
ref_count=1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 资源管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载
|
* 资源管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载
|
||||||
*/
|
*/
|
||||||
template<typename F,typename T> class ResManage
|
template<typename K,typename V> class ResManage
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
using ResItem=RefFlagData<F,T> ;
|
using ResItem=RefKeyValue<K,V>;
|
||||||
|
|
||||||
_Map<F,T *,ResItem> items;
|
_Map<K,V *,ResItem> items;
|
||||||
|
|
||||||
uint ReleaseBySerial(int,bool);
|
int ReleaseBySerial(int,bool);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
virtual void Clear(T *obj){delete obj;} ///<资源释放虚拟函数(缺省为直接delete对象)
|
virtual void Clear(V *obj){delete obj;} ///<资源释放虚拟函数(缺省为直接delete对象)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -42,42 +42,42 @@ namespace hgl
|
|||||||
|
|
||||||
const int GetCount()const{return items.GetCount();} ///<取得数据数量
|
const int GetCount()const{return items.GetCount();} ///<取得数据数量
|
||||||
|
|
||||||
virtual bool Add(const F &,T *); ///<添加一个数据
|
virtual bool Add(const K &,V *); ///<添加一个数据
|
||||||
virtual T * Find(const F &); ///<查找一个数据(不增加引用计数)
|
virtual V * Find(const K &); ///<查找一个数据(不增加引用计数)
|
||||||
virtual T * Get(const F &); ///<取得一个数据(增加引用计数)
|
virtual V * Get(const K &); ///<取得一个数据(增加引用计数)
|
||||||
|
|
||||||
virtual bool ValueExist(T *); ///<确认这个数据是否存在
|
virtual bool ValueExist(V *); ///<确认这个数据是否存在
|
||||||
virtual bool GetKeyByValue(T *,F *,uint *,bool inc_ref_count=false); ///<取得一个数据的Key和引用次数
|
virtual bool GetKeyByValue(V *,K *,uint *,bool inc_ref=false); ///<取得一个数据的Key和引用次数
|
||||||
|
|
||||||
virtual uint Release(const F &,bool zero_clear=false); ///<释放一个数据
|
virtual int Release(const K &,bool zero_clear=false); ///<释放一个数据
|
||||||
virtual uint Release(T *,bool zero_clear=false); ///<释放一个数据
|
virtual int Release(V *,bool zero_clear=false); ///<释放一个数据
|
||||||
};//template<typename F,typename T> class ResManage
|
};//template<typename F,typename T> class ResManage
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用int类做数标致的资源管理器
|
* 使用int类做数标致的资源管理器
|
||||||
*/
|
*/
|
||||||
template<typename F,typename T> class IDResManage:public ResManage<F,T>
|
template<typename K,typename V> class IDResManage:public ResManage<K,V>
|
||||||
{
|
{
|
||||||
F id_count=0;
|
K id_count=0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using ResManage<F,T>::ResManage;
|
using ResManage<K,V>::ResManage;
|
||||||
virtual ~IDResManage()=default;
|
virtual ~IDResManage()=default;
|
||||||
|
|
||||||
virtual F Add(T *value)
|
virtual K Add(V *value)
|
||||||
{
|
{
|
||||||
if(!value)return(-1);
|
if(!value)return(-1);
|
||||||
|
|
||||||
{
|
{
|
||||||
F key;
|
K key;
|
||||||
uint count;
|
uint count;
|
||||||
|
|
||||||
if(ResManage<F,T>::GetKeyByValue(value,&key,&count,true))
|
if(ResManage<K,V>::GetKeyByValue(value,&key,&count,true))
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!ResManage<F,T>::Add(id_count,value))
|
if(!ResManage<K,V>::Add(id_count,value))
|
||||||
return(-1);
|
return(-1);
|
||||||
|
|
||||||
return id_count++;
|
return id_count++;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user