From 05ac58b0c8fe5b1112f634b55ff0b4caab3ac603 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Tue, 1 Aug 2023 19:37:24 +0800 Subject: [PATCH] added LifetimeCallback.h --- inc/hgl/type/LifetimeCallback.h | 37 +++++ inc/hgl/type/Pool.cpp | 9 +- inc/hgl/type/Queue.cpp | 265 -------------------------------- inc/hgl/type/Queue.h | 188 ++++++++++++++++------ inc/hgl/type/Stack.h | 8 +- 5 files changed, 190 insertions(+), 317 deletions(-) create mode 100644 inc/hgl/type/LifetimeCallback.h delete mode 100644 inc/hgl/type/Queue.cpp diff --git a/inc/hgl/type/LifetimeCallback.h b/inc/hgl/type/LifetimeCallback.h new file mode 100644 index 0000000..ddbb2b0 --- /dev/null +++ b/inc/hgl/type/LifetimeCallback.h @@ -0,0 +1,37 @@ +#pragma once + +#include +namespace hgl +{ + /** + * 原生数据生命周期处理回调 + */ + template struct RawLifetimeCallback + { + virtual bool Create(T *){return true;} + virtual void Clear(T *){return;} + + virtual void Copy(T *dst,T *src,int count) + { + hgl_cpy(dst,src,count); + } + }; + + /** + * 对像生命周期处理回调 + */ + template struct ObjectLifetimeCallback:public RawLifetimeCallback + { + virtual bool Create(T **) override=0; + virtual void Clear(T **) 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;} + };//struct DefaultObjectLifetimeCallback +}//namespace hgl diff --git a/inc/hgl/type/Pool.cpp b/inc/hgl/type/Pool.cpp index 2837b2a..7818cc3 100644 --- a/inc/hgl/type/Pool.cpp +++ b/inc/hgl/type/Pool.cpp @@ -11,7 +11,10 @@ namespace hgl if(num<=0)return; for(int i=0;ihistory_max) @@ -160,14 +163,14 @@ namespace hgl Clear(p,ic); - Inactive.ClearData(); + Inactive.Clear(); } template void Pool::ClearAll() { Clear(Inactive.GetData(),Inactive.GetCount()); - Inactive.ClearData(); + Inactive.Clear(); Clear(Active.GetData(),Active.GetCount()); Active.ClearData(); diff --git a/inc/hgl/type/Queue.cpp b/inc/hgl/type/Queue.cpp deleted file mode 100644 index 754980d..0000000 --- a/inc/hgl/type/Queue.cpp +++ /dev/null @@ -1,265 +0,0 @@ -锘#ifndef HGL_QUEUE_CPP -#define HGL_QUEUE_CPP - -namespace hgl -{ - /** - * 鏈被鏋勯犲嚱鏁 - * @param m 濡傛灉m鐨勫间笉涓0锛屽垯琛ㄧず浣跨敤鍥哄畾鐨勯槦鍒楀ぇ灏忋傚浐瀹氬ぇ灏忕殑闃熷垪浼氬湪涓寮濮嬪嵆鍒嗛厤濂藉唴瀛樸 - */ - template - Queue::Queue(int m) - { - cur_count=0; - - if(m) - { - max_count=m; - - items=hgl_align_malloc(max_count); - } - else max_count=0; - - alloc_count=max_count; - } - - template - Queue::~Queue() - { - if(cur_count||max_count)hgl_free(items); - } - - template - bool Queue::AllocMemory(int count) - { - if(max_count) - if(count>max_count)return(false); - - if(cur_count) - { - if(count>alloc_count) - { - alloc_count=power_to_2(count); - - items=(T *)hgl_align_realloc(items,alloc_count); - } - } - else - { - alloc_count=power_to_2(count); - - items=hgl_align_malloc(alloc_count); - } - - return(true); - } - - /** - * 淇敼闃熷垪鐨勬渶澶у - */ - template - bool Queue::SetMaxCount(int m) - { - if(max_count) - { - if(max_count==m)return(true); - } - - if(cur_count) - { - if(cur_count>m) - return(false); - } - - max_count=m; - return(true); - } - - /** - * 娓呴櫎闃熷垪涓殑鎵鏈夋暟鎹 - */ - template - void Queue::Free() - { - if(max_count==0) - if(cur_count) - { - hgl_free(items); - alloc_count=0; - } - - cur_count=0; - } - - /** - * 娓呴櫎闃熷垪涓殑鎵鏈夋暟鎹紝浣嗕笉閲婃斁鍐呭瓨 - */ - template - void Queue::ClearData() - { - cur_count=0; - } - - /** - * 璁块棶闃熷垪涓殑涓涓暟鎹紝浣嗕笉娓呴櫎瀹 - * @param t 鍙栧嚭鐨勬暟鎹繚瀛樺湴 - * @return 鏄惁鎴愬姛鍙栧嚭鏁版嵁 - */ - template - bool Queue::Peek(T &t) - { - if(cur_count) - { -// t=items[0]; - memcpy(&t,items,sizeof(T)); - return(true); - } - else - return(false); - } - - /** - * 鍒犻櫎闃熷垪涓殑鎸囧畾鏁版嵁 - * @param index 绱㈠紩 - */ - template - bool Queue::Delete(int index) - { - if(index<0||index>=cur_count) - return(false); - - cur_count--; - - if(cur_count) - { - if(index - bool Queue::Pop(T &t) - { - if(cur_count) - { -// t=items[0]; - memcpy(&t,items,sizeof(T)); - - cur_count--; - - if(max_count==0) - { - if(cur_count) - { - //memcpy(items,items+1,count*sizeof(T)); - memmove(items,items+1,cur_count*sizeof(T)); -// items=(T *)hgl_realloc(items,count*sizeof(T)); - } - } - else - { - memcpy(items,items+1,cur_count*sizeof(T)); - } - - return(true); - } - else - return(false); - } - - /** - * 鍚戦槦鍒椾腑鏀惧叆涓涓暟鎹 - * @param data 瑕佹斁鍏ョ殑鏁版嵁鎸囬拡 - * @return true 鏀惧叆鏁版嵁鎴愬姛 - * @return false 鏀惧叆鏁版嵁澶辫触 - */ - template - bool Queue::Push(const T &data) - { - if(!AllocMemory(cur_count+1)) - return(false); - -// items[count++]=data; - memcpy(items+cur_count,&data,sizeof(T)); - cur_count++; - - return(true); - } - - /** - * 鍚戦槦鍒椾腑鍘嬪叆涓鍫嗘暟鎹 - * @param dp 瑕佹斁鍏ョ殑鏁版嵁鎸囬拡 - * @param dc 瑕佹斁鍏ョ殑鏁版嵁鏁伴噺 - */ - template - bool Queue::Push(T *dp,const int dc) - { - if(!AllocMemory(cur_count+dc)) - return(false); - - memcpy(items+cur_count,dp,sizeof(T)*dc); - cur_count+=dc; - - return(true); - } - - template - int Queue::Find(const T &data) - { - if(cur_count<=0) - return(-1); - - T *p=items; - for(int i=0;i - void Queue::operator =(const Queue &ori) - { - if(ori.cur_count==0)return; - - Free(); - - max_count=ori.cur_count; - cur_count=ori.cur_count; - - if(max_count==0) - alloc_count=cur_count; - else - alloc_count=max_count; - - items=hgl_align_malloc(alloc_count); - - memcpy(items,ori.items,alloc_count*sizeof(T)); - } -} - -namespace hgl -{ - template - void QueueObject::Clear() - { - int n=Queue::cur_count; - - while(n--) - delete Queue::items[n]; - - Queue::Free(); - } -} -#endif//HGL_QUEUE_CPP diff --git a/inc/hgl/type/Queue.h b/inc/hgl/type/Queue.h index 31a130e..1cb7f61 100644 --- a/inc/hgl/type/Queue.h +++ b/inc/hgl/type/Queue.h @@ -1,82 +1,180 @@ -锘#ifndef HGL_QUEUE_INCLUDE -#define HGL_QUEUE_INCLUDE +锘#pragma once -#include +#include namespace hgl { /** * Queue妯℃澘绫荤敤浜庝繚瀛樹竴涓厛杩涘厛鍑恒佸悗杩涘悗鍑虹殑鏁版嵁闃熷垪 - * - * 娉細杩欎釜绫昏繕鍦ㄦ祴璇曚腑锛岃涓嶈淇敼杩欎釜绫荤殑婧愪唬鐮侊紝濡傛湁淇敼鎰忚锛岃鑷寸數浣滆呫 */ template class Queue ///闃熷垪椤哄簭鏁版嵁璁块棶绫 { protected: - int max_count; ///<鏈澶у彲鍒嗛厤绌洪棿鏁伴噺 - int alloc_count; ///<宸插垎閰嶇殑绌洪棿鏁伴噺 - int cur_count; - T *items; + DataArray data_array[2]; + + int read_index; + int write_index; + + int read_offset; protected: - bool AllocMemory(int); + void SwapIndex() + { + if(read_index==0) + { + read_index=1; + write_index=0; + } + else + { + read_index=0; + write_index=1; + } + + read_offset=0; + } public: //灞炴 - T * GetData ()const{return items;} ///<鍙栧緱鍘熷鏁版嵁 - int GetCount ()const{return cur_count;} ///<鍙栧緱鏁版嵁鏁伴噺 + const int GetAllocCount ()const{return data_array[0].GetAllocCount()+ + data_array[1].GetAllocCount();} ///<鍙栧緱宸插垎閰嶇殑鏁版嵁鏁伴噺 - int GetMaxCount ()const{return max_count;} ///<鍙栧緱鏈澶ф暟閲 - bool SetMaxCount (int); ///<璁剧疆鏈澶ф暟閲 + const int GetCount ()const{return data_array[0].GetCount()+ + data_array[1].GetCount();} ///<鍙栧緱鍒楄〃鍐呮暟鎹暟閲 - const T *begin()const{return items;} - const T *end()const{return items+cur_count;} + virtual bool PreAlloc (int count) ///<棰勫垎閰嶆暟鎹┖闂 + { + if(!data_array[0].Alloc(count))return(false); + if(!data_array[1].Alloc(count))return(false); + + return(true); + } + + const bool IsEmpty ()const{return GetCount()==0;} ///<纭鍒楄〃鏄惁涓虹┖ public: //鏂规硶 - Queue(int=0); - virtual ~Queue(); - - bool Peek (T &); ///<灏濊瘯璁块棶涓涓暟鎹 - bool Pop (T &); ///<寮瑰嚭涓涓暟鎹 - bool Push (const T &); ///<鍘嬪叆涓涓暟鎹 - bool Push (T *,int); ///<鍘嬪叆涓鎵规暟鎹 - - int Find (const T &); ///<鏌ユ壘闃熷垪涓繖涓暟鎹殑缂栧彿 - bool IsExist (const T &data)const{return this->Find(data)!=-1;} ///<纭鏄惁鏈夎繖涓暟鎹 - bool Delete (int); ///<鍒犻櫎闃熷垪涓寚瀹氱紪鍙风殑鏁版嵁 - - void Free (); ///<娓呴櫎鎵鏈夋暟鎹 - void ClearData (); ///<娓呴櫎鎵鏈夋暟鎹紝浣嗕笉閲婃斁鍐呭瓨 - - bool GetItem (int n,T &ti) ///<鍙栧緱鎸囧畾椤规暟鎹 + Queue() { - if(n<0||n>=cur_count)return(false); + write_index=0; + read_index=1; + read_offset=0; + } + + virtual ~Queue()=default; + + virtual bool Push (T *data,int count) ///<鍘嬪叆涓鎵规暟鎹 + { + if(!data||count<=0)return(false); + + int offset=data_array[write_index].GetCount(); + + if(!data_array[write_index].AddCount(count)) + return(false); + + data_array[write_index].WriteAt(data,offset,count); - ti=items[n]; return(true); } - virtual void operator =(const Queue &); + virtual bool Push (T &data){return Push(&data,1);} ///<鍘嬪叆涓涓暟鎹 + + virtual bool Peek (T &data) ///<灏濊瘯璁块棶涓涓暟鎹 + { + if(data_array[read_index].GetCount()>=read_offset) + { + if(data_array[write_index].GetCount()<=0) + return(false); + + data_array[write_index].ReadAt(data,0); + + } + else + { + data_array[read_index].ReadAt(data,read_offset); + } + + return(true); + } + + virtual bool Pop (T &data) ///<寮瑰嚭涓涓暟鎹 + { + if(data_array[read_index].GetCount()>=read_offset) + { + if(data_array[write_index].GetCount()<=0) + return(false); + + data_array[read_index].Clear(); + + SwapIndex(); + + data_array[read_index].ReadAt(data,0); + + ++read_offset; + } + else + { + data_array[read_index].ReadAt(data,read_offset); + + ++read_offset; + } + + return(true); + } + + public: + + virtual void Clear (){data_array[0].Clear();data_array[1].Clear();} ///<娓呴櫎鎵鏈夋暟鎹 + virtual void Free (){data_array[0].Free();data_array[1].Free();} ///<娓呴櫎鎵鏈夋暟鎹苟閲婃斁鍐呭瓨 };//template class Queue - template class QueueObject:public Queue ///鍫嗘爤瀵硅薄 + template class ObjectQueue:public Queue ///瀵硅薄闃熷垪 { + protected: + + virtual void DeleteObject(T *obj){if(obj)delete obj;} + public: using Queue::Queue; - virtual ~QueueObject(){Clear();} + virtual ~ObjectQueue(){Free();} - void Clear(); - - T *operator[](int n)const + virtual bool Push(T *obj) { - if(n<0||n>=Queue::cur_count)return(nullptr); + if(!obj)return(false); - return Queue::items[n]; + return Queue::Push(obj); } - };//template class QueueObject + + virtual T *Pop() + { + T *obj; + + if(!Queue::Pop(obj)) + return(nullptr); + + return obj; + } + + void Clear() + { + for(T *obj:data_array[0]) + DeleteObject(obj); + + for(T *obj:data_array[1]) + DeleteObject(obj); + + data_array[0].Clear(); + data_array[1].Clear(); + } + + void Free() + { + ObjectQueue::Clear(); + + data_array[0].Free(); + data_array[1].Free(); + } + };//template class ObjectQueue }//namespace hgl -#include -#endif//HGL_QUEUE_INCLUDE diff --git a/inc/hgl/type/Stack.h b/inc/hgl/type/Stack.h index 5c7c7f0..a304bb4 100644 --- a/inc/hgl/type/Stack.h +++ b/inc/hgl/type/Stack.h @@ -5,8 +5,6 @@ namespace hgl { /** * Stack妯℃澘绫荤敤浜庝繚瀛樹竴涓厛杩涘悗鍑恒佸悗杩涘厛鍑虹殑鏁版嵁鍫嗘爤 - * - * 娉細杩欎釜绫昏繕鍦ㄦ祴璇曚腑锛岃涓嶈淇敼杩欎釜绫荤殑婧愪唬鐮侊紝濡傛湁淇敼鎰忚锛岃鑷寸數浣滆呫 */ template class Stack ///鍫嗘爤椤哄簭鏁版嵁璁块棶绫 { @@ -75,8 +73,10 @@ namespace hgl return(true); } - virtual void Clear(){data_array.Clear();} ///<娓呴櫎鎵鏈夋暟鎹 - virtual void Free(){data_array.Free();} ///<娓呴櫎鎵鏈夋暟鎹苟閲婃斁鍐呭瓨 + public: + + virtual void Clear (){data_array.Clear();} ///<娓呴櫎鎵鏈夋暟鎹 + virtual void Free (){data_array.Free();} ///<娓呴櫎鎵鏈夋暟鎹苟閲婃斁鍐呭瓨 virtual void operator =(const DataArray &da) ///<澶嶅埗涓涓爢鏍 {