added DataStackPool<>
This commit is contained in:
56
inc/hgl/type/DataStackPool.h
Normal file
56
inc/hgl/type/DataStackPool.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/DataArray.h>
|
||||
#include<hgl/type/SeriesPool.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 数据堆栈池(一种简单的数据池结构,不支持动态调整大小)
|
||||
*/
|
||||
template<typename T> class DataStackPool
|
||||
{
|
||||
protected:
|
||||
|
||||
T *data_array; ///<数据区
|
||||
T *end; ///<结束指针
|
||||
SeriesInt series; ///<序号池
|
||||
|
||||
public:
|
||||
|
||||
DataStackPool(const int max_count):series(max_count)
|
||||
{
|
||||
data_array=hgl_zero_new<T>(max_count);
|
||||
|
||||
end=data_array+max_count;
|
||||
}
|
||||
|
||||
~DataStackPool()
|
||||
{
|
||||
delete[] data_array;
|
||||
}
|
||||
|
||||
T *Acquire() ///<请求一个数据
|
||||
{
|
||||
int pos;
|
||||
|
||||
if(!series.Acquire(&pos))
|
||||
return nullptr;
|
||||
|
||||
return data_array+pos;
|
||||
}
|
||||
|
||||
bool Release(T *obj) ///<释放一个数据
|
||||
{
|
||||
if(!obj)
|
||||
return(false);
|
||||
|
||||
if(obj<data_array||obj>=end)
|
||||
return(false);
|
||||
|
||||
const int pos=obj-data_array;
|
||||
|
||||
return series.Release(pos);
|
||||
}
|
||||
};//template<typename T> class DataStackPool
|
||||
}//namespace
|
Reference in New Issue
Block a user