newly DataLifecycleManager instead of DataLifetimeCallback,

newly DataClifecycleManager in data collection template<>
This commit is contained in:
hyzboy 2024-10-31 01:34:41 +08:00
parent 2377c39642
commit 1fe8f51faa
8 changed files with 136 additions and 103 deletions

View File

@ -6,7 +6,7 @@ namespace hgl
/** /**
* *
*/ */
template<typename T> struct DataLifetimeCallback template<typename T> struct DataLifecycleManager
{ {
virtual bool Create(T *){return true;} virtual bool Create(T *){return true;}
virtual void Clear(T *){}; virtual void Clear(T *){};
@ -31,7 +31,7 @@ namespace hgl
/** /**
* *
*/ */
template<typename T> struct ObjectLifetimeCallback:public DataLifetimeCallback<T *> template<typename T> struct ObjectLifecycleManager:public DataLifecycleManager<T *>
{ {
virtual bool Create(T **) override {return false;} virtual bool Create(T **) override {return false;}
virtual void Clear(T **obj) override {if(obj&&*obj)delete *obj;} virtual void Clear(T **obj) override {if(obj&&*obj)delete *obj;}
@ -42,17 +42,19 @@ namespace hgl
for(int i=0;i<count;i++) for(int i=0;i<count;i++)
{ {
delete *obj; if(*obj)
delete *obj;
*obj=nullptr; *obj=nullptr;
++obj; ++obj;
} }
} }
};//struct ObjectLifetimeCallback };//struct ObjectLifecycleManager
/** /**
* *
*/ */
template<typename T> struct DefaultObjectLifetimeCallback:public ObjectLifetimeCallback<T> template<typename T> struct DefaultObjectLifetimeCallback:public ObjectLifecycleManager<T>
{ {
virtual bool Create(T **obj) override {*obj=new T;return(true);} virtual bool Create(T **obj) override {*obj=new T;return(true);}
};//struct DefaultObjectLifetimeCallback };//struct DefaultObjectLifetimeCallback

View File

@ -185,7 +185,7 @@ namespace hgl
* Map<int ID,struct DATA>DATA需要复制会消耗一些时间DATA * * Map<int ID,struct DATA>DATA需要复制会消耗一些时间DATA *
*/ */
template<typename K,typename V,typename KVData> template<typename K,typename V,typename KVData>
V *_Map<K,V,KVData>::GetPointer(const K &key)const V *_Map<K,V,KVData>::GetValuePointer(const K &key)const
{ {
int index=Find(key); int index=Find(key);

View File

@ -50,7 +50,7 @@ namespace hgl
bool ContainsKey(const K &key)const{return(Find(key)!=-1);} ///<确认这个数据是否存在 bool ContainsKey(const K &key)const{return(Find(key)!=-1);} ///<确认这个数据是否存在
bool ContainsValue(const V &value)const{return(FindByValue(value)!=-1);} ///<确认这个数据是否存在 bool ContainsValue(const V &value)const{return(FindByValue(value)!=-1);} ///<确认这个数据是否存在
bool Check(const K &key,const V &value)const; ///<确认数据是否是这个 bool Check(const K &key,const V &value)const; ///<确认数据是否是这个
virtual V * GetPointer(const K &key)const; ///<取得数据指针 virtual V * GetValuePointer(const K &key)const; ///<取得数据指针
virtual int GetValueAndSerial(const K &,V &) const; ///<取得数据与索引 virtual int GetValueAndSerial(const K &,V &) const; ///<取得数据与索引
bool Get(const K &key,V &value)const{return(GetValueAndSerial(key,value)>=0);} ///<取得数据 bool Get(const K &key,V &value)const{return(GetValueAndSerial(key,value)>=0);} ///<取得数据
virtual bool Delete(const K &,V &); ///<将指定数据从列表中移除,并获得这个数据 virtual bool Delete(const K &,V &); ///<将指定数据从列表中移除,并获得这个数据
@ -167,8 +167,8 @@ namespace hgl
typedef _Map<K,V *,KVData> SuperClass; typedef _Map<K,V *,KVData> SuperClass;
ObjectLifetimeCallback<V> *olc; ObjectLifecycleManager<V> *olc;
ObjectLifetimeCallback<V> default_olc; ObjectLifecycleManager<V> default_olc;
void DeleteObject(KVData *ds) void DeleteObject(KVData *ds)
{ {
@ -195,7 +195,7 @@ namespace hgl
Clear(); Clear();
} }
virtual void SetDataLifetimeCallback(ObjectLifetimeCallback<V> *cb) ///<设定数据生命周期回调函数 virtual void SetDataLifetimeCallback(ObjectLifecycleManager<V> *cb) ///<设定数据生命周期回调函数
{ {
olc=cb; olc=cb;
} }

View File

@ -7,18 +7,20 @@ namespace hgl
/** /**
* delete * delete
*/ */
template<typename T> class ObjectList:public List<T *> ///对象列表处理类 template<typename T> class ObjectListTemplate:public List<T *> ///对象列表处理类
{ {
protected:
DataLifecycleManager<T> *dlm; ///<数据生命周期回调函数
public: public:
using ItemPointer=T *; using ItemPointer=T *;
virtual void DeleteObject(T *obj){if(obj)delete obj;}
public: //方法 public: //方法
using List<T *>::List; ObjectListTemplate(DataLifecycleManager<T> *_dlm):List<T *>(){dlm=_dlm;}
virtual ~ObjectList(){Free();} virtual ~ObjectListTemplate(){Free();}
public: public:
@ -38,8 +40,7 @@ namespace hgl
virtual void Clear() override ///<清除所有数据,但不清空缓冲区 virtual void Clear() override ///<清除所有数据,但不清空缓冲区
{ {
for(auto *obj:List<T *>::data_array) dlm->Clear(this->data_array.GetData(),this->data_array.GetCount());
DeleteObject(obj);
List<T *>::Clear(); List<T *>::Clear();
} }
@ -63,13 +64,7 @@ namespace hgl
if(index<0||num<=0||index+num>=List<T *>::GetCount()) if(index<0||num<=0||index+num>=List<T *>::GetCount())
return(false); return(false);
ItemPointer *p=this->data_array.data()+index; dlm->Clear(this->data_array.GetData()+index,num);
for(int i=0;i<num;i++)
{
DeleteObject(*p);
++p;
}
return true; return true;
} }
@ -121,20 +116,14 @@ namespace hgl
{ {
if(!obj_list||num<=0)return(-1); if(!obj_list||num<=0)return(-1);
ItemPointer *p=(ItemPointer *)obj_list; dlm->Clear(obj_list,num);
for(int i=0;i<num;i++)
{
DeleteObject(*p);
++p;
}
return List<T *>::DeleteByValue(obj_list,num); return List<T *>::DeleteByValue(obj_list,num);
} }
virtual T *operator[](int n)const ///<操作符重载取得指定索引处的数据 virtual T *operator[](int n)const ///<操作符重载取得指定索引处的数据
{ {
T **obj=this->data_array.GetPointer(n); T **obj=this->data_array.At(n);
if(!obj)return(nullptr); if(!obj)return(nullptr);
@ -166,7 +155,7 @@ namespace hgl
if(!this->data_array.SetCount(new_count)) if(!this->data_array.SetCount(new_count))
return(false); return(false);
ItemPointer *p=this->data_array.GetPointer(cur_count); ItemPointer *p=this->data_array.At(cur_count);
hgl_zero<ItemPointer>(p,new_count-cur_count); hgl_zero<ItemPointer>(p,new_count-cur_count);
@ -183,17 +172,27 @@ namespace hgl
virtual bool Set(int index,const ItemPointer &data) override virtual bool Set(int index,const ItemPointer &data) override
{ {
ItemPointer *p=this->data_array.GetPointer(index); ItemPointer *p=this->data_array.At(index);
if(!p)return(false); if(!p)return(false);
DeleteObject(*p); dlm->Clear(p);
*p=data; *p=data;
return(true); return(true);
} }
};//template <typename T> class ObjectList };//template <typename T> class ObjectList
template<typename T> class ObjectList:public ObjectListTemplate<T>
{
ObjectLifecycleManager<T> DefaultOLM;
public:
ObjectList():ObjectListTemplate<T>(&DefaultOLM){}
virtual ~ObjectList()=default;
};//template<typename T> class ObjectList:public ObjectListTemplate<T>
#define OBJECT_LIST_ARRAY_FREE(object_list) for(auto &obj:object_list)obj.Free(); #define OBJECT_LIST_ARRAY_FREE(object_list) for(auto &obj:object_list)obj.Free();
#define OBJECT_LIST_ARRAY_CLEAR(object_list) for(auto &obj:object_list)obj.Clear(); #define OBJECT_LIST_ARRAY_CLEAR(object_list) for(auto &obj:object_list)obj.Clear();
}//namespace hgl }//namespace hgl

View File

@ -18,7 +18,7 @@ namespace hgl
/** /**
* , * ,
*/ */
template<typename K,typename V> class ObjectManage template<typename K,typename V> class ObjectManageTemplate
{ {
public: public:
@ -28,30 +28,28 @@ namespace hgl
_Map<K,V *,KVObject> items; _Map<K,V *,KVObject> items;
ObjectLifetimeCallback<V> *dlc; ///<数据生命周期回调函数 ObjectLifecycleManager<V> *olm; ///<数据生命周期回调函数
ObjectLifetimeCallback<V> default_dlc;
public: public:
ObjectManage() ObjectManageTemplate(ObjectLifecycleManager<V> *_olm)
{ {
dlc=&default_dlc; olm=_olm;
} }
virtual ~ObjectManage() virtual ~ObjectManageTemplate()
{ {
Clear(); Clear();
} }
virtual void SetDataLifetimeCallback(ObjectLifetimeCallback<V> *cb) ///<设定数据生命周期回调函数 virtual void SetDataLifetimeCallback(ObjectLifecycleManager<V> *cb) ///<设定数据生命周期回调函数
{ {
dlc=cb; olm=cb;
} }
virtual void Clear() ///<清除所有对象 virtual void Clear() ///<清除所有对象
{ {
if(dlc) if(olm)
{ {
int n=items.GetCount(); int n=items.GetCount();
@ -59,7 +57,7 @@ namespace hgl
{ {
KVObject *obj=items.GetItem(n); KVObject *obj=items.GetItem(n);
dlc->Clear(&(obj->value)); olm->Clear(&(obj->value));
} }
} }
@ -76,8 +74,8 @@ namespace hgl
if(obj->ref_count<=0) if(obj->ref_count<=0)
{ {
if(dlc) if(olm)
dlc->Clear(&(obj->value)); olm->Clear(&(obj->value));
items.DeleteAt(n); items.DeleteAt(n);
} }
@ -181,8 +179,8 @@ namespace hgl
if(zero_clear) if(zero_clear)
{ {
if(dlc) if(olm)
dlc->Clear(&(obj->value)); olm->Clear(&(obj->value));
items.DeleteAt(index); items.DeleteAt(index);
} }
@ -211,7 +209,17 @@ namespace hgl
{ {
return ReleaseBySerial(items.FindByValue(td),zero_clear); return ReleaseBySerial(items.FindByValue(td),zero_clear);
} }
};//template<typename K,typename V> class ObjectManage };//template<typename K,typename V> class ObjectManageTemplate
template<typename K,typename V> class ObjectManage:public ObjectManageTemplate<K,V>
{
ObjectLifecycleManager<V> DefaultOLM;
public:
ObjectManage():ObjectManageTemplate(&DefaultOLM){}
virtual ~ObjectManage()=default;
};//template<typename K,typename V> class ObjectManage:public ObjectManageTemplate<K,V>
/** /**
* 使 * 使

View File

@ -2,7 +2,7 @@
#include<hgl/type/List.h> #include<hgl/type/List.h>
#include<hgl/type/Queue.h> #include<hgl/type/Queue.h>
#include<hgl/type/LifetimeCallback.h> #include<hgl/type/LifecycleManager.h>
namespace hgl namespace hgl
{ {
/** /**
@ -10,7 +10,7 @@ namespace hgl
* 使Queue模板管理(使Stack运行性能更好) * 使Queue模板管理(使Stack运行性能更好)
* 使List模板管理() * 使List模板管理()
*/ */
template<typename T,typename AT,typename IT,typename DEFAULT_DLC> class _Pool ///数据池 template<typename T,typename AT,typename IT> class PoolTemplate ///数据池模板
{ {
protected: protected:
@ -28,9 +28,7 @@ namespace hgl
protected: protected:
DataLifetimeCallback<T> *dlc; ///<数据生命周期回调函数 DataLifecycleManager<T> *dlm; ///<数据生命周期回调函数
DEFAULT_DLC default_dlc;
public: //属性 public: //属性
@ -40,8 +38,8 @@ namespace hgl
DataArray<T> & GetActiveArray(){return Active.GetArray();} ///<取得所有活跃数据 DataArray<T> & GetActiveArray(){return Active.GetArray();} ///<取得所有活跃数据
bool IsActive (const T &data)const{return Active.Contains(data);} ///<是否为活跃的 bool IsActive (const T &data)const{return Active.Contains(data);} ///<是否为活跃的
bool IsIdle (const T &data)const{return Idle.Contains(data);} ///<是否为非活跃的 bool IsIdle (const T &data)const{return Idle.Contains(data);} ///<是否为非活跃的
bool IsFull()const ///<活跃队列是否已满 bool IsFull()const ///<活跃队列是否已满
{ {
@ -53,21 +51,21 @@ namespace hgl
public: public:
_Pool() PoolTemplate(DataLifecycleManager<T> *_dlc)
{ {
max_active_count=0; max_active_count=0;
history_max=0; history_max=0;
dlc=&default_dlc; dlm=_dlc;
} }
virtual ~_Pool() virtual ~PoolTemplate()
{ {
Clear(); //有一些数据需要特别的Clear处理所以不能依赖Active/InActive模板本身的自晰构 Clear(); //有一些数据需要特别的Clear处理所以不能依赖Active/InActive模板本身的自晰构
} }
virtual void SetDataLifetimeCallback(DataLifetimeCallback<T> *cb) ///<设定数据生命周期回调函数 virtual void SetDataLifetimeCallback(DataLifecycleManager<T> *cb) ///<设定数据生命周期回调函数
{ {
dlc=cb; dlm=cb;
} }
virtual void PreAlloc(int count,bool set_to_max=false) ///<预分配空间 virtual void PreAlloc(int count,bool set_to_max=false) ///<预分配空间
@ -83,12 +81,12 @@ namespace hgl
virtual bool Create(T &value) ///<创建一个数据,并放置在活跃队列中 virtual bool Create(T &value) ///<创建一个数据,并放置在活跃队列中
{ {
if(!dlc)return(false); if(!dlm)return(false);
if(IsFull()) if(IsFull())
return(false); return(false);
if(!dlc->Create(&value)) if(!dlm->Create(&value))
return(false); return(false);
Active.Add(value); Active.Add(value);
@ -103,14 +101,14 @@ namespace hgl
if(IsFull()) if(IsFull())
return(false); return(false);
if(!dlc)return(false); if(!dlm)return(false);
if(!dlc->Create(&value)) if(!dlm->Create(&value))
return(false); return(false);
} }
else if(dlc) else if(dlm)
{ {
dlc->OnActive(&value); dlm->OnActive(&value);
} }
Active.Add(value); Active.Add(value);
@ -123,8 +121,8 @@ namespace hgl
if(!Idle.Pop(value)) if(!Idle.Pop(value))
return(false); return(false);
if(dlc) if(dlm)
dlc->OnActive(&value); dlm->OnActive(&value);
Active.Add(value); Active.Add(value);
return(true); return(true);
@ -160,8 +158,8 @@ namespace hgl
if(!Idle.Push(value)) if(!Idle.Push(value))
return(false); return(false);
if(dlc) if(dlm)
dlc->OnIdle(&value); dlm->OnIdle(&value);
return(true); return(true);
} }
@ -187,8 +185,8 @@ namespace hgl
virtual void ReleaseActive() ///<释放所有活跃数据 virtual void ReleaseActive() ///<释放所有活跃数据
{ {
if(dlc) if(dlm)
dlc->OnIdle(Active.GetData(),Active.GetCount()); dlm->OnIdle(Active.GetData(),Active.GetCount());
Idle.Push(Active.GetData(),Active.GetCount()); Idle.Push(Active.GetData(),Active.GetCount());
Active.Clear(); Active.Clear();
@ -196,15 +194,15 @@ namespace hgl
virtual void ClearActive() virtual void ClearActive()
{ {
if(dlc) if(dlm)
dlc->Clear(Active.GetData(),Active.GetCount()); dlm->Clear(Active.GetData(),Active.GetCount());
Active.Clear(); Active.Clear();
} }
virtual void ClearIdle() ///<清除所有非活跃数据 virtual void ClearIdle() ///<清除所有非活跃数据
{ {
Idle.Clear(dlc); Idle.Clear(dlm);
} }
virtual void Clear() ///<清除所有数据 virtual void Clear() ///<清除所有数据
@ -212,8 +210,18 @@ namespace hgl
ClearActive(); ClearActive();
ClearIdle(); ClearIdle();
} }
};//template<typename T,typename AT,typename IT> class _Pool };//template<typename T,typename AT,typename IT> class PoolTemplate
template<typename T> using Pool =_Pool<T, List<T>, Queue<T>, DataLifetimeCallback<T>>; ///<数据池模板 template<typename T,typename AT,typename IT,typename DLM> class PoolWithDLM:public PoolTemplate<T,AT,IT>
template<typename T> using ObjectPool =_Pool<T *, List<T *>, ObjectQueue<T>, ObjectLifetimeCallback<T>>; ///<对象池 {
DLM DefaultLifecycleManager;
public:
PoolWithDLM():PoolTemplate(&DefaultLifecycleManager){}
virtual ~PoolWithDLM()=default;
};//template<typename T,typename AT,typename IT,typename DLM> class PoolWithDLM:public PoolTemplate<T,AT,IT>
template<typename T> using Pool =PoolWithDLM<T, List<T>, Queue<T>, DataLifecycleManager<T>>; ///<数据池模板
template<typename T> using ObjectPool =PoolWithDLM<T *, List<T *>, ObjectQueue<T>, ObjectLifecycleManager<T>>; ///<对象池
}//namespace hgl }//namespace hgl

View File

@ -1,16 +1,18 @@
#pragma once #pragma once
#include<hgl/type/DataArray.h> #include<hgl/type/DataArray.h>
#include<hgl/type/LifetimeCallback.h> #include<hgl/type/LifecycleManager.h>
namespace hgl namespace hgl
{ {
/** /**
* Queue模板类用于保存一个先进先出 * Queue模板类用于保存一个先进先出
*/ */
template<typename T> class Queue ///队列顺序数据访问类 template<typename T> class QueueTemplate ///队列顺序数据访问类
{ {
protected: protected:
DataLifecycleManager<T> *dlm;
DataArray<T> data_array[2]; DataArray<T> data_array[2];
int read_index; int read_index;
@ -64,14 +66,16 @@ namespace hgl
public: //方法 public: //方法
Queue() QueueTemplate(DataLifecycleManager<T> *_dlm)
{ {
dlm=_dlm;
write_index=0; write_index=0;
read_index=1; read_index=1;
read_offset=0; read_offset=0;
} }
virtual ~Queue()=default; virtual ~QueueTemplate()=default;
virtual bool Push (T *data,int count) ///<压入一批数据 virtual bool Push (T *data,int count) ///<压入一批数据
{ {
@ -128,15 +132,15 @@ namespace hgl
public: public:
virtual void Clear (DataLifetimeCallback<T> *dlc=nullptr) ///<清除所有数据 virtual void Clear () ///<清除所有数据
{ {
if(dlc) if(dlm)
{ {
if(data_array[read_index].GetCount()>read_offset) //还有没读完的,需要清掉 if(data_array[read_index].GetCount()>read_offset) //还有没读完的,需要清掉
dlc->Clear(data_array[read_index].GetData()+read_offset, dlm->Clear(data_array[read_index].GetData()+read_offset,
data_array[read_index].GetCount()-read_offset); data_array[read_index].GetCount()-read_offset);
dlc->Clear(data_array[write_index].GetData(), dlm->Clear(data_array[write_index].GetData(),
data_array[write_index].GetCount()); data_array[write_index].GetCount());
} }
@ -144,22 +148,34 @@ namespace hgl
data_array[1].Clear(); data_array[1].Clear();
} }
virtual void Free (DataLifetimeCallback<T> *dlc=nullptr) ///<清除所有数据并释放内存 virtual void Free () ///<清除所有数据并释放内存
{ {
Clear(dlc); Clear(dlm);
data_array[0].Free(); data_array[0].Free();
data_array[1].Free(); data_array[1].Free();
} }
};//template<typename T> class Queue };//template<typename T> class QueueTemplate
template<typename T> class ObjectQueue:public Queue<T *> ///对象队列 template<typename T> class Queue:public QueueTemplate<T>
{
protected:
DataLifecycleManager<T> DefaultDLM;
public:
Queue():QueueTemplate(&DefaultDLM){};
virtual ~Queue()=default;
};//template<typename T> class Queue:public QueueTemplate<T>
template<typename T> class ObjectQueue:public QueueTemplate<T *> ///对象队列
{ {
DefaultObjectLifetimeCallback<T> default_olc; DefaultObjectLifetimeCallback<T> default_olc;
public: public:
using Queue<T *>::Queue; ObjectQueue():QueueTemplate(&default_olc){}
virtual ~ObjectQueue() override { Free(); } virtual ~ObjectQueue() override { Free(); }
virtual bool Push(T *obj) virtual bool Push(T *obj)
@ -186,7 +202,7 @@ namespace hgl
// return obj; // return obj;
//} //}
void Clear(DataLifetimeCallback<T *> *olc=nullptr) void Clear(DataLifecycleManager<T *> *olc=nullptr)
{ {
if(!olc) if(!olc)
olc=&default_olc; olc=&default_olc;
@ -194,7 +210,7 @@ namespace hgl
Queue<T *>::Clear(olc); Queue<T *>::Clear(olc);
} }
void Free(DataLifetimeCallback<T *> *olc=nullptr) void Free(DataLifecycleManager<T *> *olc=nullptr)
{ {
ObjectQueue<T>::Clear(olc); ObjectQueue<T>::Clear(olc);

View File

@ -87,17 +87,17 @@ namespace hgl
public: public:
virtual void Clear (DataLifetimeCallback<T> *dlc=nullptr) ///<清除所有数据 virtual void Clear (DataLifecycleManager<T> *dlm=nullptr) ///<清除所有数据
{ {
if(dlc) if(dlm)
dlc->Clear(data_array.GetData(),data_array.GetCount()); dlm->Clear(data_array.GetData(),data_array.GetCount());
data_array.Clear(); data_array.Clear();
} }
virtual void Free (DataLifetimeCallback<T> *dlc=nullptr) ///<清除所有数据并释放内存 virtual void Free (DataLifecycleManager<T> *dlm=nullptr) ///<清除所有数据并释放内存
{ {
Clear(dlc); Clear(dlm);
data_array.Free(); data_array.Free();
} }
@ -137,7 +137,7 @@ namespace hgl
return obj; return obj;
} }
void Clear(ObjectLifetimeCallback<T> *olc=nullptr) void Clear(ObjectLifecycleManager<T> *olc=nullptr)
{ {
if(!olc) if(!olc)
olc=&default_olc; olc=&default_olc;
@ -145,7 +145,7 @@ namespace hgl
Stack<T *>::Clear(olc); Stack<T *>::Clear(olc);
} }
void Free(ObjectLifetimeCallback<T> *olc=nullptr) void Free(ObjectLifecycleManager<T> *olc=nullptr)
{ {
ObjectStack<T>::Clear(olc); ObjectStack<T>::Clear(olc);