From bf8be47d3ae3e92b5e3d979e97a492865e51bda2 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 31 Oct 2024 01:47:36 +0800 Subject: [PATCH] newly DataLifecycleManager in Map/ObjectManage/Queue/Stack --- inc/hgl/type/Map.cpp | 58 ++++++++++++++++++------------------- inc/hgl/type/Map.h | 54 +++++++++++++++++----------------- inc/hgl/type/ObjectManage.h | 2 +- inc/hgl/type/Queue.h | 36 +++++++++-------------- inc/hgl/type/Stack.h | 51 +++++++++++++++++--------------- 5 files changed, 100 insertions(+), 101 deletions(-) diff --git a/inc/hgl/type/Map.cpp b/inc/hgl/type/Map.cpp index 358a3e5..16c3beb 100644 --- a/inc/hgl/type/Map.cpp +++ b/inc/hgl/type/Map.cpp @@ -9,7 +9,7 @@ namespace hgl * @return 数据所在索引,-1表示不存在 */ template - int _Map::Find(const K &flag)const + int MapTemplate::Find(const K &flag)const { int left=0,right=data_list.GetCount()-1; //使用left,right而不使用min,max是为了让代码能够更好的阅读。 int mid; @@ -42,7 +42,7 @@ namespace hgl } template - bool _Map::FindPos(const K &flag,int &pos)const + bool MapTemplate::FindPos(const K &flag,int &pos)const { int left=0,right=data_list.GetCount()-1; int mid; @@ -125,7 +125,7 @@ namespace hgl } template - int _Map::FindByValue(const V &data)const + int MapTemplate::FindByValue(const V &data)const { const int count=data_list.GetCount(); @@ -150,7 +150,7 @@ namespace hgl * @return 新创建好的数据结构 */ template - KVData *_Map::Add(const K &flag,const V &data) + KVData *MapTemplate::Add(const K &flag,const V &data) { KVData *dp=new KVData; @@ -175,7 +175,7 @@ namespace hgl * @param obj 数据 */ template - void _Map::Add(KVData *obj) + void MapTemplate::Add(KVData *obj) { data_list.Insert(FindPos(obj->key),obj); } @@ -185,7 +185,7 @@ namespace hgl * 比如定义了Map,直接返回DATA需要复制会消耗一些时间,直接返回DATA *会更好一些 */ template - V *_Map::GetValuePointer(const K &key)const + V *MapTemplate::GetValuePointer(const K &key)const { int index=Find(key); @@ -205,7 +205,7 @@ namespace hgl * @return 数据序号,<0表示失败 */ template - int _Map::GetValueAndSerial(const K &flag,V &data) const + int MapTemplate::GetValueAndSerial(const K &flag,V &data) const { int index=Find(flag); @@ -220,7 +220,7 @@ namespace hgl } template - bool _Map::Check(const K &key,const V &value) const + bool MapTemplate::Check(const K &key,const V &value) const { int index=Find(key); @@ -240,7 +240,7 @@ namespace hgl * @return 是否取得成功 */ template - bool _Map::GetBySerial(int index,K &f,V &t) const + bool MapTemplate::GetBySerial(int index,K &f,V &t) const { if(index<0||index>=data_list.GetCount())return(false); @@ -262,7 +262,7 @@ namespace hgl * @return 是否取得成功 */ template - bool _Map::GetKey(int index,K &f) + bool MapTemplate::GetKey(int index,K &f) { if(index<0||index>=data_list.GetCount())return(false); @@ -283,7 +283,7 @@ namespace hgl * @return 是否取得成功 */ template - bool _Map::GetValue(int index,V &t) + bool MapTemplate::GetValue(int index,V &t) { if(index<0||index>=data_list.GetCount())return(false); @@ -303,7 +303,7 @@ namespace hgl * @param t 数据 */ template - bool _Map::SetValueBySerial(int index,V &t) + bool MapTemplate::SetValueBySerial(int index,V &t) { if(index<0||index>=data_list.GetCount())return(false); @@ -319,7 +319,7 @@ namespace hgl * @return 是否成功 */ template - bool _Map::Delete(const K &flag,V &data) + bool MapTemplate::Delete(const K &flag,V &data) { int index=Find(flag); @@ -342,7 +342,7 @@ namespace hgl * @return 是否成功 */ template - bool _Map::DeleteByKey(const K &flag) + bool MapTemplate::DeleteByKey(const K &flag) { return DeleteAt(Find(flag)); } @@ -354,7 +354,7 @@ namespace hgl * @return 是否成功 */ template - int _Map::DeleteByKey(const K *fp,const int count) + int MapTemplate::DeleteByKey(const K *fp,const int count) { if(!fp||count<=0)return(0); @@ -378,7 +378,7 @@ namespace hgl * @return 是否成功 */ template - bool _Map::DeleteByValue(const V &data) + bool MapTemplate::DeleteByValue(const V &data) { return DeleteAt(FindByValue(data)); } @@ -389,7 +389,7 @@ namespace hgl * @return 是否成功 */ template - bool _Map::DeleteAt(int index) + bool MapTemplate::DeleteAt(int index) { if(index<0 ||index>=data_list.GetCount())return(false); @@ -407,7 +407,7 @@ namespace hgl * @return 是否成功 */ template - bool _Map::DeleteAt(int start,int number) + bool MapTemplate::DeleteAt(int start,int number) { KVData **dp=data_list.GetData()+start; @@ -426,7 +426,7 @@ namespace hgl * @param data 新的数据内容 */ template - bool _Map::ChangeOrAdd(const K &flag,const V &data) + bool MapTemplate::ChangeOrAdd(const K &flag,const V &data) { int result; KVData *dp; @@ -467,7 +467,7 @@ namespace hgl * @param return 是否更改成功 */ template - bool _Map::Change(const K &flag,const V &data) + bool MapTemplate::Change(const K &flag,const V &data) { KVData *dp=GetObjectFromList(data_list,Find(flag)); @@ -482,7 +482,7 @@ namespace hgl * 清除所有数据 */ template - void _Map::Free() + void MapTemplate::Free() { data_pool.Clear(); data_list.Free(); @@ -492,14 +492,14 @@ namespace hgl * 清除所有数据,但不释放内存 */ template - void _Map::Clear() + void MapTemplate::Clear() { data_pool.ReleaseActive(); data_list.Clear(); } template - void _Map::operator=(const ThisClass &ftd) + void MapTemplate::operator=(const ThisClass &ftd) { Free(); @@ -529,7 +529,7 @@ namespace hgl } template - void _Map::Enum(void (*enum_func)(const K &,V &)) + void MapTemplate::Enum(void (*enum_func)(const K &,V &)) { const int count=data_list.GetCount(); @@ -547,7 +547,7 @@ namespace hgl } template - void _Map::EnumKey(void (*enum_func)(const K &)) + void MapTemplate::EnumKey(void (*enum_func)(const K &)) { const int count=data_list.GetCount(); @@ -565,7 +565,7 @@ namespace hgl } template - void _Map::EnumAllValue(void (*enum_func)(V &)) + void MapTemplate::EnumAllValue(void (*enum_func)(V &)) { const int count=data_list.GetCount(); @@ -583,7 +583,7 @@ namespace hgl } template - void _Map::EnumValue(bool (*enum_func)(V &)) + void MapTemplate::EnumValue(bool (*enum_func)(V &)) { const int count=data_list.GetCount(); @@ -605,7 +605,7 @@ namespace hgl * 统计出所有在in_list中出现的数据,产生的结果写入with_list */ template - void _Map::WithList(_Map::KVDataList &with_list,const List &in_list) + void MapTemplate::WithList(MapTemplate::KVDataList &with_list,const List &in_list) { with_list.Clear(); const int count=this->GetCount(); @@ -629,7 +629,7 @@ namespace hgl * 统计出所有没有出现在in_list中的数据,产生的结果写入without_list */ template - void _Map::WithoutList(_Map::KVDataList &without_list,const List &in_list) + void MapTemplate::WithoutList(MapTemplate::KVDataList &without_list,const List &in_list) { without_list.Clear(); const int count=this->GetCount(); diff --git a/inc/hgl/type/Map.h b/inc/hgl/type/Map.h index 63b2a08..ee8e3aa 100644 --- a/inc/hgl/type/Map.h +++ b/inc/hgl/type/Map.h @@ -1,5 +1,4 @@ -#ifndef HGL_MAP_INCLUDE -#define HGL_MAP_INCLUDE +#pragma once #include #include @@ -16,11 +15,11 @@ namespace hgl /** * 索引数据模板 */ - template class _Map + template class MapTemplate { protected: - using ThisClass=_Map; + using ThisClass=MapTemplate; using KVDataPool=ObjectPool; using KVDataList=List; @@ -35,8 +34,8 @@ namespace hgl public: //方法 - _Map()=default; - virtual ~_Map()=default; + MapTemplate()=default; + virtual ~MapTemplate()=default; const int GetCount()const{return data_list.GetCount();} ///<取得数据总量 const bool IsEmpty()const{return data_list.IsEmpty();} ///<是否为空 @@ -125,7 +124,7 @@ namespace hgl return count; } - KVData *GetItem(int n){return GetObjectFromList(data_list,n);} ///<取指定序号的数据 + KVData *GetItem(int n){return GetObjectFromList(data_list,n);} ///<取指定序号的数据 bool GetBySerial(int,K &,V &) const; ///<取指定序号的数据 bool GetKey(int,K &); ///<取指定序号的索引 bool GetValue(int,V &); ///<取指定序号的数据 @@ -141,9 +140,9 @@ namespace hgl void WithList(KVDataList &with_list,const List &in_list); ///<统计出所有在in_list中出现的数据,产生的结果写入with_list void WithoutList(KVDataList &without_list,const List &in_list); ///<统计出所有没有出现在in_list中的数据,产生的结果写入without_list - };//class _Map + };//class MapTemplate - template class Map:public _Map > + template class Map:public MapTemplate > { public: @@ -161,21 +160,19 @@ namespace hgl return result; } - template class _ObjectMap:public _Map + template class ObjectMapTemplate:public MapTemplate { protected: - typedef _Map SuperClass; + typedef MapTemplate SuperClass; - ObjectLifecycleManager *olc; - ObjectLifecycleManager default_olc; + ObjectLifecycleManager *olm; void DeleteObject(KVData *ds) { if(!ds)return; - if(olc) - olc->Clear(&(ds->value)); + olm->Clear(&(ds->value)); } void DeleteObject(int index) @@ -185,21 +182,16 @@ namespace hgl public: - _ObjectMap() + ObjectMapTemplate(ObjectLifecycleManager *_olm) { - olc=&default_olc; + olm=_olm; } - virtual ~_ObjectMap() + virtual ~ObjectMapTemplate() { Clear(); } - virtual void SetDataLifetimeCallback(ObjectLifecycleManager *cb) ///<设定数据生命周期回调函数 - { - olc=cb; - } - /** * 断开一个数据 * @param flag 要断开的数据标识 @@ -349,9 +341,19 @@ namespace hgl else return nullptr; }; - };//class _ObjectMap + };//class ObjectMapTemplate - template using ObjectMap=_ObjectMap >; + template class ObjectMap:public ObjectMapTemplate> + { + protected: + + ObjectLifecycleManager DefaultOLM; + + public: + + ObjectMap():ObjectMapTemplate(&DefaultOLM){}; + virtual ~ObjectMap()=default; + };//template class ObjectMap:public ObjectMapTemplate> }//namespace hgl + #include -#endif//HGL_MAP_INCLUDE diff --git a/inc/hgl/type/ObjectManage.h b/inc/hgl/type/ObjectManage.h index 234017c..c9a5877 100644 --- a/inc/hgl/type/ObjectManage.h +++ b/inc/hgl/type/ObjectManage.h @@ -26,7 +26,7 @@ namespace hgl protected: - _Map items; + MapTemplate items; ObjectLifecycleManager *olm; ///<数据生命周期回调函数 diff --git a/inc/hgl/type/Queue.h b/inc/hgl/type/Queue.h index ca9d030..e743715 100644 --- a/inc/hgl/type/Queue.h +++ b/inc/hgl/type/Queue.h @@ -132,7 +132,7 @@ namespace hgl public: - virtual void Clear () ///<清除所有数据 + virtual void Clear () ///<清除所有数据 { if(dlm) { @@ -148,7 +148,7 @@ namespace hgl data_array[1].Clear(); } - virtual void Free () ///<清除所有数据并释放内存 + virtual void Free () ///<清除所有数据并释放内存 { Clear(dlm); @@ -169,13 +169,13 @@ namespace hgl virtual ~Queue()=default; };//template class Queue:public QueueTemplate - template class ObjectQueue:public QueueTemplate ///对象队列 + template class ObjectQueue:public QueueTemplate ///对象队列 { - DefaultObjectLifetimeCallback default_olc; + ObjectLifecycleManager DefaultOLM; public: - ObjectQueue():QueueTemplate(&default_olc){} + ObjectQueue():QueueTemplate(&DefaultOLM){} virtual ~ObjectQueue() override { Free(); } virtual bool Push(T *obj) @@ -192,27 +192,19 @@ namespace hgl return Queue::Push(obj_list,count); } - //virtual T *Pop() - //{ - // T *obj; - - // if(!Queue::Pop(obj)) - // return(nullptr); - - // return obj; - //} - - void Clear(DataLifecycleManager *olc=nullptr) + T *Pop() { - if(!olc) - olc=&default_olc; - - Queue::Clear(olc); + T *obj; + + if(!Queue::Pop(obj)) + return(nullptr); + + return obj; } - void Free(DataLifecycleManager *olc=nullptr) + void Free() { - ObjectQueue::Clear(olc); + ObjectQueue::Clear(); this->data_array[0].Free(); this->data_array[1].Free(); diff --git a/inc/hgl/type/Stack.h b/inc/hgl/type/Stack.h index c660cfb..4618f89 100644 --- a/inc/hgl/type/Stack.h +++ b/inc/hgl/type/Stack.h @@ -1,15 +1,17 @@ #pragma once #include -#include +#include namespace hgl { /** * Stack模板类用于保存一个先进后出、后进先出的数据堆栈 */ - template class Stack ///堆栈顺序数据访问类 + template class StackTemplate ///堆栈顺序数据访问类 { protected: + + DataLifecycleManager *dlm; DataArray data_array; @@ -38,8 +40,8 @@ namespace hgl public: //方法 - Stack()=default; - virtual ~Stack()=default; + StackTemplate(DataLifecycleManager *_dlm){dlm=_dlm;} + virtual ~StackTemplate()=default; virtual bool Push(T *data,int count) ///<压入多个数据 { @@ -87,15 +89,14 @@ namespace hgl public: - virtual void Clear (DataLifecycleManager *dlm=nullptr) ///<清除所有数据 + virtual void Clear () ///<清除所有数据 { - if(dlm) - dlm->Clear(data_array.GetData(),data_array.GetCount()); + dlm->Clear(data_array.GetData(),data_array.GetCount()); data_array.Clear(); } - virtual void Free (DataLifecycleManager *dlm=nullptr) ///<清除所有数据并释放内存 + virtual void Free () ///<清除所有数据并释放内存 { Clear(dlm); data_array.Free(); @@ -109,15 +110,27 @@ namespace hgl } virtual void operator =(const Stack &s){this->operator=(s.data_array);} - };//template class Stack + };//template class StackTemplate - template class ObjectStack:public Stack ///堆栈对象 + template class Stack:public StackTemplate { - DefaultObjectLifetimeCallback default_olc; + protected: + + DataLifecycleManager DefaultDLM; public: - using Stack::Stack; + Stack():StackTemplate(&DefaultDLM){}; + virtual ~Stack()=default; + };//template class Stack:public StackTemplate + + template class ObjectStack:public StackTemplate ///堆栈对象 + { + ObjectLifecycleManager DefaultOLM; + + public: + + ObjectStack():StackTemplate(&DefaultOLM){} virtual ~ObjectStack() override {Free();} virtual bool Push(T *obj) @@ -127,7 +140,7 @@ namespace hgl return Stack::Push(obj); } - virtual T *Pop() + T *Pop() { T *obj; @@ -137,17 +150,9 @@ namespace hgl return obj; } - void Clear(ObjectLifecycleManager *olc=nullptr) + void Free() { - if(!olc) - olc=&default_olc; - - Stack::Clear(olc); - } - - void Free(ObjectLifecycleManager *olc=nullptr) - { - ObjectStack::Clear(olc); + ObjectStack::Clear(); this->data_array.Free(); }