From 25cd2fee89566a070ffa53ede3f37b2f2882b38d Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 13 Jul 2023 23:09:09 +0800 Subject: [PATCH 01/10] use utf8 encode for TextInputStream.cpp --- src/IO/TextInputStream.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IO/TextInputStream.cpp b/src/IO/TextInputStream.cpp index 248e543..f490271 100644 --- a/src/IO/TextInputStream.cpp +++ b/src/IO/TextInputStream.cpp @@ -1,4 +1,4 @@ -#include +#include namespace hgl { @@ -69,7 +69,7 @@ namespace hgl { uint8 *p=buffer; - if(stream_pos==0) //ʼǼһBOMͷ + if(stream_pos==0) //最开始,那检测一下BOM头 { if(cur_buf_size>=2) { From 13dbf78aa7cf292317d10a039a504752c21553c9 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 14 Jul 2023 00:46:45 +0800 Subject: [PATCH 02/10] removed EnumClass.h --- inc/hgl/type/EnumClass.h | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 inc/hgl/type/EnumClass.h diff --git a/inc/hgl/type/EnumClass.h b/inc/hgl/type/EnumClass.h deleted file mode 100644 index a5384d1..0000000 --- a/inc/hgl/type/EnumClass.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef HGL_ENUM_CLASS_INCLUDE -#define HGL_ENUM_CLASS_INCLUDE - -namespace hgl -{ - template class EnumAsUInteger - { - public: - - - };//template class EnumAsUInteger -}//namespace hgl -#endif//HGL_ENUM_CLASS_INCLUDE From e4beccacd013242b6cd45c3b96a54ee389bab50f Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 14 Jul 2023 23:21:03 +0800 Subject: [PATCH 03/10] renamed to ObjectManage instead of ResManage --- inc/hgl/plugin/PlugInManage.h | 6 ++-- .../type/{ResManage.cpp => ObjectManage.cpp} | 30 ++++++++-------- inc/hgl/type/{ResManage.h => ObjectManage.h} | 35 +++++++++---------- inc/hgl/type/ResPoolManage.h | 6 ++-- 4 files changed, 38 insertions(+), 39 deletions(-) rename inc/hgl/type/{ResManage.cpp => ObjectManage.cpp} (78%) rename inc/hgl/type/{ResManage.h => ObjectManage.h} (68%) diff --git a/inc/hgl/plugin/PlugInManage.h b/inc/hgl/plugin/PlugInManage.h index ab0bf53..3a60c5e 100644 --- a/inc/hgl/plugin/PlugInManage.h +++ b/inc/hgl/plugin/PlugInManage.h @@ -2,14 +2,14 @@ #define HGL_PLUG_IN_MANAGE_INCLUDE #include -#include +#include #include namespace hgl { /** * 插件管理 */ - class PlugInManage:public ResManage + class PlugInManage:public ObjectManage { 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 + };//class PlugInManage:public ObjectManage /** * 插件注册模板 diff --git a/inc/hgl/type/ResManage.cpp b/inc/hgl/type/ObjectManage.cpp similarity index 78% rename from inc/hgl/type/ResManage.cpp rename to inc/hgl/type/ObjectManage.cpp index 351c3ab..7e4b255 100644 --- a/inc/hgl/type/ResManage.cpp +++ b/inc/hgl/type/ObjectManage.cpp @@ -1,17 +1,17 @@ -#ifndef HGL_RES_MANAGE_CPP -#define HGL_RES_MANAGE_CPP +#ifndef HGL_OBJECT_MANAGE_CPP +#define HGL_OBJECT_MANAGE_CPP -#include +#include namespace hgl { template - ResManage::~ResManage() + ObjectManage::~ObjectManage() { Clear(); } template - void ResManage::Clear() + void ObjectManage::Clear() { int n=items.GetCount(); @@ -26,7 +26,7 @@ namespace hgl } template - void ResManage::ClearFree() + void ObjectManage::ClearFree() { int n=items.GetCount(); @@ -43,7 +43,7 @@ namespace hgl } template - bool ResManage::Add(const K &flag,V *obj) + bool ObjectManage::Add(const K &flag,V *obj) { if(!obj)return(false); @@ -55,7 +55,7 @@ namespace hgl } template - V *ResManage::Find(const K &flag) + V *ObjectManage::Find(const K &flag) { int index=items.Find(flag); @@ -68,7 +68,7 @@ namespace hgl } template - V *ResManage::Get(const K &flag) + V *ObjectManage::Get(const K &flag) { int index=items.Find(flag); @@ -88,7 +88,7 @@ namespace hgl * 确认指定数据是否存在 */ template - bool ResManage::ValueExist(V *value) + bool ObjectManage::ValueExist(V *value) { return(items.FindByValue(value)!=-1); } @@ -101,7 +101,7 @@ namespace hgl * @param 是否增加引用计数 */ template - bool ResManage::GetKeyByValue(V *value,K *key,uint *ref_count,bool inc_ref_count) + bool ObjectManage::GetKeyByValue(V *value,K *key,uint *ref_count,bool inc_ref_count) { int index=items.FindByValue(value); @@ -122,7 +122,7 @@ namespace hgl } template - int ResManage::ReleaseBySerial(int index,bool zero_clear) + int ObjectManage::ReleaseBySerial(int index,bool zero_clear) { if(index==-1) { @@ -148,15 +148,15 @@ namespace hgl } template - int ResManage::Release(const K &flag,bool zero_clear) + int ObjectManage::Release(const K &flag,bool zero_clear) { return ReleaseBySerial(items.Find(flag),zero_clear); } template - int ResManage::Release(V *td,bool zero_clear) + int ObjectManage::Release(V *td,bool zero_clear) { return ReleaseBySerial(items.FindByValue(td),zero_clear); } }//namespace hgl -#endif//HGL_RES_MANAGE_CPP +#endif//HGL_OBJECT_MANAGE_CPP diff --git a/inc/hgl/type/ResManage.h b/inc/hgl/type/ObjectManage.h similarity index 68% rename from inc/hgl/type/ResManage.h rename to inc/hgl/type/ObjectManage.h index 2845fe1..cc77782 100644 --- a/inc/hgl/type/ResManage.h +++ b/inc/hgl/type/ObjectManage.h @@ -1,5 +1,4 @@ -#ifndef HGL_RES_MANAGE_INCLUDE -#define HGL_RES_MANAGE_INCLUDE +#pragma once #include namespace hgl @@ -17,9 +16,9 @@ namespace hgl }; /** - * 资源管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载 + * 对象管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载 */ - template class ResManage + template class ObjectManage { protected: @@ -31,11 +30,11 @@ namespace hgl protected: - virtual void Clear(V *obj){delete obj;} ///<资源释放虚拟函数(缺省为直接delete对象) + virtual void Clear(V *obj){delete obj;} ///<对象释放虚拟函数(缺省为直接delete对象) public: - virtual ~ResManage(); + virtual ~ObjectManage(); virtual void Clear(); ///<清除所有数据 virtual void ClearFree(); ///<清除所有引用计数为0的数据 @@ -51,19 +50,19 @@ namespace hgl virtual int Release(const K &,bool zero_clear=false); ///<释放一个数据 virtual int Release(V *,bool zero_clear=false); ///<释放一个数据 - };//template class ResManage + };//template class ObjectManage /** - * 使用int类做数标致的资源管理器 + * 使用整型数据类做数标识的对象管理器 */ - template class IDResManage:public ResManage + template class IDObjectManage:public ObjectManage { K id_count=0; public: - using ResManage::ResManage; - virtual ~IDResManage()=default; + using ObjectManage::ObjectManage; + virtual ~IDObjectManage()=default; virtual K Add(V *value) { @@ -73,19 +72,19 @@ namespace hgl K key; uint count; - if(ResManage::GetKeyByValue(value,&key,&count,true)) + if(ObjectManage::GetKeyByValue(value,&key,&count,true)) return key; } - if(!ResManage::Add(id_count,value)) + if(!ObjectManage::Add(id_count,value)) return(-1); return id_count++; } - };//template class IDResManage:public ResManage + };//template class IDObjectManage:public ObjectManage - template using ID32ResManage=IDResManage; - template using ID64ResManage=IDResManage; + template using U32ObjectManage=IDObjectManage; + template using U64ObjectManage=IDObjectManage; }//namespace hgl -#include -#endif//HGL_RES_MANAGE_INCLUDE + +#include diff --git a/inc/hgl/type/ResPoolManage.h b/inc/hgl/type/ResPoolManage.h index 01f2dfd..c1079a9 100644 --- a/inc/hgl/type/ResPoolManage.h +++ b/inc/hgl/type/ResPoolManage.h @@ -2,11 +2,11 @@ #define HGL_RES_POOL_MANAGE_INCLUDE #include -#include +#include namespace hgl { - template class _ResPoolManage:public ResManage + template class _ResPoolManage:public ObjectManage { protected: @@ -21,7 +21,7 @@ namespace hgl _ResPoolManage(OP *op):data_pool(op){} virtual ~_ResPoolManage()=default; - };//template class _ResPoolManage:public ResManage + };//template class _ResPoolManage:public ObjectManage /** * 资源池是Pool/ResManage两个模板的组合应用 From c26c55f0be69202710a35864fdfac5bfff2dfc6b Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 14 Jul 2023 23:35:41 +0800 Subject: [PATCH 04/10] moved codes to .h from .cpp and then deleted ObjectManage.cpp --- inc/hgl/type/ObjectManage.cpp | 162 ------------------------------- inc/hgl/type/ObjectManage.h | 177 ++++++++++++++++++++++++++++++---- 2 files changed, 159 insertions(+), 180 deletions(-) delete mode 100644 inc/hgl/type/ObjectManage.cpp diff --git a/inc/hgl/type/ObjectManage.cpp b/inc/hgl/type/ObjectManage.cpp deleted file mode 100644 index 7e4b255..0000000 --- a/inc/hgl/type/ObjectManage.cpp +++ /dev/null @@ -1,162 +0,0 @@ -#ifndef HGL_OBJECT_MANAGE_CPP -#define HGL_OBJECT_MANAGE_CPP - -#include -namespace hgl -{ - template - ObjectManage::~ObjectManage() - { - Clear(); - } - - template - void ObjectManage::Clear() - { - int n=items.GetCount(); - - while(n--) - { - ResItem *obj=items.GetItem(n); - - Clear(obj->value); - } - - items.Clear(); - } - - template - void ObjectManage::ClearFree() - { - int n=items.GetCount(); - - while(n--) - { - ResItem *obj=items.GetItem(n); - - if(obj->ref_count<=0) - { - Clear(obj->value); - items.DeleteBySerial(n); - } - } - } - - template - bool ObjectManage::Add(const K &flag,V *obj) - { - if(!obj)return(false); - - if(items.KeyExist(flag)) - return(false); - - items.Add(flag,obj); - return(true); - } - - template - V *ObjectManage::Find(const K &flag) - { - int index=items.Find(flag); - - if(index==-1) - return(nullptr); - - ResItem *obj=items.GetItem(index); - - return obj->value; - } - - template - V *ObjectManage::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 - bool ObjectManage::ValueExist(V *value) - { - return(items.FindByValue(value)!=-1); - } - - /** - * 获取指定数据的Key和引用计数 - * @param value 数据 - * @param key Key存放地址 - * @param ref_count 引用计数存放地址 - * @param 是否增加引用计数 - */ - template - bool ObjectManage::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 - int ObjectManage::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 - int ObjectManage::Release(const K &flag,bool zero_clear) - { - return ReleaseBySerial(items.Find(flag),zero_clear); - } - - template - int ObjectManage::Release(V *td,bool zero_clear) - { - return ReleaseBySerial(items.FindByValue(td),zero_clear); - } -}//namespace hgl -#endif//HGL_OBJECT_MANAGE_CPP diff --git a/inc/hgl/type/ObjectManage.h b/inc/hgl/type/ObjectManage.h index cc77782..c684d36 100644 --- a/inc/hgl/type/ObjectManage.h +++ b/inc/hgl/type/ObjectManage.h @@ -3,7 +3,7 @@ #include namespace hgl { - template struct RefKeyValue:public KeyValue ///<带引用计数的Key/value数据结构 + template struct RefKeyValue:public KeyValue ///<带引用计数的Key/value对象结构 { int ref_count; ///<引用计数 @@ -16,7 +16,7 @@ namespace hgl }; /** - * 对象管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载 + * 对象管理器,它没有缓冲管理,仅仅是统一管理对象,并保证不会被重复加载 */ template class ObjectManage { @@ -26,34 +26,177 @@ namespace hgl _Map items; - int ReleaseBySerial(int,bool); - protected: virtual void Clear(V *obj){delete obj;} ///<对象释放虚拟函数(缺省为直接delete对象) public: - virtual ~ObjectManage(); + virtual ~ObjectManage() + { + Clear(); + } - virtual void Clear(); ///<清除所有数据 - virtual void ClearFree(); ///<清除所有引用计数为0的数据 + virtual void Clear() ///<清除所有对象 + { + int n=items.GetCount(); - const int GetCount()const{return items.GetCount();} ///<取得数据数量 + while(n--) + { + ResItem *obj=items.GetItem(n); - virtual bool Add(const K &,V *); ///<添加一个数据 - virtual V * Find(const K &); ///<查找一个数据(不增加引用计数) - virtual V * Get(const K &); ///<取得一个数据(增加引用计数) + Clear(obj->value); + } - virtual bool ValueExist(V *); ///<确认这个数据是否存在 - virtual bool GetKeyByValue(V *,K *,uint *,bool inc_ref=false); ///<取得一个数据的Key和引用次数 + items.Clear(); + } - virtual int Release(const K &,bool zero_clear=false); ///<释放一个数据 - virtual int Release(V *,bool zero_clear=false); ///<释放一个数据 + virtual void ClearFree() ///<清除所有引用计数为0的对象 + { + int n=items.GetCount(); + + while(n--) + { + ResItem *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); + + ResItem *obj=items.GetItem(index); + + return obj->value; + } + + virtual V * Get(const K &key) ///<取得一个对象(增加引用计数) + { + int index=items.Find(key); + + if(index!=-1) + { + ResItem *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); + + 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); + } + + /** + * 根据序列号释放一个对象(减少引用计数). + * + * \param index + * \param zero_clear 引用为0后是否清除对象 + * \return + */ + virtual int ReleaseBySerial(int index,bool zero_clear=false) + { + 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; + } + + /** + * 根据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 class ObjectManage /** - * 使用整型数据类做数标识的对象管理器 + * 使用整型对象类做数标识的对象管理器 */ template class IDObjectManage:public ObjectManage { @@ -86,5 +229,3 @@ namespace hgl template using U32ObjectManage=IDObjectManage; template using U64ObjectManage=IDObjectManage; }//namespace hgl - -#include From 404acf8f582f747f3e203997c1bae2b8da5423f0 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 14 Jul 2023 23:40:28 +0800 Subject: [PATCH 05/10] rename to KVObject instead of ResItem --- inc/hgl/type/ObjectManage.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/inc/hgl/type/ObjectManage.h b/inc/hgl/type/ObjectManage.h index c684d36..63d570d 100644 --- a/inc/hgl/type/ObjectManage.h +++ b/inc/hgl/type/ObjectManage.h @@ -20,11 +20,14 @@ namespace hgl */ template class ObjectManage { + public: + + using KVObject=RefKeyValue; + protected: - using ResItem=RefKeyValue; - _Map items; + _Map items; protected: @@ -43,7 +46,7 @@ namespace hgl while(n--) { - ResItem *obj=items.GetItem(n); + KVObject *obj=items.GetItem(n); Clear(obj->value); } @@ -57,7 +60,7 @@ namespace hgl while(n--) { - ResItem *obj=items.GetItem(n); + KVObject *obj=items.GetItem(n); if(obj->ref_count<=0) { @@ -87,7 +90,7 @@ namespace hgl if(index==-1) return(nullptr); - ResItem *obj=items.GetItem(index); + KVObject *obj=items.GetItem(index); return obj->value; } @@ -98,7 +101,7 @@ namespace hgl if(index!=-1) { - ResItem *obj=items.GetItem(index); + KVObject *obj=items.GetItem(index); ++obj->ref_count; @@ -126,7 +129,7 @@ namespace hgl if(index==-1)return(false); - ResItem *obj=items.GetItem(index); + KVObject *obj=items.GetItem(index); if(inc_ref_count) ++obj->ref_count; @@ -155,7 +158,7 @@ namespace hgl return(-1); } - ResItem *obj=items.GetItem(index); + KVObject *obj=items.GetItem(index); --obj->ref_count; From c722d40049dc6a0907c3b2310622c36a55436ec3 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 15 Jul 2023 00:03:48 +0800 Subject: [PATCH 06/10] moved to "io" folder from "type" --- inc/hgl/{type => io}/LoadString.h | 0 inc/hgl/{type => io}/LoadStringList.h | 0 inc/hgl/{type => io}/SaveStringList.h | 0 src/Text/LoadString.cpp | 2 +- src/Text/LoadStringList.cpp | 4 ++-- 5 files changed, 3 insertions(+), 3 deletions(-) rename inc/hgl/{type => io}/LoadString.h (100%) rename inc/hgl/{type => io}/LoadStringList.h (100%) rename inc/hgl/{type => io}/SaveStringList.h (100%) diff --git a/inc/hgl/type/LoadString.h b/inc/hgl/io/LoadString.h similarity index 100% rename from inc/hgl/type/LoadString.h rename to inc/hgl/io/LoadString.h diff --git a/inc/hgl/type/LoadStringList.h b/inc/hgl/io/LoadStringList.h similarity index 100% rename from inc/hgl/type/LoadStringList.h rename to inc/hgl/io/LoadStringList.h diff --git a/inc/hgl/type/SaveStringList.h b/inc/hgl/io/SaveStringList.h similarity index 100% rename from inc/hgl/type/SaveStringList.h rename to inc/hgl/io/SaveStringList.h diff --git a/src/Text/LoadString.cpp b/src/Text/LoadString.cpp index f4d7194..0b0b26a 100644 --- a/src/Text/LoadString.cpp +++ b/src/Text/LoadString.cpp @@ -1,4 +1,4 @@ -#include +#include #include namespace hgl { diff --git a/src/Text/LoadStringList.cpp b/src/Text/LoadStringList.cpp index 9a5bfac..b90fdcf 100644 --- a/src/Text/LoadStringList.cpp +++ b/src/Text/LoadStringList.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include From 11ef085d9707f287822d5c23438b0f05bd095345 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 15 Jul 2023 00:06:30 +0800 Subject: [PATCH 07/10] Created LoadMemBlock/SaveMemBlock in "inc/hgl/io", their code comes from MemBlock.h --- inc/hgl/io/LoadMemBlock.h | 25 ++++++++++++++++++++ inc/hgl/io/SaveMemBlock.h | 18 +++++++++++++++ inc/hgl/type/MemBlock.h | 40 +++++--------------------------- src/CMakeLists.txt | 48 ++++++++++++++++++++++----------------- 4 files changed, 76 insertions(+), 55 deletions(-) create mode 100644 inc/hgl/io/LoadMemBlock.h create mode 100644 inc/hgl/io/SaveMemBlock.h diff --git a/inc/hgl/io/LoadMemBlock.h b/inc/hgl/io/LoadMemBlock.h new file mode 100644 index 0000000..fa587fa --- /dev/null +++ b/inc/hgl/io/LoadMemBlock.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +namespace hgl +{ + /** + * 加载一个文件到内存块类中 + */ + template MemBlock *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 *mb=new MemBlock(size); + + fis.Read(mb->data(),file_size); + + return(mb); + } +}//namespace hgl diff --git a/inc/hgl/io/SaveMemBlock.h b/inc/hgl/io/SaveMemBlock.h new file mode 100644 index 0000000..36a7a34 --- /dev/null +++ b/inc/hgl/io/SaveMemBlock.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +namespace hgl +{ + /** + * 保存一个内存块到文件 + */ + template bool SaveMemBlockToFile(const OSString &filename,const MemBlock &mb) + { + const size_t size=mb.bytes(); + + if(size<=0)return(true); + + return(hgl::filesystem::SaveMemoryToFile(filename,mb.data(),mb.bytes())==size); + } +}//namespace hgl diff --git a/inc/hgl/type/MemBlock.h b/inc/hgl/type/MemBlock.h index 5dea4b6..ba93a77 100644 --- a/inc/hgl/type/MemBlock.h +++ b/inc/hgl/type/MemBlock.h @@ -2,9 +2,6 @@ #define HGL_MEM_BLOCK_INCLUDE #include -#include -#include -#include namespace hgl { /** @@ -25,6 +22,12 @@ namespace hgl const size_t GetBytes ()const{return cur_size*sizeof(T);} ///<取得内存块字节数 const size_t GetMaxBytes ()const{return buf_size*sizeof(T);} ///<取得内存块最大字节数 + T * begin (){return buf;} ///<取得内存块起始指针 + T * end (){return buf+cur_size;} ///<取得内存块结束地址指针 + + const T * begin ()const{return buf;} ///<取得内存块起始 + const T * end ()const{return buf+cur_size;} ///<取得内存块结束地址指针 + /** * 分配指定空间出来,供未来使用 */ @@ -173,36 +176,5 @@ namespace hgl return buf[n]; } };//template class MemBlock - - /** - * 加载一个文件到内存块类中 - */ - template MemBlock *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 *mb=new MemBlock(size); - - fis.Read(mb->data(),file_size); - - return(mb); - } - - /** - * 保存一个内存块到文件 - */ - template bool SaveMemBlockToFile(const OSString &filename,const MemBlock &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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2e1e7d0..46bc364 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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_MEM_BLOCK_FILES ${IO_INCLUDE_PATH}/LoadMemBlock.h + ${IO_INCLUDE_PATH}/SaveMemBlock.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,19 +137,23 @@ 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\\MemBlock" FILES ${IO_MEM_BLOCK_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_MEM_BLOCK_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) From 96c89b000f7920960eab021358cbc6c67e97b200 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 15 Jul 2023 00:27:06 +0800 Subject: [PATCH 08/10] renamed to DataArray instead of MemBlock --- .../io/{LoadMemBlock.h => LoadDataArray.h} | 8 +- .../io/{SaveMemBlock.h => SaveDataArray.h} | 6 +- inc/hgl/type/DataArray.h | 245 ++++++++++++++++++ inc/hgl/type/MemBlock.h | 180 ------------- src/CMakeLists.txt | 24 +- 5 files changed, 264 insertions(+), 199 deletions(-) rename inc/hgl/io/{LoadMemBlock.h => LoadDataArray.h} (63%) rename inc/hgl/io/{SaveMemBlock.h => SaveDataArray.h} (60%) create mode 100644 inc/hgl/type/DataArray.h delete mode 100644 inc/hgl/type/MemBlock.h diff --git a/inc/hgl/io/LoadMemBlock.h b/inc/hgl/io/LoadDataArray.h similarity index 63% rename from inc/hgl/io/LoadMemBlock.h rename to inc/hgl/io/LoadDataArray.h index fa587fa..12009c6 100644 --- a/inc/hgl/io/LoadMemBlock.h +++ b/inc/hgl/io/LoadDataArray.h @@ -1,13 +1,13 @@ #pragma once -#include +#include #include namespace hgl { /** - * 加载一个文件到内存块类中 + * 加载一个文件到数据阵列中 */ - template MemBlock *LoadFileToMemBlock(const OSString &filename) + template static DataArray *LoadFileToDataArray(const OSString &filename) { io::FileInputStream fis; @@ -16,7 +16,7 @@ namespace hgl const size_t file_size =fis.GetSize(); const size_t size =(file_size+sizeof(T)-1)/sizeof(T); - MemBlock *mb=new MemBlock(size); + DataArray *mb=new DataArray(size); fis.Read(mb->data(),file_size); diff --git a/inc/hgl/io/SaveMemBlock.h b/inc/hgl/io/SaveDataArray.h similarity index 60% rename from inc/hgl/io/SaveMemBlock.h rename to inc/hgl/io/SaveDataArray.h index 36a7a34..0cd1a88 100644 --- a/inc/hgl/io/SaveMemBlock.h +++ b/inc/hgl/io/SaveDataArray.h @@ -1,13 +1,13 @@ #pragma once -#include +#include #include namespace hgl { /** - * 保存一个内存块到文件 + * 保存一个数据阵列到文件 */ - template bool SaveMemBlockToFile(const OSString &filename,const MemBlock &mb) + template static bool SaveDataArrayToFile(const OSString &filename,const DataArray &mb) { const size_t size=mb.bytes(); diff --git a/inc/hgl/type/DataArray.h b/inc/hgl/type/DataArray.h new file mode 100644 index 0000000..01e88ee --- /dev/null +++ b/inc/hgl/type/DataArray.h @@ -0,0 +1,245 @@ +#pragma once + +#include +namespace hgl +{ + /** + * 数据阵列 + */ + template 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);} ///<取得阵列已分配空间字节数 + + T * GetData()const{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&&index0&&index>=0&&index=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()const + { + return items; + } + + T &operator[](int n) + { + return items[n]; + } + + const T &operator[](int n)const + { + return items[n]; + } + };//template class DataArray +}//namespace hgl diff --git a/inc/hgl/type/MemBlock.h b/inc/hgl/type/MemBlock.h deleted file mode 100644 index ba93a77..0000000 --- a/inc/hgl/type/MemBlock.h +++ /dev/null @@ -1,180 +0,0 @@ -#ifndef HGL_MEM_BLOCK_INCLUDE -#define HGL_MEM_BLOCK_INCLUDE - -#include -namespace hgl -{ - /** - * 内存块模版 - */ - template 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);} ///<取得内存块最大字节数 - - T * begin (){return buf;} ///<取得内存块起始指针 - T * end (){return buf+cur_size;} ///<取得内存块结束地址指针 - - const T * begin ()const{return buf;} ///<取得内存块起始 - const T * end ()const{return buf+cur_size;} ///<取得内存块结束地址指针 - - /** - * 分配指定空间出来,供未来使用 - */ - 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 class MemBlock -}//namespace hgl -#endif//HGL_MEM_BLOCK_INCLUDE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 46bc364..2a22fa1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -105,8 +105,8 @@ SET(IO_DATA_FILES ${IO_INCLUDE_PATH}/DataInputStream.h SET(IO_MEMORY_STREAM_FILES ${IO_INCLUDE_PATH}/MemoryInputStream.h ${IO_INCLUDE_PATH}/MemoryOutputStream.h) -SET(IO_MEM_BLOCK_FILES ${IO_INCLUDE_PATH}/LoadMemBlock.h - ${IO_INCLUDE_PATH}/SaveMemBlock.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 @@ -139,22 +139,22 @@ SOURCE_GROUP("IO\\Event" FILES ${INPUT_EVENT_FILES}) SOURCE_GROUP("IO\\Base" FILES ${IO_BASE_FILES}) SOURCE_GROUP("IO\\DataIOStream" FILES ${IO_DATA_FILES}) -SOURCE_GROUP("IO\\MemBlock" FILES ${IO_MEM_BLOCK_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_MEM_BLOCK_FILES} - ${IO_MEMORY_STREAM_FILES} - ${IO_FILE_FILES} - ${IO_JAVA_FILES} - ${IO_TEXT_FILES} - ${IO_STRING_LIST_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) From ef36b4af21455c00759cf1eb66c6b0df25a5d7bc Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 15 Jul 2023 00:45:24 +0800 Subject: [PATCH 09/10] added data() in DataArray<> --- inc/hgl/type/DataArray.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/hgl/type/DataArray.h b/inc/hgl/type/DataArray.h index 01e88ee..4e1b05c 100644 --- a/inc/hgl/type/DataArray.h +++ b/inc/hgl/type/DataArray.h @@ -21,7 +21,8 @@ namespace hgl const size_t GetBytes ()const{return count*sizeof(T);} ///<取得阵列已使用的字节数 const size_t GetAllocBytes()const{return alloc_count*sizeof(T);} ///<取得阵列已分配空间字节数 - T * GetData()const{return items;} + const T * GetData()const{return items;} + T * data() {return items;} public: From 8554f5414cbbe7e6aededd71173121ae49f1a592 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 15 Jul 2023 00:45:46 +0800 Subject: [PATCH 10/10] updated include --- inc/hgl/thread/Loader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/hgl/thread/Loader.h b/inc/hgl/thread/Loader.h index 188c545..8dfbaff 100644 --- a/inc/hgl/thread/Loader.h +++ b/inc/hgl/thread/Loader.h @@ -2,7 +2,7 @@ #define HGL_LOADER_INCLUDE #include -#include +#include #include #include namespace hgl