From 1fe8f51faa72aeac6ed0b95da396b6ab5bf07d21 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 31 Oct 2024 01:34:41 +0800 Subject: [PATCH] newly DataLifecycleManager instead of DataLifetimeCallback, newly DataClifecycleManager in data collection template<> --- ...{LifetimeCallback.h => LifecycleManager.h} | 12 ++-- inc/hgl/type/Map.cpp | 2 +- inc/hgl/type/Map.h | 8 +-- inc/hgl/type/ObjectList.h | 49 +++++++------ inc/hgl/type/ObjectManage.h | 40 ++++++----- inc/hgl/type/Pool.h | 68 +++++++++++-------- inc/hgl/type/Queue.h | 46 +++++++++---- inc/hgl/type/Stack.h | 14 ++-- 8 files changed, 136 insertions(+), 103 deletions(-) rename inc/hgl/type/{LifetimeCallback.h => LifecycleManager.h} (83%) diff --git a/inc/hgl/type/LifetimeCallback.h b/inc/hgl/type/LifecycleManager.h similarity index 83% rename from inc/hgl/type/LifetimeCallback.h rename to inc/hgl/type/LifecycleManager.h index 007400c..9869885 100644 --- a/inc/hgl/type/LifetimeCallback.h +++ b/inc/hgl/type/LifecycleManager.h @@ -6,7 +6,7 @@ namespace hgl /** * 原生数据生命周期处理回调 */ - template struct DataLifetimeCallback + template struct DataLifecycleManager { virtual bool Create(T *){return true;} virtual void Clear(T *){}; @@ -31,7 +31,7 @@ namespace hgl /** * 对像生命周期处理回调 */ - template struct ObjectLifetimeCallback:public DataLifetimeCallback + template struct ObjectLifecycleManager:public DataLifecycleManager { virtual bool Create(T **) override {return false;} virtual void Clear(T **obj) override {if(obj&&*obj)delete *obj;} @@ -42,17 +42,19 @@ namespace hgl for(int i=0;i struct DefaultObjectLifetimeCallback:public ObjectLifetimeCallback + template struct DefaultObjectLifetimeCallback:public ObjectLifecycleManager { virtual bool Create(T **obj) override {*obj=new T;return(true);} };//struct DefaultObjectLifetimeCallback diff --git a/inc/hgl/type/Map.cpp b/inc/hgl/type/Map.cpp index 4177292..358a3e5 100644 --- a/inc/hgl/type/Map.cpp +++ b/inc/hgl/type/Map.cpp @@ -185,7 +185,7 @@ namespace hgl * 比如定义了Map,直接返回DATA需要复制会消耗一些时间,直接返回DATA *会更好一些 */ template - V *_Map::GetPointer(const K &key)const + V *_Map::GetValuePointer(const K &key)const { int index=Find(key); diff --git a/inc/hgl/type/Map.h b/inc/hgl/type/Map.h index 20fcefe..63b2a08 100644 --- a/inc/hgl/type/Map.h +++ b/inc/hgl/type/Map.h @@ -50,7 +50,7 @@ namespace hgl bool ContainsKey(const K &key)const{return(Find(key)!=-1);} ///<确认这个数据是否存在 bool ContainsValue(const V &value)const{return(FindByValue(value)!=-1);} ///<确认这个数据是否存在 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; ///<取得数据与索引 bool Get(const K &key,V &value)const{return(GetValueAndSerial(key,value)>=0);} ///<取得数据 virtual bool Delete(const K &,V &); ///<将指定数据从列表中移除,并获得这个数据 @@ -167,8 +167,8 @@ namespace hgl typedef _Map SuperClass; - ObjectLifetimeCallback *olc; - ObjectLifetimeCallback default_olc; + ObjectLifecycleManager *olc; + ObjectLifecycleManager default_olc; void DeleteObject(KVData *ds) { @@ -195,7 +195,7 @@ namespace hgl Clear(); } - virtual void SetDataLifetimeCallback(ObjectLifetimeCallback *cb) ///<设定数据生命周期回调函数 + virtual void SetDataLifetimeCallback(ObjectLifecycleManager *cb) ///<设定数据生命周期回调函数 { olc=cb; } diff --git a/inc/hgl/type/ObjectList.h b/inc/hgl/type/ObjectList.h index 621168c..d0c5150 100644 --- a/inc/hgl/type/ObjectList.h +++ b/inc/hgl/type/ObjectList.h @@ -7,18 +7,20 @@ namespace hgl /** * 对象列表处理类与标准列表处理类的区别在于它对数据清除时会调用delete */ - template class ObjectList:public List ///对象列表处理类 + template class ObjectListTemplate:public List ///对象列表处理类 { + protected: + + DataLifecycleManager *dlm; ///<数据生命周期回调函数 + public: using ItemPointer=T *; - virtual void DeleteObject(T *obj){if(obj)delete obj;} - public: //方法 - using List::List; - virtual ~ObjectList(){Free();} + ObjectListTemplate(DataLifecycleManager *_dlm):List(){dlm=_dlm;} + virtual ~ObjectListTemplate(){Free();} public: @@ -38,8 +40,7 @@ namespace hgl virtual void Clear() override ///<清除所有数据,但不清空缓冲区 { - for(auto *obj:List::data_array) - DeleteObject(obj); + dlm->Clear(this->data_array.GetData(),this->data_array.GetCount()); List::Clear(); } @@ -63,13 +64,7 @@ namespace hgl if(index<0||num<=0||index+num>=List::GetCount()) return(false); - ItemPointer *p=this->data_array.data()+index; - - for(int i=0;iClear(this->data_array.GetData()+index,num); return true; } @@ -121,20 +116,14 @@ namespace hgl { if(!obj_list||num<=0)return(-1); - ItemPointer *p=(ItemPointer *)obj_list; - - for(int i=0;iClear(obj_list,num); return List::DeleteByValue(obj_list,num); } virtual T *operator[](int n)const ///<操作符重载取得指定索引处的数据 { - T **obj=this->data_array.GetPointer(n); + T **obj=this->data_array.At(n); if(!obj)return(nullptr); @@ -166,7 +155,7 @@ namespace hgl if(!this->data_array.SetCount(new_count)) return(false); - ItemPointer *p=this->data_array.GetPointer(cur_count); + ItemPointer *p=this->data_array.At(cur_count); hgl_zero(p,new_count-cur_count); @@ -183,17 +172,27 @@ namespace hgl 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); - DeleteObject(*p); + dlm->Clear(p); *p=data; return(true); } };//template class ObjectList + template class ObjectList:public ObjectListTemplate + { + ObjectLifecycleManager DefaultOLM; + + public: + + ObjectList():ObjectListTemplate(&DefaultOLM){} + virtual ~ObjectList()=default; + };//template class ObjectList:public ObjectListTemplate + #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(); }//namespace hgl diff --git a/inc/hgl/type/ObjectManage.h b/inc/hgl/type/ObjectManage.h index 6542cea..234017c 100644 --- a/inc/hgl/type/ObjectManage.h +++ b/inc/hgl/type/ObjectManage.h @@ -18,7 +18,7 @@ namespace hgl /** * 对象管理器,它没有缓冲管理,仅仅是统一管理对象,并保证不会被重复加载 */ - template class ObjectManage + template class ObjectManageTemplate { public: @@ -28,30 +28,28 @@ namespace hgl _Map items; - ObjectLifetimeCallback *dlc; ///<数据生命周期回调函数 - - ObjectLifetimeCallback default_dlc; + ObjectLifecycleManager *olm; ///<数据生命周期回调函数 public: - ObjectManage() + ObjectManageTemplate(ObjectLifecycleManager *_olm) { - dlc=&default_dlc; + olm=_olm; } - virtual ~ObjectManage() + virtual ~ObjectManageTemplate() { Clear(); } - virtual void SetDataLifetimeCallback(ObjectLifetimeCallback *cb) ///<设定数据生命周期回调函数 + virtual void SetDataLifetimeCallback(ObjectLifecycleManager *cb) ///<设定数据生命周期回调函数 { - dlc=cb; + olm=cb; } virtual void Clear() ///<清除所有对象 { - if(dlc) + if(olm) { int n=items.GetCount(); @@ -59,7 +57,7 @@ namespace hgl { 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(dlc) - dlc->Clear(&(obj->value)); + if(olm) + olm->Clear(&(obj->value)); items.DeleteAt(n); } @@ -181,8 +179,8 @@ namespace hgl if(zero_clear) { - if(dlc) - dlc->Clear(&(obj->value)); + if(olm) + olm->Clear(&(obj->value)); items.DeleteAt(index); } @@ -211,7 +209,17 @@ namespace hgl { return ReleaseBySerial(items.FindByValue(td),zero_clear); } - };//template class ObjectManage + };//template class ObjectManageTemplate + + template class ObjectManage:public ObjectManageTemplate + { + ObjectLifecycleManager DefaultOLM; + + public: + + ObjectManage():ObjectManageTemplate(&DefaultOLM){} + virtual ~ObjectManage()=default; + };//template class ObjectManage:public ObjectManageTemplate /** * 使用整型对象类做数标识的对象管理器 diff --git a/inc/hgl/type/Pool.h b/inc/hgl/type/Pool.h index 12da0b8..52336f8 100644 --- a/inc/hgl/type/Pool.h +++ b/inc/hgl/type/Pool.h @@ -2,7 +2,7 @@ #include #include -#include +#include namespace hgl { /** @@ -10,7 +10,7 @@ namespace hgl * 默认情部下空闲队列使用Queue模板管理(先入先出,总是使用最早扔进去的数据。可手动换成Stack运行性能更好,但逻辑性能更差。), * 活动队列使用List模板管理(无序)。 */ - template class _Pool ///数据池 + template class PoolTemplate ///数据池模板 { protected: @@ -28,9 +28,7 @@ namespace hgl protected: - DataLifetimeCallback *dlc; ///<数据生命周期回调函数 - - DEFAULT_DLC default_dlc; + DataLifecycleManager *dlm; ///<数据生命周期回调函数 public: //属性 @@ -40,8 +38,8 @@ namespace hgl DataArray & GetActiveArray(){return Active.GetArray();} ///<取得所有活跃数据 - bool IsActive (const T &data)const{return Active.Contains(data);} ///<是否为活跃的 - bool IsIdle (const T &data)const{return Idle.Contains(data);} ///<是否为非活跃的 + bool IsActive (const T &data)const{return Active.Contains(data);} ///<是否为活跃的 + bool IsIdle (const T &data)const{return Idle.Contains(data);} ///<是否为非活跃的 bool IsFull()const ///<活跃队列是否已满 { @@ -53,21 +51,21 @@ namespace hgl public: - _Pool() + PoolTemplate(DataLifecycleManager *_dlc) { max_active_count=0; history_max=0; - dlc=&default_dlc; + dlm=_dlc; } - virtual ~_Pool() + virtual ~PoolTemplate() { Clear(); //有一些数据需要特别的Clear处理,所以不能依赖Active/InActive模板本身的自晰构 } - virtual void SetDataLifetimeCallback(DataLifetimeCallback *cb) ///<设定数据生命周期回调函数 + virtual void SetDataLifetimeCallback(DataLifecycleManager *cb) ///<设定数据生命周期回调函数 { - dlc=cb; + dlm=cb; } virtual void PreAlloc(int count,bool set_to_max=false) ///<预分配空间 @@ -83,12 +81,12 @@ namespace hgl virtual bool Create(T &value) ///<创建一个数据,并放置在活跃队列中 { - if(!dlc)return(false); + if(!dlm)return(false); if(IsFull()) return(false); - if(!dlc->Create(&value)) + if(!dlm->Create(&value)) return(false); Active.Add(value); @@ -103,14 +101,14 @@ namespace hgl if(IsFull()) return(false); - if(!dlc)return(false); + if(!dlm)return(false); - if(!dlc->Create(&value)) + if(!dlm->Create(&value)) return(false); } - else if(dlc) + else if(dlm) { - dlc->OnActive(&value); + dlm->OnActive(&value); } Active.Add(value); @@ -123,8 +121,8 @@ namespace hgl if(!Idle.Pop(value)) return(false); - if(dlc) - dlc->OnActive(&value); + if(dlm) + dlm->OnActive(&value); Active.Add(value); return(true); @@ -160,8 +158,8 @@ namespace hgl if(!Idle.Push(value)) return(false); - if(dlc) - dlc->OnIdle(&value); + if(dlm) + dlm->OnIdle(&value); return(true); } @@ -187,8 +185,8 @@ namespace hgl virtual void ReleaseActive() ///<释放所有活跃数据 { - if(dlc) - dlc->OnIdle(Active.GetData(),Active.GetCount()); + if(dlm) + dlm->OnIdle(Active.GetData(),Active.GetCount()); Idle.Push(Active.GetData(),Active.GetCount()); Active.Clear(); @@ -196,15 +194,15 @@ namespace hgl virtual void ClearActive() { - if(dlc) - dlc->Clear(Active.GetData(),Active.GetCount()); + if(dlm) + dlm->Clear(Active.GetData(),Active.GetCount()); Active.Clear(); } virtual void ClearIdle() ///<清除所有非活跃数据 { - Idle.Clear(dlc); + Idle.Clear(dlm); } virtual void Clear() ///<清除所有数据 @@ -212,8 +210,18 @@ namespace hgl ClearActive(); ClearIdle(); } - };//template class _Pool + };//template class PoolTemplate - template using Pool =_Pool, Queue, DataLifetimeCallback>; ///<数据池模板 - template using ObjectPool =_Pool, ObjectQueue, ObjectLifetimeCallback>; ///<对象池 + template class PoolWithDLM:public PoolTemplate + { + DLM DefaultLifecycleManager; + + public: + + PoolWithDLM():PoolTemplate(&DefaultLifecycleManager){} + virtual ~PoolWithDLM()=default; + };//template class PoolWithDLM:public PoolTemplate + + template using Pool =PoolWithDLM, Queue, DataLifecycleManager>; ///<数据池模板 + template using ObjectPool =PoolWithDLM, ObjectQueue, ObjectLifecycleManager>; ///<对象池 }//namespace hgl diff --git a/inc/hgl/type/Queue.h b/inc/hgl/type/Queue.h index b64007a..ca9d030 100644 --- a/inc/hgl/type/Queue.h +++ b/inc/hgl/type/Queue.h @@ -1,16 +1,18 @@ #pragma once #include -#include +#include namespace hgl { /** * Queue模板类用于保存一个先进先出、后进后出的数据队列 */ - template class Queue ///队列顺序数据访问类 + template class QueueTemplate ///队列顺序数据访问类 { protected: + DataLifecycleManager *dlm; + DataArray data_array[2]; int read_index; @@ -64,14 +66,16 @@ namespace hgl public: //方法 - Queue() + QueueTemplate(DataLifecycleManager *_dlm) { + dlm=_dlm; + write_index=0; read_index=1; read_offset=0; } - virtual ~Queue()=default; + virtual ~QueueTemplate()=default; virtual bool Push (T *data,int count) ///<压入一批数据 { @@ -128,15 +132,15 @@ namespace hgl public: - virtual void Clear (DataLifetimeCallback *dlc=nullptr) ///<清除所有数据 + virtual void Clear () ///<清除所有数据 { - if(dlc) + if(dlm) { 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); - dlc->Clear(data_array[write_index].GetData(), + dlm->Clear(data_array[write_index].GetData(), data_array[write_index].GetCount()); } @@ -144,22 +148,34 @@ namespace hgl data_array[1].Clear(); } - virtual void Free (DataLifetimeCallback *dlc=nullptr) ///<清除所有数据并释放内存 + virtual void Free () ///<清除所有数据并释放内存 { - Clear(dlc); + Clear(dlm); data_array[0].Free(); data_array[1].Free(); } - };//template class Queue + };//template class QueueTemplate - template class ObjectQueue:public Queue ///对象队列 + template class Queue:public QueueTemplate + { + protected: + + DataLifecycleManager DefaultDLM; + + public: + + Queue():QueueTemplate(&DefaultDLM){}; + virtual ~Queue()=default; + };//template class Queue:public QueueTemplate + + template class ObjectQueue:public QueueTemplate ///对象队列 { DefaultObjectLifetimeCallback default_olc; public: - using Queue::Queue; + ObjectQueue():QueueTemplate(&default_olc){} virtual ~ObjectQueue() override { Free(); } virtual bool Push(T *obj) @@ -186,7 +202,7 @@ namespace hgl // return obj; //} - void Clear(DataLifetimeCallback *olc=nullptr) + void Clear(DataLifecycleManager *olc=nullptr) { if(!olc) olc=&default_olc; @@ -194,7 +210,7 @@ namespace hgl Queue::Clear(olc); } - void Free(DataLifetimeCallback *olc=nullptr) + void Free(DataLifecycleManager *olc=nullptr) { ObjectQueue::Clear(olc); diff --git a/inc/hgl/type/Stack.h b/inc/hgl/type/Stack.h index 3c2b491..c660cfb 100644 --- a/inc/hgl/type/Stack.h +++ b/inc/hgl/type/Stack.h @@ -87,17 +87,17 @@ namespace hgl public: - virtual void Clear (DataLifetimeCallback *dlc=nullptr) ///<清除所有数据 + virtual void Clear (DataLifecycleManager *dlm=nullptr) ///<清除所有数据 { - if(dlc) - dlc->Clear(data_array.GetData(),data_array.GetCount()); + if(dlm) + dlm->Clear(data_array.GetData(),data_array.GetCount()); data_array.Clear(); } - virtual void Free (DataLifetimeCallback *dlc=nullptr) ///<清除所有数据并释放内存 + virtual void Free (DataLifecycleManager *dlm=nullptr) ///<清除所有数据并释放内存 { - Clear(dlc); + Clear(dlm); data_array.Free(); } @@ -137,7 +137,7 @@ namespace hgl return obj; } - void Clear(ObjectLifetimeCallback *olc=nullptr) + void Clear(ObjectLifecycleManager *olc=nullptr) { if(!olc) olc=&default_olc; @@ -145,7 +145,7 @@ namespace hgl Stack::Clear(olc); } - void Free(ObjectLifetimeCallback *olc=nullptr) + void Free(ObjectLifecycleManager *olc=nullptr) { ObjectStack::Clear(olc);