clean up codes.
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
#ifndef HGL_MACRO_INCLUDE
|
#pragma once
|
||||||
#define HGL_MACRO_INCLUDE
|
|
||||||
|
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
@@ -57,64 +56,18 @@ namespace hgl
|
|||||||
|
|
||||||
#define SAFE_FREE_OBJECT_ARRAY(name,num) { \
|
#define SAFE_FREE_OBJECT_ARRAY(name,num) { \
|
||||||
if(name) \
|
if(name) \
|
||||||
|
{ \
|
||||||
FREE_OBJECT_ARRAY(name,num); \
|
FREE_OBJECT_ARRAY(name,num); \
|
||||||
|
\
|
||||||
|
name=null; \
|
||||||
|
}\
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SAFE_FREE(name) { \
|
#define SAFE_FREE(name) { \
|
||||||
if(name) \
|
if(name) \
|
||||||
|
{ \
|
||||||
hgl_free(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
|
}//namespace hgl
|
||||||
#endif//HGL_MACRO_INCLUDE
|
|
||||||
|
@@ -183,7 +183,7 @@ namespace hgl
|
|||||||
};//class Collection
|
};//class Collection
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 合集数据枚举器.
|
* 合集数据枚举器
|
||||||
*/
|
*/
|
||||||
template<typename T> class ElementEnumerator
|
template<typename T> class ElementEnumerator
|
||||||
{
|
{
|
||||||
|
@@ -11,15 +11,17 @@ namespace hgl
|
|||||||
protected:
|
protected:
|
||||||
|
|
||||||
T *items;
|
T *items;
|
||||||
size_t count;
|
size_t count; ///<当前数据数量
|
||||||
size_t alloc_count;
|
size_t alloc_count; ///<已分配的数据数量
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
size_t GetCount ()const{return count;} ///<取得数据数量(注:非字节数)
|
size_t GetCount ()const{return count;} ///<取得数据数量(注:非字节数)
|
||||||
const size_t GetAllocCount()const{return alloc_count;} ///<取得已分配的阵列大小(注:非字节数)
|
const size_t GetAllocCount ()const{return alloc_count;} ///<取得已分配的阵列大小(注:非字节数)
|
||||||
const size_t GetBytes ()const{return count*sizeof(T);} ///<取得阵列已使用的字节数
|
const size_t GetBytes ()const{return count*sizeof(T);} ///<取得阵列已使用的字节数
|
||||||
const size_t GetAllocBytes()const{return alloc_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;}
|
const T * GetData()const{return items;}
|
||||||
T * data() {return items;}
|
T * data() {return items;}
|
||||||
@@ -72,7 +74,7 @@ namespace hgl
|
|||||||
|
|
||||||
DataArray()
|
DataArray()
|
||||||
{
|
{
|
||||||
items=0;
|
items=nullptr;
|
||||||
count=0;
|
count=0;
|
||||||
alloc_count=0;
|
alloc_count=0;
|
||||||
}
|
}
|
||||||
@@ -80,7 +82,7 @@ namespace hgl
|
|||||||
DataArray(size_t size)
|
DataArray(size_t size)
|
||||||
{
|
{
|
||||||
if(size<=0)
|
if(size<=0)
|
||||||
items=0;
|
items=nullptr;
|
||||||
else
|
else
|
||||||
items=(T *)hgl_malloc(size*sizeof(T));
|
items=(T *)hgl_malloc(size*sizeof(T));
|
||||||
|
|
||||||
@@ -103,8 +105,7 @@ namespace hgl
|
|||||||
|
|
||||||
void Clear()
|
void Clear()
|
||||||
{
|
{
|
||||||
if(items)
|
SAFE_FREE(items);
|
||||||
hgl_free(items);
|
|
||||||
|
|
||||||
count=0;
|
count=0;
|
||||||
alloc_count=0;
|
alloc_count=0;
|
||||||
@@ -121,18 +122,6 @@ namespace hgl
|
|||||||
memset(items,0,alloc_count);
|
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;
|
alloc_count=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
operator T *()const
|
||||||
* 复制内存块中的数据
|
|
||||||
* @param d 复制出来的数据指针
|
|
||||||
* @param s 要复制出来的数据个数
|
|
||||||
*/
|
|
||||||
void CopyData(const T *d,int s)
|
|
||||||
{
|
{
|
||||||
SetCount(s);
|
return items;
|
||||||
memcpy(items,d,s*sizeof(T));
|
}
|
||||||
|
|
||||||
|
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&&index<count)
|
|
||||||
{
|
|
||||||
--count;
|
|
||||||
|
|
||||||
if(index<count)
|
|
||||||
memcpy(items+index,items+count,sizeof(T)); //将最后一个数据移到当前位置
|
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除列表中的指定项,删除后将后面的数据整体前移
|
|
||||||
* @param index 要删除的数据项的索引值
|
|
||||||
* @return 是否成功
|
|
||||||
*/
|
|
||||||
bool DeleteMove(int index)
|
|
||||||
{
|
|
||||||
if(count>0&&index>=0&&index<count)
|
|
||||||
{
|
|
||||||
--count;
|
|
||||||
|
|
||||||
if(index<count)
|
|
||||||
memmove(items+index,items+index+1,(count-index)*sizeof(T));
|
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除列表中的指定项
|
|
||||||
* @param start 要删除的数据项的索引起始值
|
* @param start 要删除的数据项的索引起始值
|
||||||
* @param number 要删除的数据项数量
|
* @param number 要删除的数据项数量
|
||||||
* @return 是否成功
|
* @return 是否成功
|
||||||
*/
|
*/
|
||||||
bool Delete(int start,int number)
|
bool Delete(int start,int number=1)
|
||||||
{
|
{
|
||||||
if(start>=count)return(false);
|
if(start>=count)return(false);
|
||||||
|
|
||||||
@@ -223,24 +181,33 @@ namespace hgl
|
|||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
operator T *()const
|
/**
|
||||||
|
* 删除列表中的指定项(关心顺序,删除中间数据后,会将后面的数据整体前移)
|
||||||
|
* @param start 要删除的数据项的索引值
|
||||||
|
* @param number 要删除的数据项数量
|
||||||
|
* @return 是否成功
|
||||||
|
*/
|
||||||
|
bool DeleteMove(int start,int number=1)
|
||||||
{
|
{
|
||||||
return items;
|
if(start>=count)return(false);
|
||||||
|
|
||||||
|
if(start<0)
|
||||||
|
{
|
||||||
|
number+=start;
|
||||||
|
start=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
T *operator ->()const
|
if(start+number>count)
|
||||||
{
|
number=count-start;
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
T &operator[](int n)
|
if(number<=0)return(false);
|
||||||
{
|
|
||||||
return items[n];
|
|
||||||
}
|
|
||||||
|
|
||||||
const T &operator[](int n)const
|
count-=number;
|
||||||
{
|
|
||||||
return items[n];
|
if(start<count)
|
||||||
|
memmove(items+start,items+start+number,(count-start)*sizeof(T));
|
||||||
|
|
||||||
|
return(true);
|
||||||
}
|
}
|
||||||
};//template<typename T> class DataArray
|
};//template<typename T> class DataArray
|
||||||
}//namespace hgl
|
}//namespace hgl
|
||||||
|
Reference in New Issue
Block a user