From 3b71e680cc71689bed3dbed0491324819f5e2edc Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Mon, 14 Aug 2023 21:02:24 +0800 Subject: [PATCH] removed DefaultObjectLifetimeCallback in Map/Pool --- inc/hgl/type/LifetimeCallback.h | 19 +++++++-------- inc/hgl/type/Map.h | 5 ++-- inc/hgl/type/Pool.h | 41 ++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/inc/hgl/type/LifetimeCallback.h b/inc/hgl/type/LifetimeCallback.h index 6f4df85..c4a162e 100644 --- a/inc/hgl/type/LifetimeCallback.h +++ b/inc/hgl/type/LifetimeCallback.h @@ -34,17 +34,6 @@ namespace hgl template struct ObjectLifetimeCallback:public DataLifetimeCallback { virtual bool Create(T **) override=0; - virtual void Clear(T **) override=0; - - virtual void Clear(T **obj,int count) override=0; - };//struct ObjectLifetimeCallback - - /** - * 缺省对像生命周期处理回调 - */ - template struct DefaultObjectLifetimeCallback:public ObjectLifetimeCallback - { - virtual bool Create(T **obj) override {*obj=new T;return(true);} virtual void Clear(T **obj) override {if(obj&&*obj)delete *obj;} virtual void Clear(T **obj,int count) override @@ -58,5 +47,13 @@ namespace hgl ++obj; } } + };//struct ObjectLifetimeCallback + + /** + * 缺省对像生命周期处理回调 + */ + template struct DefaultObjectLifetimeCallback:public ObjectLifetimeCallback + { + virtual bool Create(T **obj) override {*obj=new T;return(true);} };//struct DefaultObjectLifetimeCallback }//namespace hgl diff --git a/inc/hgl/type/Map.h b/inc/hgl/type/Map.h index 76fa9e0..6c0ef17 100644 --- a/inc/hgl/type/Map.h +++ b/inc/hgl/type/Map.h @@ -167,7 +167,6 @@ namespace hgl typedef _Map SuperClass; - DefaultObjectLifetimeCallback default_olc; DataLifetimeCallback *olc; void DeleteObject(KVData *ds) @@ -187,7 +186,7 @@ namespace hgl _ObjectMap() { - olc=&default_olc; + olc=nullptr; } virtual ~_ObjectMap() @@ -197,7 +196,7 @@ namespace hgl virtual void SetDataLifetimeCallback(DataLifetimeCallback *cb) ///<设定数据生命周期回调函数 { - olc=cb?cb:&default_olc; + olc=cb; } /** diff --git a/inc/hgl/type/Pool.h b/inc/hgl/type/Pool.h index 14b8b7e..27292e4 100644 --- a/inc/hgl/type/Pool.h +++ b/inc/hgl/type/Pool.h @@ -10,12 +10,10 @@ namespace hgl * 默认情部下空闲队列使用Queue模板管理(先入先出,总是使用最早扔进去的数据。可手动换成Stack运行性能更好,但逻辑性能更差。), * 活动队列使用List模板管理(无序)。 */ - template class _Pool ///数据池 + template class _Pool ///数据池 { protected: - DEFAULT_DLC default_dlc; - AT Active; IT Idle; @@ -57,7 +55,7 @@ namespace hgl { max_active_count=0; history_max=0; - dlc=&default_dlc; + dlc=nullptr; } virtual ~_Pool() @@ -67,7 +65,7 @@ namespace hgl virtual void SetDataLifetimeCallback(DataLifetimeCallback *cb) ///<设定数据生命周期回调函数 { - dlc=cb?cb:&default_dlc; + dlc=cb; } virtual void PreAlloc(int count,bool set_to_max=false) ///<预分配空间 @@ -83,6 +81,8 @@ namespace hgl virtual bool Create(T &value) ///<创建一个数据,并放置在活跃队列中 { + if(!dlc)return(false); + if(IsFull()) return(false); @@ -101,10 +101,12 @@ namespace hgl if(IsFull()) return(false); + if(!dlc)return(false); + if(!dlc->Create(&value)) return(false); } - else + else if(dlc) { dlc->OnActive(&value); } @@ -119,13 +121,14 @@ namespace hgl if(!Idle.Pop(value)) return(false); - dlc->OnActive(&value); + if(dlc) + dlc->OnActive(&value); Active.Add(value); return(true); } - virtual bool Append(T value) ///<添加一个外部创建的数据入活跃队列 + virtual bool AppendToActive(T value) ///<添加一个外部创建的数据入活跃队列 { if(IsFull()) return(false); @@ -136,6 +139,14 @@ namespace hgl return(true); } + virtual bool AppendToIdle(T value) ///<添加一个外部创建的数据入非活跃队列 + { + if(!Idle.Push(value)) + return(false); + + return(true); + } + virtual bool Release(T value) ///<释放一个数据 { int index=Active.Find(value); @@ -147,7 +158,9 @@ namespace hgl if(!Idle.Push(value)) return(false); - dlc->OnIdle(&value); + if(dlc) + dlc->OnIdle(&value); + return(true); } @@ -172,7 +185,8 @@ namespace hgl virtual void ReleaseActive() ///<释放所有活跃数据 { - dlc->OnIdle(Active.GetData(),Active.GetCount()); + if(dlc) + dlc->OnIdle(Active.GetData(),Active.GetCount()); Idle.Push(Active.GetData(),Active.GetCount()); Active.Clear(); @@ -180,7 +194,8 @@ namespace hgl virtual void ClearActive() { - dlc->Clear(Active.GetData(),Active.GetCount()); + if(dlc) + dlc->Clear(Active.GetData(),Active.GetCount()); Active.Clear(); } @@ -197,6 +212,6 @@ namespace hgl } };//template class _Pool - template using Pool =_Pool, Queue, DataLifetimeCallback>; ///<数据池模板 - template using ObjectPool =_Pool, ObjectQueue, DefaultObjectLifetimeCallback>; ///<对象池 + template using Pool =_Pool, Queue>; ///<数据池模板 + template using ObjectPool =_Pool, ObjectQueue>; ///<对象池 }//namespace hgl