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