From 9504560e44a932beee3deaf2e695d50d912de8fd Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Tue, 18 Jul 2023 20:54:21 +0800 Subject: [PATCH] clean up codes. --- inc/hgl/Macro.h | 65 +++--------------- inc/hgl/type/Collection.h | 6 +- inc/hgl/type/DataArray.h | 139 +++++++++++++++----------------------- 3 files changed, 65 insertions(+), 145 deletions(-) diff --git a/inc/hgl/Macro.h b/inc/hgl/Macro.h index ce84daa..9a337bf 100644 --- a/inc/hgl/Macro.h +++ b/inc/hgl/Macro.h @@ -1,5 +1,4 @@ -#ifndef HGL_MACRO_INCLUDE -#define HGL_MACRO_INCLUDE +#pragma once namespace hgl { @@ -57,64 +56,18 @@ namespace hgl #define SAFE_FREE_OBJECT_ARRAY(name,num) { \ if(name) \ + { \ FREE_OBJECT_ARRAY(name,num); \ + \ + name=null; \ + }\ } #define SAFE_FREE(name) { \ if(name) \ + { \ hgl_free(name); \ + name=nullptr; \ + } \ } - - #define SAFE_RECREATE(name,code) { \ - if(name) \ - delete name; \ - \ - name=code; \ - } - - #define ARRAY_CALL(name,num,code) { \ - int array_call_number=num; \ - \ - while(array_call_number--) \ - name[array_call_number]->code; \ - } - - #define LOAD_FUNC(type,func) type func(void *buf,int buf_size) \ - { \ - if(!buf||buf_size<=0)return 0; \ - \ - MemoryInputStream ms(buf,buf_size); \ - \ - return(func(&ms)); \ - } \ - \ - type func(const UTF16String &filename) \ - { \ - FileInputStream fs; \ - \ - if(fs.Open(filename)) \ - return(func(&fs)); \ - else \ - return 0; \ - } - - #define SAVE_FUNC(type,func) bool func(type data,void *buf,int buf_size) \ - { \ - if(!buf||buf_size<=0)return(false); \ - \ - MemoryOutputStream ms(buf,buf_size); \ - \ - return(func(data,&ms)); \ - } \ - \ - bool func(type data,const UTF16String &filename) \ - { \ - FileOutputStream fs; \ - \ - if(fs.CreateTrunc(filename)) \ - return(func(data,&fs)); \ - else \ - return(false); \ - } -}//namespace hgl -#endif//HGL_MACRO_INCLUDE +}//namespace hgl \ No newline at end of file diff --git a/inc/hgl/type/Collection.h b/inc/hgl/type/Collection.h index 6686ed3..4b3fd42 100644 --- a/inc/hgl/type/Collection.h +++ b/inc/hgl/type/Collection.h @@ -138,7 +138,7 @@ namespace hgl } template - int64 indexOfValue(const T &value) ///<获取数据在合集中的索引 + int64 indexOfValue(const T &value) ///<获取数据在合集中的索引 { CheckElementEqual cee(value); @@ -148,7 +148,7 @@ namespace hgl virtual bool isMember(const void *value) const{return indexOf(value)!=-1;} ///<判断当前数据是否是其成员 template - bool isMemberValue(const T &value)const{return indexOf(value)!=-1;} ///<判断当前数据是否是其成员 + bool isMemberValue(const T &value)const{return indexOf(value)!=-1;} ///<判断当前数据是否是其成员 virtual int64 RemoveCondition(CheckElement *condition,int max_count=1); ///<按条件移除 @@ -183,7 +183,7 @@ namespace hgl };//class Collection /** - * 合集数据枚举器. + * 合集数据枚举器 */ template class ElementEnumerator { diff --git a/inc/hgl/type/DataArray.h b/inc/hgl/type/DataArray.h index 4e1b05c..e700a7c 100644 --- a/inc/hgl/type/DataArray.h +++ b/inc/hgl/type/DataArray.h @@ -11,15 +11,17 @@ namespace hgl protected: T *items; - size_t count; - size_t alloc_count; + size_t count; ///<当前数据数量 + size_t alloc_count; ///<已分配的数据数量 public: - size_t GetCount ()const{return count;} ///<取得数据数量(注:非字节数) - const size_t GetAllocCount()const{return alloc_count;} ///<取得已分配的阵列大小(注:非字节数) - const size_t GetBytes ()const{return count*sizeof(T);} ///<取得阵列已使用的字节数 - const size_t GetAllocBytes()const{return alloc_count*sizeof(T);} ///<取得阵列已分配空间字节数 + size_t GetCount ()const{return count;} ///<取得数据数量(注:非字节数) + const size_t GetAllocCount ()const{return alloc_count;} ///<取得已分配的阵列大小(注:非字节数) + const size_t GetBytes ()const{return count*sizeof(T);} ///<取得阵列已使用的字节数 + const size_t GetAllocBytes ()const{return alloc_count*sizeof(T);} ///<取得阵列已分配空间字节数 + + const bool IsEmpty ()const{count==0;} ///<是否为空 const T * GetData()const{return items;} T * data() {return items;} @@ -72,7 +74,7 @@ namespace hgl DataArray() { - items=0; + items=nullptr; count=0; alloc_count=0; } @@ -80,7 +82,7 @@ namespace hgl DataArray(size_t size) { if(size<=0) - items=0; + items=nullptr; else items=(T *)hgl_malloc(size*sizeof(T)); @@ -103,8 +105,7 @@ namespace hgl void Clear() { - if(items) - hgl_free(items); + SAFE_FREE(items); count=0; alloc_count=0; @@ -121,18 +122,6 @@ namespace hgl memset(items,0,alloc_count); } - /** - * 设置数据,请保证数据使用hgl_malloc分配,否则会因为释放函数不配对出现错误 - */ - void SetData(T *d,int s) - { - Clear(); - - items=d; - alloc_count=s; - count=s; - } - /** * 解除数据关联 */ @@ -143,64 +132,33 @@ namespace hgl alloc_count=0; } - /** - * 复制内存块中的数据 - * @param d 复制出来的数据指针 - * @param s 要复制出来的数据个数 - */ - void CopyData(const T *d,int s) + operator T *()const { - SetCount(s); - memcpy(items,d,s*sizeof(T)); + return items; + } + + T *operator ->()const + { + return items; + } + + T &operator[](int n) + { + return items[n]; + } + + const T &operator[](int n)const + { + return items[n]; } /** - * 删除列表中的指定项,删除后将最后一个数据移到被删除的位置 - * @param index 要删除的数据项的索引值 - * @return 是否成功 - */ - bool Delete(int index) - { - if(count>0&&index>=0&&index0&&index>=0&&index=count)return(false); @@ -223,24 +181,33 @@ namespace hgl return(true); } - operator T *()const + /** + * 删除列表中的指定项(关心顺序,删除中间数据后,会将后面的数据整体前移) + * @param start 要删除的数据项的索引值 + * @param number 要删除的数据项数量 + * @return 是否成功 + */ + bool DeleteMove(int start,int number=1) { - return items; - } + if(start>=count)return(false); - T *operator ->()const - { - return items; - } + if(start<0) + { + number+=start; + start=0; + } - T &operator[](int n) - { - return items[n]; - } + if(start+number>count) + number=count-start; - const T &operator[](int n)const - { - return items[n]; + if(number<=0)return(false); + + count-=number; + + if(start class DataArray }//namespace hgl