moved codes to .h from .cpp and then deleted ObjectManage.cpp
This commit is contained in:
@@ -1,162 +0,0 @@
|
||||
#ifndef HGL_OBJECT_MANAGE_CPP
|
||||
#define HGL_OBJECT_MANAGE_CPP
|
||||
|
||||
#include<hgl/type/ObjectManage.h>
|
||||
namespace hgl
|
||||
{
|
||||
template<typename K,typename V>
|
||||
ObjectManage<K,V>::~ObjectManage()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
void ObjectManage<K,V>::Clear()
|
||||
{
|
||||
int n=items.GetCount();
|
||||
|
||||
while(n--)
|
||||
{
|
||||
ResItem *obj=items.GetItem(n);
|
||||
|
||||
Clear(obj->value);
|
||||
}
|
||||
|
||||
items.Clear();
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
void ObjectManage<K,V>::ClearFree()
|
||||
{
|
||||
int n=items.GetCount();
|
||||
|
||||
while(n--)
|
||||
{
|
||||
ResItem *obj=items.GetItem(n);
|
||||
|
||||
if(obj->ref_count<=0)
|
||||
{
|
||||
Clear(obj->value);
|
||||
items.DeleteBySerial(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
bool ObjectManage<K,V>::Add(const K &flag,V *obj)
|
||||
{
|
||||
if(!obj)return(false);
|
||||
|
||||
if(items.KeyExist(flag))
|
||||
return(false);
|
||||
|
||||
items.Add(flag,obj);
|
||||
return(true);
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
V *ObjectManage<K,V>::Find(const K &flag)
|
||||
{
|
||||
int index=items.Find(flag);
|
||||
|
||||
if(index==-1)
|
||||
return(nullptr);
|
||||
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
return obj->value;
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
V *ObjectManage<K,V>::Get(const K &flag)
|
||||
{
|
||||
int index=items.Find(flag);
|
||||
|
||||
if(index!=-1)
|
||||
{
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
++obj->ref_count;
|
||||
|
||||
return obj->value;
|
||||
}
|
||||
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认指定数据是否存在
|
||||
*/
|
||||
template<typename K,typename V>
|
||||
bool ObjectManage<K,V>::ValueExist(V *value)
|
||||
{
|
||||
return(items.FindByValue(value)!=-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定数据的Key和引用计数
|
||||
* @param value 数据
|
||||
* @param key Key存放地址
|
||||
* @param ref_count 引用计数存放地址
|
||||
* @param 是否增加引用计数
|
||||
*/
|
||||
template<typename K,typename V>
|
||||
bool ObjectManage<K,V>::GetKeyByValue(V *value,K *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->ref_count;
|
||||
|
||||
if(key)
|
||||
*key=obj->key;
|
||||
|
||||
if(ref_count)
|
||||
*ref_count=obj->ref_count;
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
int ObjectManage<K,V>::ReleaseBySerial(int index,bool zero_clear)
|
||||
{
|
||||
if(index==-1)
|
||||
{
|
||||
// ErrorHint(u"所释放的资源不存在");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
--obj->ref_count;
|
||||
|
||||
if(obj->ref_count>0)
|
||||
return obj->ref_count;
|
||||
|
||||
if(zero_clear)
|
||||
{
|
||||
Clear(obj->value);
|
||||
|
||||
items.DeleteBySerial(index);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
int ObjectManage<K,V>::Release(const K &flag,bool zero_clear)
|
||||
{
|
||||
return ReleaseBySerial(items.Find(flag),zero_clear);
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
int ObjectManage<K,V>::Release(V *td,bool zero_clear)
|
||||
{
|
||||
return ReleaseBySerial(items.FindByValue(td),zero_clear);
|
||||
}
|
||||
}//namespace hgl
|
||||
#endif//HGL_OBJECT_MANAGE_CPP
|
@@ -3,7 +3,7 @@
|
||||
#include<hgl/type/Map.h>
|
||||
namespace hgl
|
||||
{
|
||||
template<typename K,typename V> struct RefKeyValue:public KeyValue<K,V *> ///<带引用计数的Key/value数据结构
|
||||
template<typename K,typename V> struct RefKeyValue:public KeyValue<K,V *> ///<带引用计数的Key/value对象结构
|
||||
{
|
||||
int ref_count; ///<引用计数
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace hgl
|
||||
};
|
||||
|
||||
/**
|
||||
* 对象管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载
|
||||
* 对象管理器,它没有缓冲管理,仅仅是统一管理对象,并保证不会被重复加载
|
||||
*/
|
||||
template<typename K,typename V> class ObjectManage
|
||||
{
|
||||
@@ -26,34 +26,177 @@ namespace hgl
|
||||
|
||||
_Map<K,V *,ResItem> items;
|
||||
|
||||
int ReleaseBySerial(int,bool);
|
||||
|
||||
protected:
|
||||
|
||||
virtual void Clear(V *obj){delete obj;} ///<对象释放虚拟函数(缺省为直接delete对象)
|
||||
|
||||
public:
|
||||
|
||||
virtual ~ObjectManage();
|
||||
virtual ~ObjectManage()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
virtual void Clear(); ///<清除所有数据
|
||||
virtual void ClearFree(); ///<清除所有引用计数为0的数据
|
||||
virtual void Clear() ///<清除所有对象
|
||||
{
|
||||
int n=items.GetCount();
|
||||
|
||||
const int GetCount()const{return items.GetCount();} ///<取得数据数量
|
||||
while(n--)
|
||||
{
|
||||
ResItem *obj=items.GetItem(n);
|
||||
|
||||
virtual bool Add(const K &,V *); ///<添加一个数据
|
||||
virtual V * Find(const K &); ///<查找一个数据(不增加引用计数)
|
||||
virtual V * Get(const K &); ///<取得一个数据(增加引用计数)
|
||||
Clear(obj->value);
|
||||
}
|
||||
|
||||
virtual bool ValueExist(V *); ///<确认这个数据是否存在
|
||||
virtual bool GetKeyByValue(V *,K *,uint *,bool inc_ref=false); ///<取得一个数据的Key和引用次数
|
||||
items.Clear();
|
||||
}
|
||||
|
||||
virtual int Release(const K &,bool zero_clear=false); ///<释放一个数据
|
||||
virtual int Release(V *,bool zero_clear=false); ///<释放一个数据
|
||||
virtual void ClearFree() ///<清除所有引用计数为0的对象
|
||||
{
|
||||
int n=items.GetCount();
|
||||
|
||||
while(n--)
|
||||
{
|
||||
ResItem *obj=items.GetItem(n);
|
||||
|
||||
if(obj->ref_count<=0)
|
||||
{
|
||||
Clear(obj->value);
|
||||
items.DeleteBySerial(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const int GetCount()const{return items.GetCount();} ///<取得对象数量
|
||||
|
||||
virtual bool Add(const K &key,V *obj) ///<添加一个对象
|
||||
{
|
||||
if(!obj)return(false);
|
||||
|
||||
if(items.KeyExist(key))
|
||||
return(false);
|
||||
|
||||
items.Add(key,obj);
|
||||
return(true);
|
||||
}
|
||||
|
||||
virtual V * Find(const K &key) ///<查找一个对象(不增加引用计数)
|
||||
{
|
||||
int index=items.Find(key);
|
||||
|
||||
if(index==-1)
|
||||
return(nullptr);
|
||||
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
return obj->value;
|
||||
}
|
||||
|
||||
virtual V * Get(const K &key) ///<取得一个对象(增加引用计数)
|
||||
{
|
||||
int index=items.Find(key);
|
||||
|
||||
if(index!=-1)
|
||||
{
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
++obj->ref_count;
|
||||
|
||||
return obj->value;
|
||||
}
|
||||
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
virtual bool ValueExist(V *value) ///<确认这个对象是否存在
|
||||
{
|
||||
return(items.FindByValue(value)!=-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定对象的Key和引用计数
|
||||
* @param value 对象
|
||||
* @param key Key存放地址
|
||||
* @param ref_count 引用计数存放地址
|
||||
* @param 是否增加引用计数
|
||||
*/
|
||||
virtual bool GetKeyByValue(V *value,K *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->ref_count;
|
||||
|
||||
if(key)
|
||||
*key=obj->key;
|
||||
|
||||
if(ref_count)
|
||||
*ref_count=obj->ref_count;
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据序列号释放一个对象(减少引用计数).
|
||||
*
|
||||
* \param index
|
||||
* \param zero_clear 引用为0后是否清除对象
|
||||
* \return
|
||||
*/
|
||||
virtual int ReleaseBySerial(int index,bool zero_clear=false)
|
||||
{
|
||||
if(index==-1)
|
||||
{
|
||||
// ErrorHint(u"所释放的资源不存在");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
--obj->ref_count;
|
||||
|
||||
if(obj->ref_count>0)
|
||||
return obj->ref_count;
|
||||
|
||||
if(zero_clear)
|
||||
{
|
||||
Clear(obj->value);
|
||||
|
||||
items.DeleteBySerial(index);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key释放一个对象(减少引用计数).
|
||||
*
|
||||
* \param key
|
||||
* \param zero_clear 引用为0后是否清除对象
|
||||
* \return
|
||||
*/
|
||||
virtual int Release(const K &key,bool zero_clear=false)
|
||||
{
|
||||
return ReleaseBySerial(items.Find(key),zero_clear);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据对象本身减少一次引用计数
|
||||
* @param td 对象指针
|
||||
* @param zero_clear 引用为0后是否清除对象
|
||||
*/
|
||||
int Release(V *td,bool zero_clear=false)
|
||||
{
|
||||
return ReleaseBySerial(items.FindByValue(td),zero_clear);
|
||||
}
|
||||
};//template<typename K,typename V> class ObjectManage
|
||||
|
||||
/**
|
||||
* 使用整型数据类做数标识的对象管理器
|
||||
* 使用整型对象类做数标识的对象管理器
|
||||
*/
|
||||
template<typename K,typename V> class IDObjectManage:public ObjectManage<K,V>
|
||||
{
|
||||
@@ -86,5 +229,3 @@ namespace hgl
|
||||
template<typename V> using U32ObjectManage=IDObjectManage<uint32,V>;
|
||||
template<typename V> using U64ObjectManage=IDObjectManage<uint64,V>;
|
||||
}//namespace hgl
|
||||
|
||||
#include<hgl/type/ObjectManage.cpp>
|
||||
|
Reference in New Issue
Block a user