Merge branch 'master' of http://www.hyzgame.com:3000/hyzboy/CMCore
This commit is contained in:
commit
641ed83442
25
inc/hgl/io/LoadDataArray.h
Normal file
25
inc/hgl/io/LoadDataArray.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/DataArray.h>
|
||||
#include<hgl/io/FileInputStream.h>
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 加载一个文件到数据阵列中
|
||||
*/
|
||||
template<typename T> static DataArray<T> *LoadFileToDataArray(const OSString &filename)
|
||||
{
|
||||
io::FileInputStream fis;
|
||||
|
||||
if(!fis.Open(filename))return(nullptr);
|
||||
|
||||
const size_t file_size =fis.GetSize();
|
||||
const size_t size =(file_size+sizeof(T)-1)/sizeof(T);
|
||||
|
||||
DataArray<T> *mb=new DataArray<T>(size);
|
||||
|
||||
fis.Read(mb->data(),file_size);
|
||||
|
||||
return(mb);
|
||||
}
|
||||
}//namespace hgl
|
18
inc/hgl/io/SaveDataArray.h
Normal file
18
inc/hgl/io/SaveDataArray.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/DataArray.h>
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 保存一个数据阵列到文件
|
||||
*/
|
||||
template<typename T> static bool SaveDataArrayToFile(const OSString &filename,const DataArray<T> &mb)
|
||||
{
|
||||
const size_t size=mb.bytes();
|
||||
|
||||
if(size<=0)return(true);
|
||||
|
||||
return(hgl::filesystem::SaveMemoryToFile(filename,mb.data(),mb.bytes())==size);
|
||||
}
|
||||
}//namespace hgl
|
@ -2,14 +2,14 @@
|
||||
#define HGL_PLUG_IN_MANAGE_INCLUDE
|
||||
|
||||
#include<hgl/plugin/ExternalPlugIn.h>
|
||||
#include<hgl/type/ResManage.h>
|
||||
#include<hgl/type/ObjectManage.h>
|
||||
#include<hgl/type/StringList.h>
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 插件管理
|
||||
*/
|
||||
class PlugInManage:public ResManage<OSString,PlugIn>
|
||||
class PlugInManage:public ObjectManage<OSString,PlugIn>
|
||||
{
|
||||
OSString name; ///<插件类目名称(必须符合代码名称规则)
|
||||
|
||||
@ -28,7 +28,7 @@ namespace hgl
|
||||
PlugIn *LoadPlugin (const OSString &,const OSString &); ///<加载一个外部插件,明确指定全路径文件名
|
||||
PlugIn *LoadPlugin (const OSString &); ///<加载一个外部插件,自行查找
|
||||
bool UnloadPlugin(const OSString &); ///<释放一个外部插件
|
||||
};//class PlugInManage:public ResManage<UTF16String,PlugIn>
|
||||
};//class PlugInManage:public ObjectManage<UTF16String,PlugIn>
|
||||
|
||||
/**
|
||||
* 插件注册模板
|
||||
|
@ -2,7 +2,7 @@
|
||||
#define HGL_LOADER_INCLUDE
|
||||
|
||||
#include<cm.h>
|
||||
#include<hgl/type/BaseString.h>
|
||||
#include<hgl/type/String.h>
|
||||
#include<hgl/object/EnumObject.h>
|
||||
#include<hgl/thread/Thread.h>
|
||||
namespace hgl
|
||||
|
246
inc/hgl/type/DataArray.h
Normal file
246
inc/hgl/type/DataArray.h
Normal file
@ -0,0 +1,246 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/DataType.h>
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 数据阵列
|
||||
*/
|
||||
template<typename T> class DataArray
|
||||
{
|
||||
protected:
|
||||
|
||||
T *items;
|
||||
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);} ///<取得阵列已分配空间字节数
|
||||
|
||||
const T * GetData()const{return items;}
|
||||
T * data() {return items;}
|
||||
|
||||
public:
|
||||
|
||||
T * begin (){return items;} ///<取得阵列起始地址指针
|
||||
T * end (){return items+count;} ///<取得阵列结束地址指针
|
||||
|
||||
const T * begin ()const{return items;} ///<取得阵列起始地址指针
|
||||
const T * end ()const{return items+count;} ///<取得阵列结束地址指针
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* 分配指定空间出来,供未来使用
|
||||
*/
|
||||
void Alloc(size_t size)
|
||||
{
|
||||
if(size<=alloc_count)
|
||||
return;
|
||||
|
||||
alloc_count=power_to_2(size);
|
||||
|
||||
if(!items)
|
||||
items=(T *)hgl_malloc(alloc_count*sizeof(T));
|
||||
else
|
||||
items=(T *)hgl_realloc(items,alloc_count*sizeof(T));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置阵列长度
|
||||
*/
|
||||
void SetCount(size_t size) ///<设置阵列长度(注:非字节数)
|
||||
{
|
||||
Alloc(size);
|
||||
|
||||
count=size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加阵列长度
|
||||
*/
|
||||
void AddCount(size_t size)
|
||||
{
|
||||
SetCount(count+size);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
DataArray()
|
||||
{
|
||||
items=0;
|
||||
count=0;
|
||||
alloc_count=0;
|
||||
}
|
||||
|
||||
DataArray(size_t size)
|
||||
{
|
||||
if(size<=0)
|
||||
items=0;
|
||||
else
|
||||
items=(T *)hgl_malloc(size*sizeof(T));
|
||||
|
||||
if(items)
|
||||
{
|
||||
count=size;
|
||||
alloc_count=size;
|
||||
}
|
||||
else
|
||||
{
|
||||
count=0;
|
||||
alloc_count=0;
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~DataArray()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
void Clear()
|
||||
{
|
||||
if(items)
|
||||
hgl_free(items);
|
||||
|
||||
count=0;
|
||||
alloc_count=0;
|
||||
}
|
||||
|
||||
void ClearData()
|
||||
{
|
||||
count=0;
|
||||
}
|
||||
|
||||
void Zero()
|
||||
{
|
||||
if(items)
|
||||
memset(items,0,alloc_count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据,请保证数据使用hgl_malloc分配,否则会因为释放函数不配对出现错误
|
||||
*/
|
||||
void SetData(T *d,int s)
|
||||
{
|
||||
Clear();
|
||||
|
||||
items=d;
|
||||
alloc_count=s;
|
||||
count=s;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解除数据关联
|
||||
*/
|
||||
void Unlink()
|
||||
{
|
||||
items=nullptr;
|
||||
count=0;
|
||||
alloc_count=0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制内存块中的数据
|
||||
* @param d 复制出来的数据指针
|
||||
* @param s 要复制出来的数据个数
|
||||
*/
|
||||
void CopyData(const T *d,int s)
|
||||
{
|
||||
SetCount(s);
|
||||
memcpy(items,d,s*sizeof(T));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除列表中的指定项,删除后将最后一个数据移到被删除的位置
|
||||
* @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 number 要删除的数据项数量
|
||||
* @return 是否成功
|
||||
*/
|
||||
bool Delete(int start,int number)
|
||||
{
|
||||
if(start>=count)return(false);
|
||||
|
||||
if(start<0)
|
||||
{
|
||||
number+=start;
|
||||
start=0;
|
||||
}
|
||||
|
||||
if(start+number>count)
|
||||
number=count-start;
|
||||
|
||||
if(number<=0)return(false);
|
||||
|
||||
count-=number;
|
||||
|
||||
if(start<count)
|
||||
memmove(items+start,items+start+number,(count-start)*sizeof(T));
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
operator T *()const
|
||||
{
|
||||
return items;
|
||||
}
|
||||
|
||||
T *operator ->()const
|
||||
{
|
||||
return items;
|
||||
}
|
||||
|
||||
T &operator[](int n)
|
||||
{
|
||||
return items[n];
|
||||
}
|
||||
|
||||
const T &operator[](int n)const
|
||||
{
|
||||
return items[n];
|
||||
}
|
||||
};//template<typename T> class DataArray
|
||||
}//namespace hgl
|
@ -1,13 +0,0 @@
|
||||
#ifndef HGL_ENUM_CLASS_INCLUDE
|
||||
#define HGL_ENUM_CLASS_INCLUDE
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
template<typename U,typename E> class EnumAsUInteger
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
};//template<typename U,typename E> class EnumAsUInteger
|
||||
}//namespace hgl
|
||||
#endif//HGL_ENUM_CLASS_INCLUDE
|
@ -1,208 +0,0 @@
|
||||
#ifndef HGL_MEM_BLOCK_INCLUDE
|
||||
#define HGL_MEM_BLOCK_INCLUDE
|
||||
|
||||
#include<hgl/type/DataType.h>
|
||||
#include<hgl/io/FileInputStream.h>
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
#include<hgl/thread/ThreadMutex.h>
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 内存块模版
|
||||
*/
|
||||
template<typename T> class MemBlock
|
||||
{
|
||||
protected:
|
||||
|
||||
T *buf;
|
||||
size_t cur_size;
|
||||
size_t buf_size;
|
||||
|
||||
public:
|
||||
|
||||
size_t GetLength ()const{return cur_size;} ///<取得内存块长度(注:非字节数)
|
||||
const size_t GetMaxLength ()const{return buf_size;} ///<取得内存块最大长度(注:非字节数)
|
||||
const size_t GetBytes ()const{return cur_size*sizeof(T);} ///<取得内存块字节数
|
||||
const size_t GetMaxBytes ()const{return buf_size*sizeof(T);} ///<取得内存块最大字节数
|
||||
|
||||
/**
|
||||
* 分配指定空间出来,供未来使用
|
||||
*/
|
||||
void Malloc(size_t size)
|
||||
{
|
||||
if(size<=buf_size)
|
||||
return;
|
||||
|
||||
buf_size=power_to_2(size);
|
||||
|
||||
if(!buf)
|
||||
buf=(T *)hgl_malloc(buf_size*sizeof(T));
|
||||
else
|
||||
buf=(T *)hgl_realloc(buf,buf_size*sizeof(T));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前数据长度
|
||||
*/
|
||||
void SetLength(size_t size) ///<设置内存块长度(注:非字节数)
|
||||
{
|
||||
Malloc(size);
|
||||
|
||||
cur_size=size;
|
||||
}
|
||||
|
||||
void AddLength(size_t size)
|
||||
{
|
||||
SetLength(cur_size+size);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
MemBlock()
|
||||
{
|
||||
buf=0;
|
||||
cur_size=0;
|
||||
buf_size=0;
|
||||
}
|
||||
|
||||
MemBlock(size_t size)
|
||||
{
|
||||
if(size<=0)
|
||||
buf=0;
|
||||
else
|
||||
buf=(T *)hgl_malloc(size*sizeof(T));
|
||||
|
||||
if(buf)
|
||||
{
|
||||
cur_size=size;
|
||||
buf_size=size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_size=0;
|
||||
buf_size=0;
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~MemBlock()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
void Clear()
|
||||
{
|
||||
if(buf)
|
||||
hgl_free(buf);
|
||||
|
||||
cur_size=0;
|
||||
buf_size=0;
|
||||
}
|
||||
|
||||
void ClearData()
|
||||
{
|
||||
cur_size=0;
|
||||
}
|
||||
|
||||
void Zero()
|
||||
{
|
||||
if(buf)
|
||||
memset(buf,0,buf_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据,请保证数据使用hgl_malloc分配,否则会因为释放函数不配对出现错误
|
||||
*/
|
||||
void SetData(T *d,int s)
|
||||
{
|
||||
Clear();
|
||||
|
||||
buf=d;
|
||||
buf_size=s;
|
||||
cur_size=s;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解除数据关联
|
||||
*/
|
||||
void Unlink()
|
||||
{
|
||||
buf=nullptr;
|
||||
cur_size=0;
|
||||
buf_size=0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制内存块中的数据
|
||||
* @param d 复制出来的缓冲区指针
|
||||
* @param s 要复制出来的数据个数
|
||||
*/
|
||||
void CopyData(T *d,int s)
|
||||
{
|
||||
SetLength(s);
|
||||
memcpy(buf,d,s*sizeof(T));
|
||||
}
|
||||
|
||||
T *data()const{return buf;}
|
||||
T *GetData()const{return buf;}
|
||||
|
||||
size_t length()const{return cur_size;}
|
||||
size_t GetCount()const{return cur_size;}
|
||||
|
||||
size_t bytes()const
|
||||
{
|
||||
return cur_size*sizeof(T);
|
||||
}
|
||||
|
||||
operator T *()const
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
|
||||
T *operator ->()const
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
|
||||
T &operator[](int n)
|
||||
{
|
||||
return buf[n];
|
||||
}
|
||||
|
||||
const T &operator[](int n)const
|
||||
{
|
||||
return buf[n];
|
||||
}
|
||||
};//template<typename T> class MemBlock
|
||||
|
||||
/**
|
||||
* 加载一个文件到内存块类中
|
||||
*/
|
||||
template<typename T> MemBlock<T> *LoadFileToMemBlock(const OSString &filename)
|
||||
{
|
||||
io::FileInputStream fis;
|
||||
|
||||
if(!fis.Open(filename))return(nullptr);
|
||||
|
||||
const size_t file_size =fis.GetSize();
|
||||
const size_t size =(file_size+sizeof(T)-1)/sizeof(T);
|
||||
|
||||
MemBlock<T> *mb=new MemBlock<T>(size);
|
||||
|
||||
fis.Read(mb->data(),file_size);
|
||||
|
||||
return(mb);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存一个内存块到文件
|
||||
*/
|
||||
template<typename T> bool SaveMemBlockToFile(const OSString &filename,const MemBlock<T> &mb)
|
||||
{
|
||||
const size_t size=mb.bytes();
|
||||
|
||||
if(size<=0)return(true);
|
||||
|
||||
return(hgl::filesystem::SaveMemoryToFile(filename,mb.data(),mb.bytes())==size);
|
||||
}
|
||||
}//namespace hgl
|
||||
#endif//HGL_MEM_BLOCK_INCLUDE
|
234
inc/hgl/type/ObjectManage.h
Normal file
234
inc/hgl/type/ObjectManage.h
Normal file
@ -0,0 +1,234 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/Map.h>
|
||||
namespace hgl
|
||||
{
|
||||
template<typename K,typename V> struct RefKeyValue:public KeyValue<K,V *> ///<带引用计数的Key/value对象结构
|
||||
{
|
||||
int ref_count; ///<引用计数
|
||||
|
||||
public:
|
||||
|
||||
RefKeyValue():KeyValue<K,V *>()
|
||||
{
|
||||
ref_count=1;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 对象管理器,它没有缓冲管理,仅仅是统一管理对象,并保证不会被重复加载
|
||||
*/
|
||||
template<typename K,typename V> class ObjectManage
|
||||
{
|
||||
public:
|
||||
|
||||
using KVObject=RefKeyValue<K,V>;
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
_Map<K,V *,KVObject> items;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void Clear(V *obj){delete obj;} ///<对象释放虚拟函数(缺省为直接delete对象)
|
||||
|
||||
public:
|
||||
|
||||
virtual ~ObjectManage()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
virtual void Clear() ///<清除所有对象
|
||||
{
|
||||
int n=items.GetCount();
|
||||
|
||||
while(n--)
|
||||
{
|
||||
KVObject *obj=items.GetItem(n);
|
||||
|
||||
Clear(obj->value);
|
||||
}
|
||||
|
||||
items.Clear();
|
||||
}
|
||||
|
||||
virtual void ClearFree() ///<清除所有引用计数为0的对象
|
||||
{
|
||||
int n=items.GetCount();
|
||||
|
||||
while(n--)
|
||||
{
|
||||
KVObject *obj=items.GetItem(n);
|
||||
|
||||
if(obj->ref_count<=0)
|
||||
{
|
||||
Clear(obj->value);
|
||||
items.DeleteBySerial(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const int GetCount()const{return items.GetCount();} ///<取得对象数量
|
||||
|
||||
virtual bool Add(const K &key,V *obj) ///<添加一个对象
|
||||
{
|
||||
if(!obj)return(false);
|
||||
|
||||
if(items.KeyExist(key))
|
||||
return(false);
|
||||
|
||||
items.Add(key,obj);
|
||||
return(true);
|
||||
}
|
||||
|
||||
virtual V * Find(const K &key) ///<查找一个对象(不增加引用计数)
|
||||
{
|
||||
int index=items.Find(key);
|
||||
|
||||
if(index==-1)
|
||||
return(nullptr);
|
||||
|
||||
KVObject *obj=items.GetItem(index);
|
||||
|
||||
return obj->value;
|
||||
}
|
||||
|
||||
virtual V * Get(const K &key) ///<取得一个对象(增加引用计数)
|
||||
{
|
||||
int index=items.Find(key);
|
||||
|
||||
if(index!=-1)
|
||||
{
|
||||
KVObject *obj=items.GetItem(index);
|
||||
|
||||
++obj->ref_count;
|
||||
|
||||
return obj->value;
|
||||
}
|
||||
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
virtual bool ValueExist(V *value) ///<确认这个对象是否存在
|
||||
{
|
||||
return(items.FindByValue(value)!=-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定对象的Key和引用计数
|
||||
* @param value 对象
|
||||
* @param key Key存放地址
|
||||
* @param ref_count 引用计数存放地址
|
||||
* @param 是否增加引用计数
|
||||
*/
|
||||
virtual bool GetKeyByValue(V *value,K *key,uint *ref_count,bool inc_ref_count)
|
||||
{
|
||||
int index=items.FindByValue(value);
|
||||
|
||||
if(index==-1)return(false);
|
||||
|
||||
KVObject *obj=items.GetItem(index);
|
||||
|
||||
if(inc_ref_count)
|
||||
++obj->ref_count;
|
||||
|
||||
if(key)
|
||||
*key=obj->key;
|
||||
|
||||
if(ref_count)
|
||||
*ref_count=obj->ref_count;
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据序列号释放一个对象(减少引用计数).
|
||||
*
|
||||
* \param index
|
||||
* \param zero_clear 引用为0后是否清除对象
|
||||
* \return
|
||||
*/
|
||||
virtual int ReleaseBySerial(int index,bool zero_clear=false)
|
||||
{
|
||||
if(index==-1)
|
||||
{
|
||||
// ErrorHint(u"所释放的资源不存在");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
KVObject *obj=items.GetItem(index);
|
||||
|
||||
--obj->ref_count;
|
||||
|
||||
if(obj->ref_count>0)
|
||||
return obj->ref_count;
|
||||
|
||||
if(zero_clear)
|
||||
{
|
||||
Clear(obj->value);
|
||||
|
||||
items.DeleteBySerial(index);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key释放一个对象(减少引用计数).
|
||||
*
|
||||
* \param key
|
||||
* \param zero_clear 引用为0后是否清除对象
|
||||
* \return
|
||||
*/
|
||||
virtual int Release(const K &key,bool zero_clear=false)
|
||||
{
|
||||
return ReleaseBySerial(items.Find(key),zero_clear);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据对象本身减少一次引用计数
|
||||
* @param td 对象指针
|
||||
* @param zero_clear 引用为0后是否清除对象
|
||||
*/
|
||||
int Release(V *td,bool zero_clear=false)
|
||||
{
|
||||
return ReleaseBySerial(items.FindByValue(td),zero_clear);
|
||||
}
|
||||
};//template<typename K,typename V> class ObjectManage
|
||||
|
||||
/**
|
||||
* 使用整型对象类做数标识的对象管理器
|
||||
*/
|
||||
template<typename K,typename V> class IDObjectManage:public ObjectManage<K,V>
|
||||
{
|
||||
K id_count=0;
|
||||
|
||||
public:
|
||||
|
||||
using ObjectManage<K,V>::ObjectManage;
|
||||
virtual ~IDObjectManage()=default;
|
||||
|
||||
virtual K Add(V *value)
|
||||
{
|
||||
if(!value)return(-1);
|
||||
|
||||
{
|
||||
K key;
|
||||
uint count;
|
||||
|
||||
if(ObjectManage<K,V>::GetKeyByValue(value,&key,&count,true))
|
||||
return key;
|
||||
}
|
||||
|
||||
if(!ObjectManage<K,V>::Add(id_count,value))
|
||||
return(-1);
|
||||
|
||||
return id_count++;
|
||||
}
|
||||
};//template<typename K,typename V> class IDObjectManage:public ObjectManage<K,V>
|
||||
|
||||
template<typename V> using U32ObjectManage=IDObjectManage<uint32,V>;
|
||||
template<typename V> using U64ObjectManage=IDObjectManage<uint64,V>;
|
||||
}//namespace hgl
|
@ -1,162 +0,0 @@
|
||||
#ifndef HGL_RES_MANAGE_CPP
|
||||
#define HGL_RES_MANAGE_CPP
|
||||
|
||||
#include<hgl/type/ResManage.h>
|
||||
namespace hgl
|
||||
{
|
||||
template<typename K,typename V>
|
||||
ResManage<K,V>::~ResManage()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
void ResManage<K,V>::Clear()
|
||||
{
|
||||
int n=items.GetCount();
|
||||
|
||||
while(n--)
|
||||
{
|
||||
ResItem *obj=items.GetItem(n);
|
||||
|
||||
Clear(obj->value);
|
||||
}
|
||||
|
||||
items.Clear();
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
void ResManage<K,V>::ClearFree()
|
||||
{
|
||||
int n=items.GetCount();
|
||||
|
||||
while(n--)
|
||||
{
|
||||
ResItem *obj=items.GetItem(n);
|
||||
|
||||
if(obj->ref_count<=0)
|
||||
{
|
||||
Clear(obj->value);
|
||||
items.DeleteBySerial(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
bool ResManage<K,V>::Add(const K &flag,V *obj)
|
||||
{
|
||||
if(!obj)return(false);
|
||||
|
||||
if(items.KeyExist(flag))
|
||||
return(false);
|
||||
|
||||
items.Add(flag,obj);
|
||||
return(true);
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
V *ResManage<K,V>::Find(const K &flag)
|
||||
{
|
||||
int index=items.Find(flag);
|
||||
|
||||
if(index==-1)
|
||||
return(nullptr);
|
||||
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
return obj->value;
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
V *ResManage<K,V>::Get(const K &flag)
|
||||
{
|
||||
int index=items.Find(flag);
|
||||
|
||||
if(index!=-1)
|
||||
{
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
++obj->ref_count;
|
||||
|
||||
return obj->value;
|
||||
}
|
||||
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认指定数据是否存在
|
||||
*/
|
||||
template<typename K,typename V>
|
||||
bool ResManage<K,V>::ValueExist(V *value)
|
||||
{
|
||||
return(items.FindByValue(value)!=-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定数据的Key和引用计数
|
||||
* @param value 数据
|
||||
* @param key Key存放地址
|
||||
* @param ref_count 引用计数存放地址
|
||||
* @param 是否增加引用计数
|
||||
*/
|
||||
template<typename K,typename V>
|
||||
bool ResManage<K,V>::GetKeyByValue(V *value,K *key,uint *ref_count,bool inc_ref_count)
|
||||
{
|
||||
int index=items.FindByValue(value);
|
||||
|
||||
if(index==-1)return(false);
|
||||
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
if(inc_ref_count)
|
||||
++obj->ref_count;
|
||||
|
||||
if(key)
|
||||
*key=obj->key;
|
||||
|
||||
if(ref_count)
|
||||
*ref_count=obj->ref_count;
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
int ResManage<K,V>::ReleaseBySerial(int index,bool zero_clear)
|
||||
{
|
||||
if(index==-1)
|
||||
{
|
||||
// ErrorHint(u"所释放的资源不存在");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
ResItem *obj=items.GetItem(index);
|
||||
|
||||
--obj->ref_count;
|
||||
|
||||
if(obj->ref_count>0)
|
||||
return obj->ref_count;
|
||||
|
||||
if(zero_clear)
|
||||
{
|
||||
Clear(obj->value);
|
||||
|
||||
items.DeleteBySerial(index);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
int ResManage<K,V>::Release(const K &flag,bool zero_clear)
|
||||
{
|
||||
return ReleaseBySerial(items.Find(flag),zero_clear);
|
||||
}
|
||||
|
||||
template<typename K,typename V>
|
||||
int ResManage<K,V>::Release(V *td,bool zero_clear)
|
||||
{
|
||||
return ReleaseBySerial(items.FindByValue(td),zero_clear);
|
||||
}
|
||||
}//namespace hgl
|
||||
#endif//HGL_RES_MANAGE_CPP
|
@ -1,91 +0,0 @@
|
||||
#ifndef HGL_RES_MANAGE_INCLUDE
|
||||
#define HGL_RES_MANAGE_INCLUDE
|
||||
|
||||
#include<hgl/type/Map.h>
|
||||
namespace hgl
|
||||
{
|
||||
template<typename K,typename V> struct RefKeyValue:public KeyValue<K,V *> ///<带引用计数的Key/value数据结构
|
||||
{
|
||||
int ref_count; ///<引用计数
|
||||
|
||||
public:
|
||||
|
||||
RefKeyValue():KeyValue<K,V *>()
|
||||
{
|
||||
ref_count=1;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 资源管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载
|
||||
*/
|
||||
template<typename K,typename V> class ResManage
|
||||
{
|
||||
protected:
|
||||
|
||||
using ResItem=RefKeyValue<K,V>;
|
||||
|
||||
_Map<K,V *,ResItem> items;
|
||||
|
||||
int ReleaseBySerial(int,bool);
|
||||
|
||||
protected:
|
||||
|
||||
virtual void Clear(V *obj){delete obj;} ///<资源释放虚拟函数(缺省为直接delete对象)
|
||||
|
||||
public:
|
||||
|
||||
virtual ~ResManage();
|
||||
|
||||
virtual void Clear(); ///<清除所有数据
|
||||
virtual void ClearFree(); ///<清除所有引用计数为0的数据
|
||||
|
||||
const int GetCount()const{return items.GetCount();} ///<取得数据数量
|
||||
|
||||
virtual bool Add(const K &,V *); ///<添加一个数据
|
||||
virtual V * Find(const K &); ///<查找一个数据(不增加引用计数)
|
||||
virtual V * Get(const K &); ///<取得一个数据(增加引用计数)
|
||||
|
||||
virtual bool ValueExist(V *); ///<确认这个数据是否存在
|
||||
virtual bool GetKeyByValue(V *,K *,uint *,bool inc_ref=false); ///<取得一个数据的Key和引用次数
|
||||
|
||||
virtual int Release(const K &,bool zero_clear=false); ///<释放一个数据
|
||||
virtual int Release(V *,bool zero_clear=false); ///<释放一个数据
|
||||
};//template<typename K,typename V> class ResManage
|
||||
|
||||
/**
|
||||
* 使用int类做数标致的资源管理器
|
||||
*/
|
||||
template<typename K,typename V> class IDResManage:public ResManage<K,V>
|
||||
{
|
||||
K id_count=0;
|
||||
|
||||
public:
|
||||
|
||||
using ResManage<K,V>::ResManage;
|
||||
virtual ~IDResManage()=default;
|
||||
|
||||
virtual K Add(V *value)
|
||||
{
|
||||
if(!value)return(-1);
|
||||
|
||||
{
|
||||
K key;
|
||||
uint count;
|
||||
|
||||
if(ResManage<K,V>::GetKeyByValue(value,&key,&count,true))
|
||||
return key;
|
||||
}
|
||||
|
||||
if(!ResManage<K,V>::Add(id_count,value))
|
||||
return(-1);
|
||||
|
||||
return id_count++;
|
||||
}
|
||||
};//template<typename K,typename V> class IDResManage:public ResManage<K,V>
|
||||
|
||||
template<typename V> using ID32ResManage=IDResManage<uint32,V>;
|
||||
template<typename V> using ID64ResManage=IDResManage<uint64,V>;
|
||||
}//namespace hgl
|
||||
#include<hgl/type/ResManage.cpp>
|
||||
#endif//HGL_RES_MANAGE_INCLUDE
|
@ -2,11 +2,11 @@
|
||||
#define HGL_RES_POOL_MANAGE_INCLUDE
|
||||
|
||||
#include<hgl/type/Pool.h>
|
||||
#include<hgl/type/ResManage.h>
|
||||
#include<hgl/type/ObjectManage.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
template <typename K,typename V,typename OP> class _ResPoolManage:public ResManage<K,V>
|
||||
template <typename K,typename V,typename OP> class _ResPoolManage:public ObjectManage<K,V>
|
||||
{
|
||||
protected:
|
||||
|
||||
@ -21,7 +21,7 @@ namespace hgl
|
||||
|
||||
_ResPoolManage(OP *op):data_pool(op){}
|
||||
virtual ~_ResPoolManage()=default;
|
||||
};//template <typename K,typename V,typename OP> class _ResPoolManage:public ResManage<K,V>
|
||||
};//template <typename K,typename V,typename OP> class _ResPoolManage:public ObjectManage<K,V>
|
||||
|
||||
/**
|
||||
* 资源池是Pool/ResManage两个模板的组合应用
|
||||
|
@ -59,9 +59,6 @@ SET(STRING_HEADER_FILES ${TYPE_INCLUDE_PATH}/String.h
|
||||
${TYPE_INCLUDE_PATH}/StringList.h
|
||||
${TYPE_INCLUDE_PATH}/SplitString.h
|
||||
${TYPE_INCLUDE_PATH}/MergeString.h
|
||||
${TYPE_INCLUDE_PATH}/LoadString.h
|
||||
${TYPE_INCLUDE_PATH}/LoadStringList.h
|
||||
${TYPE_INCLUDE_PATH}/SaveStringList.h
|
||||
${TYPE_INCLUDE_PATH}/StdString.h)
|
||||
|
||||
SET(TEXT_HEADER_FILES ${CMCORE_ROOT_INCLUDE_PATH}/hgl/Endian.h
|
||||
@ -72,9 +69,7 @@ SET(TEXT_HEADER_FILES ${CMCORE_ROOT_INCLUDE_PATH}/hgl/Endian.h
|
||||
|
||||
SET(TEXT_SOURCE_FILES Text/Endian.cpp
|
||||
Text/CodePage.cpp
|
||||
Text/UnicodeBlocks.cpp
|
||||
Text/LoadString.cpp
|
||||
Text/LoadStringList.cpp)
|
||||
Text/UnicodeBlocks.cpp)
|
||||
|
||||
SOURCE_GROUP("Text\\String" FILES ${STRING_HEADER_FILES}
|
||||
Text/StringList.cpp)
|
||||
@ -93,13 +88,13 @@ SOURCE_GROUP("DataType\\Template\\Memory" FILES ${BASE_OTHER_SOURCE})
|
||||
##I/O------------------------------------------------------------
|
||||
SET(IO_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/io)
|
||||
|
||||
SET(IO_BASE_FILES ${IO_INCLUDE_PATH}/InputStream.h
|
||||
SET(IO_BASE_FILES ${IO_INCLUDE_PATH}/InputStream.h
|
||||
${IO_INCLUDE_PATH}/IOType.h
|
||||
${IO_INCLUDE_PATH}/OutputStream.h
|
||||
${IO_INCLUDE_PATH}/SeekAccess.h
|
||||
IO/IOType.cpp)
|
||||
|
||||
SET(IO_DATA_FILES ${IO_INCLUDE_PATH}/DataInputStream.h
|
||||
SET(IO_DATA_FILES ${IO_INCLUDE_PATH}/DataInputStream.h
|
||||
${IO_INCLUDE_PATH}/DataOutputStream.h
|
||||
${IO_INCLUDE_PATH}/EndianDataInputStream.h
|
||||
${IO_INCLUDE_PATH}/EndianDataOutputStream.h
|
||||
@ -107,10 +102,13 @@ SET(IO_DATA_FILES ${IO_INCLUDE_PATH}/DataInputStream.h
|
||||
IO/DataInputStream.cpp
|
||||
IO/DataOutputStream.cpp)
|
||||
|
||||
SET(IO_MEMORY_FILES ${IO_INCLUDE_PATH}/MemoryInputStream.h
|
||||
${IO_INCLUDE_PATH}/MemoryOutputStream.h)
|
||||
SET(IO_MEMORY_STREAM_FILES ${IO_INCLUDE_PATH}/MemoryInputStream.h
|
||||
${IO_INCLUDE_PATH}/MemoryOutputStream.h)
|
||||
|
||||
SET(IO_FILE_FILES ${IO_INCLUDE_PATH}/FileAccess.h
|
||||
SET(IO_DATA_ARRAY_FILES ${IO_INCLUDE_PATH}/LoadDataArray.h
|
||||
${IO_INCLUDE_PATH}/SaveDataArray.h)
|
||||
|
||||
SET(IO_FILE_FILES ${IO_INCLUDE_PATH}/FileAccess.h
|
||||
${IO_INCLUDE_PATH}/FileInputStream.h
|
||||
${IO_INCLUDE_PATH}/FileOutputStream.h
|
||||
${IO_INCLUDE_PATH}/RandomAccessFile.h
|
||||
@ -122,11 +120,15 @@ SET(IO_FILE_FILES ${IO_INCLUDE_PATH}/FileAccess.h
|
||||
IO/FileOutputStream.cpp
|
||||
IO/RandomAccessFile.cpp)
|
||||
|
||||
|
||||
SET(IO_JAVA_FILES ${IO_INCLUDE_PATH}/JavaInputStream.h
|
||||
${IO_INCLUDE_PATH}/JavaOutputStream.h)
|
||||
${IO_INCLUDE_PATH}/JavaOutputStream.h)
|
||||
|
||||
SET(IO_TEXT_FILES IO/TextOutputStream.cpp)
|
||||
SET(IO_TEXT_FILES IO/TextOutputStream.cpp
|
||||
Text/LoadString.cpp
|
||||
Text/LoadStringList.cpp)
|
||||
|
||||
SET(IO_STRING_LIST_FILES ${IO_INCLUDE_PATH}/LoadStringList.h
|
||||
${IO_INCLUDE_PATH}/SaveStringList.h)
|
||||
|
||||
SET(INPUT_EVENT_FILES ${IO_INCLUDE_PATH}/event/KeyboardEvent.h
|
||||
${IO_INCLUDE_PATH}/event/MouseEvent.h
|
||||
@ -135,20 +137,24 @@ SET(INPUT_EVENT_FILES ${IO_INCLUDE_PATH}/event/KeyboardEvent.h
|
||||
|
||||
SOURCE_GROUP("IO\\Event" FILES ${INPUT_EVENT_FILES})
|
||||
|
||||
SOURCE_GROUP("IO\\Base" FILES ${IO_BASE_FILES})
|
||||
SOURCE_GROUP("IO\\Data" FILES ${IO_DATA_FILES})
|
||||
SOURCE_GROUP("IO\\Memory" FILES ${IO_MEMORY_FILES})
|
||||
SOURCE_GROUP("IO\\File" FILES ${IO_FILE_FILES})
|
||||
SOURCE_GROUP("IO\\Jave" FILES ${IO_JAVA_FILES})
|
||||
SOURCE_GROUP("IO\\Text" FILES ${IO_TEXT_FILES})
|
||||
SOURCE_GROUP("IO\\Base" FILES ${IO_BASE_FILES})
|
||||
SOURCE_GROUP("IO\\DataIOStream" FILES ${IO_DATA_FILES})
|
||||
SOURCE_GROUP("IO\\DataArray" FILES ${IO_DATA_ARRAY_FILES})
|
||||
SOURCE_GROUP("IO\\MemoryStream" FILES ${IO_MEMORY_STREAM_FILES})
|
||||
SOURCE_GROUP("IO\\File" FILES ${IO_FILE_FILES})
|
||||
SOURCE_GROUP("IO\\Jave" FILES ${IO_JAVA_FILES})
|
||||
SOURCE_GROUP("IO\\Text" FILES ${IO_TEXT_FILES})
|
||||
SOURCE_GROUP("ID\\StringList" FILES ${IO_STRING_LIST_FILES})
|
||||
|
||||
SET(IO_SOURCE_FILES ${IO_BASE_FILES}
|
||||
${IO_DATA_FILES}
|
||||
${IO_MEMORY_FILES}
|
||||
${IO_FILE_FILES}
|
||||
${IO_JAVA_FILES}
|
||||
${IO_TEXT_FILES}
|
||||
${INPUT_EVENT_FILES})
|
||||
SET(IO_SOURCE_FILES ${IO_BASE_FILES}
|
||||
${IO_DATA_FILES}
|
||||
${IO_DATA_ARRAY_FILES}
|
||||
${IO_MEMORY_STREAM_FILES}
|
||||
${IO_FILE_FILES}
|
||||
${IO_JAVA_FILES}
|
||||
${IO_TEXT_FILES}
|
||||
${IO_STRING_LIST_FILES}
|
||||
${INPUT_EVENT_FILES})
|
||||
|
||||
SET(FILESYSTEM_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/filesystem)
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include<hgl/io/TextInputStream.h>
|
||||
#include<hgl/io/TextInputStream.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
@ -77,7 +77,7 @@ namespace hgl
|
||||
{
|
||||
uint8 *p=buffer;
|
||||
|
||||
if(stream_pos==0) //最开始,那检测一下BOM头
|
||||
if(stream_pos==0) //最开始,那检测一下BOM头
|
||||
{
|
||||
if(cur_buf_size>=2)
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include<hgl/type/LoadString.h>
|
||||
#include<hgl/io/LoadString.h>
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
namespace hgl
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include<hgl/type/LoadStringList.h>
|
||||
#include<hgl/type/LoadString.h>
|
||||
#include<hgl/io/LoadStringList.h>
|
||||
#include<hgl/io/LoadString.h>
|
||||
#include<hgl/type/SplitString.h>
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user