added ActiveDataManager.h
This commit is contained in:
134
inc/hgl/type/ActiveDataManager.h
Normal file
134
inc/hgl/type/ActiveDataManager.h
Normal file
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/ActiveMemoryBlockManager.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 活动数据管理模板类<br>
|
||||
* 通过ActiveIDManager管理活跃的数据ID,在要使用时通过ID来获取或写入数据。
|
||||
*/
|
||||
template<typename T> class ActiveDataManager
|
||||
{
|
||||
ActiveIDManager aim;
|
||||
|
||||
DataArray<T> data_array;
|
||||
|
||||
public:
|
||||
|
||||
ActiveDataManager(){}
|
||||
virtual ~ActiveDataManager()=default;
|
||||
|
||||
void Alloc(int c)
|
||||
{
|
||||
aim.Alloc(c);
|
||||
data_array.Alloc(c);
|
||||
}
|
||||
|
||||
int GetActiveCount ()const{return aim.GetActiveCount();}
|
||||
int GetIdleCount ()const{return aim.GetIdleCount();}
|
||||
int GetTotalCount ()const{return aim.GetTotalCount();}
|
||||
int GetHistoryMaxId ()const{return aim.GetHistoryMaxId();}
|
||||
|
||||
const DataArray<int> &GetActiveArray()const{return aim.GetActiveArray();}
|
||||
const DataArray<int> &GetIdleArray()const{return aim.GetIdleArray();}
|
||||
|
||||
public:
|
||||
|
||||
bool WriteData(const T &d,const int id)
|
||||
{
|
||||
return data_array.WriteAt(d,id);
|
||||
}
|
||||
|
||||
int WriteDataArray(const T **da,const int *idp,const int count)
|
||||
{
|
||||
int result=0;
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
if(data_array.WriteAt(**da,*idp))
|
||||
++result;
|
||||
|
||||
++da;
|
||||
++idp;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int WriteDataArray(T *da,const int *idp,const int count)const
|
||||
{
|
||||
int result=0;
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
if(data_array.WriteAt(*da,*idp))
|
||||
++result;
|
||||
|
||||
++da;
|
||||
++idp;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool GetData(T &d,const int id)const
|
||||
{
|
||||
return data_array.ReadAt(d,id);
|
||||
}
|
||||
|
||||
bool GetData(T **da,const int *idp,const int count)const
|
||||
{
|
||||
int result=0;
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
*da=data_array.GetPointer(*idp);
|
||||
|
||||
if(*da)++result;
|
||||
|
||||
++da;
|
||||
++idp;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool GetData(T *da,const int *idp,const int count)const
|
||||
{
|
||||
int result=0;
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
if(data_array.ReadAt(*da,*idp))
|
||||
++result;
|
||||
|
||||
++da;
|
||||
++idp;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
void CreateActive(int *id,const int count=1)
|
||||
{
|
||||
aim.CreateActive(id,count);
|
||||
|
||||
data_array.AddCount(count);
|
||||
}
|
||||
|
||||
void CreateIdle(int *idp=nullptr,const int count=1)
|
||||
{
|
||||
aim.CreateIdle(idp,count);
|
||||
|
||||
data_array.AddCount(count);
|
||||
}
|
||||
|
||||
void CreateIdle (const int count=1) {CreateIdle(nullptr,count);}
|
||||
bool Get (int *id,const int count=1) {return aim.Get(id,count);}
|
||||
int Release (int *id,const int count=1) {return aim.Release(id,count);}
|
||||
int ReleaseAllActive() {return aim.ReleaseAllActive();}
|
||||
};//template<typename T> class ActiveDataManager
|
||||
}//namespace hgl
|
@@ -5,6 +5,10 @@
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 活跃内存块管理类<br>
|
||||
* 通过ActiveIDManager管理活跃的数据ID,在要使用时通过ID来获取或写入数据。
|
||||
*/
|
||||
class ActiveMemoryBlockManager
|
||||
{
|
||||
uint unit_size; ///<单个数据大小
|
||||
@@ -35,12 +39,15 @@ namespace hgl
|
||||
public:
|
||||
|
||||
bool WriteData (void *d,const int id);
|
||||
bool WriteDataArray (void **da,const int *idp,const int count);
|
||||
bool WriteDataArray (void *da,const int *idp,const int count)const;
|
||||
int WriteDataArray (void **da,const int *idp,const int count);
|
||||
int WriteDataArray (void *da,const int *idp,const int count)const;
|
||||
|
||||
bool GetDataArrayPointer(void **da,const int *idp,const int count)const; ///<根据ID获取数据
|
||||
bool GetData(void *,const int id)const;
|
||||
bool GetData(void **da,const int *idp,const int count)const; ///<根据ID获取数据
|
||||
bool GetData(void *da,const int *idp,const int count)const; ///<根据ID获取数据,并整齐排列到一起
|
||||
|
||||
public:
|
||||
|
||||
int CreateActive(int *da,const int count=1);
|
||||
int CreateIdle(int *da,const int count=1);
|
||||
|
||||
|
@@ -24,6 +24,7 @@ SOURCE_GROUP("DataType\\Collection" FILES ${TYPE_INCLUDE_PATH}/Collection.h
|
||||
|
||||
SET(ACTIVE_MANAGER_FILES ${TYPE_INCLUDE_PATH}/ActiveIDManager.h
|
||||
${TYPE_INCLUDE_PATH}/ActiveMemoryBlockManager.h
|
||||
${TYPE_INCLUDE_PATH}/ActiveDataManager.h
|
||||
Type/ActiveIDManager.cpp
|
||||
Type/ActiveMemoryBlockManager.cpp)
|
||||
|
||||
@@ -219,11 +220,12 @@ add_cm_library(CMCore "CM" ${CORE_PLATFORM_HEADER_FILES}
|
||||
${TEXT_HEADER_FILES}
|
||||
${TEXT_SOURCE_FILES}
|
||||
|
||||
${TYPE_TEMPLATE_HEADER}
|
||||
${TYPE_SOURCE_FILES}
|
||||
|
||||
${ACTIVE_MANAGER_FILES}
|
||||
|
||||
${TYPE_TEMPLATE_HEADER}
|
||||
${TYPE_SOURCE_FILES}
|
||||
|
||||
${IO_SOURCE_FILES}
|
||||
|
||||
${MATH_HEADER_FILES}
|
||||
|
@@ -42,51 +42,65 @@ namespace hgl
|
||||
return data_mb->Write(unit_size*id,d,unit_size);
|
||||
}
|
||||
|
||||
bool ActiveMemoryBlockManager::WriteDataArray(void **da,const int *idp,const int count)
|
||||
int ActiveMemoryBlockManager::WriteDataArray(void **da,const int *idp,const int count)
|
||||
{
|
||||
if(!da||!idp||count<=0)return(false);
|
||||
if(!da||!idp||count<=0)return(0);
|
||||
|
||||
uint8 *sp=(uint8 *)(data_mb->Get());
|
||||
int result=0;
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
if(*idp<0||*idp>=aim.GetHistoryMaxId())
|
||||
return(false);
|
||||
|
||||
memcpy(sp+(*idp)*unit_size,da,unit_size);
|
||||
if(*idp>=0&&*idp<aim.GetHistoryMaxId())
|
||||
{
|
||||
memcpy(sp+(*idp)*unit_size,*da,unit_size);
|
||||
++result;
|
||||
}
|
||||
|
||||
++da;
|
||||
++idp;
|
||||
}
|
||||
|
||||
return(true);
|
||||
return(result);
|
||||
}
|
||||
|
||||
bool ActiveMemoryBlockManager::WriteDataArray(void *da,const int *idp,const int count)const
|
||||
int ActiveMemoryBlockManager::WriteDataArray(void *da,const int *idp,const int count)const
|
||||
{
|
||||
if(!da||!idp||count<=0)return(false);
|
||||
if(!da||!idp||count<=0)return(0);
|
||||
|
||||
uint8 *sp=(uint8 *)da;
|
||||
uint8 *tp=(uint8 *)(data_mb->Get());
|
||||
int result=0;
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
if(*idp<0||*idp>=aim.GetHistoryMaxId())
|
||||
return(false);
|
||||
|
||||
if(*idp>=0&&*idp<aim.GetHistoryMaxId())
|
||||
{
|
||||
memcpy(tp+(*idp)*unit_size,sp,unit_size);
|
||||
++result;
|
||||
}
|
||||
|
||||
sp+=unit_size;
|
||||
++idp;
|
||||
}
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
bool ActiveMemoryBlockManager::GetData(void *da,const int id)const
|
||||
{
|
||||
if(!id||id<0||id>=aim.GetHistoryMaxId())
|
||||
return(false);
|
||||
|
||||
memcpy(da,(uint8 *)(data_mb->Get())+id*unit_size,unit_size);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取数据
|
||||
*/
|
||||
bool ActiveMemoryBlockManager::GetDataArrayPointer(void **da,const int *idp,const int count)const
|
||||
bool ActiveMemoryBlockManager::GetData(void **da,const int *idp,const int count)const
|
||||
{
|
||||
if(!da||!idp||count<=0)return(false);
|
||||
|
||||
|
Reference in New Issue
Block a user