first finished TileFont::Registry/Unregistry functions. but it not test.
This commit is contained in:
@@ -3,103 +3,12 @@
|
||||
|
||||
#include<hgl/graph/TileData.h>
|
||||
#include<hgl/graph/font/FontSource.h>
|
||||
#include<hgl/type/ResPool.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace graph
|
||||
{
|
||||
template<typename T> struct RefData
|
||||
{
|
||||
int ref_count;
|
||||
T data;
|
||||
|
||||
public:
|
||||
|
||||
RefData(const T &value)
|
||||
{
|
||||
ref_count=1;
|
||||
data=value;
|
||||
}
|
||||
};//template<typename T> struct RefData
|
||||
|
||||
template<typename T> class DataPool
|
||||
{
|
||||
public:
|
||||
|
||||
DataPool();
|
||||
virtual ~DataPool()=default;
|
||||
|
||||
virtual bool Acquire(T &)=0;
|
||||
virtual void Release(const T)=0;
|
||||
};//
|
||||
|
||||
template<typename K,typename V> class ResPool
|
||||
{
|
||||
protected:
|
||||
|
||||
using ActiveItem=RefData<V>;
|
||||
|
||||
DataPool<V> *pool;
|
||||
|
||||
MapObject<K,ActiveItem> active_items; ///<活跃的数据
|
||||
Map<K,V> idle_items; ///<引用计数为0的
|
||||
|
||||
public:
|
||||
|
||||
ResPool(DataPool<V> *dp):pool(dp){}
|
||||
virtual ~ResPool()=default;
|
||||
|
||||
bool Get(const K &key,V &value)
|
||||
{
|
||||
ActiveItem *ai;
|
||||
|
||||
if(active_items.Get(key,ai)) //在活跃列表中找
|
||||
{
|
||||
++ai->ref_count;
|
||||
value=ai->data;
|
||||
return(true);
|
||||
}
|
||||
|
||||
if(idle_items.Get(key,value)) //在限制列表中找
|
||||
{
|
||||
active_items.Add(key,new ActiveItem(value));
|
||||
return(true);
|
||||
}
|
||||
|
||||
return(false);
|
||||
}
|
||||
|
||||
bool Create(const K &key,V &value)
|
||||
{
|
||||
if(!pool->Acquire(value))
|
||||
return(false);
|
||||
|
||||
active_items.Add(key,new ActiveItem(value));
|
||||
return(true);
|
||||
}
|
||||
|
||||
void Release(const K &key)
|
||||
{
|
||||
int pos;
|
||||
ActiveItem *ai;
|
||||
|
||||
pos=active_items.GetValueAndSerial(key,ai);
|
||||
|
||||
if(pos>0)
|
||||
{
|
||||
--ai->ref_count;
|
||||
|
||||
if(ai->ref_count==0)
|
||||
{
|
||||
idle_items.Add(ai->data);
|
||||
active_items.DeleteBySerial(pos);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
};//template<typename K,typename V> class ResPool
|
||||
|
||||
using TileResPool=ResPool<u32char,TileObject *>;
|
||||
|
||||
/**
|
||||
@@ -111,8 +20,7 @@ namespace hgl
|
||||
FontSource *source;
|
||||
TileData *tile_data;
|
||||
|
||||
DataPool<TileObject *> *to_pool;
|
||||
TileResPool *to_res;
|
||||
TileResPool to_res;
|
||||
|
||||
public:
|
||||
|
||||
|
Reference in New Issue
Block a user