diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index b35e4695..99ee7d69 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -9,7 +9,7 @@ constexpr uint32_t SCREEN_HEIGHT=720; struct WorldConfig { - Matrix4f mvp; + Matrix4f mvp; }world; constexpr uint32_t VERTEX_COUNT=3; @@ -29,12 +29,12 @@ constexpr float color_data[VERTEX_COUNT][3]= class TestApp:public VulkanApplicationFramework { -private: +private: uint swap_chain_count=0; vulkan::Material * material =nullptr; - vulkan::Renderable * render_obj =nullptr; + vulkan::Renderable * render_obj =nullptr; vulkan::Buffer * ubo_mvp =nullptr; vulkan::Pipeline * pipeline =nullptr; @@ -84,7 +84,7 @@ private: } void InitVBO() - { + { vertex_buffer =device->CreateVBO(FMT_RG32F, VERTEX_COUNT,vertex_data); color_buffer =device->CreateVBO(FMT_RGB32F, VERTEX_COUNT,color_data); @@ -135,7 +135,7 @@ private: public: - bool Init() + bool Init() { if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) return(false); @@ -182,6 +182,6 @@ int main(int,char **) return(-1); while(app.Run()); - + return 0; } diff --git a/inc/hgl/CodePage.h b/inc/hgl/CodePage.h index ef366f78..c4deb9fb 100644 --- a/inc/hgl/CodePage.h +++ b/inc/hgl/CodePage.h @@ -6,222 +6,222 @@ #include namespace hgl { - struct CodePageAndCharSet - { - CharCodePage codepage; - CharSetName charset; - }; + struct CodePageAndCharSet + { + CharCodePage codepage; + CharSetName charset; + }; - constexpr struct CodePageAndCharSet CodePage2CharSet[]= - { - {ccpNone, "us-ascii" }, + constexpr struct CodePageAndCharSet CodePage2CharSet[]= + { + {ccpNone, "us-ascii" }, - {ccpGBK, "gbk" }, - {ccpBig5, "big5" }, - {ccpGB2312, "gb2312" }, - {ccpGB18030, "gb18030" }, + {ccpGBK, "gbk" }, + {ccpBig5, "big5" }, + {ccpGB2312, "gb2312" }, + {ccpGB18030, "gb18030" }, - {ccpShiftJIS, "shift-jis" }, - {ccpJISX, "iso-2022-jp" }, + {ccpShiftJIS, "shift-jis" }, + {ccpJISX, "iso-2022-jp" }, - {ccpKorean, "ks_c_5601-1987"}, + {ccpKorean, "ks_c_5601-1987"}, - {ccpMacJanpan, "x-mac-japanese" }, - {ccpMacTraditionalChinese, "x-mac-chinesetrad" }, - {ccpMacSimplifiedChinese, "x-mac-chinesesimp" }, + {ccpMacJanpan, "x-mac-japanese" }, + {ccpMacTraditionalChinese, "x-mac-chinesetrad" }, + {ccpMacSimplifiedChinese, "x-mac-chinesesimp" }, - {ccpUTF7, "utf-7" }, - {ccpUTF8, "utf-8" }, + {ccpUTF7, "utf-7" }, + {ccpUTF8, "utf-8" }, - {ccpUTF16LE, "utf-16le" }, - {ccpUTF16BE, "utf-16be" }, - {ccpUTF32LE, "utf-32le" }, - {ccpUTF32BE, "utf-32be" }, - };//const struct + {ccpUTF16LE, "utf-16le" }, + {ccpUTF16BE, "utf-16be" }, + {ccpUTF32LE, "utf-32le" }, + {ccpUTF32BE, "utf-32be" }, + };//const struct - constexpr int CharSetCount=sizeof(CodePage2CharSet)/sizeof(CodePageAndCharSet); + constexpr int CharSetCount=sizeof(CodePage2CharSet)/sizeof(CodePageAndCharSet); - inline const char *FindCharSet(CharCodePage ccp) - { - for(int i=0;i(str)+1,len); - } + inline char * u16_to_u8(const u16char *str) + { + int len; + return u16_to_u8(str,hgl::strlen(str)+1,len); + } - inline u16char *u8_to_u16(const char *str) - { - int len; - return u8_to_u16(str,hgl::strlen(str)+1,len); - } + inline u16char *u8_to_u16(const char *str) + { + int len; + return u8_to_u16(str,hgl::strlen(str)+1,len); + } - inline UTF16String to_u16(const char *u8_str,int length) - { - int wlen; - u16char *ws=u8_to_u16(u8_str,length,wlen); + inline UTF16String to_u16(const char *u8_str,int length) + { + int wlen; + u16char *ws=u8_to_u16(u8_str,length,wlen); - return UTF16String(ws,wlen,true); - } + return UTF16String(ws,wlen,true); + } - inline UTF16String to_u16(const UTF8String &u8str) - { - return to_u16(u8str.c_str(),u8str.Length()); - } + inline UTF16String to_u16(const UTF8String &u8str) + { + return to_u16(u8str.c_str(),u8str.Length()); + } - inline UTF16String to_u16(const char *str) - { - int wlen; + inline UTF16String to_u16(const char *str) + { + int wlen; - u16char *ws=u8_to_u16(str,strlen(str),wlen); + u16char *ws=u8_to_u16(str,strlen(str),wlen); - return UTF16String(ws,wlen,true); - } + return UTF16String(ws,wlen,true); + } - inline UTF8String to_u8(const u16char *wide_str,int length) - { - int ulen; + inline UTF8String to_u8(const u16char *wide_str,int length) + { + int ulen; - char *us=u16_to_u8(wide_str,length,ulen); + char *us=u16_to_u8(wide_str,length,ulen); - return UTF8String(us,ulen,true); - } + return UTF8String(us,ulen,true); + } - inline UTF8String to_u8(const UTF16String &ws) - { - return to_u8(ws.c_str(),ws.Length()); - } + inline UTF8String to_u8(const UTF16String &ws) + { + return to_u8(ws.c_str(),ws.Length()); + } #if HGL_OS == HGL_OS_Windows inline OSString ToOSString(const char *str){return to_u16(str);} diff --git a/inc/hgl/CompOperator.h b/inc/hgl/CompOperator.h index 78ebf6a5..5d4dd831 100644 --- a/inc/hgl/CompOperator.h +++ b/inc/hgl/CompOperator.h @@ -3,14 +3,14 @@ namespace hgl { - #define CompOperator(name,compfunc) const bool operator > (name i)const {return compfunc(i)>0;} \ - const bool operator < (name i)const {return compfunc(i)<0;} \ - const bool operator >=(name i)const {return compfunc(i)>=0;}\ - const bool operator <=(name i)const {return compfunc(i)<=0;}\ - const bool operator ==(name i)const {return compfunc(i)==0;}\ - const bool operator !=(name i)const {return compfunc(i)!=0;} + #define CompOperator(name,compfunc) const bool operator > (name i)const {return compfunc(i)>0;} \ + const bool operator < (name i)const {return compfunc(i)<0;} \ + const bool operator >=(name i)const {return compfunc(i)>=0;}\ + const bool operator <=(name i)const {return compfunc(i)<=0;}\ + const bool operator ==(name i)const {return compfunc(i)==0;}\ + const bool operator !=(name i)const {return compfunc(i)!=0;} - #define CompOperatorMemcmp(name) int _Comp(name data)const{return memcmp(this,&data,sizeof(name));} \ - CompOperator(name,_Comp) + #define CompOperatorMemcmp(name) int _Comp(name data)const{return memcmp(this,&data,sizeof(name));} \ + CompOperator(name,_Comp) }//namespace hgl #endif//HGL_COMP_OPERATOR_INCLUDE diff --git a/inc/hgl/LogInfo.h b/inc/hgl/LogInfo.h index 3099136e..71ac103a 100644 --- a/inc/hgl/LogInfo.h +++ b/inc/hgl/LogInfo.h @@ -18,42 +18,42 @@ namespace hgl { enum LogLevel { - llError=0, //错误,肯定出对话框 - llProblem, //问题,默认出对话框 - llHint, //提示,不重要,debug状态默认出对话框 - llLog //记录一下 + llError=0, //错误,肯定出对话框 + llProblem, //问题,默认出对话框 + llHint, //提示,不重要,debug状态默认出对话框 + llLog //记录一下 };//enum LogLevel - inline void Log(LogLevel ll,const UTF16String &str) + inline void Log(LogLevel ll,const UTF16String &str) { std::wcout<>LogFrom(\"")+to_u16(filename)+U16_TEXT("\", ")+UTF16String(line)+U16_TEXT(" line,func:\"")+to_u16(funcname)+U16_TEXT("\")")); } - inline void DebugLog(LogLevel ll,const UTF8String &str,const char *filename,int line,const char *funcname) + inline void DebugLog(LogLevel ll,const UTF8String &str,const char *filename,int line,const char *funcname) { Log(ll,str+U8_TEXT(">>LogFrom(\"")+UTF8String(filename)+U8_TEXT("\", ")+UTF8String(line)+U8_TEXT(" line,func:\"")+UTF8String(funcname)+U8_TEXT("\")")); } - #define LOG_INFO(str) {Log(llLog, str);} - #define LOG_HINT(str) {Log(llHint, str);} - #define LOG_PROBLEM(str) {Log(llProblem, str);} - #define LOG_ERROR(str) {Log(llError, str);} + #define LOG_INFO(str) {Log(llLog, str);} + #define LOG_HINT(str) {Log(llHint, str);} + #define LOG_PROBLEM(str) {Log(llProblem, str);} + #define LOG_ERROR(str) {Log(llError, str);} - #define RETURN_FALSE {DebugLog(llLog,OS_TEXT("return(false)" ),__FILE__,__LINE__,__HGL_FUNC__);return(false);} - #define RETURN_ERROR(v) {DebugLog(llLog,OS_TEXT("return error(")+OSString(v)+OS_TEXT(")"),__FILE__,__LINE__,__HGL_FUNC__);return(v);} - #define RETURN_ERROR_NULL {DebugLog(llLog,OS_TEXT("return error(nullptr)" ),__FILE__,__LINE__,__HGL_FUNC__);return(nullptr);} + #define RETURN_FALSE {DebugLog(llLog,OS_TEXT("return(false)" ),__FILE__,__LINE__,__HGL_FUNC__);return(false);} + #define RETURN_ERROR(v) {DebugLog(llLog,OS_TEXT("return error(")+OSString(v)+OS_TEXT(")"),__FILE__,__LINE__,__HGL_FUNC__);return(v);} + #define RETURN_ERROR_NULL {DebugLog(llLog,OS_TEXT("return error(nullptr)" ),__FILE__,__LINE__,__HGL_FUNC__);return(nullptr);} - #define RETURN_BOOL(proc) {if(proc)return(true);RETURN_FALSE} + #define RETURN_BOOL(proc) {if(proc)return(true);RETURN_FALSE} #define IF_FALSE_RETURN(str) if(!str)RETURN_FALSE; }//namespace logger diff --git a/inc/hgl/Logger.h b/inc/hgl/Logger.h index 4897b7e2..aa1283d7 100644 --- a/inc/hgl/Logger.h +++ b/inc/hgl/Logger.h @@ -8,10 +8,10 @@ namespace hgl { enum LogLevel { - llError=0, //错误,肯定出对话框 - llProblem, //问题,默认出对话框 - llHint, //提示,不重要,debug状态默认出对话框 - llLog //记录一下 + llError=0, //错误,肯定出对话框 + llProblem, //问题,默认出对话框 + llHint, //提示,不重要,debug状态默认出对话框 + llLog //记录一下 };//enum LogLevel /** @@ -21,7 +21,7 @@ namespace hgl { protected: - LogLevel min_level; ///<最小输出级别 + LogLevel min_level; ///<最小输出级别 UTF16String project_code; public: @@ -29,12 +29,12 @@ namespace hgl Logger(LogLevel l){min_level=l;} virtual ~Logger()=default; - const LogLevel GetLevel()const{return min_level;} ///<取得最小输出级别 + const LogLevel GetLevel()const{return min_level;} ///<取得最小输出级别 - virtual void Close()=0; ///<关闭日志 + virtual void Close()=0; ///<关闭日志 - virtual void Write(const u16char *,int)=0; ///<输出一行u16char日志 - virtual void Write(const char *,int)=0; ///<输出一行char(utf8)日志 + virtual void Write(const u16char *,int)=0; ///<输出一行u16char日志 + virtual void Write(const char *,int)=0; ///<输出一行char(utf8)日志 };//class Logger }//namespace logger diff --git a/inc/hgl/Macro.h b/inc/hgl/Macro.h index 5c8eb327..4e954ff9 100644 --- a/inc/hgl/Macro.h +++ b/inc/hgl/Macro.h @@ -3,118 +3,118 @@ namespace hgl { - #define RETURN_OBJECT_OF_ARRAY(array,index,max_count) return (index<0||index>=max_count)?nullptr:array[index]; + #define RETURN_OBJECT_OF_ARRAY(array,index,max_count) return (index<0||index>=max_count)?nullptr:array[index]; - #define NEW_NULL_ARRAY(name,type,count) { \ - name=new type[count]; \ - \ - memset(name,0,sizeof(type)*count); \ - } + #define NEW_NULL_ARRAY(name,type,count) { \ + name=new type[count]; \ + \ + memset(name,0,sizeof(type)*count); \ + } - #define SAFE_CLEAR(name) { \ - if(name) \ - { \ - delete name; \ - name=nullptr; \ - } \ - } + #define SAFE_CLEAR(name) { \ + if(name) \ + { \ + delete name; \ + name=nullptr; \ + } \ + } - #define SAFE_CLEAR_ARRAY(name) { \ - if(name) \ - { \ - delete[] name; \ - name=nullptr; \ - } \ - } + #define SAFE_CLEAR_ARRAY(name) { \ + if(name) \ + { \ + delete[] name; \ + name=nullptr; \ + } \ + } - #define SAFE_CLEAR_OBJECT_ARRAY(name,num) { \ - if(name&&num>=0) \ - { \ - int safe_clear_object_array_number=num; \ - \ - while(safe_clear_object_array_number--) \ - if(name[safe_clear_object_array_number]) \ - delete name[safe_clear_object_array_number]; \ - \ - delete[] name; \ - name=nullptr; \ - } \ - } + #define SAFE_CLEAR_OBJECT_ARRAY(name,num) { \ + if(name&&num>=0) \ + { \ + int safe_clear_object_array_number=num; \ + \ + while(safe_clear_object_array_number--) \ + if(name[safe_clear_object_array_number]) \ + delete name[safe_clear_object_array_number]; \ + \ + delete[] name; \ + name=nullptr; \ + } \ + } - #define FREE_OBJECT_ARRAY(name,num) { \ - if(name&&num>=0) \ - { \ - int free_object_array_number=num; \ - \ - while(free_object_array_number--) \ - if(name[free_object_array_number]) \ - delete name[free_object_array_number]; \ - \ - hgl_free(name); \ - name=nullptr; \ - } \ - } - - #define SAFE_FREE_OBJECT_ARRAY(name,num) { \ - if(name) \ - FREE_OBJECT_ARRAY(name,num); \ - } - - #define SAFE_FREE(name) { \ - if(name) \ - hgl_free(name); \ + #define FREE_OBJECT_ARRAY(name,num) { \ + if(name&&num>=0) \ + { \ + int free_object_array_number=num; \ + \ + while(free_object_array_number--) \ + if(name[free_object_array_number]) \ + delete name[free_object_array_number]; \ + \ + hgl_free(name); \ + name=nullptr; \ + } \ } - #define SAFE_RECREATE(name,code) { \ - if(name) \ - delete name; \ - \ - name=code; \ - } + #define SAFE_FREE_OBJECT_ARRAY(name,num) { \ + if(name) \ + FREE_OBJECT_ARRAY(name,num); \ + } - #define ARRAY_CALL(name,num,code) { \ - int array_call_number=num; \ - \ - while(array_call_number--) \ - name[array_call_number]->code; \ - } + #define SAFE_FREE(name) { \ + if(name) \ + hgl_free(name); \ + } - #define LOAD_FUNC(type,func) type func(void *buf,int buf_size) \ - { \ - if(!buf||buf_size<=0)return 0; \ - \ - MemoryInputStream ms(buf,buf_size); \ - \ - return(func(&ms)); \ - } \ - \ - type func(const UTF16String &filename) \ - { \ - FileInputStream fs; \ - \ - if(fs.Open(filename)) \ - return(func(&fs)); \ - else \ - return 0; \ - } + #define SAFE_RECREATE(name,code) { \ + if(name) \ + delete name; \ + \ + name=code; \ + } - #define SAVE_FUNC(type,func) bool func(type data,void *buf,int buf_size) \ - { \ - if(!buf||buf_size<=0)return(false); \ - \ - MemoryOutputStream ms(buf,buf_size); \ - \ - return(func(data,&ms)); \ - } \ - \ - bool func(type data,const UTF16String &filename) \ - { \ - FileOutputStream fs; \ - \ - if(fs.CreateTrunc(filename)) \ - return(func(data,&fs)); \ - else \ - return(false); \ - } + #define ARRAY_CALL(name,num,code) { \ + int array_call_number=num; \ + \ + while(array_call_number--) \ + name[array_call_number]->code; \ + } + + #define LOAD_FUNC(type,func) type func(void *buf,int buf_size) \ + { \ + if(!buf||buf_size<=0)return 0; \ + \ + MemoryInputStream ms(buf,buf_size); \ + \ + return(func(&ms)); \ + } \ + \ + type func(const UTF16String &filename) \ + { \ + FileInputStream fs; \ + \ + if(fs.Open(filename)) \ + return(func(&fs)); \ + else \ + return 0; \ + } + + #define SAVE_FUNC(type,func) bool func(type data,void *buf,int buf_size) \ + { \ + if(!buf||buf_size<=0)return(false); \ + \ + MemoryOutputStream ms(buf,buf_size); \ + \ + return(func(data,&ms)); \ + } \ + \ + bool func(type data,const UTF16String &filename) \ + { \ + FileOutputStream fs; \ + \ + if(fs.CreateTrunc(filename)) \ + return(func(data,&fs)); \ + else \ + return(false); \ + } }//namespace hgl #endif//HGL_MACRO_INCLUDE diff --git a/inc/hgl/Str.h b/inc/hgl/Str.h index d5fea49c..952e8325 100644 --- a/inc/hgl/Str.h +++ b/inc/hgl/Str.h @@ -4,42 +4,42 @@ #include namespace hgl { - // 32 空格 - // 33 ! - // 34 " - // 35 # - // 36 $ - // 37 % - // 38 & - // 39 ' - // 40 ( - // 41 ) - // 42 * - // 43 + - // 44 , - // 45 - - // 46 . - // 47 / - // 48 0123456789 - // 58 : - // 59 ; - // 60 < - // 61 = - // 62 > - // 63 ? - // 64 @ - // 65 ABCDEFGHIJKLMNOPQRSTUVWXYZ - // 91 [ - // 92 \ - // 93 ] - // 94 ^ - // 95 _ - // 96 ` - // 97 abcdefghijklmnopqrstuvwxyz - // 123 { - // 124 | - // 125 } - // 126 ~ + // 32 空格 + // 33 ! + // 34 " + // 35 # + // 36 $ + // 37 % + // 38 & + // 39 ' + // 40 ( + // 41 ) + // 42 * + // 43 + + // 44 , + // 45 - + // 46 . + // 47 / + // 48 0123456789 + // 58 : + // 59 ; + // 60 < + // 61 = + // 62 > + // 63 ? + // 64 @ + // 65 ABCDEFGHIJKLMNOPQRSTUVWXYZ + // 91 [ + // 92 \ + // 93 ] + // 94 ^ + // 95 _ + // 96 ` + // 97 abcdefghijklmnopqrstuvwxyz + // 123 { + // 124 | + // 125 } + // 126 ~ /** * 测试字符是否是emoji表情
@@ -497,7 +497,7 @@ namespace hgl while(*dst&&max_count) { - ++dst; //找到结束 + ++dst; //找到结束 --max_count; } @@ -1532,7 +1532,7 @@ namespace hgl return(true); } - ++str; //跳过小数点 + ++str; //跳过小数点 R pos=0.1f; @@ -1594,7 +1594,7 @@ namespace hgl return(true); } - ++str; //跳过小数点 + ++str; //跳过小数点 --size; R pos=0.1f; @@ -1667,10 +1667,10 @@ namespace hgl return(false); } - if(*str=='T'||*str=='t' //true/false - ||*str=='Y'||*str=='y' //yes/no - ||*str=='M'||*str=='m' //male/women - ||*str=='1') //1/0 + if(*str=='T'||*str=='t' //true/false + ||*str=='Y'||*str=='y' //yes/no + ||*str=='M'||*str=='m' //male/women + ||*str=='1') //1/0 { value=true; return(true); @@ -1870,11 +1870,11 @@ namespace hgl template T *ftos(T *str,int size,int fsize,F value) { - const long integer=(long)value; //整数部分 + const long integer=(long)value; //整数部分 T *p=str; - if(integer==0&&value<0) //如果为-0.xx这种,integer会为0所以不增加-号 + if(integer==0&&value<0) //如果为-0.xx这种,integer会为0所以不增加-号 *p++='-'; itos(p,size,integer); @@ -1886,7 +1886,7 @@ namespace hgl p=str+len; - value-=integer; //保留小数部分 + value-=integer; //保留小数部分 if(value<0) value=-value; @@ -1899,15 +1899,15 @@ namespace hgl return(str); } - *p++='.'; //加小数点 + *p++='.'; //加小数点 ++len; while(value>min_value&&len inline int parse_float_array(const T *str,I *result,int max_count,const T end_char=0,const T **end_pointer=0){return parse_number_array(str,result,max_count,end_char,end_pointer);} - template inline int parse_int_array (const T *str,I *result,int max_count,const T end_char=0,const T **end_pointer=0){return parse_number_array(str,result,max_count,end_char,end_pointer);} - template inline int parse_uint_array (const T *str,I *result,int max_count,const T end_char=0,const T **end_pointer=0){return parse_number_array(str,result,max_count,end_char,end_pointer);} - template inline int parse_xint_array (const T *str,I *result,int max_count,const T end_char=0,const T **end_pointer=0){return parse_number_array(str,result,max_count,end_char,end_pointer);} + template inline int parse_float_array(const T *str,I *result,int max_count,const T end_char=0,const T **end_pointer=0){return parse_number_array(str,result,max_count,end_char,end_pointer);} + template inline int parse_int_array (const T *str,I *result,int max_count,const T end_char=0,const T **end_pointer=0){return parse_number_array(str,result,max_count,end_char,end_pointer);} + template inline int parse_uint_array (const T *str,I *result,int max_count,const T end_char=0,const T **end_pointer=0){return parse_number_array(str,result,max_count,end_char,end_pointer);} + template inline int parse_xint_array (const T *str,I *result,int max_count,const T end_char=0,const T **end_pointer=0){return parse_number_array(str,result,max_count,end_char,end_pointer);} /** * 解析数值阵列字符串到数组,如"1,2,3"或"1 2 3" @@ -2099,10 +2099,10 @@ namespace hgl return(count); } - template inline int parse_float_array (const T *str,const int len,SET &result_list){return parse_number_array(str,len,result_list);} - template inline int parse_int_array (const T *str,const int len,SET &result_list){return parse_number_array(str,len,result_list);} - template inline int parse_uint_array (const T *str,const int len,SET &result_list){return parse_number_array(str,len,result_list);} - template inline int parse_xint_array (const T *str,const int len,SET &result_list){return parse_number_array(str,len,result_list);} + template inline int parse_float_array (const T *str,const int len,SET &result_list){return parse_number_array(str,len,result_list);} + template inline int parse_int_array (const T *str,const int len,SET &result_list){return parse_number_array(str,len,result_list);} + template inline int parse_uint_array (const T *str,const int len,SET &result_list){return parse_number_array(str,len,result_list);} + template inline int parse_xint_array (const T *str,const int len,SET &result_list){return parse_number_array(str,len,result_list);} /** * 按指定分隔符拆分字符串为多个字符串 @@ -2183,12 +2183,12 @@ namespace hgl { if(!str)return(false); - if((!isalpha(*str))&&(*str!='_')) //不是字母或下划线 + if((!isalpha(*str))&&(*str!='_')) //不是字母或下划线 return(false); ++str; - if(!(*str)) //不能仅一个字符 + if(!(*str)) //不能仅一个字符 return(false); while(*str) diff --git a/inc/hgl/Time.h b/inc/hgl/Time.h index a7983646..7b7655bf 100644 --- a/inc/hgl/Time.h +++ b/inc/hgl/Time.h @@ -5,19 +5,19 @@ namespace hgl //ʱ { - int GetTimeZone(); ///<ʱʱ(λ) + int GetTimeZone(); ///<ʱʱ(λ) - uint64 GetMilliStartTime(); ///<ȡúʱ(λ1/1000) - uint64 GetMicroStartTime(); ///<ȡ΢ʱ(λ1/1000000) - double GetDoubleStartTime(); ///<ȡʱ(λ) + uint64 GetMilliStartTime(); ///<ȡúʱ(λ1/1000) + uint64 GetMicroStartTime(); ///<ȡ΢ʱ(λ1/1000000) + double GetDoubleStartTime(); ///<ȡʱ(λ) - uint64 GetTime(); ///<ȡõǰʱ(λ1/1000) - uint64 GetMicroTime(); ///<ȡõǰʱ(λ1/1000000) - double GetDoubleTime(); ///<ȡõǰʱ(λ) + uint64 GetTime(); ///<ȡõǰʱ(λ1/1000) + uint64 GetMicroTime(); ///<ȡõǰʱ(λ1/1000000) + double GetDoubleTime(); ///<ȡõǰʱ(λ) - double GetLocalDoubleTime(); ///<ȡñصǰʱ(λ) + double GetLocalDoubleTime(); ///<ȡñصǰʱ(λ) - void WaitTime(double); ///<ȴһʱ(λ) + void WaitTime(double); ///<ȴһʱ(λ) }//namespace hgl #endif//HGL_TIME_INCLUDE diff --git a/inc/hgl/TypeFunc.h b/inc/hgl/TypeFunc.h index 27f61aa9..d925004b 100644 --- a/inc/hgl/TypeFunc.h +++ b/inc/hgl/TypeFunc.h @@ -5,11 +5,11 @@ #include namespace hgl { - #define HGL_CONVER_TO_MEM_ALIGN(x) ((((x)+HGL_MEM_ALIGN-1)/HGL_MEM_ALIGN)*HGL_MEM_ALIGN) //内存对齐转换宏 + #define HGL_CONVER_TO_MEM_ALIGN(x) ((((x)+HGL_MEM_ALIGN-1)/HGL_MEM_ALIGN)*HGL_MEM_ALIGN) //内存对齐转换宏 - #ifndef NULL - #define NULL 0 - #endif// + #ifndef NULL + #define NULL 0 + #endif// template inline T *zero_new(const int count) @@ -41,144 +41,144 @@ namespace hgl new (static_cast(obj)) T(); } - #define HGL_BIT(n) (1<<(n)) - #define HGL_64BIT(n) (1L<<(n)) + #define HGL_BIT(n) (1<<(n)) + #define HGL_64BIT(n) (1L<<(n)) - template - inline bool hgl_is_one(const T value,int off) - { - return value&(1< + inline bool hgl_is_one(const T value,int off) + { + return value&(1< - inline bool hgl_is_zero(const T value,int off) - { - return !(value&(1< + inline bool hgl_is_zero(const T value,int off) + { + return !(value&(1< - inline void hgl_set_one(T &value,int off) - { - value|=(1< + inline void hgl_set_one(T &value,int off) + { + value|=(1< - inline void hgl_set_zero(T &value,int off) - { - value&=~T(1< + inline void hgl_set_zero(T &value,int off) + { + value&=~T(1< - inline void hgl_set_bit(T &value,int off,bool b) - { - if(b) - hgl_set_one(value,off); - else - hgl_set_zero(value,off); - } + template + inline void hgl_set_bit(T &value,int off,bool b) + { + if(b) + hgl_set_one(value,off); + else + hgl_set_zero(value,off); + } - template - inline int hgl_bit_count(const T value) - { - int count=0; - T bit=1; + template + inline int hgl_bit_count(const T value) + { + int count=0; + T bit=1; - for(int i=0;i - inline int hgl_bit_count(const T value,int size) - { - int count=0; - T bit=1; + template + inline int hgl_bit_count(const T value,int size) + { + int count=0; + T bit=1; - for(int i=0;i T HGL_INTEGER_MAX(); - template T HGL_INTEGER_MIN(); + template T HGL_INTEGER_MAX(); + template T HGL_INTEGER_MIN(); - template<> inline uint8 HGL_INTEGER_MAX() { return HGL_U8_MAX; } - template<> inline uint16 HGL_INTEGER_MAX() { return HGL_U16_MAX; } - template<> inline uint32 HGL_INTEGER_MAX() { return HGL_U32_MAX; } - template<> inline uint64 HGL_INTEGER_MAX() { return HGL_U64_MAX; } + template<> inline uint8 HGL_INTEGER_MAX() { return HGL_U8_MAX; } + template<> inline uint16 HGL_INTEGER_MAX() { return HGL_U16_MAX; } + template<> inline uint32 HGL_INTEGER_MAX() { return HGL_U32_MAX; } + template<> inline uint64 HGL_INTEGER_MAX() { return HGL_U64_MAX; } - template<> inline int8 HGL_INTEGER_MAX() { return HGL_S8_MAX; } - template<> inline int16 HGL_INTEGER_MAX() { return HGL_S16_MAX; } - template<> inline int32 HGL_INTEGER_MAX() { return HGL_S32_MAX; } - template<> inline int64 HGL_INTEGER_MAX() { return HGL_S64_MAX; } + template<> inline int8 HGL_INTEGER_MAX() { return HGL_S8_MAX; } + template<> inline int16 HGL_INTEGER_MAX() { return HGL_S16_MAX; } + template<> inline int32 HGL_INTEGER_MAX() { return HGL_S32_MAX; } + template<> inline int64 HGL_INTEGER_MAX() { return HGL_S64_MAX; } - template<> inline int8 HGL_INTEGER_MIN() { return HGL_S8_MIN; } - template<> inline int16 HGL_INTEGER_MIN() { return HGL_S16_MIN; } - template<> inline int32 HGL_INTEGER_MIN() { return HGL_S32_MIN; } - template<> inline int64 HGL_INTEGER_MIN() { return HGL_S64_MIN; } + template<> inline int8 HGL_INTEGER_MIN() { return HGL_S8_MIN; } + template<> inline int16 HGL_INTEGER_MIN() { return HGL_S16_MIN; } + template<> inline int32 HGL_INTEGER_MIN() { return HGL_S32_MIN; } + template<> inline int64 HGL_INTEGER_MIN() { return HGL_S64_MIN; } - /** - * 星期每天枚举 - */ - enum WeekDay - { - weekNone=-1, + /** + * 星期每天枚举 + */ + enum WeekDay + { + weekNone=-1, - weekSunday, ///<星期天 - weekMonday, ///<星期一 - weekTuesday, ///<星期二 - weekWednesday, ///<星期三 - weekThursday, ///<星期四 - weekFriday, ///<星期五 - weekSaturday, ///<星期六 + weekSunday, ///<星期天 + weekMonday, ///<星期一 + weekTuesday, ///<星期二 + weekWednesday, ///<星期三 + weekThursday, ///<星期四 + weekFriday, ///<星期五 + weekSaturday, ///<星期六 - weekEnd - }; + weekEnd + }; /** * 月份枚举 @@ -203,77 +203,77 @@ namespace hgl monthEnd }; - constexpr uint HGL_TIME_ONE_SECOND =1; - constexpr uint HGL_TIME_HALF_MINUTE =30; - constexpr uint HGL_TIME_ONE_MINUTE =60; - constexpr uint HGL_TIME_HALF_HOUR =30*HGL_TIME_ONE_MINUTE; - constexpr uint HGL_TIME_ONE_HOUR =60*HGL_TIME_ONE_MINUTE; - constexpr uint HGL_TIME_HALF_DAY =12*HGL_TIME_ONE_HOUR; - constexpr uint HGL_TIME_ONE_DAY =24*HGL_TIME_ONE_HOUR; - constexpr uint HGL_TIME_ONE_WEEK =7*HGL_TIME_ONE_DAY; - constexpr uint HGL_TIME_ONE_YEAR =365*HGL_TIME_ONE_DAY; + constexpr uint HGL_TIME_ONE_SECOND =1; + constexpr uint HGL_TIME_HALF_MINUTE =30; + constexpr uint HGL_TIME_ONE_MINUTE =60; + constexpr uint HGL_TIME_HALF_HOUR =30*HGL_TIME_ONE_MINUTE; + constexpr uint HGL_TIME_ONE_HOUR =60*HGL_TIME_ONE_MINUTE; + constexpr uint HGL_TIME_HALF_DAY =12*HGL_TIME_ONE_HOUR; + constexpr uint HGL_TIME_ONE_DAY =24*HGL_TIME_ONE_HOUR; + constexpr uint HGL_TIME_ONE_WEEK =7*HGL_TIME_ONE_DAY; + constexpr uint HGL_TIME_ONE_YEAR =365*HGL_TIME_ONE_DAY; - constexpr uint HGL_HOUR_HALF_DAY =12; - constexpr uint HGL_HOUR_ONE_DAY =24; + constexpr uint HGL_HOUR_HALF_DAY =12; + constexpr uint HGL_HOUR_ONE_DAY =24; - constexpr uint HGL_DAY_ONE_WEEK =7; - constexpr uint HGL_DAY_ONE_YEAR =365; + constexpr uint HGL_DAY_ONE_WEEK =7; + constexpr uint HGL_DAY_ONE_YEAR =365; - constexpr uint HGL_MONTH_ONE_YEAR =12; + constexpr uint HGL_MONTH_ONE_YEAR =12; - constexpr uint HGL_MILLI_SEC_PRE_SEC =1000; //毫秒 - constexpr uint HGL_MICRO_SEC_PER_SEC =1000*1000; //微秒 - constexpr uint HGL_NANO_SEC_PER_SEC =1000*1000*1000; //纳秒 + constexpr uint HGL_MILLI_SEC_PRE_SEC =1000; //毫秒 + constexpr uint HGL_MICRO_SEC_PER_SEC =1000*1000; //微秒 + constexpr uint HGL_NANO_SEC_PER_SEC =1000*1000*1000; //纳秒 - constexpr char LowerHexChar[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; ///<小写16进制字符 - constexpr char UpperHexChar[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; ///<大写16进制字符 + constexpr char LowerHexChar[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; ///<小写16进制字符 + constexpr char UpperHexChar[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; ///<大写16进制字符 - constexpr double HGL_E =2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274; //欧拉数(自然对数的底数) - constexpr double HGL_LOG2E =1.44269504088896340736; - constexpr double HGL_LOG10E =0.434294481903251827651; - constexpr double HGL_LN2 =0.693147180559945309417; - constexpr double HGL_LN10 =2.30258509299404568402; - constexpr double HGL_PI =3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068; - constexpr double HGL_PI_2 =1.57079632679489661923; - constexpr double HGL_PI_4 =0.785398163397448309616; - constexpr double HGL_PI_3_4 =2.356194490192344928938; - constexpr double HGL_1_PI =0.318309886183790671538; - constexpr double HGL_2_PI =0.636619772367581343076; - constexpr double HGL_2_SQRTPI =1.12837916709551257390; - constexpr double HGL_SQRT2 =1.41421356237309504880168872420969807; - constexpr double HGL_SQRT1_2 =0.707106781186547524401; + constexpr double HGL_E =2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274; //欧拉数(自然对数的底数) + constexpr double HGL_LOG2E =1.44269504088896340736; + constexpr double HGL_LOG10E =0.434294481903251827651; + constexpr double HGL_LN2 =0.693147180559945309417; + constexpr double HGL_LN10 =2.30258509299404568402; + constexpr double HGL_PI =3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068; + constexpr double HGL_PI_2 =1.57079632679489661923; + constexpr double HGL_PI_4 =0.785398163397448309616; + constexpr double HGL_PI_3_4 =2.356194490192344928938; + constexpr double HGL_1_PI =0.318309886183790671538; + constexpr double HGL_2_PI =0.636619772367581343076; + constexpr double HGL_2_SQRTPI =1.12837916709551257390; + constexpr double HGL_SQRT2 =1.41421356237309504880168872420969807; + constexpr double HGL_SQRT1_2 =0.707106781186547524401; - constexpr double HGL_COS_ANG_0 =1; - constexpr double HGL_COS_ANG_45 =0.707106781187; - constexpr double HGL_COS_ANG_90 =0; - constexpr double HGL_COS_ANG_135=-0.707106781187; - constexpr double HGL_COS_ANG_180=-1; - constexpr double HGL_COS_ANG_225=-0.707106781187; - constexpr double HGL_COS_ANG_270=0; - constexpr double HGL_COS_ANG_315=0.707106781187; + constexpr double HGL_COS_ANG_0 =1; + constexpr double HGL_COS_ANG_45 =0.707106781187; + constexpr double HGL_COS_ANG_90 =0; + constexpr double HGL_COS_ANG_135=-0.707106781187; + constexpr double HGL_COS_ANG_180=-1; + constexpr double HGL_COS_ANG_225=-0.707106781187; + constexpr double HGL_COS_ANG_270=0; + constexpr double HGL_COS_ANG_315=0.707106781187; - constexpr double HGL_GOLDEN_RATIO =0.61803398874989484820458683436563811772030917980576;//黄金比例 - constexpr double HGL_SILVER_RATIO =2.4142135623730950488; //白银比例 + constexpr double HGL_GOLDEN_RATIO =0.61803398874989484820458683436563811772030917980576;//黄金比例 + constexpr double HGL_SILVER_RATIO =2.4142135623730950488; //白银比例 constexpr double HGL_SPEED_OF_SOUND =331.3f; //音速(米/秒) - constexpr double HGL_SPEED_OF_LIGHT =299792458; //光速(米/秒) + constexpr double HGL_SPEED_OF_LIGHT =299792458; //光速(米/秒) constexpr double HGL_ABSOLUTE_ZERO =-273.15f; //绝对零度 - constexpr double HGL_UNIVERSAL_GRAVITATION =6.67384e-11; //万有引力常数 + constexpr double HGL_UNIVERSAL_GRAVITATION =6.67384e-11; //万有引力常数 - constexpr double HGL_EARTH_GRAVITATIONAL_ACCELERATION =9.80665; //地球上的重力加速度(牛顿) - constexpr double HGL_EARTH_MASS =5.9722e+24; //地球质量 - constexpr double HGL_EARTH_RADIUS =6371000; //地球半径(米) + constexpr double HGL_EARTH_GRAVITATIONAL_ACCELERATION =9.80665; //地球上的重力加速度(牛顿) + constexpr double HGL_EARTH_MASS =5.9722e+24; //地球质量 + constexpr double HGL_EARTH_RADIUS =6371000; //地球半径(米) - /** + /** * 物体万有引力计算 * @param m1 星球质量 * @param m2 物体质量 * @param length 到星球中心的距离 */ - template - inline T UniversalGravitation(const T m1,const T m2,const T length) + template + inline T UniversalGravitation(const T m1,const T m2,const T length) { return HGL_UNIVERSAL_GRAVITATION*((m1*m2)/(length*length)); } @@ -325,211 +325,211 @@ namespace hgl return m0/sqrt(1-(v*v)/(HGL_SPEED_OF_LIGHT*HGL_SPEED_OF_LIGHT)); } - inline float half_to_float(const uint16 &h) - { - return ((h&0x8000)<<16) | (((h&0x7c00)+0x1C000)<<13) | ((h&0x03FF)<<13); - } + inline float half_to_float(const uint16 &h) + { + return ((h&0x8000)<<16) | (((h&0x7c00)+0x1C000)<<13) | ((h&0x03FF)<<13); + } - inline uint16 float_to_half(const float &f) - { - const uint32 x = *((uint32 *)&f); + inline uint16 float_to_half(const float &f) + { + const uint32 x = *((uint32 *)&f); - return ((x>>16)&0x8000)|((((x&0x7f800000)-0x38000000)>>13)&0x7c00)|((x>>13)&0x03ff); - } + return ((x>>16)&0x8000)|((((x&0x7f800000)-0x38000000)>>13)&0x7c00)|((x>>13)&0x03ff); + } - /** - * 正圆面积计算 - * @param radius 半径 - */ - inline double CircleArea(const double radius) - { - return(radius*radius*HGL_PI); - } + /** + * 正圆面积计算 + * @param radius 半径 + */ + inline double CircleArea(const double radius) + { + return(radius*radius*HGL_PI); + } - /** - * 椭圆面积计算 - * @param l_radius 长半径 - * @param s_radius 短半径 - */ - inline double ElipseArea(const double l_radius,const double s_radius) - { - return(l_radius*s_radius*HGL_PI); - } + /** + * 椭圆面积计算 + * @param l_radius 长半径 + * @param s_radius 短半径 + */ + inline double ElipseArea(const double l_radius,const double s_radius) + { + return(l_radius*s_radius*HGL_PI); + } - /** - * 球体积计算 - * @param radius 球半径 - */ - inline double SphereVolume(const double radius) - { - return(radius*radius*radius*(HGL_PI*4.0f))/3.0f; - } + /** + * 球体积计算 + * @param radius 球半径 + */ + inline double SphereVolume(const double radius) + { + return(radius*radius*radius*(HGL_PI*4.0f))/3.0f; + } - /** - * 椭球体积计算 - * @param x_radius x半径 - * @param y_radius y半径 - * @param z_radius z半径 - */ - inline double EllipsoidVolume(const double x_radius,const double y_radius,const double z_radius) - { - return(x_radius*y_radius*z_radius*(HGL_PI*4.0f))/3.0f; - } + /** + * 椭球体积计算 + * @param x_radius x半径 + * @param y_radius y半径 + * @param z_radius z半径 + */ + inline double EllipsoidVolume(const double x_radius,const double y_radius,const double z_radius) + { + return(x_radius*y_radius*z_radius*(HGL_PI*4.0f))/3.0f; + } - /** - * 取适合正巧大于当前数的2次幂值 - */ - template - inline T power_to_2(T value) - { - T result=1; + /** + * 取适合正巧大于当前数的2次幂值 + */ + template + inline T power_to_2(T value) + { + T result=1; - while(result inline void hgl_swap(T &x,T &y) - { - T t; + template inline void hgl_swap(T &x,T &y) + { + T t; - memcpy(&t,&x,sizeof(T)); - memcpy(&x,&y,sizeof(T)); - memcpy(&y,&t,sizeof(T)); - } + memcpy(&t,&x,sizeof(T)); + memcpy(&x,&y,sizeof(T)); + memcpy(&y,&t,sizeof(T)); + } - template inline T hgl_abs(const T &v){return (v>=0?v:-v);} + template inline T hgl_abs(const T &v){return (v>=0?v:-v);} - template inline T hgl_min(const T &a,const T &b){return (a>b?b:a);} - template inline T hgl_max(const T &a,const T &b){return (a>b?a:b);} + template inline T hgl_min(const T &a,const T &b){return (a>b?b:a);} + template inline T hgl_max(const T &a,const T &b){return (a>b?a:b);} - template inline T hgl_min(const T *data,int count,T min_value) - { - T value=min_value; + template inline T hgl_min(const T *data,int count,T min_value) + { + T value=min_value; - for(int i=0;i inline T hgl_max(const T *data,int count,T max_value) - { - T value=max_value; + template inline T hgl_max(const T *data,int count,T max_value) + { + T value=max_value; - for(int i=0;ivalue) - value=*data; + for(int i=0;ivalue) + value=*data; - ++data; - } + ++data; + } - return(value); - } + return(value); + } - /** - * 角度转弧度 - */ - inline double hgl_ang2rad(const double ang) - { - return ang*(HGL_PI/180.0f); - } + /** + * 角度转弧度 + */ + inline double hgl_ang2rad(const double ang) + { + return ang*(HGL_PI/180.0f); + } - /** - * 弧度转角度 - */ - inline double hgl_rad2ang(const double rad) - { - return rad*(180.0f/HGL_PI); - } + /** + * 弧度转角度 + */ + inline double hgl_rad2ang(const double rad) + { + return rad*(180.0f/HGL_PI); + } - /** + /** * 浮点数截取小数点后指定位度 * @param value 要截取的浮点数 * @param num 要截取的位数 */ - template - inline T hgl_clip_float(const T value,const int num) - { - if(num<=0) - return int64(value); + template + inline T hgl_clip_float(const T value,const int num) + { + if(num<=0) + return int64(value); - if(value==0)return(0); + if(value==0)return(0); - double per=pow(10,num); + double per=pow(10,num); - return double(floor(value*per))/per; - } + return double(floor(value*per))/per; + } - /** + /** * 等值类型复制 */ - template - inline void hgl_cpy(T &dst,const T &src) - { - memcpy(&dst,&src,sizeof(T)); - } + template + inline void hgl_cpy(T &dst,const T &src) + { + memcpy(&dst,&src,sizeof(T)); + } - /** + /** * 不同类型数据块复制 */ - template - inline void hgl_cpy(D *dst,const S *src,const size_t count) - { - for(size_t i=0;i + inline void hgl_cpy(D *dst,const S *src,const size_t count) + { + for(size_t i=0;i - inline void hgl_typecpy(T *dst,const T *src,const size_t count) - { - memcpy(dst,src,count*sizeof(T)); - } + template + inline void hgl_typecpy(T *dst,const T *src,const size_t count) + { + memcpy(dst,src,count*sizeof(T)); + } - /** + /** * 同类型数据块移动 */ - template - inline void hgl_typemove(T *dst,const T *src,const size_t count) - { - memmove(dst,src,count*sizeof(T)); - } + template + inline void hgl_typemove(T *dst,const T *src,const size_t count) + { + memmove(dst,src,count*sizeof(T)); + } - /** + /** * 指定类型数据块赋值 */ - template - inline void hgl_set(T *data,const T value,const size_t count) - { - for(size_t i=0;i + inline void hgl_set(T *data,const T value,const size_t count) + { + for(size_t i=0;i - inline void hgl_zero(T *data,const size_t count) - { - memset(data,0,count*sizeof(T)); - } + template + inline void hgl_zero(T *data,const size_t count) + { + memset(data,0,count*sizeof(T)); + } /** * 分配指定类型数据块并清0 @@ -554,81 +554,81 @@ namespace hgl return data; } - /** + /** * 指定类型数据清0 */ - template - inline void hgl_zero(T &data) - { - memset(&data,0,sizeof(T)); - } + template + inline void hgl_zero(T &data) + { + memset(&data,0,sizeof(T)); + } }//namespace hgl /** * 比较处理模板基类 */ -template class Comparator ///比较处理模板基类 +template class Comparator ///比较处理模板基类 { public: - /** - * 比较函数,需被特例化或派生实现 - */ - virtual int compare(const T &a,const T &b)const - { - return 0; // 如 return(a-b); ,但这个函数正确情况下不应该会被调用 - } + /** + * 比较函数,需被特例化或派生实现 + */ + virtual int compare(const T &a,const T &b)const + { + return 0; // 如 return(a-b); ,但这个函数正确情况下不应该会被调用 + } - /** - * 交换函数 - */ - virtual void exchange(T &a,T &b) - { - T t; + /** + * 交换函数 + */ + virtual void exchange(T &a,T &b) + { + T t; - memcpy(&t,&a,sizeof(T)); - memcpy(&a,&b,sizeof(T)); - memcpy(&b,&t,sizeof(T)); - } + memcpy(&t,&a,sizeof(T)); + memcpy(&a,&b,sizeof(T)); + memcpy(&b,&t,sizeof(T)); + } - /** - * 复制数据 - */ - virtual void cpy(T *t,T *s) - { - memcpy(t,s,sizeof(T)); - } + /** + * 复制数据 + */ + virtual void cpy(T *t,T *s) + { + memcpy(t,s,sizeof(T)); + } };//class Comparator //针对原生类型的特例化版本,做适当加速 -#define COMPARATOR_ORIGIN_TYPE(type) template<> class Comparator \ - { \ - public: \ - int compare(const type &a,const type &b)const{return a-b;} \ - void exchange(type &a,type &b){type t;t=a;a=b;b=t;} \ - void cpy(type *t,type *s){*t=*s;} \ - }; +#define COMPARATOR_ORIGIN_TYPE(type) template<> class Comparator \ + { \ + public: \ + int compare(const type &a,const type &b)const{return a-b;} \ + void exchange(type &a,type &b){type t;t=a;a=b;b=t;} \ + void cpy(type *t,type *s){*t=*s;} \ + }; - COMPARATOR_ORIGIN_TYPE(hgl::int8) - COMPARATOR_ORIGIN_TYPE(hgl::int16) - COMPARATOR_ORIGIN_TYPE(hgl::int32) - COMPARATOR_ORIGIN_TYPE(hgl::int64) + COMPARATOR_ORIGIN_TYPE(hgl::int8) + COMPARATOR_ORIGIN_TYPE(hgl::int16) + COMPARATOR_ORIGIN_TYPE(hgl::int32) + COMPARATOR_ORIGIN_TYPE(hgl::int64) - COMPARATOR_ORIGIN_TYPE(hgl::uint8) - COMPARATOR_ORIGIN_TYPE(hgl::uint16) - COMPARATOR_ORIGIN_TYPE(hgl::uint32) - COMPARATOR_ORIGIN_TYPE(hgl::uint64) + COMPARATOR_ORIGIN_TYPE(hgl::uint8) + COMPARATOR_ORIGIN_TYPE(hgl::uint16) + COMPARATOR_ORIGIN_TYPE(hgl::uint32) + COMPARATOR_ORIGIN_TYPE(hgl::uint64) - COMPARATOR_ORIGIN_TYPE(float) - COMPARATOR_ORIGIN_TYPE(double) + COMPARATOR_ORIGIN_TYPE(float) + COMPARATOR_ORIGIN_TYPE(double) - COMPARATOR_ORIGIN_TYPE(char) - COMPARATOR_ORIGIN_TYPE(wchar_t) + COMPARATOR_ORIGIN_TYPE(char) + COMPARATOR_ORIGIN_TYPE(wchar_t) #if HGL_OS != HGL_OS_Windows - COMPARATOR_ORIGIN_TYPE(u16char) + COMPARATOR_ORIGIN_TYPE(u16char) #endif//HGL_OS != HGL_OS_Windows - COMPARATOR_ORIGIN_TYPE(char32_t) + COMPARATOR_ORIGIN_TYPE(char32_t) #undef COMPARATOR_ORIGIN_TYPE #endif//HGL_TYPE_FUNC_INCLUDE diff --git a/inc/hgl/endian/Endian.h b/inc/hgl/endian/Endian.h index b65e9ae4..77574fef 100644 --- a/inc/hgl/endian/Endian.h +++ b/inc/hgl/endian/Endian.h @@ -1,67 +1,67 @@ #ifndef HGL_ENDIAN_INCLUDE #define HGL_ENDIAN_INCLUDE -#include // 平台定义 +#include // 平台定义 namespace hgl { - namespace endian - { - /** - * Windows代码页枚举 - * 全部Windows所支持代码页请参见 http://msdn.microsoft.com/en-us/library/dd317756 - */ - enum CharCodePage ///代码页枚举 - { - ccpNone=0, ///<起始定义,无意义 + namespace endian + { + /** + * Windows代码页枚举 + * 全部Windows所支持代码页请参见 http://msdn.microsoft.com/en-us/library/dd317756 + */ + enum CharCodePage ///代码页枚举 + { + ccpNone=0, ///<起始定义,无意义 - //中文 - ccpGBK =936, ///<中国GBK标准中文 - ccpBig5 =950, ///<中国台湾Big5标准繁体中文 - ccpGB2312 =20936, ///<中国GB2312标准简体中文 - ccpGB18030 =54936, ///<中国GB18030-2000标准中文 + //中文 + ccpGBK =936, ///<中国GBK标准中文 + ccpBig5 =950, ///<中国台湾Big5标准繁体中文 + ccpGB2312 =20936, ///<中国GB2312标准简体中文 + ccpGB18030 =54936, ///<中国GB18030-2000标准中文 - //日文 - ccpShiftJIS =932, ///<日文ShiftJIS - ccpJISX =50222, ///<日文JIS X/ISO 2022 + //日文 + ccpShiftJIS =932, ///<日文ShiftJIS + ccpJISX =50222, ///<日文JIS X/ISO 2022 - //韩文 - ccpKorean =949, ///<韩文 + //韩文 + ccpKorean =949, ///<韩文 - //苹果编码 - ccpMacJanpan =10001, ///<日文 - ccpMacTraditionalChinese =10002, ///<繁体中文 - ccpMacSimplifiedChinese =10008, ///<简体中文 + //苹果编码 + ccpMacJanpan =10001, ///<日文 + ccpMacTraditionalChinese =10002, ///<繁体中文 + ccpMacSimplifiedChinese =10008, ///<简体中文 - //unicode - ccpUTF7 =65000, /// const CharSetName &GetCurCharSet(); ///<取得当前程序编码字符集 + template const CharSetName &GetCurCharSet(); ///<取得当前程序编码字符集 constexpr CharSetName utf8_charset="utf8"; constexpr CharSetName utf16le_charset="utf-16le"; @@ -69,61 +69,61 @@ namespace hgl constexpr CharSetName utf32le_charset="utf-32le"; constexpr CharSetName utf32be_charset="utf-32be"; - template<> inline const CharSetName &GetCurCharSet<2,HGL_LITTLE_ENDIAN >(){return utf16le_charset;} - template<> inline const CharSetName &GetCurCharSet<2,HGL_BIG_ENDIAN >(){return utf16be_charset;} - template<> inline const CharSetName &GetCurCharSet<4,HGL_LITTLE_ENDIAN >(){return utf32le_charset;} - template<> inline const CharSetName &GetCurCharSet<4,HGL_BIG_ENDIAN >(){return utf32be_charset;} + template<> inline const CharSetName &GetCurCharSet<2,HGL_LITTLE_ENDIAN >(){return utf16le_charset;} + template<> inline const CharSetName &GetCurCharSet<2,HGL_BIG_ENDIAN >(){return utf16be_charset;} + template<> inline const CharSetName &GetCurCharSet<4,HGL_LITTLE_ENDIAN >(){return utf32le_charset;} + template<> inline const CharSetName &GetCurCharSet<4,HGL_BIG_ENDIAN >(){return utf32be_charset;} - template inline const CharSetName &GetCharSet() - { - return GetCurCharSet(); - } + template inline const CharSetName &GetCharSet() + { + return GetCurCharSet(); + } - /** - * 字节序文件头数据结构 - */ - struct BOMFileHeader - { - int size; ///<字节序文件头长度 - unsigned char data[4]; ///<字节序数据 + /** + * 字节序文件头数据结构 + */ + struct BOMFileHeader + { + int size; ///<字节序文件头长度 + unsigned char data[4]; ///<字节序数据 ByteOrderMask bom; ///<字节序枚举 const CharSetName *char_set;///<字符集名称 CharCodePage code_page; ///<代码页 - }; + }; - /** - * 字节序文件头定义 - */ - constexpr BOMFileHeader BOMData[bomEnd]= - { - {0,{} ,bomNone, nullptr ,ccpNone }, - {3,{0xEF,0xBB,0xBF} ,bomUTF8, &utf8_charset ,ccpUTF8 }, - {2,{0xFF,0xFE} ,bomUTF16LE,&utf16le_charset ,ccpUTF16LE }, - {2,{0xFE,0xFF} ,bomUTF16BE,&utf16be_charset ,ccpUTF16BE }, - {4,{0xFF,0xFE,0x00,0x00},bomUTF32LE,&utf32le_charset ,ccpUTF32LE }, - {4,{0x00,0x00,0xFE,0xFF},bomUTF32BE,&utf32be_charset ,ccpUTF32BE } - }; + /** + * 字节序文件头定义 + */ + constexpr BOMFileHeader BOMData[bomEnd]= + { + {0,{} ,bomNone, nullptr ,ccpNone }, + {3,{0xEF,0xBB,0xBF} ,bomUTF8, &utf8_charset ,ccpUTF8 }, + {2,{0xFF,0xFE} ,bomUTF16LE,&utf16le_charset ,ccpUTF16LE }, + {2,{0xFE,0xFF} ,bomUTF16BE,&utf16be_charset ,ccpUTF16BE }, + {4,{0xFF,0xFE,0x00,0x00},bomUTF32LE,&utf32le_charset ,ccpUTF32LE }, + {4,{0x00,0x00,0xFE,0xFF},bomUTF32BE,&utf32be_charset ,ccpUTF32BE } + }; - template - inline T EndianSwap(const T value) - { - union - { - T v; - uint8 bytes[sizeof(T)]; - }a,b; + template + inline T EndianSwap(const T value) + { + union + { + T v; + uint8 bytes[sizeof(T)]; + }a,b; - a.v=value; + a.v=value; - for(uint i=0;i inline int8 EndianSwap< int8>(const int8 value){return value;} - template<> inline uint8 EndianSwap(const uint8 value){return value;} + template<> inline int8 EndianSwap< int8>(const int8 value){return value;} + template<> inline uint8 EndianSwap(const uint8 value){return value;} template<> inline uint16 EndianSwap(const uint16 value) { @@ -153,123 +153,123 @@ namespace hgl template<> inline u16char EndianSwap(const u16char value){return EndianSwap(uint16(value));} - template - inline void EndianSwap(T *value,const int64 count) - { - for(int64 i=0;i + inline void EndianSwap(T *value,const int64 count) + { + for(int64 i=0;i - inline void EndianSwap(T *dst,const T *src,const int64 count) - { - for(uint i=0;i + inline void EndianSwap(T *dst,const T *src,const int64 count) + { + for(uint i=0;i - inline void EndianSwap(D *dst,const S *src,const int64 count) - { - for(uint i=0;i + inline void EndianSwap(D *dst,const S *src,const int64 count) + { + for(uint i=0;i T ToBigEndian(T value){return value;} - - template inline void ToBigEndian(T *value,const int64 count){} - template inline void ToBigEndian(D *dst,const S *src,const int64 count){hgl_cpy(dst,src,count);} - - template T ToLittleEndian(T value){return EndianSwap(value);} - - template inline void ToLittleEndian(T *value,const int64 count){EndianSwap(value,count);} - template inline void ToLittleEndian(D *dst,const S *src,const int64 count){EndianSwap(dst,src,count);} - - #else - - constexpr uint HGL_BOM_UTF16LE =0xfeff; - constexpr uint HGL_BOM_UTF16BE =0xfffe; - constexpr uint HGL_BOM_UTF32LE =0x0000feff; - constexpr uint HGL_BOM_UTF32BE =0xfffe0000; - - #define LittleToCurrentEndian ToLittleEndian - #define BigToCurrentEndian EndianSwap - - template T ToBigEndian(T value){return EndianSwap(value);} - - template inline void ToBigEndian(T *value,const int64 count){EndianSwap(value,count);} - template inline void ToBigEndian(D *dst,const S *src,const int64 count){EndianSwap(dst,src,count);} - - template T ToLittleEndian(T value){return value;} - - template inline void ToLittleEndian(T *,const int64){} - template inline void ToLittleEndian(D *dst,const S *src,const int64 count){hgl_cpy(dst,src,count);} - - #endif//HGL_BIG_ENDIAN - - template struct UTF16CharConvert; - - template<> struct UTF16CharConvert - { #if HGL_ENDIAN == HGL_BIG_ENDIAN - static void convert(u16char *str,const int length) - { - EndianSwap(str,length); - } + + constexpr uint HGL_BOM_UTF16LE =0xfffe; + constexpr uint HGL_BOM_UTF16BE =0xfeff; + constexpr uint HGL_BOM_UTF32LE =0xfffe0000; + constexpr uint HGL_BOM_UTF32BE =0x0000feff; + + #define LittleToCurrentEndian EndianSwap + #define BigToCurrentEndian ToBigEndian + + template T ToBigEndian(T value){return value;} + + template inline void ToBigEndian(T *value,const int64 count){} + template inline void ToBigEndian(D *dst,const S *src,const int64 count){hgl_cpy(dst,src,count);} + + template T ToLittleEndian(T value){return EndianSwap(value);} + + template inline void ToLittleEndian(T *value,const int64 count){EndianSwap(value,count);} + template inline void ToLittleEndian(D *dst,const S *src,const int64 count){EndianSwap(dst,src,count);} + + #else + + constexpr uint HGL_BOM_UTF16LE =0xfeff; + constexpr uint HGL_BOM_UTF16BE =0xfffe; + constexpr uint HGL_BOM_UTF32LE =0x0000feff; + constexpr uint HGL_BOM_UTF32BE =0xfffe0000; + + #define LittleToCurrentEndian ToLittleEndian + #define BigToCurrentEndian EndianSwap + + template T ToBigEndian(T value){return EndianSwap(value);} + + template inline void ToBigEndian(T *value,const int64 count){EndianSwap(value,count);} + template inline void ToBigEndian(D *dst,const S *src,const int64 count){EndianSwap(dst,src,count);} + + template T ToLittleEndian(T value){return value;} + + template inline void ToLittleEndian(T *,const int64){} + template inline void ToLittleEndian(D *dst,const S *src,const int64 count){hgl_cpy(dst,src,count);} + + #endif//HGL_BIG_ENDIAN + + template struct UTF16CharConvert; + + template<> struct UTF16CharConvert + { + #if HGL_ENDIAN == HGL_BIG_ENDIAN + static void convert(u16char *str,const int length) + { + EndianSwap(str,length); + } #else static void convert(const u16char *,const int){} - #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN + #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN - static void convert(u16char *out_str,const u16char *in_str,const int length) - { - #if HGL_ENDIAN == HGL_LITTLE_ENDIAN - memcpy(out_str,in_str,length*sizeof(u16char)); - #else - EndianSwap(in_str,length); - #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN - } - };//template<> struct UTF16CharConvert + static void convert(u16char *out_str,const u16char *in_str,const int length) + { + #if HGL_ENDIAN == HGL_LITTLE_ENDIAN + memcpy(out_str,in_str,length*sizeof(u16char)); + #else + EndianSwap(in_str,length); + #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN + } + };//template<> struct UTF16CharConvert - template<> struct UTF16CharConvert - { - static void convert(u16char *str,const int length) - { - #if HGL_ENDIAN == HGL_LITTLE_ENDIAN - EndianSwap(str,length); - #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN - } + template<> struct UTF16CharConvert + { + static void convert(u16char *str,const int length) + { + #if HGL_ENDIAN == HGL_LITTLE_ENDIAN + EndianSwap(str,length); + #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN + } - static void convert(u16char *out_str,const u16char *in_str,const int length) - { - #if HGL_ENDIAN == HGL_LITTLE_ENDIAN - memcpy(out_str,in_str,length*sizeof(u16char)); - #else - EndianSwap(in_str,length); - #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN - } - };//template<> struct UTF16ToWideChar - }//namespace endian + static void convert(u16char *out_str,const u16char *in_str,const int length) + { + #if HGL_ENDIAN == HGL_LITTLE_ENDIAN + memcpy(out_str,in_str,length*sizeof(u16char)); + #else + EndianSwap(in_str,length); + #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN + } + };//template<> struct UTF16ToWideChar + }//namespace endian - using namespace endian; + using namespace endian; }//namespace hgl #endif//HGL_ENDIAN_INCLUDE diff --git a/inc/hgl/filesystem/EnumVolume.h b/inc/hgl/filesystem/EnumVolume.h index 5cfd4c3b..47e7a105 100644 --- a/inc/hgl/filesystem/EnumVolume.h +++ b/inc/hgl/filesystem/EnumVolume.h @@ -13,36 +13,36 @@ namespace hgl { enum DriverType { - dtNone=0, ///<未知类型 + dtNone=0, ///<未知类型 - dtRemovable, ///<可移动设备 - dtFixed, ///<固定设备 - dtRemote, ///<远程设备 - dtCDROM, ///<光盘驱动器 - dtRamDisk, ///<内存虚拟设备 + dtRemovable, ///<可移动设备 + dtFixed, ///<固定设备 + dtRemote, ///<远程设备 + dtCDROM, ///<光盘驱动器 + dtRamDisk, ///<内存虚拟设备 - dtEnd ///<结束定义 + dtEnd ///<结束定义 }; - u16char name[HGL_MAX_PATH]; ///<卷名称 + u16char name[HGL_MAX_PATH]; ///<卷名称 - u16char path[HGL_MAX_PATH]; ///<卷所对应的路径名(注意:不是所有卷都有对应路径) + u16char path[HGL_MAX_PATH]; ///<卷所对应的路径名(注意:不是所有卷都有对应路径) - DriverType driver_type; ///<驱动器类型(注意:不是所有的卷都对应驱动器) + DriverType driver_type; ///<驱动器类型(注意:不是所有的卷都对应驱动器) - uint32 serial; ///<卷序列号 + uint32 serial; ///<卷序列号 - u16char volume_label[HGL_MAX_PATH]; ///<卷标名称 + u16char volume_label[HGL_MAX_PATH]; ///<卷标名称 - u16char file_system[HGL_MAX_PATH]; ///<文件系统名称 + u16char file_system[HGL_MAX_PATH]; ///<文件系统名称 - uint32 filename_max_length; ///<文件名最大长度 + uint32 filename_max_length; ///<文件名最大长度 - bool unicode; ///<文件名支持UNICODE + bool unicode; ///<文件名支持UNICODE - uint64 available_space; ///<有效容量 - uint64 total_space; ///<总空量 - uint64 free_space; ///<自由容量 + uint64 available_space; ///<有效容量 + uint64 total_space; ///<总空量 + uint64 free_space; ///<自由容量 };//struct VolumeInfo #if HGL_OS == HGL_OS_Windows @@ -52,7 +52,7 @@ namespace hgl */ struct VolumeCheckConfig { - bool removable =false; + bool removable =false; bool fixed =false; bool remote =false; bool cdrom =false; @@ -64,7 +64,7 @@ namespace hgl /** * 设置为全部检测 */ - void SetFullCheck() + void SetFullCheck() { memset(this,0xff,sizeof(VolumeCheckConfig)); } diff --git a/inc/hgl/filesystem/FileSystem.h b/inc/hgl/filesystem/FileSystem.h index b41190ef..6cd772c1 100644 --- a/inc/hgl/filesystem/FileSystem.h +++ b/inc/hgl/filesystem/FileSystem.h @@ -53,7 +53,7 @@ namespace hgl if(fullname.Length()<=1) return(BaseString()); - const T spear_char[] = { '/','\\' }; + const T spear_char[] = { '/','\\' }; const int pos=fullname.FindRightChar(spear_char); @@ -131,60 +131,60 @@ namespace hgl inline WideString MergeFilename(const WideString &pathname,const WideString &filename) ///<组合路径名与文件名 {return MergeFilename(pathname,filename,L'\\',L"\\");} - bool FileCopy(const OSString &,const OSString &); ///<文件复制 - bool FileDelete(const OSString &); ///<文件删除 - bool FileMove(const OSString &,const OSString &); ///<文件移动 - bool FileRename(const OSString &,const OSString &); ///<文件改名 - bool FileExist(const OSString &); ///<文件确认是否存在 - bool FileComp(const OSString &,const OSString &); ///<文件比较 + bool FileCopy(const OSString &,const OSString &); ///<文件复制 + bool FileDelete(const OSString &); ///<文件删除 + bool FileMove(const OSString &,const OSString &); ///<文件移动 + bool FileRename(const OSString &,const OSString &); ///<文件改名 + bool FileExist(const OSString &); ///<文件确认是否存在 + bool FileComp(const OSString &,const OSString &); ///<文件比较 - bool FileCanRead(const OSString &); ///<检测文件是否可读 - bool FileCanWrite(const OSString &); ///<检测文件是否可写 - bool FileCanExec(const OSString &); ///<检测文件是否可执行 + bool FileCanRead(const OSString &); ///<检测文件是否可读 + bool FileCanWrite(const OSString &); ///<检测文件是否可写 + bool FileCanExec(const OSString &); ///<检测文件是否可执行 - int64 LoadFileToMemory(const OSString &,void **); ///<加载一个文件到内存 + int64 LoadFileToMemory(const OSString &,void **); ///<加载一个文件到内存 int64 SaveMemoryToFile(const OSString &,const void *,const int64 &); ///<保存一块内存成文件 - int64 SaveMemoryToFile(const OSString &,void **,const int64 *,const int &); ///<保存多块内存成一个文件 + int64 SaveMemoryToFile(const OSString &,void **,const int64 *,const int &); ///<保存多块内存成一个文件 - void *LoadFileToMemory(const OSString &,int64,void *buf,int64); ///<加载一个文件的一部分到内存 - bool SaveMemoryToFile(const OSString &,int64,const void *,int64); ///<保存一块内存到一个文件 + void *LoadFileToMemory(const OSString &,int64,void *buf,int64); ///<加载一个文件的一部分到内存 + bool SaveMemoryToFile(const OSString &,int64,const void *,int64); ///<保存一块内存到一个文件 bool IsDirectory(const os_char *); - inline bool IsDirectory(const OSString &str){return IsDirectory(str.c_str());} ///<判断这个名称是否是目录 + inline bool IsDirectory(const OSString &str){return IsDirectory(str.c_str());} ///<判断这个名称是否是目录 #if HGL_OS != HGL_OS_Windows bool IsLink(const os_char *); ///<判断这个名称是否是链接 #endif// - bool MakePath(const OSString &); ///<创建一个路径 - bool DeletePath(const OSString &); ///<删除一个路径 - void DeleteTree(const OSString &); ///<删除一个路径(包含所有文件) + bool MakePath(const OSString &); ///<创建一个路径 + bool DeletePath(const OSString &); ///<删除一个路径 + void DeleteTree(const OSString &); ///<删除一个路径(包含所有文件) - bool GetCurrentPath(OSString &); ///<取得当前路径 - bool GetCurrentProgram(OSString &); ///<取得当前程序全路径名称 - bool GetCurrentProgramPath(OSString &); ///<取得当前程序所在路径 - void GetLocalAppdataPath(os_char fn[HGL_MAX_PATH]); ///<取得当前用户应用程序数据存放路径 + bool GetCurrentPath(OSString &); ///<取得当前路径 + bool GetCurrentProgram(OSString &); ///<取得当前程序全路径名称 + bool GetCurrentProgramPath(OSString &); ///<取得当前程序所在路径 + void GetLocalAppdataPath(os_char fn[HGL_MAX_PATH]); ///<取得当前用户应用程序数据存放路径 //使用int64而不是__int64是因为不是所有编译器都支持__int64的写法,必须使用DataType.H中引入的定义 /** * 文件信息数据结构 */ - struct FileInfo ///文件信息 + struct FileInfo ///文件信息 { - os_char name[HGL_MAX_PATH]; ///<文件名(不包含路径) - os_char fullname[HGL_MAX_PATH]; ///<完整名称(包含路径) + os_char name[HGL_MAX_PATH]; ///<文件名(不包含路径) + os_char fullname[HGL_MAX_PATH]; ///<完整名称(包含路径) - uint64 size; ///<文件长度 + uint64 size; ///<文件长度 union { - uint32 attrib; ///<文件属性 + uint32 attrib; ///<文件属性 struct { - bool is_file:1; ///<是文件 - bool is_directory:1; ///<是目录 + bool is_file:1; ///<是文件 + bool is_directory:1; ///<是目录 bool is_hiddle:1; ///<是否隐藏文件 @@ -192,15 +192,15 @@ namespace hgl bool is_link:1; ///<是否是链接 #endif//HGL_OS != HGL_OS_Windows - bool can_read:1; ///<可以读 - bool can_write:1; ///<可以写 + bool can_read:1; ///<可以读 + bool can_write:1; ///<可以写 }; }; uint64 mtime; ///<最后修改日期(这个值在win/unix下不通用) };//struct FileInfo - bool GetFileInfo(const os_char *filename,struct FileInfo &); ///<取得文件信息 + bool GetFileInfo(const os_char *filename,struct FileInfo &); ///<取得文件信息 int GetFileInfoList(List &, const OSString &folder_name, bool proc_folder, bool proc_file, bool sub_folder); }//namespace filesystem diff --git a/inc/hgl/graph/Light.h b/inc/hgl/graph/Light.h index bad3b7c7..3b853ffa 100644 --- a/inc/hgl/graph/Light.h +++ b/inc/hgl/graph/Light.h @@ -21,18 +21,18 @@ namespace hgl */ enum LightType { - ltNone=0, ///<起始定义,无意义 + ltNone=0, ///<起始定义,无意义 ltSunLight, ///<太阳光 - ltPoint, ///<点光源 - ltDirection, ///<方向光 - ltSpot, ///<聚光灯 + ltPoint, ///<点光源 + ltDirection, ///<方向光 + ltSpot, ///<聚光灯 ltInfiniteSpotLight,///<无尽聚光灯 - ltAreaQuad, ///<四边形面光源 + ltAreaQuad, ///<四边形面光源 - ltEnd ///<结束定义,无意义 + ltEnd ///<结束定义,无意义 };//enum LightType /** @@ -40,13 +40,13 @@ namespace hgl */ struct Light { - LightType type; ///<光源类型 + LightType type; ///<光源类型 - bool enabled; ///<是否开启 + bool enabled; ///<是否开启 - Color4f ambient; ///<环境光 - Color4f specular; ///<镜面光 - Color4f diffuse; ///<漫反射 + Color4f ambient; ///<环境光 + Color4f specular; ///<镜面光 + Color4f diffuse; ///<漫反射 };//struct Light /** @@ -62,10 +62,10 @@ namespace hgl */ struct DirectionLight:public Light { - Vector3f direction; ///<方向 + Vector3f direction; ///<方向 - float nDotVP; ///; diff --git a/inc/hgl/graph/vulkan/VKBufferData.h b/inc/hgl/graph/vulkan/VKBufferData.h index 56be1311..901ec28d 100644 --- a/inc/hgl/graph/vulkan/VKBufferData.h +++ b/inc/hgl/graph/vulkan/VKBufferData.h @@ -23,7 +23,7 @@ VK_NAMESPACE_BEGIN friend BufferData *CreateBufferData(void *data,const VkFormat f,const uint32_t count,const uint32_t stride); BufferData(uint8_t *data,const VkFormat f,const uint32_t c,const uint32_t s) - { + { buffer_data =data; format=f; @@ -74,7 +74,7 @@ VK_NAMESPACE_BEGIN { VkFormat format; ///<ݸʽ uint byte; ///<ֽ - + uint compoment; ///<ݳɷ public: @@ -141,7 +141,7 @@ VK_NAMESPACE_BEGIN #define VBDATA_CREATE_FUNC(short_name,type,comp_count,vk_type) \ inline VertexBufferData *VB##comp_count##short_name(const uint32_t &count){return CreateVertexBufferData(vk_type,sizeof(type),comp_count,count);} \ - inline VertexBufferData *VB##comp_count##short_name(const uint32_t &count,const type *data){return CreateVertexBufferData((void *)data,vk_type,sizeof(type),comp_count,count);} + inline VertexBufferData *VB##comp_count##short_name(const uint32_t &count,const type *data){return CreateVertexBufferData((void *)data,vk_type,sizeof(type),comp_count,count);} // UNORM ָ޷ԶתΪ 0.0 to 1.0 ĸ // SNORM ָзԶתΪ-1.0 to +1.0 ĸ @@ -166,7 +166,7 @@ VK_NAMESPACE_BEGIN VBDATA_UIF_1234(uint32,int32,float ,VK_FORMAT_R32_UINT) VBDATA_UIF_1234(uint64,int64,double,VK_FORMAT_R64_UINT) #undef VBDATA_CREATE_FUNC - + //inline VertexBufferData *EB16(const uint16 &count){return CreateVertexBufferData(GL_UNSIGNED_SHORT, 2,1,count);} //inline VertexBufferData *EB32(const uint32 &count){return CreateVertexBufferData(GL_UNSIGNED_INT, 4,1,count);} VK_NAMESPACE_END diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index be111f80..208fd189 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -34,11 +34,11 @@ class Device RenderPass *main_rp; ObjectList main_fb; - + uint32_t current_frame; VkPresentInfoKHR present; - + private: friend Device *CreateRenderDevice(VkInstance,const PhysicalDevice *,Window *); @@ -74,7 +74,7 @@ public: //Buffer Buffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,const void *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE); Buffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateBuffer(buf_usage,size,nullptr,sharing_mode);} - + VertexBuffer * CreateVBO(VkFormat format,uint32_t count,const void *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE); VertexBuffer * CreateVBO(VkFormat format,uint32_t count,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateVBO(format,count,nullptr,sharing_mode);} @@ -96,7 +96,7 @@ public: //Buffer #undef CREATE_BUFFER_OBJECT public: //material - + ShaderModuleManage *CreateShaderModuleManage(); public: //Command Buffer diff --git a/inc/hgl/graph/vulkan/VKDeviceAttribute.h b/inc/hgl/graph/vulkan/VKDeviceAttribute.h index 73c95655..48b2da8d 100644 --- a/inc/hgl/graph/vulkan/VKDeviceAttribute.h +++ b/inc/hgl/graph/vulkan/VKDeviceAttribute.h @@ -51,7 +51,7 @@ struct DeviceAttribute }depth; VkDescriptorPool desc_pool =nullptr; - + VkPipelineCache pipeline_cache =nullptr; public: diff --git a/inc/hgl/graph/vulkan/VKFormat.h b/inc/hgl/graph/vulkan/VKFormat.h index d59d55fa..6c409f07 100644 --- a/inc/hgl/graph/vulkan/VKFormat.h +++ b/inc/hgl/graph/vulkan/VKFormat.h @@ -45,14 +45,14 @@ #define FMT_ABGR8I VK_FORMAT_A8B8G8R8_SINT_PACK32 #define FMT_ABGR8s VK_FORMAT_A8B8G8R8_SRGB_PACK32 -#define FMT_A2RGB10UN VK_FORMAT_A2R10G10B10_UNORM_PACK32 +#define FMT_A2RGB10UN VK_FORMAT_A2R10G10B10_UNORM_PACK32 #define FMT_A2RGB10SN VK_FORMAT_A2R10G10B10_SNORM_PACK32 -#define FMT_A2RGB10U VK_FORMAT_A2R10G10B10_UINT_PACK32 +#define FMT_A2RGB10U VK_FORMAT_A2R10G10B10_UINT_PACK32 #define FMT_A2RGB10I VK_FORMAT_A2R10G10B10_SINT_PACK32 -#define FMT_A2BGR10UN VK_FORMAT_A2B10G10R10_UNORM_PACK32 +#define FMT_A2BGR10UN VK_FORMAT_A2B10G10R10_UNORM_PACK32 #define FMT_A2BGR10SN VK_FORMAT_A2B10G10R10_SNORM_PACK32 -#define FMT_A2BGR10U VK_FORMAT_A2B10G10R10_UINT_PACK32 +#define FMT_A2BGR10U VK_FORMAT_A2B10G10R10_UINT_PACK32 #define FMT_A2BGR10I VK_FORMAT_A2B10G10R10_SINT_PACK32 #define FMT_R16UN VK_FORMAT_R16_UNORM diff --git a/inc/hgl/graph/vulkan/VKPhysicalDevice.h b/inc/hgl/graph/vulkan/VKPhysicalDevice.h index aaac1102..bf66209c 100644 --- a/inc/hgl/graph/vulkan/VKPhysicalDevice.h +++ b/inc/hgl/graph/vulkan/VKPhysicalDevice.h @@ -35,7 +35,7 @@ public: return(false); } - + const bool isDiscreteGPU ()const{return(properties.deviceType==VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU);} ///<ǷǶԿ const bool isIntegratedGPU ()const{return(properties.deviceType==VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU);} ///<ǷǼԿ const bool isVirtualGPU ()const{return(properties.deviceType==VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU);} ///<ǷԿ diff --git a/inc/hgl/graph/vulkan/VKPrimivate.h b/inc/hgl/graph/vulkan/VKPrimivate.h index c2bd0869..13b52c77 100644 --- a/inc/hgl/graph/vulkan/VKPrimivate.h +++ b/inc/hgl/graph/vulkan/VKPrimivate.h @@ -13,10 +13,10 @@ #define PRIM_LINE_STRIP_ADJ VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY ///<与LINES_ADJACENCY类似,第一个点跟最后一个点提供信息,剩下的点则跟Line Strip一样形成线段. #define PRIM_TRIANGLES_ADJ VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY ///<代表一个有六个顶点的Primitive,其中第1,3,5个顶点代表一个Triangle,而地2,4,6个点提供邻近信息.(由1起算) #define PRIM_TRIANGLE_STRIP_ADJ VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY ///<4+2N个Vertices代表N个Primitive,其中1,3,5,7,9...代表原本的Triangle strip形成Triangle,而2,4,6,8,10...代表邻近提供信息的点.(由1起算) -#define PRIM_PATCHS VK_PRIMITIVE_TOPOLOGY_PATCH_LIST +#define PRIM_PATCHS VK_PRIMITIVE_TOPOLOGY_PATCH_LIST #define PRIM_RECTANGLE 0x100 ///<矩形(并非原生支持。以画点形式在每个点的Position中传递Left,Top,Width,Height。在Geometry Shader中转换为2个三角形。用于2D游戏或UI) -#define PRIM_BEGIN VK_PRIMITIVE_TOPOLOGY_BEGIN_RANGE -#define PRIM_END VK_PRIMITIVE_TOPOLOGY_END_RANGE -#define PRIM_RANGE VK_PRIMITIVE_TOPOLOGY_RANGE_SIZE +#define PRIM_BEGIN VK_PRIMITIVE_TOPOLOGY_BEGIN_RANGE +#define PRIM_END VK_PRIMITIVE_TOPOLOGY_END_RANGE +#define PRIM_RANGE VK_PRIMITIVE_TOPOLOGY_RANGE_SIZE #endif//HGL_GRAPH_VULKAN_PRIMITIVE_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKShaderModule.h b/inc/hgl/graph/vulkan/VKShaderModule.h index f7c42359..9da1aaf6 100644 --- a/inc/hgl/graph/vulkan/VKShaderModule.h +++ b/inc/hgl/graph/vulkan/VKShaderModule.h @@ -33,7 +33,7 @@ protected: void ParseUBO(const ShaderParse *); public: - + ShaderModule(int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *); virtual ~ShaderModule(); @@ -50,10 +50,10 @@ public: const int GetUBO (const UTF8String &name)const { int binding; - + if(ubo_map.Get(name,binding)) return binding; - else + else return -1; } diff --git a/inc/hgl/graph/vulkan/VKShaderParse.h b/inc/hgl/graph/vulkan/VKShaderParse.h index cfcee472..1ee04127 100644 --- a/inc/hgl/graph/vulkan/VKShaderParse.h +++ b/inc/hgl/graph/vulkan/VKShaderParse.h @@ -48,7 +48,7 @@ public: const VkFormat GetFormat(const spirv_cross::Resource &res)const { const spirv_cross::SPIRType &type=compiler->get_type(res.type_id); - + constexpr VkFormat format[][4]= { {FMT_R8I,FMT_RG8I,FMT_RGB8I,FMT_RGBA8I}, //sbyte diff --git a/inc/hgl/io/EndianDataInputStream.h b/inc/hgl/io/EndianDataInputStream.h index c313e066..54442b35 100644 --- a/inc/hgl/io/EndianDataInputStream.h +++ b/inc/hgl/io/EndianDataInputStream.h @@ -5,96 +5,96 @@ #include namespace hgl { - namespace io - { - class DirectDataInputStream:public DataInputStream - { - public: + namespace io + { + class DirectDataInputStream:public DataInputStream + { + public: - using DataInputStream::DataInputStream; - virtual ~DirectDataInputStream()=default; + using DataInputStream::DataInputStream; + virtual ~DirectDataInputStream()=default; - #define STREAM_DIRECT_READ(type,name) virtual bool Read##name(type &value) \ - { \ - return Read(value); \ - } \ - \ - virtual int64 Read##name(type *data,const int64 count) \ - { \ - if(count<=0)return(count); \ - if(!data)return(-1); \ - \ - return ReadArrays(data,count); \ - } + #define STREAM_DIRECT_READ(type,name) virtual bool Read##name(type &value) \ + { \ + return Read(value); \ + } \ + \ + virtual int64 Read##name(type *data,const int64 count) \ + { \ + if(count<=0)return(count); \ + if(!data)return(-1); \ + \ + return ReadArrays(data,count); \ + } - STREAM_DIRECT_READ(int16, Int16 ); - STREAM_DIRECT_READ(int32, Int32 ); - STREAM_DIRECT_READ(int64, Int64 ); + STREAM_DIRECT_READ(int16, Int16 ); + STREAM_DIRECT_READ(int32, Int32 ); + STREAM_DIRECT_READ(int64, Int64 ); - STREAM_DIRECT_READ(uint16, Uint16 ); - STREAM_DIRECT_READ(uint32, Uint32 ); - STREAM_DIRECT_READ(uint64, Uint64 ); + STREAM_DIRECT_READ(uint16, Uint16 ); + STREAM_DIRECT_READ(uint32, Uint32 ); + STREAM_DIRECT_READ(uint64, Uint64 ); - STREAM_DIRECT_READ(u16char,UTF16Char); - STREAM_DIRECT_READ(float, Float ); - STREAM_DIRECT_READ(double, Double ); + STREAM_DIRECT_READ(u16char,UTF16Char); + STREAM_DIRECT_READ(float, Float ); + STREAM_DIRECT_READ(double, Double ); - #undef STREAM_DIRECT_READ - };//class DirectDataInputStream + #undef STREAM_DIRECT_READ + };//class DirectDataInputStream - class SwapDataInputStream:public DataInputStream - { - public: + class SwapDataInputStream:public DataInputStream + { + public: - using DataInputStream::DataInputStream; - virtual ~SwapDataInputStream()=default; + using DataInputStream::DataInputStream; + virtual ~SwapDataInputStream()=default; - #define STREAM_SWAP_READ(type,name) virtual bool Read##name(type &value) \ - {\ - type swap_value; \ - \ - if(!Read(swap_value)) \ - return(false); \ - \ - value=EndianSwap(swap_value); \ - return(true); \ - } \ - \ - virtual int64 Read##name(type *data,const int64 count) \ - { \ - if(count<=0)return(count); \ - if(!data)return(-1); \ - \ - const int64 result=ReadArrays(data,count); \ - \ - if(result<=0)return(result); \ - \ - EndianSwap(data,result); \ - return(result); \ - } + #define STREAM_SWAP_READ(type,name) virtual bool Read##name(type &value) \ + {\ + type swap_value; \ + \ + if(!Read(swap_value)) \ + return(false); \ + \ + value=EndianSwap(swap_value); \ + return(true); \ + } \ + \ + virtual int64 Read##name(type *data,const int64 count) \ + { \ + if(count<=0)return(count); \ + if(!data)return(-1); \ + \ + const int64 result=ReadArrays(data,count); \ + \ + if(result<=0)return(result); \ + \ + EndianSwap(data,result); \ + return(result); \ + } - STREAM_SWAP_READ(int16, Int16 ); - STREAM_SWAP_READ(int32, Int32 ); - STREAM_SWAP_READ(int64, Int64 ); + STREAM_SWAP_READ(int16, Int16 ); + STREAM_SWAP_READ(int32, Int32 ); + STREAM_SWAP_READ(int64, Int64 ); - STREAM_SWAP_READ(uint16, Uint16 ); - STREAM_SWAP_READ(uint32, Uint32 ); - STREAM_SWAP_READ(uint64, Uint64 ); + STREAM_SWAP_READ(uint16, Uint16 ); + STREAM_SWAP_READ(uint32, Uint32 ); + STREAM_SWAP_READ(uint64, Uint64 ); - STREAM_SWAP_READ(u16char, UTF16Char); - STREAM_SWAP_READ(float, Float ); - STREAM_SWAP_READ(double, Double ); + STREAM_SWAP_READ(u16char, UTF16Char); + STREAM_SWAP_READ(float, Float ); + STREAM_SWAP_READ(double, Double ); - #undef STREAM_SWAP_READ - };//class SwapDataInputStream + #undef STREAM_SWAP_READ + };//class SwapDataInputStream #if HGL_ENDIAN == HGL_LITTLE_ENDIAN - typedef DirectDataInputStream LEDataInputStream; - typedef SwapDataInputStream BEDataInputStream; + typedef DirectDataInputStream LEDataInputStream; + typedef SwapDataInputStream BEDataInputStream; #else - typedef DirectDataInputStream BEDataInputStream; - typedef SwapDataInputStream LEDataInputStream; + typedef DirectDataInputStream BEDataInputStream; + typedef SwapDataInputStream LEDataInputStream; #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN - }//namespace io + }//namespace io }//namespace hgl #endif//HGL_IO_ENDIAN_DATA_INPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/EndianDataOutputStream.h b/inc/hgl/io/EndianDataOutputStream.h index 66fbfb8c..90190378 100644 --- a/inc/hgl/io/EndianDataOutputStream.h +++ b/inc/hgl/io/EndianDataOutputStream.h @@ -5,99 +5,99 @@ #include namespace hgl { - namespace io - { - class DirectDataOutputStream:public DataOutputStream - { - public: + namespace io + { + class DirectDataOutputStream:public DataOutputStream + { + public: - DirectDataOutputStream(OutputStream *os):DataOutputStream(os) - { - } + DirectDataOutputStream(OutputStream *os):DataOutputStream(os) + { + } - virtual ~DirectDataOutputStream() - { - } + virtual ~DirectDataOutputStream() + { + } - #define STREAM_DIRECT_WRITE(type,name) virtual bool Write##name(const type &value) \ - { \ - return Write(value); \ - } \ - \ - virtual int64 Write##name(const type *data,const int64 count) \ - { \ - if(count<=0)return(count); \ - if(!data)return(-1); \ - \ - return WriteArrays(data,count); \ - } + #define STREAM_DIRECT_WRITE(type,name) virtual bool Write##name(const type &value) \ + { \ + return Write(value); \ + } \ + \ + virtual int64 Write##name(const type *data,const int64 count) \ + { \ + if(count<=0)return(count); \ + if(!data)return(-1); \ + \ + return WriteArrays(data,count); \ + } - STREAM_DIRECT_WRITE(int16, Int16 ); - STREAM_DIRECT_WRITE(int32, Int32 ); - STREAM_DIRECT_WRITE(int64, Int64 ); + STREAM_DIRECT_WRITE(int16, Int16 ); + STREAM_DIRECT_WRITE(int32, Int32 ); + STREAM_DIRECT_WRITE(int64, Int64 ); - STREAM_DIRECT_WRITE(uint16, Uint16 ); - STREAM_DIRECT_WRITE(uint32, Uint32 ); - STREAM_DIRECT_WRITE(uint64, Uint64 ); + STREAM_DIRECT_WRITE(uint16, Uint16 ); + STREAM_DIRECT_WRITE(uint32, Uint32 ); + STREAM_DIRECT_WRITE(uint64, Uint64 ); - STREAM_DIRECT_WRITE(u16char,UTF16Char); - STREAM_DIRECT_WRITE(float, Float ); - STREAM_DIRECT_WRITE(double, Double ); + STREAM_DIRECT_WRITE(u16char,UTF16Char); + STREAM_DIRECT_WRITE(float, Float ); + STREAM_DIRECT_WRITE(double, Double ); - #undef STREAM_DIRECT_WRITE - };//class DirectDataOutputStream + #undef STREAM_DIRECT_WRITE + };//class DirectDataOutputStream - class SwapDataOutputStream:public DataOutputStream - { - public: + class SwapDataOutputStream:public DataOutputStream + { + public: - SwapDataOutputStream(OutputStream *os):DataOutputStream(os) - { - } + SwapDataOutputStream(OutputStream *os):DataOutputStream(os) + { + } - virtual ~SwapDataOutputStream() - { - } + virtual ~SwapDataOutputStream() + { + } - #define STREAM_SWAP_WRITE(type,name) virtual bool Write##name(const type &value) \ - {\ - return Write(EndianSwap(value)); \ - } \ - \ - virtual int64 Write##name(const type *data,const int64 count) \ - { \ - if(count<=0)return(count); \ - if(!data)return(-1); \ - \ - SharedArray swap_data=new type[count]; \ - \ - EndianSwap(swap_data,data,count); \ - \ - return WriteArrays(swap_data,count); \ - } + #define STREAM_SWAP_WRITE(type,name) virtual bool Write##name(const type &value) \ + {\ + return Write(EndianSwap(value)); \ + } \ + \ + virtual int64 Write##name(const type *data,const int64 count) \ + { \ + if(count<=0)return(count); \ + if(!data)return(-1); \ + \ + SharedArray swap_data=new type[count]; \ + \ + EndianSwap(swap_data,data,count); \ + \ + return WriteArrays(swap_data,count); \ + } - STREAM_SWAP_WRITE(int16, Int16 ); - STREAM_SWAP_WRITE(int32, Int32 ); - STREAM_SWAP_WRITE(int64, Int64 ); + STREAM_SWAP_WRITE(int16, Int16 ); + STREAM_SWAP_WRITE(int32, Int32 ); + STREAM_SWAP_WRITE(int64, Int64 ); - STREAM_SWAP_WRITE(uint16, Uint16 ); - STREAM_SWAP_WRITE(uint32, Uint32 ); - STREAM_SWAP_WRITE(uint64, Uint64 ); + STREAM_SWAP_WRITE(uint16, Uint16 ); + STREAM_SWAP_WRITE(uint32, Uint32 ); + STREAM_SWAP_WRITE(uint64, Uint64 ); - STREAM_SWAP_WRITE(u16char, UTF16Char); - STREAM_SWAP_WRITE(float, Float ); - STREAM_SWAP_WRITE(double, Double ); + STREAM_SWAP_WRITE(u16char, UTF16Char); + STREAM_SWAP_WRITE(float, Float ); + STREAM_SWAP_WRITE(double, Double ); - #undef STREAM_SWAP_WRITE - };//class SwapDataOutputStream + #undef STREAM_SWAP_WRITE + };//class SwapDataOutputStream #if HGL_ENDIAN == HGL_LITTLE_ENDIAN - typedef DirectDataOutputStream LEDataOutputStream; - typedef SwapDataOutputStream BEDataOutputStream; + typedef DirectDataOutputStream LEDataOutputStream; + typedef SwapDataOutputStream BEDataOutputStream; #else - typedef DirectDataOutputStream BEDataOutputStream; - typedef SwapDataOutputStream LEDataOutputStream; + typedef DirectDataOutputStream BEDataOutputStream; + typedef SwapDataOutputStream LEDataOutputStream; #endif//HGL_ENDIAN == HGL_LITTLE_ENDIAN - }//namespace io + }//namespace io }//namespace hgl #endif//HGL_IO_ENDIAN_DATA_OUTPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/FileAccess.h b/inc/hgl/io/FileAccess.h index 8d61fe2f..85669978 100644 --- a/inc/hgl/io/FileAccess.h +++ b/inc/hgl/io/FileAccess.h @@ -6,78 +6,78 @@ #include namespace hgl { - namespace io - { - enum FileOpenMode ///文件访问模式枚举 - { - fomNone=0, + namespace io + { + enum FileOpenMode ///文件访问模式枚举 + { + fomNone=0, - fomCreate, ///<创建文件,如存在则失败 - fomCreateTrunc, ///<强制创建,如存在则抹掉 - fomCreateTemp, - fomOnlyRead, ///<只读方式 - fomOnlyWrite, ///<只写方式 - fomReadWrite, ///<可读可写 - fomAppend, ///<追加模式 + fomCreate, ///<创建文件,如存在则失败 + fomCreateTrunc, ///<强制创建,如存在则抹掉 + fomCreateTemp, + fomOnlyRead, ///<只读方式 + fomOnlyWrite, ///<只写方式 + fomReadWrite, ///<可读可写 + fomAppend, ///<追加模式 - fomEnd - };//enum FileOpenMode + fomEnd + };//enum FileOpenMode - /** - * 文件访问实例管理类 - */ - class FileAccess ///文件访问实例管理类 - { - protected: + /** + * 文件访问实例管理类 + */ + class FileAccess ///文件访问实例管理类 + { + protected: - OSString filename; + OSString filename; - int fp; + int fp; - struct_stat64 file_state; + struct_stat64 file_state; - FileOpenMode mode; + FileOpenMode mode; - public: + public: - FileAccess(); - virtual ~FileAccess(); + FileAccess(); + virtual ~FileAccess(); - virtual bool Open(const OSString &,FileOpenMode fom); ///<以指定模式打开一个文件 - virtual bool Create(const OSString &fn){return Open(fn,fomCreate);} ///<创建一个新文件,如文件已存在则创建失败 - virtual bool CreateTrunc(const OSString &fn){return Open(fn,fomCreateTrunc);} ///<创建一个新文件,如文件已存在则抹消它 - virtual bool OpenRead(const OSString &fn){return Open(fn,fomOnlyRead);} ///<以只读模式打开一个文件 - virtual bool OpenWrite(const OSString &fn){return Open(fn,fomOnlyWrite);} ///<以只写模式打开一个文件 - virtual bool OpenReadWrite(const OSString &fn){return Open(fn,fomReadWrite);} ///<以读写模式打开一个文件 - virtual bool CreateTemp(); + virtual bool Open(const OSString &,FileOpenMode fom); ///<以指定模式打开一个文件 + virtual bool Create(const OSString &fn){return Open(fn,fomCreate);} ///<创建一个新文件,如文件已存在则创建失败 + virtual bool CreateTrunc(const OSString &fn){return Open(fn,fomCreateTrunc);} ///<创建一个新文件,如文件已存在则抹消它 + virtual bool OpenRead(const OSString &fn){return Open(fn,fomOnlyRead);} ///<以只读模式打开一个文件 + virtual bool OpenWrite(const OSString &fn){return Open(fn,fomOnlyWrite);} ///<以只写模式打开一个文件 + virtual bool OpenReadWrite(const OSString &fn){return Open(fn,fomReadWrite);} ///<以读写模式打开一个文件 + virtual bool CreateTemp(); - virtual void Close(); ///<关闭文件 - virtual void CloseRead(); ///<仅关闭读取 - virtual void CloseWrite(); ///<仅关闭写入 + virtual void Close(); ///<关闭文件 + virtual void CloseRead(); ///<仅关闭读取 + virtual void CloseWrite(); ///<仅关闭写入 - virtual int GetFileHandle()const{return fp;} + virtual int GetFileHandle()const{return fp;} - virtual bool CanRead()const; ///<文件是否可读 - virtual bool CanPeek()const{return(CanRead()&&CanSeek());} ///<文件是否可预读 - virtual bool CanWrite()const; ///<文件是否可写 - virtual bool CanSeek()const; ///<文件是否可定位访问指针 - virtual bool CanRestart()const{return CanSeek();} ///<文件是否可复位访问 - virtual bool CanSize()const{return(true);} ///<文件是否可取得长度 + virtual bool CanRead()const; ///<文件是否可读 + virtual bool CanPeek()const{return(CanRead()&&CanSeek());} ///<文件是否可预读 + virtual bool CanWrite()const; ///<文件是否可写 + virtual bool CanSeek()const; ///<文件是否可定位访问指针 + virtual bool CanRestart()const{return CanSeek();} ///<文件是否可复位访问 + virtual bool CanSize()const{return(true);} ///<文件是否可取得长度 - virtual int64 Seek(int64,SeekOrigin=soBegin); ///<定位访问指针 - virtual int64 Tell()const; ///<取得访问指针位置 - virtual int64 GetSize(); ///<取得文件长度 - virtual bool Restart(); ///<复位访问指针 + virtual int64 Seek(int64,SeekOrigin=soBegin); ///<定位访问指针 + virtual int64 Tell()const; ///<取得访问指针位置 + virtual int64 GetSize(); ///<取得文件长度 + virtual bool Restart(); ///<复位访问指针 - virtual int64 Read(void *,int64); ///<读取指定长度数据 - virtual int64 Peek(void *,int64); ///<预读指定长度数据 - virtual int64 Write(const void *,int64); ///<写入指定长度数据 + virtual int64 Read(void *,int64); ///<读取指定长度数据 + virtual int64 Peek(void *,int64); ///<预读指定长度数据 + virtual int64 Write(const void *,int64); ///<写入指定长度数据 - virtual int64 AvailableRead(); ///<剩下的可读数据 + virtual int64 AvailableRead(); ///<剩下的可读数据 - virtual int64 Read(int64,void *,int64); ///<在指定位置读取指定长度的数据 - virtual int64 Write(int64,const void *,int64); ///<在指定位置写入指定长度的数据 - };//class FileAccess - }//namespace io + virtual int64 Read(int64,void *,int64); ///<在指定位置读取指定长度的数据 + virtual int64 Write(int64,const void *,int64); ///<在指定位置写入指定长度的数据 + };//class FileAccess + }//namespace io }//namespace hgl #endif//HGL_IO_FILE_ACCESS_INCLUDE diff --git a/inc/hgl/io/FileInputStream.h b/inc/hgl/io/FileInputStream.h index 5490bf2a..72fe582e 100644 --- a/inc/hgl/io/FileInputStream.h +++ b/inc/hgl/io/FileInputStream.h @@ -5,74 +5,74 @@ #include namespace hgl { - namespace io - { - /** - * 文件输入流类 - */ - class FileInputStream:public InputStream ///文件输入流类 - { - protected: + namespace io + { + /** + * 文件输入流类 + */ + class FileInputStream:public InputStream ///文件输入流类 + { + protected: - SharedPtr file; ///<文件访问指针 + SharedPtr file; ///<文件访问指针 - public: + public: - FileInputStream(); - FileInputStream(SharedPtr &); - virtual ~FileInputStream(); + FileInputStream(); + FileInputStream(SharedPtr &); + virtual ~FileInputStream(); - virtual bool Open(const OSString &); ///<打开文件 - virtual void Close(); ///<关闭文件输入流 + virtual bool Open(const OSString &); ///<打开文件 + virtual void Close(); ///<关闭文件输入流 - virtual int GetFileHandle()const{return file->GetFileHandle();} + virtual int GetFileHandle()const{return file->GetFileHandle();} - virtual int64 Read(void *,int64); ///<读取数据 - virtual int64 Peek(void *,int64); ///<预读数据 + virtual int64 Read(void *,int64); ///<读取数据 + virtual int64 Peek(void *,int64); ///<预读数据 - virtual bool CanSeek()const; ///<是否可移动访问指针 - virtual bool CanRestart()const; ///<是否可复位访问 - virtual bool CanSize()const; ///<是否可访问文件长度 - virtual bool CanPeek()const; ///<是否可预读数据 + virtual bool CanSeek()const; ///<是否可移动访问指针 + virtual bool CanRestart()const; ///<是否可复位访问 + virtual bool CanSize()const; ///<是否可访问文件长度 + virtual bool CanPeek()const; ///<是否可预读数据 - virtual int64 Skip(int64); ///<跳过指定字节 - virtual int64 Tell()const; ///<取当前位置 - virtual int64 GetSize()const; ///<取得文件长度 - virtual bool Restart(); ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 + virtual int64 Skip(int64); ///<跳过指定字节 + virtual int64 Tell()const; ///<取当前位置 + virtual int64 GetSize()const; ///<取得文件长度 + virtual bool Restart(); ///<复位访问指针 + virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 - virtual int64 Available()const; ///<剩下的可以不受阻塞访问的字节数 + virtual int64 Available()const; ///<剩下的可以不受阻塞访问的字节数 - virtual int64 Read(int64,void *,int64); ///<在指定位置读取指定长度的数据 - };//class FileInputStream + virtual int64 Read(int64,void *,int64); ///<在指定位置读取指定长度的数据 + };//class FileInputStream - class OpenFileInputStream - { - FileInputStream *fis; + class OpenFileInputStream + { + FileInputStream *fis; - public: + public: - OpenFileInputStream(const OSString &filename) - { - fis=new FileInputStream(); + OpenFileInputStream(const OSString &filename) + { + fis=new FileInputStream(); - if(!fis->Open(filename)) + if(!fis->Open(filename)) { delete fis; fis=nullptr; } - } + } - ~OpenFileInputStream() - { - SAFE_CLEAR(fis); - } + ~OpenFileInputStream() + { + SAFE_CLEAR(fis); + } - const bool operator !(){return !fis;} - operator FileInputStream *(){return fis;} - FileInputStream *operator &(){return fis;} - FileInputStream *operator ->(){return fis;} - };//class OpenFileInputStream - }//namespace io + const bool operator !(){return !fis;} + operator FileInputStream *(){return fis;} + FileInputStream *operator &(){return fis;} + FileInputStream *operator ->(){return fis;} + };//class OpenFileInputStream + }//namespace io }//namespace hgl #endif//HGL_IO_FILE_INPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/FileOutputStream.h b/inc/hgl/io/FileOutputStream.h index ca497a84..e55d798b 100644 --- a/inc/hgl/io/FileOutputStream.h +++ b/inc/hgl/io/FileOutputStream.h @@ -5,89 +5,89 @@ #include namespace hgl { - namespace io - { - /** - * 文件输出流类 - */ - class FileOutputStream:public OutputStream ///文件输出流类 - { - protected: + namespace io + { + /** + * 文件输出流类 + */ + class FileOutputStream:public OutputStream ///文件输出流类 + { + protected: - SharedPtr file; ///<文件访问指针 + SharedPtr file; ///<文件访问指针 - virtual bool OpenFile(const OSString &,FileOpenMode); + virtual bool OpenFile(const OSString &,FileOpenMode); - public: + public: - FileOutputStream(); - FileOutputStream(SharedPtr &); - virtual ~FileOutputStream(); + FileOutputStream(); + FileOutputStream(SharedPtr &); + virtual ~FileOutputStream(); - virtual bool Open(const OSString &fn,FileOpenMode mode){return OpenFile(fn,mode);} ///<打开文件,指定一个模式 - virtual bool Open(const OSString &fn){return OpenFile(fn,fomOnlyWrite);} ///<打开文件 - virtual bool Create(const OSString &fn){return OpenFile(fn,fomCreate);} ///<创建文件,如存在创建失败 - virtual bool CreateTrunc(const OSString &fn){return OpenFile(fn,fomCreateTrunc);} ///<创建文件,如存在则抹去 - virtual bool OpenAppend(const OSString &fn){return OpenFile(fn,fomAppend);} ///<打开文件,追加模式 + virtual bool Open(const OSString &fn,FileOpenMode mode){return OpenFile(fn,mode);} ///<打开文件,指定一个模式 + virtual bool Open(const OSString &fn){return OpenFile(fn,fomOnlyWrite);} ///<打开文件 + virtual bool Create(const OSString &fn){return OpenFile(fn,fomCreate);} ///<创建文件,如存在创建失败 + virtual bool CreateTrunc(const OSString &fn){return OpenFile(fn,fomCreateTrunc);} ///<创建文件,如存在则抹去 + virtual bool OpenAppend(const OSString &fn){return OpenFile(fn,fomAppend);} ///<打开文件,追加模式 - virtual void Close(); ///<关闭文件 + virtual void Close(); ///<关闭文件 - virtual int64 Write(const void *,int64); ///<写入数据 + virtual int64 Write(const void *,int64); ///<写入数据 - virtual bool CanSeek()const; ///<是否可移动访问指针 - virtual bool CanRestart()const; ///<是否可复位访问 - virtual bool CanSize()const; ///<是否可访问文件长度 + virtual bool CanSeek()const; ///<是否可移动访问指针 + virtual bool CanRestart()const; ///<是否可复位访问 + virtual bool CanSize()const; ///<是否可访问文件长度 - virtual int64 Tell()const; ///<取当前位置 - virtual int64 GetSize()const; ///<取得文件长度 - virtual bool Restart(); ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 - virtual int64 Available()const{return -1;} ///<可不受影响写入的字节数 + virtual int64 Tell()const; ///<取当前位置 + virtual int64 GetSize()const; ///<取得文件长度 + virtual bool Restart(); ///<复位访问指针 + virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 + virtual int64 Available()const{return -1;} ///<可不受影响写入的字节数 - virtual int64 Write(int64,const void *,int64); ///<在指定位置写入指定长度的数据 - };//class FileOutputStream + virtual int64 Write(int64,const void *,int64); ///<在指定位置写入指定长度的数据 + };//class FileOutputStream - /** - * 打开一个文件输出流 - */ - class OpenFileOutputStream - { - FileOutputStream *fos; + /** + * 打开一个文件输出流 + */ + class OpenFileOutputStream + { + FileOutputStream *fos; - public: + public: OpenFileOutputStream() { fos=nullptr; } - /** - * 打开一个文件输出流构造函数 - * @param filename 文件名 - * @param mode 打开模式,默认只写(必然可读) - * @see FileOpenMode - */ - OpenFileOutputStream(const OSString &filename,FileOpenMode mode=fomOnlyWrite) - { - fos=new FileOutputStream(); + /** + * 打开一个文件输出流构造函数 + * @param filename 文件名 + * @param mode 打开模式,默认只写(必然可读) + * @see FileOpenMode + */ + OpenFileOutputStream(const OSString &filename,FileOpenMode mode=fomOnlyWrite) + { + fos=new FileOutputStream(); - if(!fos->Open(filename)) + if(!fos->Open(filename)) { delete fos; fos=nullptr; } - } + } - ~OpenFileOutputStream() - { - SAFE_CLEAR(fos); + ~OpenFileOutputStream() + { + SAFE_CLEAR(fos); } const bool operator !(){return !fos;} - operator FileOutputStream *(){return fos;} - FileOutputStream *operator &(){return fos;} - FileOutputStream *operator ->(){return fos;} - };//class OpenFileInputStream - }//namespace io + operator FileOutputStream *(){return fos;} + FileOutputStream *operator &(){return fos;} + FileOutputStream *operator ->(){return fos;} + };//class OpenFileInputStream + }//namespace io }//namespace hgl #endif//HGL_IO_FILE_OUTPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/IOType.h b/inc/hgl/io/IOType.h index 5456d091..d09068a5 100644 --- a/inc/hgl/io/IOType.h +++ b/inc/hgl/io/IOType.h @@ -4,31 +4,31 @@ #include namespace hgl { - namespace io - { - class DataInputStream; - class DataOutputStream; + namespace io + { + class DataInputStream; + class DataOutputStream; - template struct io_type - { - T value; + template struct io_type + { + T value; - public: + public: - io_type()=default; - io_type(const T &v){value=v;} + io_type()=default; + io_type(const T &v){value=v;} - bool Read(DataInputStream *); - bool Write(DataOutputStream *)const; + bool Read(DataInputStream *); + bool Write(DataOutputStream *)const; - void operator = (const T &v) - { - value=v; - } + void operator = (const T &v) + { + value=v; + } - operator T (){return value;} - operator const T ()const{return value;} - };//template struct io_type - }//namespace io + operator T (){return value;} + operator const T ()const{return value;} + };//template struct io_type + }//namespace io }//namespace hgl #endif//HGL_IO_TYPE_INCLUDE diff --git a/inc/hgl/io/InputStream.h b/inc/hgl/io/InputStream.h index 9f81e226..c2dd5963 100644 --- a/inc/hgl/io/InputStream.h +++ b/inc/hgl/io/InputStream.h @@ -5,35 +5,35 @@ #include namespace hgl { - namespace io - { - /** - * 数据输入流基类 - */ - class InputStream:public SeekAccess ///数据输入流基类 - { - public: + namespace io + { + /** + * 数据输入流基类 + */ + class InputStream:public SeekAccess ///数据输入流基类 + { + public: - virtual ~InputStream()=default; + virtual ~InputStream()=default; - virtual void Close()=0; ///<关闭输入流 + virtual void Close()=0; ///<关闭输入流 - virtual int64 Read(void *,int64)=0; ///<读取数据 - virtual int64 Peek(void *,int64)=0; ///<预览数据 - virtual int64 ReadFully(void *buf,int64 buf_size){return Read(buf,buf_size);} ///<充分读取,保证读取到指定长度的数据(不计算超时) + virtual int64 Read(void *,int64)=0; ///<读取数据 + virtual int64 Peek(void *,int64)=0; ///<预览数据 + virtual int64 ReadFully(void *buf,int64 buf_size){return Read(buf,buf_size);} ///<充分读取,保证读取到指定长度的数据(不计算超时) - virtual bool CanRestart()const=0; ///<是否可以复位 - virtual bool CanSeek()const=0; ///<是否可以定位 - virtual bool CanSize()const=0; ///<是否可以取得尺寸 - virtual bool CanPeek()const=0; ///<是否可以预览数据 + virtual bool CanRestart()const=0; ///<是否可以复位 + virtual bool CanSeek()const=0; ///<是否可以定位 + virtual bool CanSize()const=0; ///<是否可以取得尺寸 + virtual bool CanPeek()const=0; ///<是否可以预览数据 - virtual bool Restart()=0; ///<复位访问指针 - virtual int64 Skip(int64)=0; ///<跳过指定字节不访问 - virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 - virtual int64 Tell()const=0; ///<返回当前访问位置 - virtual int64 GetSize()const=0; ///<取得流长度 - virtual int64 Available()const=0; ///<剩下的可以不受阻塞访问的字节数 - };//class InputStream - }//namespace io + virtual bool Restart()=0; ///<复位访问指针 + virtual int64 Skip(int64)=0; ///<跳过指定字节不访问 + virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 + virtual int64 Tell()const=0; ///<返回当前访问位置 + virtual int64 GetSize()const=0; ///<取得流长度 + virtual int64 Available()const=0; ///<剩下的可以不受阻塞访问的字节数 + };//class InputStream + }//namespace io }//namespace hgl #endif//HGL_IO_INPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/JavaInputStream.h b/inc/hgl/io/JavaInputStream.h index 7b17b5b3..3b07a1c4 100644 --- a/inc/hgl/io/JavaInputStream.h +++ b/inc/hgl/io/JavaInputStream.h @@ -4,68 +4,68 @@ #include namespace hgl { - namespace io - { - /** - * Java数据输入流
- * 接口类似于java.io.DataInputStream,用于和Java程序进行交互 - */ - class JavaInputStream - { - protected: + namespace io + { + /** + * Java数据输入流
+ * 接口类似于java.io.DataInputStream,用于和Java程序进行交互 + */ + class JavaInputStream + { + protected: - BEDataInputStream *in; + BEDataInputStream *in; - public: + public: - JavaInputStream(InputStream *is) - { - in=new BEDataInputStream(is); - } + JavaInputStream(InputStream *is) + { + in=new BEDataInputStream(is); + } - virtual ~JavaInputStream() - { - delete in; - } + virtual ~JavaInputStream() + { + delete in; + } - int64 read (void *ptr,int size){return in?in->ReadFully(ptr,size):-1;} - int skipBytes (int size) {return in?in->Seek(size,soCurrent):-1;} + int64 read (void *ptr,int size){return in?in->ReadFully(ptr,size):-1;} + int skipBytes (int size) {return in?in->Seek(size,soCurrent):-1;} - bool readBoolean (bool &b) {return in?in->ReadBool (b):false;} - bool readByte (int8 &i) {return in?in->ReadInt8 (i):false;} - bool readUnsignedByte (uint8 &i) {return in?in->ReadUint8 (i):false;} + bool readBoolean (bool &b) {return in?in->ReadBool (b):false;} + bool readByte (int8 &i) {return in?in->ReadInt8 (i):false;} + bool readUnsignedByte (uint8 &i) {return in?in->ReadUint8 (i):false;} - bool readShort (int16 &i) {return in?in->ReadInt16 (i):false;} - bool readUnsignedShort (uint16 &i) {return in?in->ReadUint16 (i):false;} - bool readInt (int32 &i) {return in?in->ReadInt32 (i):false;} - bool readLong (int64 &i) {return in?in->ReadInt64 (i):false;} - bool readFloat (float &f) {return in?in->ReadFloat (f):false;} - bool readDouble (double &d) {return in?in->ReadDouble (d):false;} + bool readShort (int16 &i) {return in?in->ReadInt16 (i):false;} + bool readUnsignedShort (uint16 &i) {return in?in->ReadUint16 (i):false;} + bool readInt (int32 &i) {return in?in->ReadInt32 (i):false;} + bool readLong (int64 &i) {return in?in->ReadInt64 (i):false;} + bool readFloat (float &f) {return in?in->ReadFloat (f):false;} + bool readDouble (double &d) {return in?in->ReadDouble (d):false;} - bool readChar (u16char &c) - { - if(!in) - return(false); + bool readChar (u16char &c) + { + if(!in) + return(false); - uint16 c16; + uint16 c16; - if(!in->Read(c16)) - return(false); + if(!in->Read(c16)) + return(false); - c=BigToCurrentEndian(c16); - return(true); - } + c=BigToCurrentEndian(c16); + return(true); + } - bool readChars (u16char *wstr,const int count) - { - return in?in->ReadUTF16BEChars(wstr,count):false; - } + bool readChars (u16char *wstr,const int count) + { + return in?in->ReadUTF16BEChars(wstr,count):false; + } - bool readUTF (UTF16String &str) + bool readUTF (UTF16String &str) { return in?in->ReadUTF8ShortString(str); } - };//class JavaInputStream - }//namespace io + };//class JavaInputStream + }//namespace io }//namespace hgl #endif//HGL_IO_JAVA_INPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/JavaOutputStream.h b/inc/hgl/io/JavaOutputStream.h index 3d53000d..bf2d91db 100644 --- a/inc/hgl/io/JavaOutputStream.h +++ b/inc/hgl/io/JavaOutputStream.h @@ -4,59 +4,59 @@ #include namespace hgl { - namespace io - { - /** - * Java数据输出流
- * 接口类似于java.io.DataOutputStream,用于和Java程序进行交互 - */ - class JavaOutputStream - { - protected: + namespace io + { + /** + * Java数据输出流
+ * 接口类似于java.io.DataOutputStream,用于和Java程序进行交互 + */ + class JavaOutputStream + { + protected: - BEDataOutputStream *out; + BEDataOutputStream *out; - public: + public: - JavaOutputStream(OutputStream *os) - { - out=new BEDataOutputStream(os); - } + JavaOutputStream(OutputStream *os) + { + out=new BEDataOutputStream(os); + } - virtual ~JavaOutputStream() - { - delete out; - } + virtual ~JavaOutputStream() + { + delete out; + } - int64 write (const void *ptr,int size) {return out?out->WriteFully(ptr,size):-1;} + int64 write (const void *ptr,int size) {return out?out->WriteFully(ptr,size):-1;} - bool writeBoolean (const bool &b) {return out?out->WriteBool (b):false;} - bool writeByte (const int8 &i) {return out?out->WriteInt8 (i):false;} - bool writeUnsignedByte (const uint8 &i) {return out?out->WriteUint8 (i):false;} + bool writeBoolean (const bool &b) {return out?out->WriteBool (b):false;} + bool writeByte (const int8 &i) {return out?out->WriteInt8 (i):false;} + bool writeUnsignedByte (const uint8 &i) {return out?out->WriteUint8 (i):false;} - bool writeShort (const int16 &i) {return out?out->WriteInt16 (i):false;} - bool writeUnsignedShort (const uint16 &i) {return out?out->WriteUint16(i):false;} - bool writeInt (const int32 &i) {return out?out->WriteInt32 (i):false;} - bool writeLong (const int64 &i) {return out?out->WriteInt64 (i):false;} - bool writeFloat (const float &f) {return out?out->WriteFloat (f):false;} - bool writeDouble (const double &d) {return out?out->WriteDouble(d):false;} + bool writeShort (const int16 &i) {return out?out->WriteInt16 (i):false;} + bool writeUnsignedShort (const uint16 &i) {return out?out->WriteUint16(i):false;} + bool writeInt (const int32 &i) {return out?out->WriteInt32 (i):false;} + bool writeLong (const int64 &i) {return out?out->WriteInt64 (i):false;} + bool writeFloat (const float &f) {return out?out->WriteFloat (f):false;} + bool writeDouble (const double &d) {return out?out->WriteDouble(d):false;} - bool writeChar (const u16char &c) - { - if(!out) - return(false); + bool writeChar (const u16char &c) + { + if(!out) + return(false); - return out->Write(ToBigEndian(c)); - } + return out->Write(ToBigEndian(c)); + } - bool writeChars (const u16char *wstr,const int64 count) - { - return out?out->WriteUTF16BEChars(wstr,count):false; - } + bool writeChars (const u16char *wstr,const int64 count) + { + return out?out->WriteUTF16BEChars(wstr,count):false; + } - bool writeUTF (const UTF8String &str){return out?out->WriteUTF8ShortString(str);} - bool writeUTF (const UTF16String &str){return out?out->WriteUTF8ShortString(str);} - };//class JavaOutputStream - }//namespace io + bool writeUTF (const UTF8String &str){return out?out->WriteUTF8ShortString(str);} + bool writeUTF (const UTF16String &str){return out?out->WriteUTF8ShortString(str);} + };//class JavaOutputStream + }//namespace io }//namespace hgl #endif//HGL_IO_JAVA_OUTPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/MemoryInputStream.h b/inc/hgl/io/MemoryInputStream.h index 78129cde..78edbbe9 100644 --- a/inc/hgl/io/MemoryInputStream.h +++ b/inc/hgl/io/MemoryInputStream.h @@ -5,159 +5,159 @@ #include namespace hgl { - namespace io - { - /** - * 内存数据输入流,从内存块中取得数据。 - */ - class MemoryInputStream:public InputStream ///内存数据输入流 - { - protected: + namespace io + { + /** + * 内存数据输入流,从内存块中取得数据。 + */ + class MemoryInputStream:public InputStream ///内存数据输入流 + { + protected: - const uint8 *buf; - size_t buf_size; - size_t cur_pos; + const uint8 *buf; + size_t buf_size; + size_t cur_pos; - public: + public: - MemoryInputStream() - { - buf=0; + MemoryInputStream() + { + buf=0; - buf_size=0; - cur_pos=0; - } + buf_size=0; + cur_pos=0; + } - virtual ~MemoryInputStream() - { - } + virtual ~MemoryInputStream() + { + } - /** - * 关联一个数据区到当前输入流 - * @param data 数据指针 - * @param size 数据长度字节数 - * @return 是否成功 - */ - bool Link(const void *data,const size_t size) - { - if(!data) - return(false); + /** + * 关联一个数据区到当前输入流 + * @param data 数据指针 + * @param size 数据长度字节数 + * @return 是否成功 + */ + bool Link(const void *data,const size_t size) + { + if(!data) + return(false); - buf=(uint8 *)data; - buf_size=size; - cur_pos=0; + buf=(uint8 *)data; + buf_size=size; + cur_pos=0; - return(true); - } + return(true); + } - /** - * 更新关联的数据区长度,不复位cur_pos - * @param data 数据指针 - * @param size 数据长度字节数 - * @return 是否成功 - */ - bool Update(void *data,size_t size) - { - if(!data) - return(false); + /** + * 更新关联的数据区长度,不复位cur_pos + * @param data 数据指针 + * @param size 数据长度字节数 + * @return 是否成功 + */ + bool Update(void *data,size_t size) + { + if(!data) + return(false); - buf=(uint8 *)data; - buf_size=size; + buf=(uint8 *)data; + buf_size=size; - return(true); - } + return(true); + } - void Unlink() - { - buf=0; - } + void Unlink() + { + buf=0; + } - void Close() - { - Unlink(); - } + void Close() + { + Unlink(); + } - int64 Read(void *data,int64 size) - { - const int result=Peek(data,size); + int64 Read(void *data,int64 size) + { + const int result=Peek(data,size); - if(result>0) - cur_pos+=result; + if(result>0) + cur_pos+=result; - return result; - } + return result; + } - int64 Peek(void *data,int64 size) - { - if(!buf||!data||size<0) - return(-1); + int64 Peek(void *data,int64 size) + { + if(!buf||!data||size<0) + return(-1); - if(cur_pos+size>buf_size) - size=buf_size-cur_pos; + if(cur_pos+size>buf_size) + size=buf_size-cur_pos; - if(size<=0) - return(0); + if(size<=0) + return(0); - memcpy(data,buf+cur_pos,size); + memcpy(data,buf+cur_pos,size); - return size; - } + return size; + } - bool CanRestart()const{return true;} - bool CanSeek()const{return true;} - bool CanSize()const{return true;} - bool CanPeek()const{return true;} + bool CanRestart()const{return true;} + bool CanSeek()const{return true;} + bool CanSize()const{return true;} + bool CanPeek()const{return true;} - bool Restart() - { - cur_pos=0; - return(true); - } + bool Restart() + { + cur_pos=0; + return(true); + } - int64 Skip(int64 bytes) - { - return Seek(bytes,soCurrent); - } + int64 Skip(int64 bytes) + { + return Seek(bytes,soCurrent); + } - int64 Seek(int64 off,SeekOrigin so) - { - if(!CanSeek())return(-1); + int64 Seek(int64 off,SeekOrigin so) + { + if(!CanSeek())return(-1); - if(so==soCurrent) - { - off+=cur_pos; - } - else - if(so==soEnd) - { - off+=buf_size; - } + if(so==soCurrent) + { + off+=cur_pos; + } + else + if(so==soEnd) + { + off+=buf_size; + } - if(off<0||off>=buf_size) - return(-1); + if(off<0||off>=buf_size) + return(-1); - cur_pos=off; - return cur_pos; - } + cur_pos=off; + return cur_pos; + } - int64 Tell()const - { - if(!buf)return(-1); - return cur_pos; - } + int64 Tell()const + { + if(!buf)return(-1); + return cur_pos; + } - int64 GetSize()const - { - if(!buf)return(-1); - return buf_size; - } + int64 GetSize()const + { + if(!buf)return(-1); + return buf_size; + } - int64 Available()const - { - if(!buf)return(-1); - return buf_size-cur_pos; - } - };//class MemoryInputStream - }//namespace io + int64 Available()const + { + if(!buf)return(-1); + return buf_size-cur_pos; + } + };//class MemoryInputStream + }//namespace io }//namespace hgl #endif//HGL_IO_MEMORY_INPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/MemoryOutputStream.h b/inc/hgl/io/MemoryOutputStream.h index 4b53205a..ef9c8822 100644 --- a/inc/hgl/io/MemoryOutputStream.h +++ b/inc/hgl/io/MemoryOutputStream.h @@ -5,48 +5,48 @@ #include namespace hgl { - namespace io - { - /** - * 内存数据输出流,将数据输出到一个内存块中。 - */ - class MemoryOutputStream:public OutputStream ///内存数据输出流 - { - protected: + namespace io + { + /** + * 内存数据输出流,将数据输出到一个内存块中。 + */ + class MemoryOutputStream:public OutputStream ///内存数据输出流 + { + protected: - uint8 *buf; + uint8 *buf; - size_t max_size; - size_t buf_size; - size_t cur_pos; + size_t max_size; + size_t buf_size; + size_t cur_pos; - bool one; + bool one; - public: + public: - MemoryOutputStream() - { - buf=0; - max_size=0; - buf_size=0; - cur_pos=0; - one=false; - } + MemoryOutputStream() + { + buf=0; + max_size=0; + buf_size=0; + cur_pos=0; + one=false; + } - virtual ~MemoryOutputStream() - { - Close(); - } + virtual ~MemoryOutputStream() + { + Close(); + } - void *GetData()const{return buf;} + void *GetData()const{return buf;} - /** + /** * 创建一个当前流数据的内存拷贝,此函数需用户自行delete[] * @param len 存放数据长度的指针 * @return 创建好的内存拷贝 */ - void *CreateCopyData(int *len)const - { + void *CreateCopyData(int *len)const + { if(buf_size<=0) return(nullptr); @@ -60,177 +60,177 @@ namespace hgl return data; } - /** - * 关联一个数据区到当前输出流 - * @param ptr 数据指针 - * @param size 数据长度字节数 - * @param one_instance 是否仅此一份实例(如果是,将由MemoryOutputStream类负责释放) - * @return 是否成功 - */ - bool Link(void *ptr,size_t size,bool one_instance=false) - { - if(!ptr||!size) - return(false); + /** + * 关联一个数据区到当前输出流 + * @param ptr 数据指针 + * @param size 数据长度字节数 + * @param one_instance 是否仅此一份实例(如果是,将由MemoryOutputStream类负责释放) + * @return 是否成功 + */ + bool Link(void *ptr,size_t size,bool one_instance=false) + { + if(!ptr||!size) + return(false); - buf=(uint8 *)ptr; - buf_size=size; - max_size=size; - cur_pos=0; + buf=(uint8 *)ptr; + buf_size=size; + max_size=size; + cur_pos=0; - one=one_instance; + one=one_instance; - return(true); - } + return(true); + } - /** - * 更新关联的数据区长度,不复位cur_pos - * @param ptr 数据指针 - * @param size 数据长度字节数 - * @return 是否成功 - */ - bool Update(void *ptr,size_t size) - { - if(!ptr||!size) - return(false); + /** + * 更新关联的数据区长度,不复位cur_pos + * @param ptr 数据指针 + * @param size 数据长度字节数 + * @return 是否成功 + */ + bool Update(void *ptr,size_t size) + { + if(!ptr||!size) + return(false); - buf=(uint8 *)ptr; - buf_size=size; - max_size=0; + buf=(uint8 *)ptr; + buf_size=size; + max_size=0; - return(true); - } + return(true); + } - void Unlink() - { - buf=0; - buf_size=0; - max_size=0; - } + void Unlink() + { + buf=0; + buf_size=0; + max_size=0; + } - bool Create(int64 size) - { - Close(); + bool Create(int64 size) + { + Close(); - if(size<=0) - return(false); + if(size<=0) + return(false); - buf=(uint8 *)hgl_malloc(size); + buf=(uint8 *)hgl_malloc(size); - if(!buf) - return(false); + if(!buf) + return(false); - one=true; - cur_pos=0; - buf_size=size; - max_size=size; + one=true; + cur_pos=0; + buf_size=size; + max_size=size; - return(true); - } + return(true); + } - void Close() override - { - if(buf) - { - if(one) - hgl_free(buf); + void Close() override + { + if(buf) + { + if(one) + hgl_free(buf); - buf=0; - } + buf=0; + } - buf_size=0; - max_size=0; - } + buf_size=0; + max_size=0; + } - void ClearData() - { - cur_pos=0; - buf_size=0; - } + void ClearData() + { + cur_pos=0; + buf_size=0; + } - int64 Write(const void *ptr,int64 size) override - { - if(!ptr||size<0) - return(-1); + int64 Write(const void *ptr,int64 size) override + { + if(!ptr||size<0) + return(-1); - if(!buf) - { - if(!Create(size)) - return(-1); - } + if(!buf) + { + if(!Create(size)) + return(-1); + } - if(cur_pos+size>buf_size) - { - if(one) - { - buf_size=cur_pos+size; + if(cur_pos+size>buf_size) + { + if(one) + { + buf_size=cur_pos+size; - if(buf_size>max_size) - { - max_size=power_to_2(buf_size); + if(buf_size>max_size) + { + max_size=power_to_2(buf_size); - buf=(uint8 *)hgl_realloc(buf,max_size); - } - } - else - size=buf_size-cur_pos; - } + buf=(uint8 *)hgl_realloc(buf,max_size); + } + } + else + size=buf_size-cur_pos; + } - if(size<=0) - return(0); + if(size<=0) + return(0); - memcpy(buf+cur_pos,ptr,size); + memcpy(buf+cur_pos,ptr,size); - cur_pos+=size; + cur_pos+=size; - return size; - } + return size; + } - bool CanRestart()const override{return true;} - bool CanSeek()const override{return true;} - bool CanSize()const override{return true;} + bool CanRestart()const override{return true;} + bool CanSeek()const override{return true;} + bool CanSize()const override{return true;} - bool Restart() override - { - cur_pos=0; - return(true); - } + bool Restart() override + { + cur_pos=0; + return(true); + } - int64 Seek(int64 off,SeekOrigin so=soBegin) override - { - if(!CanSeek())return(-1); + int64 Seek(int64 off,SeekOrigin so=soBegin) override + { + if(!CanSeek())return(-1); - if(so==soCurrent) - { - off+=cur_pos; - } - else - if(so==soEnd) - { - off+=buf_size; - } + if(so==soCurrent) + { + off+=cur_pos; + } + else + if(so==soEnd) + { + off+=buf_size; + } - if(off<0||off>=buf_size) - return(-1); + if(off<0||off>=buf_size) + return(-1); - cur_pos=off; - return cur_pos; - } + cur_pos=off; + return cur_pos; + } - int64 Tell()const override - { - return cur_pos; - } + int64 Tell()const override + { + return cur_pos; + } - int64 GetSize()const override - { - return buf_size; - } + int64 GetSize()const override + { + return buf_size; + } - int64 Available()const override - { - return buf_size-cur_pos; - } - };//class MemoryOutputStream - }//namespace io + int64 Available()const override + { + return buf_size-cur_pos; + } + };//class MemoryOutputStream + }//namespace io }//namespace hgl #endif//HGL_IO_MEMORY_OUTPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/OutputStream.h b/inc/hgl/io/OutputStream.h index b528851f..a0022836 100644 --- a/inc/hgl/io/OutputStream.h +++ b/inc/hgl/io/OutputStream.h @@ -5,32 +5,32 @@ #include namespace hgl { - namespace io - { - /** - * 数据输出流基类 - */ - class OutputStream:public SeekAccess ///数据输出流基类 - { - public: + namespace io + { + /** + * 数据输出流基类 + */ + class OutputStream:public SeekAccess ///数据输出流基类 + { + public: - virtual ~OutputStream()=default; + virtual ~OutputStream()=default; - virtual void Close()=0; ///<关闭输出流 + virtual void Close()=0; ///<关闭输出流 - virtual int64 Write(const void *,int64)=0; ///<写入数据 - virtual int64 WriteFully(const void *buf,int64 buf_size){return Write(buf,buf_size);} ///<充分写入,保证完整写入指定长度的数据 + virtual int64 Write(const void *,int64)=0; ///<写入数据 + virtual int64 WriteFully(const void *buf,int64 buf_size){return Write(buf,buf_size);} ///<充分写入,保证完整写入指定长度的数据 - virtual bool CanRestart()const=0; ///<是否可以复位 - virtual bool CanSeek()const=0; ///<是否可以定位 - virtual bool CanSize()const=0; ///<是否可以取得尺寸 + virtual bool CanRestart()const=0; ///<是否可以复位 + virtual bool CanSeek()const=0; ///<是否可以定位 + virtual bool CanSize()const=0; ///<是否可以取得尺寸 - virtual bool Restart()=0; ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 - virtual int64 Tell()const=0; ///<返回当前访问位置 - virtual int64 GetSize()const=0; ///<取得流长度 - virtual int64 Available()const=0; ///<剩下的可以不受阻塞写入的字节数 - };//class OutputStream - }//namespace io + virtual bool Restart()=0; ///<复位访问指针 + virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 + virtual int64 Tell()const=0; ///<返回当前访问位置 + virtual int64 GetSize()const=0; ///<取得流长度 + virtual int64 Available()const=0; ///<剩下的可以不受阻塞写入的字节数 + };//class OutputStream + }//namespace io }//namespace hgl #endif//HGL_IO_OUTPUT_STREAM_INCLUDE diff --git a/inc/hgl/io/RandomAccessFile.h b/inc/hgl/io/RandomAccessFile.h index e395cd67..01f1e4ed 100644 --- a/inc/hgl/io/RandomAccessFile.h +++ b/inc/hgl/io/RandomAccessFile.h @@ -6,42 +6,42 @@ #include namespace hgl { - namespace io - { - /** - * 随机文件访问

- * 可同时被DataInputStream/DataOutputStream包含引用,但需注意他们将共用一个访问指针 - */ - class RandomAccessFile:public InputStream,public OutputStream - { - protected: + namespace io + { + /** + * 随机文件访问

+ * 可同时被DataInputStream/DataOutputStream包含引用,但需注意他们将共用一个访问指针 + */ + class RandomAccessFile:public InputStream,public OutputStream + { + protected: - SharedPtr file; ///<文件访问指针 + SharedPtr file; ///<文件访问指针 - public: + public: - RandomAccessFile(); - RandomAccessFile(SharedPtr &); - ~RandomAccessFile(); + RandomAccessFile(); + RandomAccessFile(SharedPtr &); + ~RandomAccessFile(); - virtual bool Open(const OSString &); ///<打开文件 - virtual void Close(); ///<关闭文件 + virtual bool Open(const OSString &); ///<打开文件 + virtual void Close(); ///<关闭文件 - virtual int64 Read(void *,int64); ///<读取数据 - virtual int64 Write(const void *,int64); ///<写入数据 + virtual int64 Read(void *,int64); ///<读取数据 + virtual int64 Write(const void *,int64); ///<写入数据 - virtual int64 Read(int64,void *,int64); ///<在指定位置读取指定长度的数据 - virtual int64 Write(int64,const void *,int64); ///<在指定位置写入指定长度的数据 + virtual int64 Read(int64,void *,int64); ///<在指定位置读取指定长度的数据 + virtual int64 Write(int64,const void *,int64); ///<在指定位置写入指定长度的数据 - virtual bool CanSeek()const; ///<是否可移动访问指针 - virtual bool CanRestart()const; ///<是否可复位访问 - virtual bool CanSize()const; ///<是否可访问文件长度 + virtual bool CanSeek()const; ///<是否可移动访问指针 + virtual bool CanRestart()const; ///<是否可复位访问 + virtual bool CanSize()const; ///<是否可访问文件长度 - virtual int64 Tell()const; ///<取当前位置 - virtual int64 GetSize(); ///<取得文件长度 - virtual bool Restart(); ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 - };//class RandomAccessFile - }//namespace io + virtual int64 Tell()const; ///<取当前位置 + virtual int64 GetSize(); ///<取得文件长度 + virtual bool Restart(); ///<复位访问指针 + virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 + };//class RandomAccessFile + }//namespace io }//namespace hgl #endif//HGL_IO_RANDOM_ACCESS_FILE_INCLUDE diff --git a/inc/hgl/io/SeekAccess.h b/inc/hgl/io/SeekAccess.h index 0f33ca86..1db8198a 100644 --- a/inc/hgl/io/SeekAccess.h +++ b/inc/hgl/io/SeekAccess.h @@ -4,33 +4,33 @@ #include namespace hgl { - namespace io - { - enum_int(SeekOrigin) /// 资源偏移方向枚举 - { - soBegin=0, ///<从资源最开始处开始,offset必须大于0。移到资源的offset位置 - soCurrent, ///<从资源当前位置开始,移到资源的Position+offset位置 - soEnd ///<从资源的结束位置开始,offset必须小于0,表示结束前的字符数 - };//enum SeekOrigin + namespace io + { + enum_int(SeekOrigin) /// 资源偏移方向枚举 + { + soBegin=0, ///<从资源最开始处开始,offset必须大于0。移到资源的offset位置 + soCurrent, ///<从资源当前位置开始,移到资源的Position+offset位置 + soEnd ///<从资源的结束位置开始,offset必须小于0,表示结束前的字符数 + };//enum SeekOrigin - /** - * 定位访问功能基类 - */ - class SeekAccess - { - public: + /** + * 定位访问功能基类 + */ + class SeekAccess + { + public: - virtual ~SeekAccess()=default; + virtual ~SeekAccess()=default; - virtual bool CanRestart()const=0; ///<是否可以复位 - virtual bool CanSeek()const=0; ///<是否可以定位 - virtual bool CanSize()const=0; ///<是否可以取得尺寸 + virtual bool CanRestart()const=0; ///<是否可以复位 + virtual bool CanSeek()const=0; ///<是否可以定位 + virtual bool CanSize()const=0; ///<是否可以取得尺寸 - virtual bool Restart()=0; ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 - virtual int64 Tell()const=0; ///<返回当前访问位置 - virtual int64 GetSize()const=0; ///<取得文件长度 - };//class SeekAccess - }//namespace io + virtual bool Restart()=0; ///<复位访问指针 + virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 + virtual int64 Tell()const=0; ///<返回当前访问位置 + virtual int64 GetSize()const=0; ///<取得文件长度 + };//class SeekAccess + }//namespace io }//namespace hgl #endif//HGL_IO_SEEK_ACCESS_INCLUDE diff --git a/inc/hgl/io/TextOutputStream.h b/inc/hgl/io/TextOutputStream.h index 0d96beb7..0f01c5d9 100644 --- a/inc/hgl/io/TextOutputStream.h +++ b/inc/hgl/io/TextOutputStream.h @@ -5,172 +5,172 @@ #include namespace hgl { - namespace io - { - /** - * 文本输出流
- * 用于将数据输出成文件,用于日志、配置等文本信息处理。 - */ + namespace io + { + /** + * 文本输出流
+ * 用于将数据输出成文件,用于日志、配置等文本信息处理。 + */ class TextOutputStream { - protected: + protected: - ByteOrderMask bom; ///<文本编码 - DataOutputStream *out; ///<数据输出流 + ByteOrderMask bom; ///<文本编码 + DataOutputStream *out; ///<数据输出流 - public: + public: - TextOutputStream(ByteOrderMask _bom,DataOutputStream *_out) + TextOutputStream(ByteOrderMask _bom,DataOutputStream *_out) { bom=_bom; out=_out; } - virtual ~TextOutputStream() - { - delete out; - } + virtual ~TextOutputStream() + { + delete out; + } - const ByteOrderMask GetBOM()const - { - return bom; - } + const ByteOrderMask GetBOM()const + { + return bom; + } - DataOutputStream *GetDataOutputStream() - { - return out; - } + DataOutputStream *GetDataOutputStream() + { + return out; + } - operator DataOutputStream *() - { - return out; - } + operator DataOutputStream *() + { + return out; + } - bool WriteBOM() ///<写入文本文件bom头 - { - if(!out)return(false); + bool WriteBOM() ///<写入文本文件bom头 + { + if(!out)return(false); - const BOMFileHeader *bom_item=BOMData+bom; + const BOMFileHeader *bom_item=BOMData+bom; - return(out->Write(bom_item->data,bom_item->size)==bom_item->size); - } + return(out->Write(bom_item->data,bom_item->size)==bom_item->size); + } - virtual bool WriteChars(const char *,int64)=0; ///<写入一个字符串 - virtual bool WriteChars(const u16char *,int64)=0; ///<写入一个字符串 + virtual bool WriteChars(const char *,int64)=0; ///<写入一个字符串 + virtual bool WriteChars(const u16char *,int64)=0; ///<写入一个字符串 - template - bool WriteString(const BaseString &str) ///<写入一个字符串 - { - return WriteChars(str.c_str(),str.Length()); - } + template + bool WriteString(const BaseString &str) ///<写入一个字符串 + { + return WriteChars(str.c_str(),str.Length()); + } - bool WriteLineEnd() ///<写入一个换行符 - { - return WriteChars(HGL_LINE_END,HGL_LINE_END_SIZE); - } + bool WriteLineEnd() ///<写入一个换行符 + { + return WriteChars(HGL_LINE_END,HGL_LINE_END_SIZE); + } - template - bool WriteLine(const N *str,int64 size) - { - if(!WriteChars(str,size)) - return(false); + template + bool WriteLine(const N *str,int64 size) + { + if(!WriteChars(str,size)) + return(false); - return WriteLineEnd(); - } + return WriteLineEnd(); + } - template - bool WriteLine(const BaseString &str) - { - return WriteLine(str.c_str(),str.Length()); - } + template + bool WriteLine(const BaseString &str) + { + return WriteLine(str.c_str(),str.Length()); + } - /** - * 向流中写入一个文本(注:整体按标准文本文件模式) - */ - template - bool WriteText(const StringList &sl) - { - const int count=sl.GetCount(); + /** + * 向流中写入一个文本(注:整体按标准文本文件模式) + */ + template + bool WriteText(const StringList &sl) + { + const int count=sl.GetCount(); - for(int i=0;i - bool Write(const StringList &sl) - { - const int count=sl.GetCount(); + /** + * 向流中写入一个字符串列表(注:整体为2进制流,不可用于文本文件) + * @return 是否成功 + */ + template + bool Write(const StringList &sl) + { + const int count=sl.GetCount(); - if(!out->WriteInt32(count)) //写入行数 - return(false); + if(!out->WriteInt32(count)) //写入行数 + return(false); - if(count<=0) - return(true); + if(count<=0) + return(true); - SharedArray line_length=new int32[count]; - SharedArray str_list=new T *[count]; - int32 *line_p=line_length; - T **str_p=str_list; + SharedArray line_length=new int32[count]; + SharedArray str_list=new T *[count]; + int32 *line_p=line_length; + T **str_p=str_list; - int32 total=0; + int32 total=0; - for(int i=0;i &str=sl[i]; + for(int i=0;i &str=sl[i]; - const int len=str.Length(); + const int len=str.Length(); - total+=len; + total+=len; - *line_p++=len; + *line_p++=len; - *str_list++=str.c_str(); - } + *str_list++=str.c_str(); + } - if(out->WriteInt32(line_length,count)!=count) //写入每行长度 - return(false); + if(out->WriteInt32(line_length,count)!=count) //写入每行长度 + return(false); - line_p=line_length; - str_p=str_list; + line_p=line_length; + str_p=str_list; - SharedArray text=new T[total]; - T *text_p=text; + SharedArray text=new T[total]; + T *text_p=text; - for(int i=0;i class EndianTextOutputStream:public TextOutputStream ///文本输出流 - { + template class EndianTextOutputStream:public TextOutputStream ///文本输出流 + { public: EndianTextOutputStream(OutputStream *); - bool WriteChars(const char *,int64); ///<写入一个字符串 - bool WriteChars(const u16char *,int64); ///<写入一个字符串 - };//template class EndianTextOutputStream + bool WriteChars(const char *,int64); ///<写入一个字符串 + bool WriteChars(const u16char *,int64); ///<写入一个字符串 + };//template class EndianTextOutputStream - typedef EndianTextOutputStream UTF8TextOutputStream; + typedef EndianTextOutputStream UTF8TextOutputStream; typedef EndianTextOutputStream UTF16LETextOutputStream; typedef EndianTextOutputStream UTF16BETextOutputStream; template TextOutputStream *CreateTextOutputStream(OutputStream *os); - }//namespace io + }//namespace io }//namespace hgl #endif//HGL_IO_TEXT_OUTPUT_STREAM_INCLUDE diff --git a/inc/hgl/platform/ExternalModule.h b/inc/hgl/platform/ExternalModule.h index bb3e85e0..97463bfb 100644 --- a/inc/hgl/platform/ExternalModule.h +++ b/inc/hgl/platform/ExternalModule.h @@ -6,31 +6,31 @@ #include OS_EXTERNAL_H namespace hgl { - /** - * 这个类用于对外部动态库的调用,支持Windows/Linux/BSD/MacOSX
- * Windows 下动态库扩展名为.DLL
- * FreeBSD/MacOSX 下动态库扩展名为.dylib
- * Linux 下动态库扩展名为.so - */ - class ExternalModule ///外部模块调用类 - { - ExternalModulePointer fp; + /** + * 这个类用于对外部动态库的调用,支持Windows/Linux/BSD/MacOSX
+ * Windows 下动态库扩展名为.DLL
+ * FreeBSD/MacOSX 下动态库扩展名为.dylib
+ * Linux 下动态库扩展名为.so + */ + class ExternalModule ///外部模块调用类 + { + ExternalModulePointer fp; - public: //方法 + public: //方法 - ExternalModule(); ///<本类构造函数 - ExternalModule(ExternalModulePointer); ///<本类构造函数 - virtual ~ExternalModule(); ///<本类析构函数 + ExternalModule(); ///<本类构造函数 + ExternalModule(ExternalModulePointer); ///<本类构造函数 + virtual ~ExternalModule(); ///<本类析构函数 - bool Load(const os_char *); ///<加载一个外部模块 - void Clear(); ///<清除当前模块 + bool Load(const os_char *); ///<加载一个外部模块 + void Clear(); ///<清除当前模块 - void *FindFunc(const char *); ///<查找函数 - void *GetFunc(const char *); ///<取得函数 + void *FindFunc(const char *); ///<查找函数 + void *GetFunc(const char *); ///<取得函数 - int Get(FuncLoad *); ///<加载函数列表 - };//class ExternalModule + int Get(FuncLoad *); ///<加载函数列表 + };//class ExternalModule - ExternalModule *LoadExternalModule(const os_char *); ///<加载一个外部模块 + ExternalModule *LoadExternalModule(const os_char *); ///<加载一个外部模块 }//namespace hgl #endif//HGL_EXTERNAL_MODULE_INCLUDE diff --git a/inc/hgl/platform/FuncLoad.h b/inc/hgl/platform/FuncLoad.h index 19777517..8686ee8c 100644 --- a/inc/hgl/platform/FuncLoad.h +++ b/inc/hgl/platform/FuncLoad.h @@ -3,29 +3,29 @@ namespace hgl { - /** - * 函数加载定义数据结构 - */ - struct FuncLoad - { - char func_name[64]; ///<函数名称 - void **func_pointer; ///<函数指针 - };//struct FuncLoad + /** + * 函数加载定义数据结构 + */ + struct FuncLoad + { + char func_name[64]; ///<函数名称 + void **func_pointer; ///<函数指针 + };//struct FuncLoad - #define HGL_FUNC_LOAD_LIST_BEGIN(name) struct FuncLoad name[]={ - #define HGL_FUNC_LOAD(name) {#name,(void **)&name}, - #define HGL_FUNC_LOAD_LIST_END {"",(void **)0}}; + #define HGL_FUNC_LOAD_LIST_BEGIN(name) struct FuncLoad name[]={ + #define HGL_FUNC_LOAD(name) {#name,(void **)&name}, + #define HGL_FUNC_LOAD_LIST_END {"",(void **)0}}; - inline void ClearFuncLoadPointer(struct FuncLoad *flp) - { - if(!flp)return; + inline void ClearFuncLoadPointer(struct FuncLoad *flp) + { + if(!flp)return; - while(flp->func_pointer) - { - flp->func_pointer=(void **)0; + while(flp->func_pointer) + { + flp->func_pointer=(void **)0; - ++flp; - } - } + ++flp; + } + } }//namespace hgl #endif//HGL_FUNC_LOAD_INCLUDE diff --git a/inc/hgl/platform/InputDevice.h b/inc/hgl/platform/InputDevice.h index c645d668..553f5842 100644 --- a/inc/hgl/platform/InputDevice.h +++ b/inc/hgl/platform/InputDevice.h @@ -8,197 +8,197 @@ namespace hgl */ enum MouseButton { - mbMove =0x00000001, ///<鼠标移动了 + mbMove =0x00000001, ///<鼠标移动了 - mbLeft =0x00000010, ///<鼠标左键处于按下状态 - mbMid =0x00000100, ///<鼠标中键处于按下状态 - mbRight =0x00001000, ///<鼠标右键处于按下状态 + mbLeft =0x00000010, ///<鼠标左键处于按下状态 + mbMid =0x00000100, ///<鼠标中键处于按下状态 + mbRight =0x00001000, ///<鼠标右键处于按下状态 - mbShift =0x10000000, /// + #include - #if HGL_COMPILER == HGL_COMPILER_Microsoft - #include - #elif HGL_COMPILER == HGL_COMPILER_Intel - #include - #elif HGL_COMPILER == HGL_COMPILER_IBM - #include - #elif HGL_COMPILER == HGL_COMPILER_MINGW32 - #include - #elif HGL_COMPILER == HGL_COMPILER_GNU - #include - #else - #error Unrecognized compiler - #endif + #if HGL_COMPILER == HGL_COMPILER_Microsoft + #include + #elif HGL_COMPILER == HGL_COMPILER_Intel + #include + #elif HGL_COMPILER == HGL_COMPILER_IBM + #include + #elif HGL_COMPILER == HGL_COMPILER_MINGW32 + #include + #elif HGL_COMPILER == HGL_COMPILER_GNU + #include + #else + #error Unrecognized compiler + #endif #elif HGL_OS == HGL_OS_Cygwin - #include + #include - #if HGL_COMPILER == HGL_COMPILER_LLVM - #include - #elif HGL_COMPILER == HGL_COMPILER_GNU - #include - #else - #error Unrecognized compiler - #endif//GNU + #if HGL_COMPILER == HGL_COMPILER_LLVM + #include + #elif HGL_COMPILER == HGL_COMPILER_GNU + #include + #else + #error Unrecognized compiler + #endif//GNU #elif (HGL_OS == HGL_OS_FreeBSD)||(HGL_OS == HGL_OS_NetBSD)||(HGL_OS == HGL_OS_OpenBSD) - #include + #include - #if HGL_COMPILER == HGL_COMPILER_LLVM - #include - #elif HGL_COMPILER == HGL_COMPILER_GNU - #include - #else - #error Unrecognized compiler - #endif//GNU + #if HGL_COMPILER == HGL_COMPILER_LLVM + #include + #elif HGL_COMPILER == HGL_COMPILER_GNU + #include + #else + #error Unrecognized compiler + #endif//GNU #elif HGL_OS == HGL_OS_Linux - #include + #include - #if HGL_COMPILER == HGL_COMPILER_CBuilder - #include - #elif HGL_COMPILER == HGL_COMPILER_Intel - #include - #elif HGL_COMPILER == HGL_COMPILER_IBM - #include - #elif HGL_COMPILER == HGL_COMPILER_Watcom - #include - #elif HGL_COMPILER == HGL_COMPILER_LLVM - #include - #elif HGL_COMPILER == HGL_COMPILER_GNU - #include - #else - #error Unrecognized compiler - #endif + #if HGL_COMPILER == HGL_COMPILER_CBuilder + #include + #elif HGL_COMPILER == HGL_COMPILER_Intel + #include + #elif HGL_COMPILER == HGL_COMPILER_IBM + #include + #elif HGL_COMPILER == HGL_COMPILER_Watcom + #include + #elif HGL_COMPILER == HGL_COMPILER_LLVM + #include + #elif HGL_COMPILER == HGL_COMPILER_GNU + #include + #else + #error Unrecognized compiler + #endif #elif HGL_OS == HGL_OS_macOS - #include + #include - #if HGL_COMPILER == HGL_COMPILER_Intel - #include - #elif HGL_COMPILER == HGL_COMPILER_LLVM - #include - #else - #error Unrecognized compiler - #endif + #if HGL_COMPILER == HGL_COMPILER_Intel + #include + #elif HGL_COMPILER == HGL_COMPILER_LLVM + #include + #else + #error Unrecognized compiler + #endif #elif HGL_OS == HGL_OS_iOS - #include - #include + #include + #include #elif HGL_OS == HGL_OS_PSP2 - #include + #include - #if HGL_COMPILER == HGL_COMPILER_GNU - #include - #else - #error Unrecognized compiler - #endif + #if HGL_COMPILER == HGL_COMPILER_GNU + #include + #else + #error Unrecognized compiler + #endif #elif HGL_OS == HGL_OS_AIX - #include + #include - #if HGL_COMPILER == HGL_COMPILER_IBM - #include - #elif HGL_COMPILER == HGL_COMPILER_SGI - #include - #elif HGL_COMPILER == HGL_COMPILER_GNU - #include - #else - #error Unrecognized compiler - #endif + #if HGL_COMPILER == HGL_COMPILER_IBM + #include + #elif HGL_COMPILER == HGL_COMPILER_SGI + #include + #elif HGL_COMPILER == HGL_COMPILER_GNU + #include + #else + #error Unrecognized compiler + #endif #elif HGL_OS == HGL_OS_Android - #include + #include - #if HGL_COMPILER == HGL_COMPILER_LLVM - #include - #elif HGL_COMPILER == HGL_COMPILER_GNU - #include - #else - #error Unrecognized compiler - #endif//GNU + #if HGL_COMPILER == HGL_COMPILER_LLVM + #include + #elif HGL_COMPILER == HGL_COMPILER_GNU + #include + #else + #error Unrecognized compiler + #endif//GNU #endif//HGL_OS namespace hgl diff --git a/inc/hgl/platform/SystemInfo.h b/inc/hgl/platform/SystemInfo.h index 987efb99..ee72268b 100644 --- a/inc/hgl/platform/SystemInfo.h +++ b/inc/hgl/platform/SystemInfo.h @@ -43,9 +43,9 @@ namespace hgl OSString mydata; ///<当前用户应用程序数据保存路径 OSString myprogram; ///<“我的程序”路径(Windows下为开始菜单程序路径,Mac下为应用程序路径) OSString mydesktop; ///<“桌面”路径 - + OSString desktop; ///<所有用户桌面路径 - + OSString engine; ///<引擎所在路径 OSString plug_ins; ///<引擎插件路径 };//struct SystemPath diff --git a/inc/hgl/platform/Window.h b/inc/hgl/platform/Window.h index 9201efbe..4c56b2c8 100644 --- a/inc/hgl/platform/Window.h +++ b/inc/hgl/platform/Window.h @@ -38,8 +38,8 @@ namespace hgl virtual void OnMouseDoubleClick (int,int,uint){} //virtual void OnJoystickDown (uint){} - //virtual void OnJoystickPress (uint){} - //virtual void OnJoystickUp (uint){} + //virtual void OnJoystickPress (uint){} + //virtual void OnJoystickUp (uint){} virtual void OnKeyDown (KeyboardButton); virtual void OnKeyPress (KeyboardButton){} @@ -85,7 +85,7 @@ namespace hgl };//class Window Window *CreateRenderWindow(const OSString &win_name); - + void InitNativeWindowSystem(); }//namespace hgl #endif//HGL_GRAPH_WINDOW_INCLUDE diff --git a/inc/hgl/platform/compiler/DataTypeGNU.h b/inc/hgl/platform/compiler/DataTypeGNU.h index de3df8d4..4fdd2c98 100644 --- a/inc/hgl/platform/compiler/DataTypeGNU.h +++ b/inc/hgl/platform/compiler/DataTypeGNU.h @@ -3,17 +3,17 @@ namespace hgl { - using int8 = signed char; ///<有符号 8位整型 - using uint8 =unsigned char; ///<无符号 8位整型 - using int16 = signed short; ///<有符号16位整型 - using uint16 =unsigned short; ///<无符号16位整型 - using int32 = signed int; ///<有符号32位整型 - using uint32 =unsigned int; ///<无符号32位整型 + using int8 = signed char; ///<有符号 8位整型 + using uint8 =unsigned char; ///<无符号 8位整型 + using int16 = signed short; ///<有符号16位整型 + using uint16 =unsigned short; ///<无符号16位整型 + using int32 = signed int; ///<有符号32位整型 + using uint32 =unsigned int; ///<无符号32位整型 - //64位系统下long/long long都是64位,32位系列下只有long long是64位 + //64位系统下long/long long都是64位,32位系列下只有long long是64位 - using int64 = signed long long; ///<有符号64位整型 - using uint64 =unsigned long long; ///<无符号64位整型 + using int64 = signed long long; ///<有符号64位整型 + using uint64 =unsigned long long; ///<无符号64位整型 using float32 =float; using float64 =double; diff --git a/inc/hgl/platform/compiler/DataTypeTiny.h b/inc/hgl/platform/compiler/DataTypeTiny.h index d3cc858e..2af4d18e 100644 --- a/inc/hgl/platform/compiler/DataTypeTiny.h +++ b/inc/hgl/platform/compiler/DataTypeTiny.h @@ -16,8 +16,8 @@ namespace hgl using f32=float; using f64=double; - #define enum_int(name) enum name:int - #define enum_uint(name) enum name:uint + #define enum_int(name) enum name:int + #define enum_uint(name) enum name:uint using void_pointer=void *; diff --git a/inc/hgl/platform/compiler/DataTypeWin.h b/inc/hgl/platform/compiler/DataTypeWin.h index 75386460..166f94fa 100644 --- a/inc/hgl/platform/compiler/DataTypeWin.h +++ b/inc/hgl/platform/compiler/DataTypeWin.h @@ -2,14 +2,14 @@ #define HGL_DATATYPE_WINDOWS_INCLUDE namespace hgl { - using int8 = signed __int8 ; ///<有符号 8位整型 - using uint8 =unsigned __int8 ; ///<无符号 8位整型 - using int16 = signed __int16; ///<有符号16位整型 - using uint16 =unsigned __int16; ///<无符号16位整型 - using int32 = signed __int32; ///<有符号32位整型 - using uint32 =unsigned __int32; ///<无符号32位整型 - using int64 = signed __int64; ///<有符号64位整型 - using uint64 =unsigned __int64; ///<无符号64位整型 + using int8 = signed __int8 ; ///<有符号 8位整型 + using uint8 =unsigned __int8 ; ///<无符号 8位整型 + using int16 = signed __int16; ///<有符号16位整型 + using uint16 =unsigned __int16; ///<无符号16位整型 + using int32 = signed __int32; ///<有符号32位整型 + using uint32 =unsigned __int32; ///<无符号32位整型 + using int64 = signed __int64; ///<有符号64位整型 + using uint64 =unsigned __int64; ///<无符号64位整型 using float32 =float; using float64 =double; diff --git a/inc/hgl/platform/compiler/EventFunc.h b/inc/hgl/platform/compiler/EventFunc.h index 84aeaeee..3bb70e0b 100644 --- a/inc/hgl/platform/compiler/EventFunc.h +++ b/inc/hgl/platform/compiler/EventFunc.h @@ -5,40 +5,40 @@ #include namespace hgl { - #ifdef __BORLANDC__ + #ifdef __BORLANDC__ - #define SetEventCall(event_obj,obj_this,class_name,event_func) event_obj=obj_this->class_name::event_func - #define SafeCallEvent(event_obj,intro) {if(event_obj)event_obj intro;} - #define CallEvent(event_obj,intro) event_obj intro - #define DefEvent(result,name,intro) result (__closure *name)intro + #define SetEventCall(event_obj,obj_this,class_name,event_func) event_obj=obj_this->class_name::event_func + #define SafeCallEvent(event_obj,intro) {if(event_obj)event_obj intro;} + #define CallEvent(event_obj,intro) event_obj intro + #define DefEvent(result,name,intro) result (__closure *name)intro - #else + #else - template struct EventFunc - { - typedef EventFunc SelfClass; + template struct EventFunc + { + typedef EventFunc SelfClass; - union - { - void *vp_this; - _Object *this_pointer; - }; + union + { + void *vp_this; + _Object *this_pointer; + }; - union - { - void *vp_func; - Func func_pointer; - ObjectMemberFunc omf; - }; + union + { + void *vp_func; + Func func_pointer; + ObjectMemberFunc omf; + }; - public: + public: - EventFunc() - { - ClearFunc(); - } + EventFunc() + { + ClearFunc(); + } - EventFunc(void *t,void *f) + EventFunc(void *t,void *f) { ClearFunc(); @@ -46,89 +46,89 @@ namespace hgl vp_func=f; } - void ClearFunc() + void ClearFunc() { memset(this,0,sizeof(SelfClass)); } - bool operator !()const - { - if(!vp_func)return(true); + bool operator !()const + { + if(!vp_func)return(true); - return(false); - } + return(false); + } - void operator = (void *v) - { - if(v==0) - memset(this,0,sizeof(SelfClass)); //omf可能不止一个指针的长度,所以必须这样清 - } + void operator = (void *v) + { + if(v==0) + memset(this,0,sizeof(SelfClass)); //omf可能不止一个指针的长度,所以必须这样清 + } - void operator = (const SelfClass &ef) - { - memcpy(this,&ef,sizeof(SelfClass)); - } + void operator = (const SelfClass &ef) + { + memcpy(this,&ef,sizeof(SelfClass)); + } - bool operator == (void *v) - { - return(vp_func==v); - } + bool operator == (void *v) + { + return(vp_func==v); + } - bool operator != (void *v) - { - return(vp_func!=v); - } + bool operator != (void *v) + { + return(vp_func!=v); + } - template - RT operator()(ARGS...args) - { - return (this_pointer->*(func_pointer))(args...); - } + template + RT operator()(ARGS...args) + { + return (this_pointer->*(func_pointer))(args...); + } - template - RT operator()(ARGS...args)const - { - return (this_pointer->*(func_pointer))(args...); - } + template + RT operator()(ARGS...args)const + { + return (this_pointer->*(func_pointer))(args...); + } - template - RT ThisCall(void *tp,ARGS...args) - { - return (((_Object *)tp)->*(func_pointer))(args...); - } + template + RT ThisCall(void *tp,ARGS...args) + { + return (((_Object *)tp)->*(func_pointer))(args...); + } - template - RT ThisCall(void *tp,ARGS...args)const - { - return (((_Object *)tp)->*(func_pointer))(args...); - } - };//template struct EventFunc + template + RT ThisCall(void *tp,ARGS...args)const + { + return (((_Object *)tp)->*(func_pointer))(args...); + } + };//template struct EventFunc - template struct EventFunc - { - typedef EventFunc SelfClass; + template struct EventFunc + { + typedef EventFunc SelfClass; - union - { - void *vp_this; - _Object *this_pointer; - }; + union + { + void *vp_this; + _Object *this_pointer; + }; - union - { - void *vp_func; - Func func_pointer; - ObjectMemberFunc omf; - }; + union + { + void *vp_func; + Func func_pointer; + ObjectMemberFunc omf; + }; - public: + public: - EventFunc() - { - memset(this,0,sizeof(SelfClass)); - } + EventFunc() + { + memset(this,0,sizeof(SelfClass)); + } - EventFunc(void *t,void *f) + EventFunc(void *t,void *f) { memset(this,0,sizeof(SelfClass)); @@ -136,115 +136,115 @@ namespace hgl vp_func=f; } - bool operator !()const - { - if(!vp_func)return(true); + bool operator !()const + { + if(!vp_func)return(true); - return(false); - } + return(false); + } - void operator = (void *v) - { - if(v==0) - memset(this,0,sizeof(SelfClass)); //omf可能不止一个指针的长度,所以必须这样清 - } + void operator = (void *v) + { + if(v==0) + memset(this,0,sizeof(SelfClass)); //omf可能不止一个指针的长度,所以必须这样清 + } - void operator = (const SelfClass &ef) - { - memcpy(this,&ef,sizeof(SelfClass)); - } + void operator = (const SelfClass &ef) + { + memcpy(this,&ef,sizeof(SelfClass)); + } - bool operator == (void *v) - { - return(vp_func==v); - } + bool operator == (void *v) + { + return(vp_func==v); + } - bool operator != (void *v) - { - return(vp_func!=v); - } + bool operator != (void *v) + { + return(vp_func!=v); + } - template - void operator()(ARGS...args) - { - (this_pointer->*(func_pointer))(args...); - } + template + void operator()(ARGS...args) + { + (this_pointer->*(func_pointer))(args...); + } - template - void operator()(ARGS...args)const - { - (this_pointer->*(func_pointer))(args...); - } + template + void operator()(ARGS...args)const + { + (this_pointer->*(func_pointer))(args...); + } - template - void ThisCall(void *tp,ARGS...args) - { - (((_Object *)tp)->*(func_pointer))(args...); - } + template + void ThisCall(void *tp,ARGS...args) + { + (((_Object *)tp)->*(func_pointer))(args...); + } - template - void ThisCall(void *tp,ARGS...args)const - { - (((_Object *)tp)->*(func_pointer))(args...); - } - };//template struct EventFunc + template + void ThisCall(void *tp,ARGS...args)const + { + (((_Object *)tp)->*(func_pointer))(args...); + } + };//template struct EventFunc - #define SetEventCall(event_obj,obj_this,class_name,event_func) { \ - event_obj.vp_this=obj_this; \ - event_obj.vp_func=GetMemberFuncPointer(class_name,event_func); \ - } + #define SetEventCall(event_obj,obj_this,class_name,event_func) { \ + event_obj.vp_this=obj_this; \ + event_obj.vp_func=GetMemberFuncPointer(class_name,event_func); \ + } - #define SetEventThis(event_obj,obj_this) event_obj.vp_this=obj_this; + #define SetEventThis(event_obj,obj_this) event_obj.vp_this=obj_this; -// #define CallEvent(event_obj,intro) ((event_obj.this_pointer->*(event_obj.func_pointer))intro) +// #define CallEvent(event_obj,intro) ((event_obj.this_pointer->*(event_obj.func_pointer))intro) - #define SafeCallEvent(event_obj,intro) {if(event_obj.vp_func)event_obj intro;} + #define SafeCallEvent(event_obj,intro) {if(event_obj.vp_func)event_obj intro;} - #define DefEvent(result,name,intro) EventFunc name; + #define DefEvent(result,name,intro) EventFunc name; - #endif//__BORLANDC__ + #endif//__BORLANDC__ - /* + /* - 使用方法: + 使用方法: - class Example - { - //原Borland/CodeGear方式 - void (__closure *OnClick)(Object *); + class Example + { + //原Borland/CodeGear方式 + void (__closure *OnClick)(Object *); - //现通用方式 - DefEvent(void,OnClick,(Object *)); - }; + //现通用方式 + DefEvent(void,OnClick,(Object *)); + }; - void Test::ClickProc(Object *); + void Test::ClickProc(Object *); - void Test::func() - { - Example *exp; + void Test::func() + { + Example *exp; - { - //原Borland/CodeGear方式 - exp->OnClick=this->ClickProc; - 或 - exp->OnClick=ClickProc; + { + //原Borland/CodeGear方式 + exp->OnClick=this->ClickProc; + 或 + exp->OnClick=ClickProc; - //现通用方式 - SetEventCall(exp->OnClick,this,Test,ClickProc); - } + //现通用方式 + SetEventCall(exp->OnClick,this,Test,ClickProc); + } - { - //原Borland/CodeGear方式 - exp->OnClick(nullptr); + { + //原Borland/CodeGear方式 + exp->OnClick(nullptr); - //现通用方式 - CallEvent(exp->OnClick,(nullptr)); + //现通用方式 + CallEvent(exp->OnClick,(nullptr)); - //C++11方式 - exp->OnClick(nullptr); - } - } + //C++11方式 + exp->OnClick(nullptr); + } + } - */ + */ }//namespace hgl #endif//HGL_EVENT_FUNC_INCLUDE diff --git a/inc/hgl/platform/compiler/GNU.h b/inc/hgl/platform/compiler/GNU.h index f7a28b27..90cdd00e 100644 --- a/inc/hgl/platform/compiler/GNU.h +++ b/inc/hgl/platform/compiler/GNU.h @@ -1,82 +1,82 @@ #ifndef HGL_COMPILER_GNU_INCLUDE #define HGL_COMPILER_GNU_INCLUDE //-------------------------------------------------------------------------------------------------- -#define HGL_COMPILER_NAME OS_TEXT("GNU C/C++") -#define HGL_LIB_COMPILER_NAME OS_TEXT("GCC") +#define HGL_COMPILER_NAME OS_TEXT("GNU C/C++") +#define HGL_LIB_COMPILER_NAME OS_TEXT("GCC") #if (__GNUC__<4)||(__GNUC__==4&&__GNUC_MINOR__<8) - #error Please upgrade your compiler or development tools to GNU C/C++ 4.8 or later + #error Please upgrade your compiler or development tools to GNU C/C++ 4.8 or later #else - #if __GNUC__ == 4 - #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("4") - #elif __GNUC__ == 5 - #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("5") - #elif __GNUC__ == 6 - #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("6") - #elif __GNUC__ == 7 - #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("7") + #if __GNUC__ == 4 + #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("4") + #elif __GNUC__ == 5 + #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("5") + #elif __GNUC__ == 6 + #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("6") + #elif __GNUC__ == 7 + #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("7") #elif __GNUC__ == 8 #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("8") #elif __GNUC__ == 9 #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("9") #elif __GNUC__ == 10 #define HGL_LIB_COMPILER_MAIOR_VER OS_TEXT("10") - #endif//__GNUC__ + #endif//__GNUC__ - #if __GNUC_MINOR__ == 0 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("0") - #elif __GNUC_MINOR__ == 1 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("1") - #elif __GNUC_MINOR__ == 2 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("2") - #elif __GNUC_MINOR__ == 3 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("3") - #elif __GNUC_MINOR__ == 4 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("4") - #elif __GNUC_MINOR__ == 5 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("5") - #elif __GNUC_MINOR__ == 6 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("6") - #elif __GNUC_MINOR__ == 7 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("7") - #elif __GNUC_MINOR__ == 8 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("8") - #elif __GNUC_MINOR__ == 9 - #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("9") - #endif// + #if __GNUC_MINOR__ == 0 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("0") + #elif __GNUC_MINOR__ == 1 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("1") + #elif __GNUC_MINOR__ == 2 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("2") + #elif __GNUC_MINOR__ == 3 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("3") + #elif __GNUC_MINOR__ == 4 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("4") + #elif __GNUC_MINOR__ == 5 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("5") + #elif __GNUC_MINOR__ == 6 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("6") + #elif __GNUC_MINOR__ == 7 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("7") + #elif __GNUC_MINOR__ == 8 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("8") + #elif __GNUC_MINOR__ == 9 + #define HGL_LIB_COMPILER_MINOR_VER OS_TEXT("9") + #endif// - #if __GNUC_PATCHLEVEL__ == 0 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("0") - #elif __GNUC_PATCHLEVEL__ == 1 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("1") - #elif __GNUC_PATCHLEVEL__ == 2 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("2") - #elif __GNUC_PATCHLEVEL__ == 3 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("3") - #elif __GNUC_PATCHLEVEL__ == 4 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("4") - #elif __GNUC_PATCHLEVEL__ == 5 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("5") - #elif __GNUC_PATCHLEVEL__ == 6 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("6") - #elif __GNUC_PATCHLEVEL__ == 7 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("7") - #elif __GNUC_PATCHLEVEL__ == 8 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("8") - #elif __GNUC_PATCHLEVEL__ == 9 - #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("9") - #endif// + #if __GNUC_PATCHLEVEL__ == 0 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("0") + #elif __GNUC_PATCHLEVEL__ == 1 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("1") + #elif __GNUC_PATCHLEVEL__ == 2 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("2") + #elif __GNUC_PATCHLEVEL__ == 3 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("3") + #elif __GNUC_PATCHLEVEL__ == 4 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("4") + #elif __GNUC_PATCHLEVEL__ == 5 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("5") + #elif __GNUC_PATCHLEVEL__ == 6 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("6") + #elif __GNUC_PATCHLEVEL__ == 7 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("7") + #elif __GNUC_PATCHLEVEL__ == 8 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("8") + #elif __GNUC_PATCHLEVEL__ == 9 + #define HGL_LIB_COMPILER_PATCH_LEVEL OS_TEXT("9") + #endif// - #define HGL_LIB_COMPILER_VERSION HGL_LIB_COMPILER_MAIOR_VER OS_TEXT(".") HGL_LIB_COMPILER_MINOR_VER OS_TEXT(".") HGL_LIB_COMPILER_PATCH_LEVEL + #define HGL_LIB_COMPILER_VERSION HGL_LIB_COMPILER_MAIOR_VER OS_TEXT(".") HGL_LIB_COMPILER_MINOR_VER OS_TEXT(".") HGL_LIB_COMPILER_PATCH_LEVEL #endif//__GNUC__ #define HGL_THREAD_LOCAL_STORAGE __thread //线程本地储存 //-------------------------------------------------------------------------------------------------- -#define HGL_LIB_FRONT HGL_LIB_OS "_" HGL_LIB_COMPILER_NAME "_" HGL_LIB_DEBUG_NAME "_" +#define HGL_LIB_FRONT HGL_LIB_OS "_" HGL_LIB_COMPILER_NAME "_" HGL_LIB_DEBUG_NAME "_" -#define HGL_LIB_END ".a" +#define HGL_LIB_END ".a" //-------------------------------------------------------------------------------------------------- #include #include diff --git a/inc/hgl/platform/compiler/Intel.h b/inc/hgl/platform/compiler/Intel.h index e9246be8..0e380743 100644 --- a/inc/hgl/platform/compiler/Intel.h +++ b/inc/hgl/platform/compiler/Intel.h @@ -1,18 +1,18 @@ #ifndef HGL_COMPILER_INTEL_INCLUDE #define HGL_COMPILER_INTEL_INCLUDE //-------------------------------------------------------------------------------------------------- -#define HGL_COMPILER_NAME u"Intel C/C++" +#define HGL_COMPILER_NAME u"Intel C/C++" //-------------------------------------------------------------------------------------------------- #if __INTEL_COMPILER < 1200 - #error Please upgrade your compiler To Intel C/C++ 12.1 or later. + #error Please upgrade your compiler To Intel C/C++ 12.1 or later. #else - #if __INTEL_COMPILER >=1200 && __INTEL_COMPILER < 1300 - #define HGL_LIB_COMPILER_NAME "IntelCPP12" - #else - #define HGL_LIB_COMPILER_NAME "IntelCPP_UnknownVersion" - #endif//__INTEL_COMPILER + #if __INTEL_COMPILER >=1200 && __INTEL_COMPILER < 1300 + #define HGL_LIB_COMPILER_NAME "IntelCPP12" + #else + #define HGL_LIB_COMPILER_NAME "IntelCPP_UnknownVersion" + #endif//__INTEL_COMPILER - #define enum_uint(name) enum name:unsigned int + #define enum_uint(name) enum name:unsigned int #endif//__INTEL_COMPILER #define vsnwprintf _vsnwprintf @@ -22,22 +22,22 @@ #include #if HGL_OS == HGL_OS_Windows - #define HGL_THREAD __declspec(thread) + #define HGL_THREAD __declspec(thread) #else - #define HGL_THREAD __thread + #define HGL_THREAD __thread #endif//HGL_OS == HGL_OS_Windows //-------------------------------------------------------------------------------------------------- #ifdef _DLL - #define HGL_LIB_CRT "Dynamic" + #define HGL_LIB_CRT "Dynamic" #else - #define HGL_LIB_CRT "Static" + #define HGL_LIB_CRT "Static" #endif//_DLL -#define HGL_LIB_FRONT HGL_LIB_OS "_" HGL_LIB_COMPILER_NAME "_" HGL_LIB_DEBUG_NAME "_" HGL_LIB_CRT "_" +#define HGL_LIB_FRONT HGL_LIB_OS "_" HGL_LIB_COMPILER_NAME "_" HGL_LIB_DEBUG_NAME "_" HGL_LIB_CRT "_" #if HGL_OS == HGL_OS_Windows - #define HGL_LIB_END ".LIB" + #define HGL_LIB_END ".LIB" #elif - #define HGL_LIB_END ".a" + #define HGL_LIB_END ".a" #endif//HGL_OS == HGL_OS_Windows #endif//HGL_COMPILER_INTEL_INCLUDE diff --git a/inc/hgl/platform/compiler/LLVM.h b/inc/hgl/platform/compiler/LLVM.h index a3cabe58..e8cd7c67 100644 --- a/inc/hgl/platform/compiler/LLVM.h +++ b/inc/hgl/platform/compiler/LLVM.h @@ -1,22 +1,22 @@ #ifndef HGL_COMPILER_LLVM_CLANG_INCLUDE #define HGL_COMPILER_LLVM_CLANG_INCLUDE //-------------------------------------------------------------------------------------------------- -#define HGL_COMPILER_NAME OS_TEXT("LLVM Clang") -#define HGL_LIB_COMPILER_NAME OS_TEXT("LLVM") +#define HGL_COMPILER_NAME OS_TEXT("LLVM Clang") +#define HGL_LIB_COMPILER_NAME OS_TEXT("LLVM") #if (__clang_major__<3)||(__clang_major__==3&&__clang_minor__<7) - #error Please upgrade your compiler or development tools to LLVM Clang 3.7 or later + #error Please upgrade your compiler or development tools to LLVM Clang 3.7 or later #else - #define HGL_LIB_COMPILER_VERSION __clang_version__ + #define HGL_LIB_COMPILER_VERSION __clang_version__ #endif//__clang__ #define HGL_THREAD_LOCAL_STORAGE __thread //线程本地储存 //-------------------------------------------------------------------------------------------------- -#define HGL_LIB_FRONT HGL_LIB_OS "_" HGL_LIB_COMPILER_NAME "_" HGL_LIB_DEBUG_NAME "_" +#define HGL_LIB_FRONT HGL_LIB_OS "_" HGL_LIB_COMPILER_NAME "_" HGL_LIB_DEBUG_NAME "_" -#define HGL_LIB_END ".a" +#define HGL_LIB_END ".a" #include #include diff --git a/inc/hgl/platform/compiler/Microsoft.h b/inc/hgl/platform/compiler/Microsoft.h index b3ee8b32..9a3b8504 100644 --- a/inc/hgl/platform/compiler/Microsoft.h +++ b/inc/hgl/platform/compiler/Microsoft.h @@ -11,8 +11,8 @@ #define HGL_LIB_COMPILER_VERSION OS_TEXT("19.1") //Visual C++ 2017 #elif _MSC_VER == 1900 #define HGL_LIB_COMPILER_VERSION OS_TEXT("19") //Visual C++ 2015 - #else - #define HGL_LIB_COMPILER_VERSION OS_TEXT("Unknow") + #else + #define HGL_LIB_COMPILER_VERSION OS_TEXT("Unknow") #endif//_MSC_VER #endif//_MSC_VER diff --git a/inc/hgl/platform/compiler/Property.h b/inc/hgl/platform/compiler/Property.h index f0f11fc3..f147f304 100644 --- a/inc/hgl/platform/compiler/Property.h +++ b/inc/hgl/platform/compiler/Property.h @@ -9,89 +9,89 @@ namespace hgl */ template class PropertyRead { - public: + public: - DefEvent(T,Get,() const); + DefEvent(T,Get,() const); };//template class PropertyRead - + /** * 属性 */ - template class Property - { - public: + template class Property + { + public: - DefEvent(T,Get,() const); - DefEvent(void,Set,(T)); + DefEvent(T,Get,() const); + DefEvent(void,Set,(T)); - public: + public: - Property() - { - Get=0; - Set=0; - } + Property() + { + Get=0; + Set=0; + } - virtual ~Property() {} + virtual ~Property() {} - operator T() const{return Get();} - void operator = (T v){Set(v);} - void operator = (const Property &v){Set(v.operator T());} + operator T() const{return Get();} + void operator = (T v){Set(v);} + void operator = (const Property &v){Set(v.operator T());} T operator !(){ return !(operator T());} T operator ~(){ return ~(operator T());} - T operator ++ () { T v=operator T(); Set(++v); return v; } ///<前置++ - T operator -- () { T v=operator T(); Set(--v); return v; } ///<前置-- + T operator ++ () { T v=operator T(); Set(++v); return v; } ///<前置++ + T operator -- () { T v=operator T(); Set(--v); return v; } ///<前置-- - T operator ++ (int) { T r,v; v=operator T(); r=v; Set(++v); return r; } ///<后置++ - T operator -- (int) { T r,v; v=operator T(); r=v; Set(--v); return r; } ///<后置-- + T operator ++ (int) { T r,v; v=operator T(); r=v; Set(++v); return r; } ///<后置++ + T operator -- (int) { T r,v; v=operator T(); r=v; Set(--v); return r; } ///<后置-- - void operator += (T v) { if(v){Set(operator T() + v);} } - void operator -= (T v) { if(v){Set(operator T() - v);} } - void operator *= (T v) { Set(operator T() * v); } - void operator /= (T v) { Set(operator T() / v); } - void operator %= (T v) { Set(operator T() % v); } + void operator += (T v) { if(v){Set(operator T() + v);} } + void operator -= (T v) { if(v){Set(operator T() - v);} } + void operator *= (T v) { Set(operator T() * v); } + void operator /= (T v) { Set(operator T() / v); } + void operator %= (T v) { Set(operator T() % v); } - void operator &= (T v) { Set(operator T() & v); } - void operator |= (T v) { Set(operator T() | v); } + void operator &= (T v) { Set(operator T() & v); } + void operator |= (T v) { Set(operator T() | v); } - void operator >>= (int n) { Set((operator T())>>n);} - void operator <<= (int n) { Set((operator T())<>= (int n) { Set((operator T())>>n);} + void operator <<= (int n) { Set((operator T())<> (int n) { return (operator T())>>n;} - T operator << (int n) { return (operator T())<> (int n) { return (operator T())>>n;} + T operator << (int n) { return (operator T())< class PropertyObject:public Property - { - public: + template class PropertyObject:public Property + { + public: - T *operator ->()const{return Property::Get();} - void operator =(void *pointer){Property::Set((T *)pointer);} - };//class PropertyObject + T *operator ->()const{return Property::Get();} + void operator =(void *pointer){Property::Set((T *)pointer);} + };//class PropertyObject - #ifdef __BORLANDC__ - #define cmSetPropertyRead(name,tp,get) {name.Get=tp->get;} - #define cmSetPropertyWrite(name,tp,set) {name.Set=tp->set;} - #else - #define cmSetPropertyRead(name,tp,get) { \ - name.Get.vp_this=tp; \ - name.Get.omf=ObjectMemberFunc(&get); \ - } + #ifdef __BORLANDC__ + #define cmSetPropertyRead(name,tp,get) {name.Get=tp->get;} + #define cmSetPropertyWrite(name,tp,set) {name.Set=tp->set;} + #else + #define cmSetPropertyRead(name,tp,get) { \ + name.Get.vp_this=tp; \ + name.Get.omf=ObjectMemberFunc(&get); \ + } - #define cmSetPropertyWrite(name,tp,set) { \ - name.Set.vp_this=tp; \ - name.Set.omf=ObjectMemberFunc(&set); \ - } - #endif// + #define cmSetPropertyWrite(name,tp,set) { \ + name.Set.vp_this=tp; \ + name.Set.omf=ObjectMemberFunc(&set); \ + } + #endif// - #define cmSetProperty(name,tp,get,set) { \ - cmSetPropertyRead(name,tp,get); \ - cmSetPropertyWrite(name,tp,set); \ - } + #define cmSetProperty(name,tp,get,set) { \ + cmSetPropertyRead(name,tp,get); \ + cmSetPropertyWrite(name,tp,set); \ + } }//namespace hgl #endif//HGL_PROPERTY_INCLUDE diff --git a/inc/hgl/platform/os/Android.h b/inc/hgl/platform/os/Android.h index 40371e09..ad871659 100644 --- a/inc/hgl/platform/os/Android.h +++ b/inc/hgl/platform/os/Android.h @@ -4,44 +4,44 @@ #include //-------------------------------------------------------------------------------------------------- using u32char =char32_t; -using u16char =char16_t; -using os_char =char; -#define to_oschar to_u8 -#define OS_TEXT(str) u8##str -#define U8_TEXT(str) u8##str -#define U16_TEXT(str) u##str +using u16char =char16_t; +using os_char =char; +#define to_oschar to_u8 +#define OS_TEXT(str) u8##str +#define U8_TEXT(str) u8##str +#define U16_TEXT(str) u##str -#define HGL_OS_NAME OS_TEXT("Android") -#define HGL_LIB_OS "Android" //库操作系统前缀 +#define HGL_OS_NAME OS_TEXT("Android") +#define HGL_LIB_OS "Android" //库操作系统前缀 //-------------------------------------------------------------------------------------------------- -#define HGL_PLUGIN_FRONTNAME "libCMP." //插件文件名前缀 -#define HGL_PLUGIN_EXTNAME ".so" //插件文件扩展名 -#define HGL_PLUGIN_FUNC extern "C" //插件函数定义 +#define HGL_PLUGIN_FRONTNAME "libCMP." //插件文件名前缀 +#define HGL_PLUGIN_EXTNAME ".so" //插件文件扩展名 +#define HGL_PLUGIN_FUNC extern "C" //插件函数定义 -#define HGL_DIRECTORY_SEPARATOR '/' //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("/") //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("/") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR '/' //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("/") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("/") //目录分隔符 -#define HGL_LINE_END "\n" //换行符 -#define HGL_LINE_END_SIZE 1 //换行符长度 +#define HGL_LINE_END "\n" //换行符 +#define HGL_LINE_END_SIZE 1 //换行符长度 -#define HGL_MEM_ALIGN 16 //内存对齐字节数 +#define HGL_MEM_ALIGN 16 //内存对齐字节数 -#define HGL_MAX_PATH _POSIX_PATH_MAX +#define HGL_MAX_PATH _POSIX_PATH_MAX -#define HGL_FMT_I64 "%lld" -#define HGL_FMT_U64 "%llu" -#define HGL_FMT_DOUBLE "%lf" -#define HGL_FMT_LONG_DOUBLE "%le" +#define HGL_FMT_I64 "%lld" +#define HGL_FMT_U64 "%llu" +#define HGL_FMT_DOUBLE "%lf" +#define HGL_FMT_LONG_DOUBLE "%le" //-------------------------------------------------------------------------------------------------- #include #include #include -#define hgl_malloc(size) memalign(HGL_MEM_ALIGN,size) //这个所有版本linux libc都支持 -//#define hgl_malloc(size) aligned_alloc(HGL_MEM_ALIGN,size) //这个是C11新增,需要libc 2.16 -#define hgl_realloc(ptr,size) realloc(ptr,size) -#define hgl_free free +#define hgl_malloc(size) memalign(HGL_MEM_ALIGN,size) //这个所有版本linux libc都支持 +//#define hgl_malloc(size) aligned_alloc(HGL_MEM_ALIGN,size) //这个是C11新增,需要libc 2.16 +#define hgl_realloc(ptr,size) realloc(ptr,size) +#define hgl_free free template inline T *hgl_aligned_malloc(size_t n) @@ -54,25 +54,25 @@ typedef void * ExternalModulePointer; #define pi_get dlsym #define pi_close dlclose -#define iconv_str char * +#define iconv_str char * -#define hgl_stat64 stat -#define hgl_open64 open -#define hgl_lseek64 lseek -#define hgl_tell64(fp) lseek(fp,0,SEEK_CUR) -#define hgl_fstat64 fstat -#define hgl_lstat64 lstat -#define hgl_read64 read -#define hgl_write64 write -#define hgl_pread64 pread -#define hgl_pwrite64 pwrite +#define hgl_stat64 stat +#define hgl_open64 open +#define hgl_lseek64 lseek +#define hgl_tell64(fp) lseek(fp,0,SEEK_CUR) +#define hgl_fstat64 fstat +#define hgl_lstat64 lstat +#define hgl_read64 read +#define hgl_write64 write +#define hgl_pread64 pread +#define hgl_pwrite64 pwrite -#define struct_stat64 struct stat -#define struct_dirent64 struct dirent -#define hgl_dirent64 dirent -#define hgl_readdir64 readdir +#define struct_stat64 struct stat +#define struct_dirent64 struct dirent +#define hgl_dirent64 dirent +#define hgl_readdir64 readdir -#define sprintf_s snprintf +#define sprintf_s snprintf //-------------------------------------------------------------------------------------------------- //初始化Android支持,以下函数二选一 diff --git a/inc/hgl/platform/os/BSD.h b/inc/hgl/platform/os/BSD.h index f093d1c8..c0a4778f 100644 --- a/inc/hgl/platform/os/BSD.h +++ b/inc/hgl/platform/os/BSD.h @@ -4,52 +4,52 @@ #include //-------------------------------------------------------------------------------------------------- using u32char =char32_t; -using u16char =char16_t; -using os_char =char; -#define to_oschar to_u8 -#define OS_TEXT(str) u8##str -#define U8_TEXT(str) u8##str -#define U16_TEXT(str) u##str +using u16char =char16_t; +using os_char =char; +#define to_oschar to_u8 +#define OS_TEXT(str) u8##str +#define U8_TEXT(str) u8##str +#define U16_TEXT(str) u##str #if defined(__FreeBSD__)||defined(__FreeBSD) - #define HGL_OS_NAME OS_TEXT("FreeBSD") - #define HGL_LIB_OS_NAME "FreeBSD" + #define HGL_OS_NAME OS_TEXT("FreeBSD") + #define HGL_LIB_OS_NAME "FreeBSD" #elif defined(__NetBSD__)||defined(__NetBSD) - #define HGL_OS_NAME OS_TEXT("NetBSD") - #define HGL_LIB_OS_NAME "NetBSD" + #define HGL_OS_NAME OS_TEXT("NetBSD") + #define HGL_LIB_OS_NAME "NetBSD" #elif defined(__OpenBSD__)||defined(__OPENBSD) - #define HGL_OS_NAME OS_TEXT("OpenBSD") - #define HGL_LIB_OS_NAME "OpenBSD" + #define HGL_OS_NAME OS_TEXT("OpenBSD") + #define HGL_LIB_OS_NAME "OpenBSD" #else - #define HGL_OS_NAME OS_TEXT("BSD") - #define HGL_LIB_OS_NAME "BSD" + #define HGL_OS_NAME OS_TEXT("BSD") + #define HGL_LIB_OS_NAME "BSD" #endif// -#define HGL_LIB_OS "BSD" //库操作系统前缀 +#define HGL_LIB_OS "BSD" //库操作系统前缀 //-------------------------------------------------------------------------------------------------- -#define HGL_PLUGIN_FRONTNAME "libCMP." //插件文件名前缀 -#define HGL_PLUGIN_EXTNAME ".so" //插件文件扩展名 -#define HGL_PLUGIN_FUNC extern "C" //插件函数定义 +#define HGL_PLUGIN_FRONTNAME "libCMP." //插件文件名前缀 +#define HGL_PLUGIN_EXTNAME ".so" //插件文件扩展名 +#define HGL_PLUGIN_FUNC extern "C" //插件函数定义 -#define HGL_DIRECTORY_SEPARATOR '/' //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("/") //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("/") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR '/' //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("/") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("/") //目录分隔符 -#define HGL_LINE_END "\n" //换行符 -#define HGL_LINE_END_SIZE 1 //换行符长度 +#define HGL_LINE_END "\n" //换行符 +#define HGL_LINE_END_SIZE 1 //换行符长度 -#define HGL_MEM_ALIGN 16 //内存对齐字节数 +#define HGL_MEM_ALIGN 16 //内存对齐字节数 -#define HGL_MAX_PATH _POSIX_PATH_MAX +#define HGL_MAX_PATH _POSIX_PATH_MAX -#define HGL_FMT_I64 "%lld" +#define HGL_FMT_I64 "%lld" //-------------------------------------------------------------------------------------------------- #include #include -#define hgl_malloc(size) aligned_alloc(HGL_MEM_ALIGN,size) //这个是C11新增,需要libc 2.16 +#define hgl_malloc(size) aligned_alloc(HGL_MEM_ALIGN,size) //这个是C11新增,需要libc 2.16 #define hgl_realloc realloc -#define hgl_free free +#define hgl_free free template inline T *hgl_aligned_malloc(size_t n) @@ -64,20 +64,20 @@ typedef void * ExternalModulePointer; typedef pthread_mutex_t hgl_thread_mutex; -#define hgl_stat64 stat -#define hgl_open64 open -#define hgl_lseek64 lseek -#define hgl_tell64(fp) lseek(fp,0,SEEK_CUR) -#define hgl_fstat64 fstat -#define hgl_lstat64 lstat -#define hgl_read64 read -#define hgl_write64 write -#define hgl_pread64 pread -#define hgl_pwrite64 pwrite +#define hgl_stat64 stat +#define hgl_open64 open +#define hgl_lseek64 lseek +#define hgl_tell64(fp) lseek(fp,0,SEEK_CUR) +#define hgl_fstat64 fstat +#define hgl_lstat64 lstat +#define hgl_read64 read +#define hgl_write64 write +#define hgl_pread64 pread +#define hgl_pwrite64 pwrite -#define struct_stat64 struct stat -#define struct_dirent64 struct dirent -#define hgl_dirent64 dirent -#define hgl_readdir64 readdir +#define struct_stat64 struct stat +#define struct_dirent64 struct dirent +#define hgl_dirent64 dirent +#define hgl_readdir64 readdir //-------------------------------------------------------------------------------------------------- #endif//HGL_OS_BSD_INCLUDE diff --git a/inc/hgl/platform/os/Linux.h b/inc/hgl/platform/os/Linux.h index 2b3f84df..2447a513 100644 --- a/inc/hgl/platform/os/Linux.h +++ b/inc/hgl/platform/os/Linux.h @@ -4,44 +4,44 @@ #include //-------------------------------------------------------------------------------------------------- using u32char =char32_t; -using u16char =char16_t; -using os_char =char; -#define to_oschar to_u8 -#define OS_TEXT(str) u8##str -#define U8_TEXT(str) u8##str -#define U16_TEXT(str) u##str +using u16char =char16_t; +using os_char =char; +#define to_oschar to_u8 +#define OS_TEXT(str) u8##str +#define U8_TEXT(str) u8##str +#define U16_TEXT(str) u##str -#define HGL_OS_NAME OS_TEXT("Linux") -#define HGL_LIB_OS "Linux" //库操作系统前缀 +#define HGL_OS_NAME OS_TEXT("Linux") +#define HGL_LIB_OS "Linux" //库操作系统前缀 //-------------------------------------------------------------------------------------------------- -#define HGL_PLUGIN_FRONTNAME "libCMP." //插件文件名前缀 -#define HGL_PLUGIN_EXTNAME ".so" //插件文件扩展名 -#define HGL_PLUGIN_FUNC extern "C" //插件函数定义 +#define HGL_PLUGIN_FRONTNAME "libCMP." //插件文件名前缀 +#define HGL_PLUGIN_EXTNAME ".so" //插件文件扩展名 +#define HGL_PLUGIN_FUNC extern "C" //插件函数定义 -#define HGL_DIRECTORY_SEPARATOR '/' //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("/") //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("/") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR '/' //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("/") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("/") //目录分隔符 -#define HGL_LINE_END "\n" //换行符 -#define HGL_LINE_END_SIZE 1 //换行符长度 +#define HGL_LINE_END "\n" //换行符 +#define HGL_LINE_END_SIZE 1 //换行符长度 -#define HGL_MEM_ALIGN 16 //内存对齐字节数 +#define HGL_MEM_ALIGN 16 //内存对齐字节数 -#define HGL_MAX_PATH _POSIX_PATH_MAX +#define HGL_MAX_PATH _POSIX_PATH_MAX -#define HGL_FMT_I64 "%lld" -#define HGL_FMT_U64 "%llu" -#define HGL_FMT_DOUBLE "%lf" -#define HGL_FMT_LONG_DOUBLE "%le" +#define HGL_FMT_I64 "%lld" +#define HGL_FMT_U64 "%llu" +#define HGL_FMT_DOUBLE "%lf" +#define HGL_FMT_LONG_DOUBLE "%le" //-------------------------------------------------------------------------------------------------- #include #include #include -//#define hgl_malloc(size) memalign(HGL_MEM_ALIGN,size) //这个所有版本linux libc都支持 -#define hgl_malloc(size) aligned_alloc(HGL_MEM_ALIGN,size) //这个是C11新增,需要libc 2.16 -#define hgl_realloc(ptr,size) realloc(ptr,size) -#define hgl_free free +//#define hgl_malloc(size) memalign(HGL_MEM_ALIGN,size) //这个所有版本linux libc都支持 +#define hgl_malloc(size) aligned_alloc(HGL_MEM_ALIGN,size) //这个是C11新增,需要libc 2.16 +#define hgl_realloc(ptr,size) realloc(ptr,size) +#define hgl_free free template inline T *hgl_aligned_malloc(size_t n) @@ -54,22 +54,22 @@ typedef void * ExternalModulePointer; #define pi_get dlsym #define pi_close dlclose -#define hgl_stat64 stat64 -#define hgl_open64 open64 -#define hgl_lseek64 lseek64 -#define hgl_tell64(fp) lseek64(fp,0,SEEK_CUR) -#define hgl_fstat64 fstat64 -#define hgl_lstat64 lstat64 -#define hgl_read64 read -#define hgl_write64 write -#define hgl_pread64 pread64 -#define hgl_pwrite64 pwrite64 +#define hgl_stat64 stat64 +#define hgl_open64 open64 +#define hgl_lseek64 lseek64 +#define hgl_tell64(fp) lseek64(fp,0,SEEK_CUR) +#define hgl_fstat64 fstat64 +#define hgl_lstat64 lstat64 +#define hgl_read64 read +#define hgl_write64 write +#define hgl_pread64 pread64 +#define hgl_pwrite64 pwrite64 -#define struct_stat64 struct stat64 -#define struct_dirent64 struct dirent64 -#define hgl_dirent64 dirent64 -#define hgl_readdir64 readdir64 +#define struct_stat64 struct stat64 +#define struct_dirent64 struct dirent64 +#define hgl_dirent64 dirent64 +#define hgl_readdir64 readdir64 -#define sprintf_s snprintf +#define sprintf_s snprintf //-------------------------------------------------------------------------------------------------- #endif//HGL_OS_LINUX_INCLUDE diff --git a/inc/hgl/platform/os/MSWindows.h b/inc/hgl/platform/os/MSWindows.h index b388f7c0..06b7b406 100644 --- a/inc/hgl/platform/os/MSWindows.h +++ b/inc/hgl/platform/os/MSWindows.h @@ -9,38 +9,38 @@ #undef max //-------------------------------------------------------------------------------------------------- using u32char =char32_t; -using u16char =wchar_t; -using os_char =wchar_t; -#define to_oschar to_u16 -#define OS_TEXT(str) L##str -#define U8_TEXT(str) u8##str -#define U16_TEXT(str) L##str +using u16char =wchar_t; +using os_char =wchar_t; +#define to_oschar to_u16 +#define OS_TEXT(str) L##str +#define U8_TEXT(str) u8##str +#define U16_TEXT(str) L##str -#define HGL_OS_NAME OS_TEXT("Windows") +#define HGL_OS_NAME OS_TEXT("Windows") #if HGL_CPU == HGL_CPU_X86_32 - #define HGL_LIB_OS "Win32" //库操作系统前缀 + #define HGL_LIB_OS "Win32" //库操作系统前缀 #elif HGL_CPU == HGL_CPU_X86_64 - #define HGL_LIB_OS "Win64" //库操作系统前缀 + #define HGL_LIB_OS "Win64" //库操作系统前缀 #endif//HGL_CPU //-------------------------------------------------------------------------------------------------- -#define HGL_PLUGIN_FRONTNAME OS_TEXT("CMP.") //插件文件名前缀 -#define HGL_PLUGIN_EXTNAME OS_TEXT(".DLL") //插件文件扩展名 -#define HGL_PLUGIN_FUNC extern "C" __declspec(dllexport) //插件函数定义 +#define HGL_PLUGIN_FRONTNAME OS_TEXT("CMP.") //插件文件名前缀 +#define HGL_PLUGIN_EXTNAME OS_TEXT(".DLL") //插件文件扩展名 +#define HGL_PLUGIN_FUNC extern "C" __declspec(dllexport) //插件函数定义 -#define HGL_DIRECTORY_SEPARATOR OS_TEXT('\\') //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("\\") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR OS_TEXT('\\') //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("\\") //目录分隔符 #define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("\\") -#define HGL_LINE_END OS_TEXT("\r\n") //换行符 -#define HGL_LINE_END_SIZE 2 //换行符长度 +#define HGL_LINE_END OS_TEXT("\r\n") //换行符 +#define HGL_LINE_END_SIZE 2 //换行符长度 -#define HGL_MAX_PATH MAX_PATH +#define HGL_MAX_PATH MAX_PATH -#define HGL_MEM_ALIGN 16 //内存对齐字节数 +#define HGL_MEM_ALIGN 16 //内存对齐字节数 -#define HGL_GL_WINDOW_INCLUDE_FILE //指定OpenGL窗口引用头文件 -#define HGL_GL_WINDOW_CLASS WinGLWindow //指定OpenGL窗口类名称 +#define HGL_GL_WINDOW_INCLUDE_FILE //指定OpenGL窗口引用头文件 +#define HGL_GL_WINDOW_CLASS WinGLWindow //指定OpenGL窗口类名称 //-------------------------------------------------------------------------------------------------- #if HGL_COMPILER == HGL_COMPILER_Microsoft #define hgl_malloc(size) _aligned_malloc(size,HGL_MEM_ALIGN) @@ -63,14 +63,14 @@ typedef HMODULE ExternalModulePointer; #define pi_get GetProcAddress #define pi_close FreeLibrary -#define struct_stat64 struct _stat64 -//#define hgl_stat64 _stat64 -#define hgl_lseek64 _lseeki64 -#define hgl_tell64(fp) _telli64(fp) -#define hgl_fstat64 _fstati64 -#define hgl_lstat64 _wstat64 -#define hgl_read64 _read -#define hgl_write64 _write +#define struct_stat64 struct _stat64 +//#define hgl_stat64 _stat64 +#define hgl_lseek64 _lseeki64 +#define hgl_tell64(fp) _telli64(fp) +#define hgl_fstat64 _fstati64 +#define hgl_lstat64 _wstat64 +#define hgl_read64 _read +#define hgl_write64 _write using hgl_thread_mutex =CRITICAL_SECTION; using thread_ptr =HANDLE; diff --git a/inc/hgl/platform/os/MacOS.h b/inc/hgl/platform/os/MacOS.h index 43d22579..b2da948d 100644 --- a/inc/hgl/platform/os/MacOS.h +++ b/inc/hgl/platform/os/MacOS.h @@ -4,49 +4,49 @@ #include //------------------------------------------------------------------ using u32char =char32_t; -using u16char =char16_t; -using os_char =char; -#define to_oschar to_u8 -#define OS_TEXT(str) u8##str -#define U8_TEXT(str) u8##str -#define U16_TEXT(str) u##str +using u16char =char16_t; +using os_char =char; +#define to_oschar to_u8 +#define OS_TEXT(str) u8##str +#define U8_TEXT(str) u8##str +#define U16_TEXT(str) u##str //-------------------------------------------------------------------------------------------------- #if HGL_OS == HGL_OS_iOS - #define HGL_OS_NAME OS_TEXT("iOS") - #define HGL_LIB_OS "iOS" //库操作系统前缀 + #define HGL_OS_NAME OS_TEXT("iOS") + #define HGL_LIB_OS "iOS" //库操作系统前缀 #else - #define HGL_OS_NAME OS_TEXT("macOS") - #define HGL_LIB_OS "mac" //库操作系统前缀 + #define HGL_OS_NAME OS_TEXT("macOS") + #define HGL_LIB_OS "mac" //库操作系统前缀 #endif//HGL_OS == HGL_OS_iOS -#define HGL_PLUGIN_FRONTNAME "libCMP." //插件文件名前缀 -#define HGL_PLUGIN_EXTNAME OS_TEXT(".dylib") //插件文件扩展名 -#define HGL_PLUGIN_FUNC extern "C" //插件函数定义 +#define HGL_PLUGIN_FRONTNAME "libCMP." //插件文件名前缀 +#define HGL_PLUGIN_EXTNAME OS_TEXT(".dylib") //插件文件扩展名 +#define HGL_PLUGIN_FUNC extern "C" //插件函数定义 -#define HGL_DIRECTORY_SEPARATOR '/' //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("/") //目录分隔符 -#define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("/") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR '/' //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_STR OS_TEXT("/") //目录分隔符 +#define HGL_DIRECTORY_SEPARATOR_U8STR U8_TEXT("/") //目录分隔符 -#define HGL_LINE_END "\n" //换行符 -#define HGL_LINE_END_SIZE 1 //换行符长度 +#define HGL_LINE_END "\n" //换行符 +#define HGL_LINE_END_SIZE 1 //换行符长度 -#define HGL_MEM_ALIGN 16 //内存对齐字节数 +#define HGL_MEM_ALIGN 16 //内存对齐字节数 -#define HGL_MAX_PATH __DARWIN_MAXPATHLEN +#define HGL_MAX_PATH __DARWIN_MAXPATHLEN -#define HGL_FMT_I64 "%lld" -#define HGL_FMT_U64 "%llu" -#define HGL_FMT_DOUBLE "%lf" -#define HGL_FMT_LONG_DOUBLE "%le" +#define HGL_FMT_I64 "%lld" +#define HGL_FMT_U64 "%llu" +#define HGL_FMT_DOUBLE "%lf" +#define HGL_FMT_LONG_DOUBLE "%le" //-------------------------------------------------------------------------------------------------- #include #include -#define hgl_malloc(size) malloc(size) -#define hgl_realloc(ptr,size) realloc(ptr,size) -#define hgl_free free +#define hgl_malloc(size) malloc(size) +#define hgl_realloc(ptr,size) realloc(ptr,size) +#define hgl_free free template inline T *hgl_aligned_malloc(size_t n) @@ -59,20 +59,20 @@ using ExternalModulePointer=void *; #define pi_get dlsym #define pi_close dlclose -#define hgl_stat64 stat -#define hgl_open64 open -#define hgl_lseek64 lseek -#define hgl_tell64(fp) lseek(fp,0,SEEK_CUR) -#define hgl_fstat64 fstat -#define hgl_lstat64 lstat -#define hgl_read64 read -#define hgl_write64 write -#define hgl_pread64 pread -#define hgl_pwrite64 pwrite +#define hgl_stat64 stat +#define hgl_open64 open +#define hgl_lseek64 lseek +#define hgl_tell64(fp) lseek(fp,0,SEEK_CUR) +#define hgl_fstat64 fstat +#define hgl_lstat64 lstat +#define hgl_read64 read +#define hgl_write64 write +#define hgl_pread64 pread +#define hgl_pwrite64 pwrite -#define struct_stat64 struct stat -#define struct_dirent64 struct dirent -#define hgl_dirent64 dirent -#define hgl_readdir64 readdir +#define struct_stat64 struct stat +#define struct_dirent64 struct dirent +#define hgl_dirent64 dirent +#define hgl_readdir64 readdir //-------------------------------------------------------------------------------------------------- #endif//HGL_OS_MACOS_INCLUDE diff --git a/inc/hgl/proc/Fifo.h b/inc/hgl/proc/Fifo.h index c5cb610e..fc455645 100644 --- a/inc/hgl/proc/Fifo.h +++ b/inc/hgl/proc/Fifo.h @@ -4,26 +4,26 @@ #include namespace hgl { - /** - * 命名管道通信 - */ - class Fifo ///命名管道通信 - { - public: + /** + * 命名管道通信 + */ + class Fifo ///命名管道通信 + { + public: - char filename[HGL_MAX_PATH]; + char filename[HGL_MAX_PATH]; - int fd; + int fd; - public: + public: - Fifo() - { - *filename=0; - fd=-1; - } + Fifo() + { + *filename=0; + fd=-1; + } - bool Create(const char *); ///<创建一个管道通信文件(注:只需要文件名,不需要完整路径) - };//namespace hgl + bool Create(const char *); ///<创建一个管道通信文件(注:只需要文件名,不需要完整路径) + };//namespace hgl }//namespace hgl #endif//HGL_MULTI_PROC_FIFO_INCLUDE diff --git a/inc/hgl/proc/FifoInputStream.h b/inc/hgl/proc/FifoInputStream.h index 6ba5319d..59e16acb 100644 --- a/inc/hgl/proc/FifoInputStream.h +++ b/inc/hgl/proc/FifoInputStream.h @@ -6,30 +6,30 @@ namespace hgl { - namespace io - { - /** - * 命名管道输入流 - */ - class FifoInputStream:public InputStream - { - Fifo *f; + namespace io + { + /** + * 命名管道输入流 + */ + class FifoInputStream:public InputStream + { + Fifo *f; - public: + public: - FifoInputStream(Fifo *_f) - { - f=_f; - } + FifoInputStream(Fifo *_f) + { + f=_f; + } - virtual ~FifoInputStream() - { - if(f) - delete f; - } + virtual ~FifoInputStream() + { + if(f) + delete f; + } - - };//class FifoInputStream - }//namespace io + + };//class FifoInputStream + }//namespace io }//namespace hgl #endif//HGL_MULTI_PROC_FIFO_INPUT_STREAM_INCLUDE diff --git a/inc/hgl/proc/Pipe.h b/inc/hgl/proc/Pipe.h index f4f957ee..42f2afd9 100644 --- a/inc/hgl/proc/Pipe.h +++ b/inc/hgl/proc/Pipe.h @@ -11,14 +11,14 @@ namespace hgl { #if HGL_OS==HGL_OS_Windows using pipe_ptr=HANDLE; - constexpr pipe_ptr PIPE_NULL=nullptr; + constexpr pipe_ptr PIPE_NULL=nullptr; #else using pipe_ptr=int; - constexpr pipe_ptr PIPE_NULL=-1; + constexpr pipe_ptr PIPE_NULL=-1; #endif// using pipe_pair=pipe_ptr[2]; - bool CreatePipe(pipe_pair &); ///<创建一对通信管道 + bool CreatePipe(pipe_pair &); ///<创建一对通信管道 }//namespace hgl #endif//HGL_MULTI_PROC_PIPE_INCLUDE diff --git a/inc/hgl/proc/Proc.h b/inc/hgl/proc/Proc.h index 01309ce2..4663d2f2 100644 --- a/inc/hgl/proc/Proc.h +++ b/inc/hgl/proc/Proc.h @@ -4,38 +4,38 @@ #include namespace hgl { - /** - * 进程管理类 - */ - class Process ///进程管理类 - { - OSString work_path; - OSString filename; - StringList args; + /** + * 进程管理类 + */ + class Process ///进程管理类 + { + OSString work_path; + OSString filename; + StringList args; - int pid; + int pid; - public: + public: - Process() - { - pid=-1; - } + Process() + { + pid=-1; + } - virtual ~Process() - { - } + virtual ~Process() + { + } - bool SetWorkPath(const OSString &wp); ///<设置工作目录 - bool SetExecFile(const OSString &ef); ///<设置执行文件 - void AddArgv(const OSString &argv){if(!argv.IsEmpty())args.Add(argv);} ///<增加一个参数 - void ClearArgs(){args.Clear();} ///<清除所有参数 + bool SetWorkPath(const OSString &wp); ///<设置工作目录 + bool SetExecFile(const OSString &ef); ///<设置执行文件 + void AddArgv(const OSString &argv){if(!argv.IsEmpty())args.Add(argv);} ///<增加一个参数 + void ClearArgs(){args.Clear();} ///<清除所有参数 - bool Execute(); ///<执行程序 + bool Execute(); ///<执行程序 - bool Wait(); ///<等待子进程暂停或是终止 - bool Kill(); ///<杀掉子进程 - bool RequestTerminate(); ///<请求子进程终止 - };//class Process + bool Wait(); ///<等待子进程暂停或是终止 + bool Kill(); ///<杀掉子进程 + bool RequestTerminate(); ///<请求子进程终止 + };//class Process }//namespace hgl #endif//HGL_PROCESS_INCLUDE diff --git a/inc/hgl/proc/ProcMutex.h b/inc/hgl/proc/ProcMutex.h index d2053691..e24bcbb3 100644 --- a/inc/hgl/proc/ProcMutex.h +++ b/inc/hgl/proc/ProcMutex.h @@ -9,29 +9,29 @@ namespace hgl { - /** - * 进程排斥 - */ - class ProcMutex ///进程排斥 - { + /** + * 进程排斥 + */ + class ProcMutex ///进程排斥 + { #if HGL_OS==HGL_OS_Windows - void *lock; + void *lock; #else sem_t *lock; #endif//HGL_OS==HGL_OS_Windows - public: + public: - ProcMutex(); - ~ProcMutex(){Clear();} + ProcMutex(); + ~ProcMutex(){Clear();} - bool Create(const os_char *); ///<创建进程排斥 - void Clear(); ///<清除进程排斥 + bool Create(const os_char *); ///<创建进程排斥 + void Clear(); ///<清除进程排斥 - bool Lock(); ///<锁定 - bool TryLock(); ///<尝试锁定 + bool Lock(); ///<锁定 + bool TryLock(); ///<尝试锁定 - bool Unlock(); ///<解锁 - };//class ProcMutex + bool Unlock(); ///<解锁 + };//class ProcMutex }//namespace hgl #endif//HGL_PROCESS_MUTEX_INCLUDE diff --git a/inc/hgl/thread/Atomic.h b/inc/hgl/thread/Atomic.h index 2fe5a37c..31629b4d 100644 --- a/inc/hgl/thread/Atomic.h +++ b/inc/hgl/thread/Atomic.h @@ -24,38 +24,38 @@ namespace hgl { - #if (HGL_OS == HGL_OS_Windows)//&&(!defined(HGL_ATOMIC_CPP11)) - typedef atom_win32 atom_int; - typedef atom_win32 atom_uint; - typedef atom_win32 atom_int32; - typedef atom_win32 atom_uint32; - typedef atom_win64 atom_int64; - typedef atom_win64 atom_uint64; - typedef atom_win32 atom_float; -// typedef atom_win64 atom_double; + #if (HGL_OS == HGL_OS_Windows)//&&(!defined(HGL_ATOMIC_CPP11)) + typedef atom_win32 atom_int; + typedef atom_win32 atom_uint; + typedef atom_win32 atom_int32; + typedef atom_win32 atom_uint32; + typedef atom_win64 atom_int64; + typedef atom_win64 atom_uint64; + typedef atom_win32 atom_float; +// typedef atom_win64 atom_double; - typedef atom_win32 atom_bool; - typedef atom_win32 atom_char; - typedef atom_win32 atom_uchar; -// typedef atom_win32 atom_wchar; - typedef atom_win32 atom_char16; -// typedef atom_win32 atom_char32; - #else - typedef atom atom_bool; - typedef atom atom_int; - typedef atom atom_uint; - typedef atom atom_int32; - typedef atom atom_uint32; - typedef atom atom_int64; - typedef atom atom_uint64; - typedef atom atom_float; -// typedef atom atom_double; + typedef atom_win32 atom_bool; + typedef atom_win32 atom_char; + typedef atom_win32 atom_uchar; +// typedef atom_win32 atom_wchar; + typedef atom_win32 atom_char16; +// typedef atom_win32 atom_char32; + #else + typedef atom atom_bool; + typedef atom atom_int; + typedef atom atom_uint; + typedef atom atom_int32; + typedef atom atom_uint32; + typedef atom atom_int64; + typedef atom atom_uint64; + typedef atom atom_float; +// typedef atom atom_double; - typedef atom atom_char; - typedef atom atom_uchar; -// typedef atom atom_wchar; - typedef atom atom_char16; -// typedef atom atom_char32; - #endif//windows & !c++11 + typedef atom atom_char; + typedef atom atom_uchar; +// typedef atom atom_wchar; + typedef atom atom_char16; +// typedef atom atom_char32; + #endif//windows & !c++11 }//namespace hgl #endif//HGL_ATOMIC_INCLUDE diff --git a/inc/hgl/thread/CondVar.h b/inc/hgl/thread/CondVar.h index 1c94a0aa..b88c2314 100644 --- a/inc/hgl/thread/CondVar.h +++ b/inc/hgl/thread/CondVar.h @@ -4,29 +4,29 @@ #include namespace hgl { - class ThreadMutex; - class RWLock; + class ThreadMutex; + class RWLock; - /** - * 条件变量 - */ - class CondVar - { - void *cond_var; + /** + * 条件变量 + */ + class CondVar + { + void *cond_var; - public: + public: - CondVar(); - virtual ~CondVar(); + CondVar(); + virtual ~CondVar(); - bool Wait(ThreadMutex *,double time=0); ///<释放指定ThreadMutex,在条件符合后重新锁定并结束等待,0表示永久 + bool Wait(ThreadMutex *,double time=0); ///<释放指定ThreadMutex,在条件符合后重新锁定并结束等待,0表示永久 #if HGL_OS == HGL_OS_Windows - bool Wait(RWLock *,double time=0,bool read=false); ///<释放指定RWLock,在条件符合后重新锁定并结束等待。(注:此函数仅在Windows 2008/Vista及更新版本中支持,Linux/BSD版不支持) + bool Wait(RWLock *,double time=0,bool read=false); ///<释放指定RWLock,在条件符合后重新锁定并结束等待。(注:此函数仅在Windows 2008/Vista及更新版本中支持,Linux/BSD版不支持) #endif//HGL_OS == HGL_OS_Windows - void Signal(); ///<发送一个信号,让一个等待的线程解锁 - void Broadcast(); ///<广播一个信号,让所有等待的线程都解锁 - };//class CondVar + void Signal(); ///<发送一个信号,让一个等待的线程解锁 + void Broadcast(); ///<广播一个信号,让所有等待的线程都解锁 + };//class CondVar }//namespace hgl #endif//HGL_MULTI_THREAD_CONDITION_VARIABLE_INCLUDE diff --git a/inc/hgl/thread/DataPost.h b/inc/hgl/thread/DataPost.h index 9e1d4ea1..c770ae4f 100644 --- a/inc/hgl/thread/DataPost.h +++ b/inc/hgl/thread/DataPost.h @@ -7,92 +7,92 @@ namespace hgl { - /** - * 多线程数据投递模板
- * 需要注意每个任务都会重新由空闲线程来进行获取,所以请将连续的任务合并为一个任务,而不是一次大量投递。 - */ - template class DataPost - { - protected: + /** + * 多线程数据投递模板
+ * 需要注意每个任务都会重新由空闲线程来进行获取,所以请将连续的任务合并为一个任务,而不是一次大量投递。 + */ + template class DataPost + { + protected: - List data_list[2]; + List data_list[2]; - int post_index,recv_index; - int recv_offset; + int post_index,recv_index; + int recv_offset; - ThreadMutex post_lock,recv_lock; + ThreadMutex post_lock,recv_lock; - protected: + protected: - void _Swap() - { + void _Swap() + { if(recv_index){recv_index=0;post_index=1;} - else{recv_index=1;post_index=0;} + else{recv_index=1;post_index=0;} - recv_offset=0; - } - - public: - - DataPost() - { - post_index=0; - recv_index=1; - - recv_offset=0; - } - - virtual ~DataPost()=default; - - /** - * 投递一个数据 - */ - void Post(T *obj) - { - if(!obj)return; - - post_lock.Lock(); - data_list[post_index].Add(obj); - post_lock.Unlock(); - } - - /** - * 投递一批数据 - */ - void Post(T **obj,int count) - { - if(!obj)return; - - post_lock.Lock(); - data_list[post_index].Add(obj,count); - post_lock.Unlock(); - } + recv_offset=0; + } public: - /** - * 获取一个数据 - */ - T *Receive() - { - T *obj=nullptr; + DataPost() + { + post_index=0; + recv_index=1; - recv_lock.Lock(); - int count=data_list[recv_index].GetCount(); + recv_offset=0; + } - if(recv_offset0) //如果换出来的区有数据 @@ -101,56 +101,56 @@ namespace hgl ++recv_offset; } - } - recv_lock.Unlock(); + } + recv_lock.Unlock(); - return obj; - } - };//template class DataPost + return obj; + } + };//template class DataPost - /** - * 信号自动交换数据访问模板 - */ - template class SemDataPost:public DataPost - { - Semaphore sem; + /** + * 信号自动交换数据访问模板 + */ + template class SemDataPost:public DataPost + { + Semaphore sem; - public: + public: - using DataPost::DataPost; - ~SemDataPost()=default; + using DataPost::DataPost; + ~SemDataPost()=default; - /** - * 释放接收信号 - * @param count 信号个数 - */ - void PostSem(int count=1) - { - sem.Post(count); - } + /** + * 释放接收信号 + * @param count 信号个数 + */ + void PostSem(int count=1) + { + sem.Post(count); + } - /** - * 等待获取一个信号并获取数据 - * @param time_out 等待时长 - */ - T *WaitSemReceive(const double time_out=5) - { - if(!sem.Acquire(time_out)) - return(nullptr); + /** + * 等待获取一个信号并获取数据 + * @param time_out 等待时长 + */ + T *WaitSemReceive(const double time_out=5) + { + if(!sem.Acquire(time_out)) + return(nullptr); - return this->Receive(); - } + return this->Receive(); + } - /** - * 尝试获取一个信号并获取数据 - */ - T *TrySemReceive() - { - if(!sem.TryAcquire()) - return(nullptr); + /** + * 尝试获取一个信号并获取数据 + */ + T *TrySemReceive() + { + if(!sem.TryAcquire()) + return(nullptr); - return this->Receive(); - } - };//template class SemDataPost:public DataPost + return this->Receive(); + } + };//template class SemDataPost:public DataPost }//namespace hgl #endif//HGL_THREAD_DATA_POST_INCLUDE diff --git a/inc/hgl/thread/Loader.h b/inc/hgl/thread/Loader.h index 3e8a61cc..0032713a 100644 --- a/inc/hgl/thread/Loader.h +++ b/inc/hgl/thread/Loader.h @@ -7,68 +7,68 @@ #include namespace hgl { - class HAC; + class HAC; - /** - * 加载器,用于异步加载数据 - */ - class Loader:public EnumObject,public Thread ///加载器 - { - public: + /** + * 加载器,用于异步加载数据 + */ + class Loader:public EnumObject,public Thread ///加载器 + { + public: - /** - * 加载器状态枚举 - */ - enum LoaderStatus //加载器状态 - { - lsNone=0, //未使用 + /** + * 加载器状态枚举 + */ + enum LoaderStatus //加载器状态 + { + lsNone=0, //未使用 - lsLoad, //正在加载 - lsProc, //正在处理 - lsProcFinish, //处理完成 - lsClear, //清理 - lsClearFinish, //清理完成 + lsLoad, //正在加载 + lsProc, //正在处理 + lsProcFinish, //处理完成 + lsClear, //清理 + lsClearFinish, //清理完成 - lsEnd, //完成 - }; + lsEnd, //完成 + }; - protected: + protected: - LoaderStatus status; + LoaderStatus status; - void SetStatus(LoaderStatus); + void SetStatus(LoaderStatus); - HAC *loader_hac; - UTF16String loader_filename; - void *loader_pointer; - void *loader_data; - int loader_size; + HAC *loader_hac; + UTF16String loader_filename; + void *loader_pointer; + void *loader_data; + int loader_size; - protected: //被动事件 + protected: //被动事件 - virtual void ProcFinish()=default; ///<加载完成(异步事件) - virtual void ProcEnd()=default; ///<结束处理 - virtual void ProcClear()=default; ///<清理处理(异步事件) + virtual void ProcFinish()=default; ///<加载完成(异步事件) + virtual void ProcEnd()=default; ///<结束处理 + virtual void ProcClear()=default; ///<清理处理(异步事件) - public: //事件 + public: //事件 - DefEvent(void,OnError,(Loader *)); ///<出错事件 - DefEvent(void,OnEnd,(Loader *)); ///<完成事件,在ProcEnd之后,ProcClear之前被调用 + DefEvent(void,OnError,(Loader *)); ///<出错事件 + DefEvent(void,OnEnd,(Loader *)); ///<完成事件,在ProcEnd之后,ProcClear之前被调用 - public: + public: - Loader(); - virtual ~Loader()=default; + Loader(); + virtual ~Loader()=default; - bool Execute(); + bool Execute(); - void Update(); + void Update(); - public: //方法 + public: //方法 - bool Load(HAC *,const u16char *); ///<加载一个文件到某个对象 + bool Load(HAC *,const u16char *); ///<加载一个文件到某个对象 - LoaderStatus GetStatus(); ///<取得状态 - };//class Loader + LoaderStatus GetStatus(); ///<取得状态 + };//class Loader }//namespace hgl #endif//HGL_LOADER_INCLUDE diff --git a/inc/hgl/thread/RWLock.h b/inc/hgl/thread/RWLock.h index 953ed418..d91bd2d6 100644 --- a/inc/hgl/thread/RWLock.h +++ b/inc/hgl/thread/RWLock.h @@ -4,184 +4,184 @@ #include namespace hgl { - /** - * 读写锁/共享锁 - */ - class RWLock ///读写锁/共享锁 - { - void *lock; + /** + * 读写锁/共享锁 + */ + class RWLock ///读写锁/共享锁 + { + void *lock; - public: + public: - RWLock(); - virtual ~RWLock(); + RWLock(); + virtual ~RWLock(); - void *GetRWLock(){ return lock; } ///<返回操作系级锁 + void *GetRWLock(){ return lock; } ///<返回操作系级锁 - bool TryReadLock(); ///<尝试读(共享访问)锁定 - bool ReadLock(); ///<读(共享访问)锁定 - bool ReadUnlock(); ///<读(共享访问)解锁 + bool TryReadLock(); ///<尝试读(共享访问)锁定 + bool ReadLock(); ///<读(共享访问)锁定 + bool ReadUnlock(); ///<读(共享访问)解锁 - bool TryWriteLock(); ///<尝试写(独占访问)锁定 - bool WriteLock(); ///<写(独占访问)锁定 - bool WriteUnlock(); ///<写(独占访问)解锁 + bool TryWriteLock(); ///<尝试写(独占访问)锁定 + bool WriteLock(); ///<写(独占访问)锁定 + bool WriteUnlock(); ///<写(独占访问)解锁 #if (HGL_OS != HGL_OS_Windows)&&(HGL_OS != HGL_OS_macOS) - bool WaitReadLock(double); ///<等待读(共享访问)锁定 - bool WaitWriteLock(double); ///<等待写(独占访问)锁定 + bool WaitReadLock(double); ///<等待读(共享访问)锁定 + bool WaitWriteLock(double); ///<等待写(独占访问)锁定 #endif//(HGL_OS != HGL_OS_Windows)&&(HGL_OS != HGL_OS_macOS) - };//class RWLock + };//class RWLock - #define HGL_RWLOCK(lock) bool ReadLock() {return lock.ReadLock(); } \ - bool WriteLock() {return lock.WriteLock(); } \ - bool TryReadLock() {return lock.TryReadLock(); } \ - bool TryWriteLock() {return lock.TryWriteLock();} \ - bool ReadUnlock() {return lock.ReadUnlock(); } \ - bool WriteUnlock() {return lock.WriteUnlock(); } + #define HGL_RWLOCK(lock) bool ReadLock() {return lock.ReadLock(); } \ + bool WriteLock() {return lock.WriteLock(); } \ + bool TryReadLock() {return lock.TryReadLock(); } \ + bool TryWriteLock() {return lock.TryWriteLock();} \ + bool ReadUnlock() {return lock.ReadUnlock(); } \ + bool WriteUnlock() {return lock.WriteUnlock(); } - #define HGL_RWLOCK_PTR(lock) bool ReadLock() {return lock->ReadLock(); } \ - bool WriteLock() {return lock->WriteLock(); } \ - bool TryReadLock() {return lock->TryReadLock(); } \ - bool TryWriteLock() {return lock->TryWriteLock(); } \ - bool ReadUnlock() {return lock->ReadUnlock(); } \ - bool WriteUnlock() {return lock->WriteUnlock(); } + #define HGL_RWLOCK_PTR(lock) bool ReadLock() {return lock->ReadLock(); } \ + bool WriteLock() {return lock->WriteLock(); } \ + bool TryReadLock() {return lock->TryReadLock(); } \ + bool TryWriteLock() {return lock->TryWriteLock(); } \ + bool ReadUnlock() {return lock->ReadUnlock(); } \ + bool WriteUnlock() {return lock->WriteUnlock(); } - #define HGL_NULL_RWLOCK bool ReadLock() {return true;} \ - bool WriteLock() {return true;} \ - bool TryReadLock() {return true;} \ - bool TryWriteLock() {return true;} \ - bool ReadUnlock() {return true;} \ - bool WriteUnlock() {return true;} \ + #define HGL_NULL_RWLOCK bool ReadLock() {return true;} \ + bool WriteLock() {return true;} \ + bool TryReadLock() {return true;} \ + bool TryWriteLock() {return true;} \ + bool ReadUnlock() {return true;} \ + bool WriteUnlock() {return true;} \ - #define HGL_VIRTUAL_NULL_RWLOCK virtual bool ReadLock() {return true;} \ - virtual bool WriteLock() {return true;} \ - virtual bool TryReadLock() {return true;} \ - virtual bool TryWriteLock() {return true;} \ - virtual bool ReadUnlock() {return true;} \ - virtual bool WriteUnlock() {return true;} \ + #define HGL_VIRTUAL_NULL_RWLOCK virtual bool ReadLock() {return true;} \ + virtual bool WriteLock() {return true;} \ + virtual bool TryReadLock() {return true;} \ + virtual bool TryWriteLock() {return true;} \ + virtual bool ReadUnlock() {return true;} \ + virtual bool WriteUnlock() {return true;} \ - /** - * 读写锁/共享锁对象 - */ - template class RWLockObject:public RWLock - { - T *data; + /** + * 读写锁/共享锁对象 + */ + template class RWLockObject:public RWLock + { + T *data; - public: + public: - RWLockObject() - { - data=new T; - } + RWLockObject() + { + data=new T; + } - RWLockObject(T *t) - { - data=t; - } + RWLockObject(T *t) + { + data=t; + } - virtual ~RWLockObject() - { - SAFE_CLEAR(data); - } + virtual ~RWLockObject() + { + SAFE_CLEAR(data); + } - void operator = (T *t) - { - if(data) - delete data; + void operator = (T *t) + { + if(data) + delete data; - data=t; - } + data=t; + } - T *operator ->() - { - return data; - } + T *operator ->() + { + return data; + } - bool valid()const - { - return data; - } + bool valid()const + { + return data; + } - bool operator !() - { - return !data; - } - };//class RWLockObject + bool operator !() + { + return !data; + } + };//class RWLockObject - /** - * 读写锁共享锁定自动释放类 - */ - class OnlyReadLock - { - RWLock *rw; + /** + * 读写锁共享锁定自动释放类 + */ + class OnlyReadLock + { + RWLock *rw; - public: + public: - OnlyReadLock(RWLock &rw_lock) - { - rw=&rw_lock; + OnlyReadLock(RWLock &rw_lock) + { + rw=&rw_lock; - rw->ReadLock(); - } + rw->ReadLock(); + } - OnlyReadLock(RWLock *rw_lock) - { - rw=rw_lock; + OnlyReadLock(RWLock *rw_lock) + { + rw=rw_lock; - rw->ReadLock(); - } + rw->ReadLock(); + } - ~OnlyReadLock() - { - Unlock(); - } + ~OnlyReadLock() + { + Unlock(); + } - void Unlock() - { - if(rw) - { - rw->ReadUnlock(); - rw=nullptr; - } - } - };//class ReadLock + void Unlock() + { + if(rw) + { + rw->ReadUnlock(); + rw=nullptr; + } + } + };//class ReadLock - /** - * 读写锁独占访问锁定自动释放类 - */ - class OnlyWriteLock - { - RWLock *rw; + /** + * 读写锁独占访问锁定自动释放类 + */ + class OnlyWriteLock + { + RWLock *rw; - public: + public: - OnlyWriteLock(RWLock &rw_lock) - { - rw=&rw_lock; + OnlyWriteLock(RWLock &rw_lock) + { + rw=&rw_lock; - rw->WriteLock(); - } + rw->WriteLock(); + } - OnlyWriteLock(RWLock *rw_lock) - { - rw=rw_lock; + OnlyWriteLock(RWLock *rw_lock) + { + rw=rw_lock; - rw->WriteLock(); - } + rw->WriteLock(); + } - ~OnlyWriteLock() - { - Unlock(); - } + ~OnlyWriteLock() + { + Unlock(); + } - void Unlock() - { - if(rw) - { - rw->WriteUnlock(); - rw=nullptr; - } - } - };//class WriteLock + void Unlock() + { + if(rw) + { + rw->WriteUnlock(); + rw=nullptr; + } + } + };//class WriteLock }//namespace hgl #endif//HGL_RWLOCK_INCLUDE diff --git a/inc/hgl/thread/RingBuffer.h b/inc/hgl/thread/RingBuffer.h index 1b9720a2..a2c1126f 100644 --- a/inc/hgl/thread/RingBuffer.h +++ b/inc/hgl/thread/RingBuffer.h @@ -6,80 +6,80 @@ #include namespace hgl { - /** - * 多线程环形数据流,用于随时被读或写的情况以及在多线程应用中同时被读与写的情况。 - */ - template class RingBuffer:protected ThreadMutex ///多线程环形数据流 - { - protected: + /** + * 多线程环形数据流,用于随时被读或写的情况以及在多线程应用中同时被读与写的情况。 + */ + template class RingBuffer:protected ThreadMutex ///多线程环形数据流 + { + protected: - T *buffer; - int buffer_size; + T *buffer; + int buffer_size; - int read_pos,write_pos; + int read_pos,write_pos; - int read_cur,write_cur; - int read_off,write_off; - int read_max,write_max; - int read_count,write_count; + int read_cur,write_cur; + int read_off,write_off; + int read_max,write_max; + int read_count,write_count; - bool read_peek; + bool read_peek; - protected: + protected: - void ClampPosition(); + void ClampPosition(); - int _GetReadSize()const{return write_pos-read_pos;} - int _GetWriteSize()const{return buffer_size-(write_pos-read_pos);} + int _GetReadSize()const{return write_pos-read_pos;} + int _GetWriteSize()const{return buffer_size-(write_pos-read_pos);} - int _SafeWriteStart(); - int _SafeReadStart(bool); + int _SafeWriteStart(); + int _SafeReadStart(bool); - int _Write(const T *,int); - int _Read(T *,int,bool); + int _Write(const T *,int); + int _Read(T *,int,bool); - public: + public: - RingBuffer(int); ///<本类构造函数 - virtual ~RingBuffer(); ///<本类析构函数 + RingBuffer(int); ///<本类构造函数 + virtual ~RingBuffer(); ///<本类析构函数 - const int GetBufferSize()const{return buffer_size;} ///<除取缓冲区长度 + const int GetBufferSize()const{return buffer_size;} ///<除取缓冲区长度 - void Clear(); ///<清除整个缓冲区 - void SafeClear(); ///<安全清除整个缓冲区 + void Clear(); ///<清除整个缓冲区 + void SafeClear(); ///<安全清除整个缓冲区 - public: //写处理函数 + public: //写处理函数 - int WriteStart(); ///<开始写入 - int GetWriteSize()const{return write_max;} ///<取得可写入数据长度 - int Write(const T *,int); ///<向流中写入数据 - int WriteEnd(); ///<结束写入 + int WriteStart(); ///<开始写入 + int GetWriteSize()const{return write_max;} ///<取得可写入数据长度 + int Write(const T *,int); ///<向流中写入数据 + int WriteEnd(); ///<结束写入 - int SafeTryWriteStart(); ///<安全尝试开始写入 - int SafeWriteStart(); ///<安全开始写入 - int SafeWriteEnd(); ///<安全结束写入 + int SafeTryWriteStart(); ///<安全尝试开始写入 + int SafeWriteStart(); ///<安全开始写入 + int SafeWriteEnd(); ///<安全结束写入 - int SafeGetWriteSize(); ///<安全取得可写入数据长度 - int SafeWrite(const T *,int); ///<安全写入,用于简单的一次性写入 + int SafeGetWriteSize(); ///<安全取得可写入数据长度 + int SafeWrite(const T *,int); ///<安全写入,用于简单的一次性写入 - public: //读处理函数 + public: //读处理函数 - int ReadStart(bool=true); ///<开始读取 - int GetReadSize()const{return read_max;} ///<取得可读取数据长度 - int Read(T *,int,bool=true); ///<从流中读取数据 - int Peek(T *ptr,int size){return Read(ptr,size,false);} ///<从流中预读数据 - int Skip(int size){return Read(0,size,true);} ///<跳过流中的指定长度数据 - int ReadEnd(); ///<结束写入 + int ReadStart(bool=true); ///<开始读取 + int GetReadSize()const{return read_max;} ///<取得可读取数据长度 + int Read(T *,int,bool=true); ///<从流中读取数据 + int Peek(T *ptr,int size){return Read(ptr,size,false);} ///<从流中预读数据 + int Skip(int size){return Read(0,size,true);} ///<跳过流中的指定长度数据 + int ReadEnd(); ///<结束写入 - int SafeTryReadStart(bool=true); ///<安全尝试开始读取 - int SafeReadStart(bool=true); ///<安全开始读取 - int SafeReadEnd(); ///<安全结束读取 + int SafeTryReadStart(bool=true); ///<安全尝试开始读取 + int SafeReadStart(bool=true); ///<安全开始读取 + int SafeReadEnd(); ///<安全结束读取 - int SafeGetReadSize(); ///<安全取得可读取数据长度 - bool SafeTryGetReadSize(int &); ///<安全尝试取得可读取数据长度 - int SafeRead(T *,int,bool=true); ///<安全读取,用于简单的一次性读取 - int SafePeek(T *ptr,int size){return SafeRead(ptr,size,false);} ///<安全预读数据 - };//class RingBuffer + int SafeGetReadSize(); ///<安全取得可读取数据长度 + bool SafeTryGetReadSize(int &); ///<安全尝试取得可读取数据长度 + int SafeRead(T *,int,bool=true); ///<安全读取,用于简单的一次性读取 + int SafePeek(T *ptr,int size){return SafeRead(ptr,size,false);} ///<安全预读数据 + };//class RingBuffer }//namespace hgl #include @@ -88,258 +88,258 @@ namespace hgl namespace hgl { - namespace io - { - class RingInputStream:public InputStream - { - RingBuffer *rb; + namespace io + { + class RingInputStream:public InputStream + { + RingBuffer *rb; - public: + public: - RingInputStream (RingBuffer *_rb){rb=_rb;} + RingInputStream (RingBuffer *_rb){rb=_rb;} - RingBuffer *GetRingBuffer(){return rb;} + RingBuffer *GetRingBuffer(){return rb;} - int Start (){return rb?rb->ReadStart():-1;} - int End (){return rb?rb->ReadEnd():-1;} - int SafeStart (){return rb?rb->SafeReadStart():-1;} - int SafeEnd (){return rb?rb->SafeReadEnd():-1;} + int Start (){return rb?rb->ReadStart():-1;} + int End (){return rb?rb->ReadEnd():-1;} + int SafeStart (){return rb?rb->SafeReadStart():-1;} + int SafeEnd (){return rb?rb->SafeReadEnd():-1;} - void Close (){} + void Close (){} - public: + public: - int64 Read (void *buf,int64 size){return rb?rb->Read((char *)buf,size):-1;} ///<读取数据 - int64 Peek (void *buf,int64 size){return rb?rb->Peek((char *)buf,size):-1;} ///<预览数据 + int64 Read (void *buf,int64 size){return rb?rb->Read((char *)buf,size):-1;} ///<读取数据 + int64 Peek (void *buf,int64 size){return rb?rb->Peek((char *)buf,size):-1;} ///<预览数据 - bool CanRestart ()const{return false;} ///<是否可以复位 - bool CanSeek ()const{return false;} ///<是否可以定位 - bool CanSize ()const{return false;} ///<是否可以取得尺寸 - bool CanPeek ()const{return false;} ///<是否可以预览数据 + bool CanRestart ()const{return false;} ///<是否可以复位 + bool CanSeek ()const{return false;} ///<是否可以定位 + bool CanSize ()const{return false;} ///<是否可以取得尺寸 + bool CanPeek ()const{return false;} ///<是否可以预览数据 - bool Restart (){return false;} ///<复位访问指针 - int64 Skip (int64 size){return rb?rb->Skip(size):-1;} ///<跳过指定字节不访问 - int64 Seek (int64,SeekOrigin){return -1;} ///<移动访问指针 - int64 Tell ()const{return -1;} ///<返回当前访问位置 - int64 GetSize ()const{return -1;} ///<取得流长度 - int64 Available ()const{return rb?rb->GetReadSize():-1;} ///<剩下的可以不受阻塞访问的字节数 - };//class RingInputStream + bool Restart (){return false;} ///<复位访问指针 + int64 Skip (int64 size){return rb?rb->Skip(size):-1;} ///<跳过指定字节不访问 + int64 Seek (int64,SeekOrigin){return -1;} ///<移动访问指针 + int64 Tell ()const{return -1;} ///<返回当前访问位置 + int64 GetSize ()const{return -1;} ///<取得流长度 + int64 Available ()const{return rb?rb->GetReadSize():-1;} ///<剩下的可以不受阻塞访问的字节数 + };//class RingInputStream - class RingOutputStream:public OutputStream - { - RingBuffer *rb; + class RingOutputStream:public OutputStream + { + RingBuffer *rb; - public: + public: - RingOutputStream (RingBuffer *_rb){rb=_rb;} + RingOutputStream (RingBuffer *_rb){rb=_rb;} - RingBuffer *GetRingBuffer(){return rb;} + RingBuffer *GetRingBuffer(){return rb;} - int Start (){return rb?rb->WriteStart():-1;} - int End (){return rb?rb->WriteEnd():-1;} - int SafeStart (){return rb?rb->SafeWriteStart():-1;} - int SafeEnd (){return rb?rb->SafeWriteEnd():-1;} + int Start (){return rb?rb->WriteStart():-1;} + int End (){return rb?rb->WriteEnd():-1;} + int SafeStart (){return rb?rb->SafeWriteStart():-1;} + int SafeEnd (){return rb?rb->SafeWriteEnd():-1;} - void Close (){} + void Close (){} - public: + public: - int64 Write (const void *buf,int64 size){return rb?rb->Write((char *)buf,size):-1;} ///<写入数据 + int64 Write (const void *buf,int64 size){return rb?rb->Write((char *)buf,size):-1;} ///<写入数据 - bool CanRestart ()const{return false;}; ///<是否可以复位 - bool CanSeek ()const{return false;}; ///<是否可以定位 - bool CanSize ()const{return false;} ///<是否可以取得尺寸 + bool CanRestart ()const{return false;}; ///<是否可以复位 + bool CanSeek ()const{return false;}; ///<是否可以定位 + bool CanSize ()const{return false;} ///<是否可以取得尺寸 - bool Restart (){return false;} ///<复位访问指针 - int64 Seek (int64,SeekOrigin){return -1;} ///<移动访问指针 - int64 Tell ()const{return -1;} ///<返回当前访问位置 - int64 GetSize ()const{return -1;} ///<取得流长度 - int64 Available ()const{return rb?rb->GetWriteSize():-1;} ///<剩下的可以不受阻塞写入的字节数 - };//class RingOutputStream - }//namespace io + bool Restart (){return false;} ///<复位访问指针 + int64 Seek (int64,SeekOrigin){return -1;} ///<移动访问指针 + int64 Tell ()const{return -1;} ///<返回当前访问位置 + int64 GetSize ()const{return -1;} ///<取得流长度 + int64 Available ()const{return rb?rb->GetWriteSize():-1;} ///<剩下的可以不受阻塞写入的字节数 + };//class RingOutputStream + }//namespace io - class RingBufferRead - { - RingBuffer *rb; - int size; + class RingBufferRead + { + RingBuffer *rb; + int size; - public: + public: - RingBufferRead(RingBuffer *_rb) - { - rb=_rb; + RingBufferRead(RingBuffer *_rb) + { + rb=_rb; - size=rb->ReadStart(); - } + size=rb->ReadStart(); + } - RingBufferRead(io::RingInputStream *ris) - { - rb=ris->GetRingBuffer(); + RingBufferRead(io::RingInputStream *ris) + { + rb=ris->GetRingBuffer(); - size=rb->ReadStart(); - } + size=rb->ReadStart(); + } - ~RingBufferRead() - { - Unlock(); - } + ~RingBufferRead() + { + Unlock(); + } - void Unlock() - { - if(size>0) - { - rb->ReadEnd(); - size=0; - } - } + void Unlock() + { + if(size>0) + { + rb->ReadEnd(); + size=0; + } + } - int GetSize() const - { - return size; - } - };//class RingBufferRead + int GetSize() const + { + return size; + } + };//class RingBufferRead - class RingBufferSafeRead - { - RingBuffer *rb; - int size; + class RingBufferSafeRead + { + RingBuffer *rb; + int size; - public: + public: - RingBufferSafeRead(RingBuffer *_rb) - { - rb=_rb; - size=-1; - } + RingBufferSafeRead(RingBuffer *_rb) + { + rb=_rb; + size=-1; + } - RingBufferSafeRead(io::RingInputStream *ris) - { - rb=ris->GetRingBuffer(); - size=-1; - } + RingBufferSafeRead(io::RingInputStream *ris) + { + rb=ris->GetRingBuffer(); + size=-1; + } - int Start() - { - size=rb->SafeReadStart(); - return size; - } + int Start() + { + size=rb->SafeReadStart(); + return size; + } - int TryStart() - { - size=rb->SafeTryReadStart(); - return size; - } + int TryStart() + { + size=rb->SafeTryReadStart(); + return size; + } - int GetSize() - { - size=rb->GetReadSize(); - return size; - } + int GetSize() + { + size=rb->GetReadSize(); + return size; + } - ~RingBufferSafeRead() - { - Unlock(); - } + ~RingBufferSafeRead() + { + Unlock(); + } - void Unlock() - { - if(size>0) - { - rb->SafeReadEnd(); - size=0; - } - } - };//class RingBufferSafeRead + void Unlock() + { + if(size>0) + { + rb->SafeReadEnd(); + size=0; + } + } + };//class RingBufferSafeRead - class RingBufferWrite - { - RingBuffer *rb; - int size; + class RingBufferWrite + { + RingBuffer *rb; + int size; - public: + public: - RingBufferWrite(RingBuffer *_rb) - { - rb=_rb; + RingBufferWrite(RingBuffer *_rb) + { + rb=_rb; - size=rb->WriteStart(); - } + size=rb->WriteStart(); + } - RingBufferWrite(io::RingOutputStream *ros) - { - rb=ros->GetRingBuffer(); + RingBufferWrite(io::RingOutputStream *ros) + { + rb=ros->GetRingBuffer(); - size=rb->WriteStart(); - } + size=rb->WriteStart(); + } - ~RingBufferWrite() - { - Unlock(); - } + ~RingBufferWrite() + { + Unlock(); + } - void Unlock() - { - if(size>0) - { - rb->WriteEnd(); - size=0; - } - } + void Unlock() + { + if(size>0) + { + rb->WriteEnd(); + size=0; + } + } - int GetSize() const - { - return size; - } - };//class RingBufferWrite + int GetSize() const + { + return size; + } + };//class RingBufferWrite - class RingBufferSafeWrite - { - RingBuffer *rb; - int size; + class RingBufferSafeWrite + { + RingBuffer *rb; + int size; - public: + public: - RingBufferSafeWrite(RingBuffer *_rb) - { - rb=_rb; + RingBufferSafeWrite(RingBuffer *_rb) + { + rb=_rb; - size=-1; - } + size=-1; + } - RingBufferSafeWrite(io::RingOutputStream *ros) - { - rb=ros->GetRingBuffer(); - } + RingBufferSafeWrite(io::RingOutputStream *ros) + { + rb=ros->GetRingBuffer(); + } - int Start() - { - size=rb->SafeWriteStart(); - return(size); - } + int Start() + { + size=rb->SafeWriteStart(); + return(size); + } - int TryStart() - { - size=rb->SafeTryWriteStart(); - return(size); - } + int TryStart() + { + size=rb->SafeTryWriteStart(); + return(size); + } - ~RingBufferSafeWrite() - { - Unlock(); - } + ~RingBufferSafeWrite() + { + Unlock(); + } - int Unlock() - { - int result=size; + int Unlock() + { + int result=size; - if(size>0) - { - rb->SafeWriteEnd(); - size=0; - } + if(size>0) + { + rb->SafeWriteEnd(); + size=0; + } - return result; - } - };//class RingBufferSafeWrite + return result; + } + };//class RingBufferSafeWrite }//namespace hgl #endif//HGL_THREAD_RING_BUFFER_INCLUDE diff --git a/inc/hgl/thread/SemLock.h b/inc/hgl/thread/SemLock.h index c5e96faf..4465f661 100644 --- a/inc/hgl/thread/SemLock.h +++ b/inc/hgl/thread/SemLock.h @@ -6,228 +6,228 @@ #include namespace hgl { - template class SemLockTemplate - { - protected: + template class SemLockTemplate + { + protected: - T data; + T data; - L lock; - Semaphore sem; + L lock; + Semaphore sem; - public: + public: - SemLockTemplate(){} + SemLockTemplate(){} - /** - * 本类构造函数 - * @param d 数据 - * @param sem_count 最大信号数量(默认0表示不限制) - */ - SemLockTemplate(T &d,int sem_count=0):sem(sem_count) - { - data=d; - } + /** + * 本类构造函数 + * @param d 数据 + * @param sem_count 最大信号数量(默认0表示不限制) + */ + SemLockTemplate(T &d,int sem_count=0):sem(sem_count) + { + data=d; + } - public: //数据相关 + public: //数据相关 - void operator = (T &d){data=d;} - T *operator->(){return &data;} + void operator = (T &d){data=d;} + T *operator->(){return &data;} - public: //信号相关 + public: //信号相关 - /** - * 释放信号 - * @param n 释放的信号数量 - * @return 是否成功 - */ - bool Post(int n=1) - { - if(n<=0)return(false); - return sem.Post(n); - } + /** + * 释放信号 + * @param n 释放的信号数量 + * @return 是否成功 + */ + bool Post(int n=1) + { + if(n<=0)return(false); + return sem.Post(n); + } - bool TryAcquire(){return sem.TryAcquire();} ///<尝试获取一个信号 - /** - * 获取一个信号 - * @param time_out 超时时间 - * @return 是否成功 - */ - bool Acquire(double time_out=0.0f){return sem.Acquire();} - };//template class SemLockTemplate + bool TryAcquire(){return sem.TryAcquire();} ///<尝试获取一个信号 + /** + * 获取一个信号 + * @param time_out 超时时间 + * @return 是否成功 + */ + bool Acquire(double time_out=0.0f){return sem.Acquire();} + };//template class SemLockTemplate - /** - * 信号线程排斥 - */ - template class SemThreadMutex:public SemLockTemplate - { - public: + /** + * 信号线程排斥 + */ + template class SemThreadMutex:public SemLockTemplate + { + public: - using SemLockTemplate::SemLockTemplate; + using SemLockTemplate::SemLockTemplate; - public: //线程排斥相关 + public: //线程排斥相关 - void Lock(){this->lock.Lock();} ///<取得的控制权(如果对象处于排斥状态,则等待) - bool TryLock(){return this->lock.TryLock();} ///<尝试取得控制权 - bool WaitLock(double time_out=0){return this->lock.WaitLock();} ///<等待并取得控制权 - void Unlock(){this->lock.Unlock();} ///<放弃控制权 + void Lock(){this->lock.Lock();} ///<取得的控制权(如果对象处于排斥状态,则等待) + bool TryLock(){return this->lock.TryLock();} ///<尝试取得控制权 + bool WaitLock(double time_out=0){return this->lock.WaitLock();} ///<等待并取得控制权 + void Unlock(){this->lock.Unlock();} ///<放弃控制权 - public: //综合应用相关 + public: //综合应用相关 - /** - * 在指定时间内如果成功捕获到信号则锁定 - * @param time_out 最长等待时间 - * @return 是否锁定了 - */ - bool WaitSemLock(double time_out) - { - if(!this->sem.Acquire(time_out)) - return(false); + /** + * 在指定时间内如果成功捕获到信号则锁定 + * @param time_out 最长等待时间 + * @return 是否锁定了 + */ + bool WaitSemLock(double time_out) + { + if(!this->sem.Acquire(time_out)) + return(false); - this->lock.Lock(); - return(true); - } + this->lock.Lock(); + return(true); + } - /** - * 尝试捕获一个信号,如果成功则锁定 - * @return 是否锁定了 - */ - bool TrySemLock() - { - if(!this->sem.TryAcquire()) - return(false); + /** + * 尝试捕获一个信号,如果成功则锁定 + * @return 是否锁定了 + */ + bool TrySemLock() + { + if(!this->sem.TryAcquire()) + return(false); - this->lock.Lock(); - return(true); - } + this->lock.Lock(); + return(true); + } - /** - * 锁定 - * @param try_lock 是否是尝试锁定 - * @param time_out 如果不是尝试锁定,则等待锁定的超时时间 - */ - bool SemLock(bool try_lock,double time_out) - { - if(try_lock) - { - if(!this->sem.TryAcquire()) - return(false); - } - else //当前没人,那就慢慢等吧 - { - if(!this->sem.Acquire(time_out)) - return(false); - } + /** + * 锁定 + * @param try_lock 是否是尝试锁定 + * @param time_out 如果不是尝试锁定,则等待锁定的超时时间 + */ + bool SemLock(bool try_lock,double time_out) + { + if(try_lock) + { + if(!this->sem.TryAcquire()) + return(false); + } + else //当前没人,那就慢慢等吧 + { + if(!this->sem.Acquire(time_out)) + return(false); + } - this->lock.Lock(); - return(true); - } + this->lock.Lock(); + return(true); + } - /** - * 解锁并释放信号 - * @param n 释放的信号数量 - */ - void SemUnlock(int n=1) - { - this->lock.Unlock(); + /** + * 解锁并释放信号 + * @param n 释放的信号数量 + */ + void SemUnlock(int n=1) + { + this->lock.Unlock(); - if(n>0) - this->sem.Post(n); - } - };//template class SemThreadMutex + if(n>0) + this->sem.Post(n); + } + };//template class SemThreadMutex - /** - * 信号读写锁 - */ - template class SemRWLock:public SemLockTemplate - { - public: + /** + * 信号读写锁 + */ + template class SemRWLock:public SemLockTemplate + { + public: - using SemLockTemplate::SemLockTemplate; + using SemLockTemplate::SemLockTemplate; - public: //读写锁相关 + public: //读写锁相关 - HGL_RWLOCK(this->lock); + HGL_RWLOCK(this->lock); - public: //综合应用相关 + public: //综合应用相关 - /** - * 在指定时间内如果成功捕获到信号则共享锁定 - * @param time_out 最长等待时间 - * @return 是否锁定了 - */ - bool WaitSemReadLock(double time_out) - { - if(!this->sem.Acquire(time_out)) - return(false); + /** + * 在指定时间内如果成功捕获到信号则共享锁定 + * @param time_out 最长等待时间 + * @return 是否锁定了 + */ + bool WaitSemReadLock(double time_out) + { + if(!this->sem.Acquire(time_out)) + return(false); - this->lock.ReadLock(); - return(true); - } + this->lock.ReadLock(); + return(true); + } - /** - * 在指定时间内如果成功捕获到信号则独占锁定 - * @param time_out 最长等待时间 - * @return 是否锁定了 - */ - bool WaitSemWriteLock(double time_out) - { - if(!this->sem.Acquire(time_out)) - return(false); + /** + * 在指定时间内如果成功捕获到信号则独占锁定 + * @param time_out 最长等待时间 + * @return 是否锁定了 + */ + bool WaitSemWriteLock(double time_out) + { + if(!this->sem.Acquire(time_out)) + return(false); - this->lock.WriteLock(); - return(true); - } + this->lock.WriteLock(); + return(true); + } - /** - * 尝试捕获一个信号,如果成功则共享锁定 - * @return 是否锁定了 - */ - bool TrySemReadLock() - { - if(!this->sem.TryAcquire()) - return(false); + /** + * 尝试捕获一个信号,如果成功则共享锁定 + * @return 是否锁定了 + */ + bool TrySemReadLock() + { + if(!this->sem.TryAcquire()) + return(false); - this->lock.ReadLock(); - return(true); - } + this->lock.ReadLock(); + return(true); + } - /** - * 尝试捕获一个信号,如果成功则独占锁定 - * @return 是否锁定了 - */ - bool TrySemWriteLock() - { - if(!this->sem.TryAcquire()) - return(false); + /** + * 尝试捕获一个信号,如果成功则独占锁定 + * @return 是否锁定了 + */ + bool TrySemWriteLock() + { + if(!this->sem.TryAcquire()) + return(false); - this->lock.WriteLock(); - return(true); - } + this->lock.WriteLock(); + return(true); + } - /** - * 解除共享锁并释放信号 - * @param n 释放的信号数量 - */ - void SemReadUnlock(int n=1) - { - this->lock.ReadUnlock(); + /** + * 解除共享锁并释放信号 + * @param n 释放的信号数量 + */ + void SemReadUnlock(int n=1) + { + this->lock.ReadUnlock(); - if(n>0) - this->sem.Post(n); - } + if(n>0) + this->sem.Post(n); + } - /** - * 解除独占锁并释放信号 - * @param n 释放的信号数量 - */ - void SemWriteUnlock(int n=1) - { - this->lock.WriteUnlock(); + /** + * 解除独占锁并释放信号 + * @param n 释放的信号数量 + */ + void SemWriteUnlock(int n=1) + { + this->lock.WriteUnlock(); - if(n>0) - this->sem.Post(n); - } - };//template class SemRWLock + if(n>0) + this->sem.Post(n); + } + };//template class SemRWLock }//namespace hgl #endif//HGL_THREAD_SEM_LOCK_INCLUDE diff --git a/inc/hgl/thread/Semaphore.h b/inc/hgl/thread/Semaphore.h index 75c9ccd0..4e016a16 100644 --- a/inc/hgl/thread/Semaphore.h +++ b/inc/hgl/thread/Semaphore.h @@ -7,27 +7,27 @@ namespace hgl { - /** - * 信号是用于多线程同步中常用的一种技术
- * 注意信号的用法是要有信号被发送出来,才能获取到信号。 - */ + /** + * 信号是用于多线程同步中常用的一种技术
+ * 注意信号的用法是要有信号被发送出来,才能获取到信号。 + */ class Semaphore ///信号 - { + { #ifdef __APPLE__ dispatch_semaphore_t ptr; #else - void *ptr; + void *ptr; #endif//__APPLE__ - public: + public: - Semaphore(int=1024); - virtual ~Semaphore(); + Semaphore(int=1024); + virtual ~Semaphore(); - virtual bool Post(int n=1); ///<发送信号 + virtual bool Post(int n=1); ///<发送信号 - virtual bool TryAcquire(); ///<尝试取得一个信号 - virtual bool Acquire(double time=0.0); ///<等待并获取一个信号 - };//class Semaphore + virtual bool TryAcquire(); ///<尝试取得一个信号 + virtual bool Acquire(double time=0.0); ///<等待并获取一个信号 + };//class Semaphore }//namespace hgl #endif//HGL_SEMAPHORE_INCLUDE diff --git a/inc/hgl/thread/SwapColl.h b/inc/hgl/thread/SwapColl.h index 92078c06..7e5a0f7a 100644 --- a/inc/hgl/thread/SwapColl.h +++ b/inc/hgl/thread/SwapColl.h @@ -7,150 +7,150 @@ #include namespace hgl { - /** - * 数据交换合集 - */ - template class S> class SwapColl - { - protected: + /** + * 数据交换合集 + */ + template class S> class SwapColl + { + protected: - S join_list; - S proc_list; + S join_list; + S proc_list; - ThreadMutex lock; + ThreadMutex lock; - public: + public: - virtual ~SwapColl(){} + virtual ~SwapColl(){} - /** - * 加入一个数据到合集中 - */ - bool Add(T &item) - { - lock.Lock(); - int result=join_list.Add(item); - lock.Unlock(); + /** + * 加入一个数据到合集中 + */ + bool Add(T &item) + { + lock.Lock(); + int result=join_list.Add(item); + lock.Unlock(); - return(result!=-1); - } + return(result!=-1); + } - /** - * 增加一批数据到合集中 - */ - int Add(T *item_list,const int count) - { - lock.Lock(); - int result=join_list.Add(item_list,count); - lock.Unlock(); + /** + * 增加一批数据到合集中 + */ + int Add(T *item_list,const int count) + { + lock.Lock(); + int result=join_list.Add(item_list,count); + lock.Unlock(); - return result; - } + return result; + } - /** - * 取得可以使用的列表 - */ - S &GetProcList() - { - lock.Lock(); - proc_list.Add(join_list); - join_list.ClearData(); - lock.Unlock(); + /** + * 取得可以使用的列表 + */ + S &GetProcList() + { + lock.Lock(); + proc_list.Add(join_list); + join_list.ClearData(); + lock.Unlock(); - return proc_list; - } + return proc_list; + } - S *operator ->(){return &proc_list;} - operator S &(){return proc_list;} - };//template class SwapColl + S *operator ->(){return &proc_list;} + operator S &(){return proc_list;} + };//template class SwapColl - template using SwapList=SwapColl; ///<安全交换列表 - template using SwapSet=SwapColl; ///<安全交换集合 + template using SwapList=SwapColl; ///<安全交换列表 + template using SwapSet=SwapColl; ///<安全交换集合 - /** - * 信号安全交换合集 - */ - template class S> class SemSwapColl - { - protected: + /** + * 信号安全交换合集 + */ + template class S> class SemSwapColl + { + protected: - S join_list; - S proc_list; + S join_list; + S proc_list; - ThreadMutex lock; - Semaphore sem; + ThreadMutex lock; + Semaphore sem; - public: + public: - virtual ~SemSwapColl()=default; + virtual ~SemSwapColl()=default; - /** - * 增加一个数据到合集中 - */ - bool Add(T &item) - { - lock.Lock(); - int result=join_list.Add(item); - lock.Unlock(); + /** + * 增加一个数据到合集中 + */ + bool Add(T &item) + { + lock.Lock(); + int result=join_list.Add(item); + lock.Unlock(); - if(result!=-1) - sem.Post(1); + if(result!=-1) + sem.Post(1); - return(result!=-1); - } + return(result!=-1); + } - /** - * 增加一批数据到合集中 - */ - int Add(T *item_list,const int count) - { - lock.Lock(); - int result=join_list.Add(item_list,count); - lock.Unlock(); + /** + * 增加一批数据到合集中 + */ + int Add(T *item_list,const int count) + { + lock.Lock(); + int result=join_list.Add(item_list,count); + lock.Unlock(); - if(result>0) - sem.Post(1); + if(result>0) + sem.Post(1); - return result; - } + return result; + } - /** - * 等待信号 - */ - bool WaitProc(const double time_out=HGL_TIME_ONE_MINUTE) - { - if(!sem.Acquire(time_out)) - return(false); + /** + * 等待信号 + */ + bool WaitProc(const double time_out=HGL_TIME_ONE_MINUTE) + { + if(!sem.Acquire(time_out)) + return(false); - lock.Lock(); - proc_list.Add(join_list); - join_list.ClearData(); - lock.Unlock(); + lock.Lock(); + proc_list.Add(join_list); + join_list.ClearData(); + lock.Unlock(); - return(true); - } + return(true); + } - /** - * 等待信号 - */ - bool TryProc() - { - if(!sem.TryAcquire()) - return(false); + /** + * 等待信号 + */ + bool TryProc() + { + if(!sem.TryAcquire()) + return(false); - lock.Lock(); - proc_list.Add(join_list); - join_list.ClearData(); - lock.Unlock(); + lock.Lock(); + proc_list.Add(join_list); + join_list.ClearData(); + lock.Unlock(); - return(true); - } + return(true); + } - S *operator ->(){return &proc_list;} - operator S &(){return proc_list;} - };//template class SemSwapColl + S *operator ->(){return &proc_list;} + operator S &(){return proc_list;} + };//template class SemSwapColl - template using SemSwapList=SemSwapColl; ///<安全信号交换列表 - template using SemSwapSet=SemSwapColl; ///<安全信号交换集合 + template using SemSwapList=SemSwapColl; ///<安全信号交换列表 + template using SemSwapSet=SemSwapColl; ///<安全信号交换集合 }//namespace hgl #endif//HGL_THREAD_SWAP_LIST_INCLUDE diff --git a/inc/hgl/thread/SwapData.h b/inc/hgl/thread/SwapData.h index 2a788cc5..5ff617d6 100644 --- a/inc/hgl/thread/SwapData.h +++ b/inc/hgl/thread/SwapData.h @@ -21,13 +21,13 @@ namespace hgl ThreadMutex lock; - protected: + protected: - void _Swap() - { + void _Swap() + { if(recv_index){recv_index=0;post_index=1;} - else{recv_index=1;post_index=0;} - } + else{recv_index=1;post_index=0;} + } public: @@ -74,85 +74,85 @@ namespace hgl lock.Unlock(); } - /** - * 尝试交换双方数据 - */ - bool TrySwap() - { - if(!lock.TryLock()) - return(false); + /** + * 尝试交换双方数据 + */ + bool TrySwap() + { + if(!lock.TryLock()) + return(false); - this->_Swap(); + this->_Swap(); lock.Unlock(); - return(true); - } + return(true); + } #ifndef __APPLE__ - /** - * 尝试交换双方数据 - */ - bool WaitSwap(const double time_out) - { - if(!lock.WaitLock(time_out)) - return(false); + /** + * 尝试交换双方数据 + */ + bool WaitSwap(const double time_out) + { + if(!lock.WaitLock(time_out)) + return(false); - this->_Swap(); + this->_Swap(); lock.Unlock(); - return(true); - } + return(true); + } #endif//__APPLE__ };//template class SwapData - /** - * 信号自动交换数据访问模板 - */ - template class SemSwapData:public SwapData - { - Semaphore sem; + /** + * 信号自动交换数据访问模板 + */ + template class SemSwapData:public SwapData + { + Semaphore sem; - public: + public: - using SwapData::SwapData; - ~SemSwapData()=default; + using SwapData::SwapData; + ~SemSwapData()=default; - /** - * 释放接收信号 - * @param count 信号个数 - */ - void PostSem(int count=1) - { - sem.Post(count); - } + /** + * 释放接收信号 + * @param count 信号个数 + */ + void PostSem(int count=1) + { + sem.Post(count); + } - /** - * 等待获取一个信号并交换前后台数据 - * @param time_out 等待时长 - */ - bool WaitSemSwap(const double time_out=5) - { - if(!sem.Acquire(time_out)) - return(false); + /** + * 等待获取一个信号并交换前后台数据 + * @param time_out 等待时长 + */ + bool WaitSemSwap(const double time_out=5) + { + if(!sem.Acquire(time_out)) + return(false); - this->Swap(); - return(true); - } + this->Swap(); + return(true); + } - /** - * 尝试获取一个信号并交换前后台数据 - */ - bool TrySemSwap() - { - if(!sem.TryAcquire()) - return(false); + /** + * 尝试获取一个信号并交换前后台数据 + */ + bool TrySemSwap() + { + if(!sem.TryAcquire()) + return(false); - this->Swap(); - return(true); - } - };//template class SemSwapData:public SwapData + this->Swap(); + return(true); + } + };//template class SemSwapData:public SwapData - /** + /** * SwapData模板自动释放Post工具模板 */ template class PostToSwapData diff --git a/inc/hgl/thread/Thread.h b/inc/hgl/thread/Thread.h index fcba970d..a0d162aa 100644 --- a/inc/hgl/thread/Thread.h +++ b/inc/hgl/thread/Thread.h @@ -11,25 +11,25 @@ namespace hgl { void WaitThreadExit(thread_ptr tp,const double &time_out); - /** - * 线程类.本类用于建立一个线程并管理它。
- * Sleep和Exit函数是给线程内的程序使用的;而Start,Close,Pause,Rewind是给线程外的程序使用的。
- * 使用本类只需要重载本类,并将代码写在Execute函数内即可。
- * 执行大概为:
- * if(ProcStartThread())
- * while(Execute());
- * else
- * return(0); - *
- * ProcEndThread(); - */ - class Thread ///线程类 - { - friend void WaitThread(Thread **,int,double); + /** + * 线程类.本类用于建立一个线程并管理它。
+ * Sleep和Exit函数是给线程内的程序使用的;而Start,Close,Pause,Rewind是给线程外的程序使用的。
+ * 使用本类只需要重载本类,并将代码写在Execute函数内即可。
+ * 执行大概为:
+ * if(ProcStartThread())
+ * while(Execute());
+ * else
+ * return(0); + *
+ * ProcEndThread(); + */ + class Thread ///线程类 + { + friend void WaitThread(Thread **,int,double); - private: + private: - thread_ptr tp=0; + thread_ptr tp=0; ThreadMutex live_lock; ThreadMutex exit_lock; @@ -40,22 +40,22 @@ namespace hgl UTF8String thread_addr_string; ///<线程地址用字符串,调试使用 #endif//_DEBUG - public: + public: - virtual ~Thread()=default; + virtual ~Thread()=default; #ifdef _DEBUG - const UTF8String &GetThreadAddressString()const{return thread_addr_string;} ///<取得线程地址字符串 + const UTF8String &GetThreadAddressString()const{return thread_addr_string;} ///<取得线程地址字符串 #endif//_DEBUG - /** - * 线程执行函数,会被反复调用 - * @return true 继续执行 - * @return false 退出执行 - */ - virtual bool Execute()=0; ///<线程的运行函数 + /** + * 线程执行函数,会被反复调用 + * @return true 继续执行 + * @return false 退出执行 + */ + virtual bool Execute()=0; ///<线程的运行函数 - virtual bool ProcStartThread() ///<线程启动运行函数,在Execute前被调用 + virtual bool ProcStartThread() ///<线程启动运行函数,在Execute前被调用 { #ifdef _DEBUG char thread_addr[(sizeof(thread_ptr)<<1)+1]; @@ -68,9 +68,9 @@ namespace hgl return(true); } - virtual void ProcEndThread(){} ///<结程结束运行函数,在Execute后被调用 + virtual void ProcEndThread(){} ///<结程结束运行函数,在Execute后被调用 - virtual bool IsExitDelete()const{return true;} ///<返回在退出线程时,是否删除本对象(注:此函数不可动态变动值) + virtual bool IsExitDelete()const{return true;} ///<返回在退出线程时,是否删除本对象(注:此函数不可动态变动值) bool IsLive() ///<当前线程是否还活着 { @@ -84,11 +84,11 @@ namespace hgl return(true); } - public: //线程运行控制 + public: //线程运行控制 - virtual bool Start(); ///<开始运行当前线程 + virtual bool Start(); ///<开始运行当前线程 - virtual bool IsCurThread(); ///<是否是当前线程 + virtual bool IsCurThread(); ///<是否是当前线程 virtual void Exit() ///<退出当前线程(其它线程调用) { @@ -99,7 +99,7 @@ namespace hgl * (线程外部调用)退出当前线程,并等待其完成退出
* @param time_out 等待的时间,如果为0表示等到线程运行结束为止。默认为0 */ - virtual void WaitExit(const double &time_out=0) + virtual void WaitExit(const double &time_out=0) { if(!tp) return; @@ -110,11 +110,11 @@ namespace hgl WaitThreadExit(self_tp,time_out); } - };//class Thread + };//class Thread - void WaitThread(Thread **,int,double time=0); ///<等待多个线程中的一个完成 + void WaitThread(Thread **,int,double time=0); ///<等待多个线程中的一个完成 - bool CreateThread(Thread *); ///<创建一个线程 + bool CreateThread(Thread *); ///<创建一个线程 /** * 简单的多线程管理 diff --git a/inc/hgl/thread/ThreadMutex.h b/inc/hgl/thread/ThreadMutex.h index 920a41aa..a7951eea 100644 --- a/inc/hgl/thread/ThreadMutex.h +++ b/inc/hgl/thread/ThreadMutex.h @@ -5,159 +5,159 @@ #include namespace hgl { - /** - * 排斥(线程)访问类可以判断数据在被一个代码段访问时,不能被另一代码段访问
+ /** + * 排斥(线程)访问类可以判断数据在被一个代码段访问时,不能被另一代码段访问
* 注:此排斥仅在当前进程内有效 - */ - class ThreadMutex ///排斥访问类(仅当前进程) - { - hgl_thread_mutex ptr; + */ + class ThreadMutex ///排斥访问类(仅当前进程) + { + hgl_thread_mutex ptr; - public: + public: - ThreadMutex(); ///<本类构造函数 - virtual ~ThreadMutex(); ///<本类析构函数 + ThreadMutex(); ///<本类构造函数 + virtual ~ThreadMutex(); ///<本类析构函数 - hgl_thread_mutex *GetThreadMutex(){return &ptr;} + hgl_thread_mutex *GetThreadMutex(){return &ptr;} - virtual void Lock(); ///<取得的控制权(如果对象处于排斥状态,则等待) - virtual bool TryLock(); ///<尝试取得控制权 + virtual void Lock(); ///<取得的控制权(如果对象处于排斥状态,则等待) + virtual bool TryLock(); ///<尝试取得控制权 #if !defined(__APPLE__)&&!defined(__ANDROID__) - virtual bool WaitLock(const double=0); ///<等待并取得控制权 + virtual bool WaitLock(const double=0); ///<等待并取得控制权 #endif//__APPLE__ - virtual void Unlock(); ///<放弃控制权 - };//class Mutex + virtual void Unlock(); ///<放弃控制权 + };//class Mutex - /** - * 线程排斥对象 - */ - template class ThreadMutexObject:public ThreadMutex - { - T *data; + /** + * 线程排斥对象 + */ + template class ThreadMutexObject:public ThreadMutex + { + T *data; - public: + public: - ThreadMutexObject() - { - data=new T; - } + ThreadMutexObject() + { + data=new T; + } - ThreadMutexObject(T *t) - { - data=t; - } + ThreadMutexObject(T *t) + { + data=t; + } - virtual ~ThreadMutexObject() - { - SAFE_CLEAR(data); - } + virtual ~ThreadMutexObject() + { + SAFE_CLEAR(data); + } - void operator = (T *t) - { - if(data) - delete data; + void operator = (T *t) + { + if(data) + delete data; - data=t; - } + data=t; + } - T *operator ->() - { - return data; - } + T *operator ->() + { + return data; + } - bool valid()const - { - return data; - } + bool valid()const + { + return data; + } - bool operator !() - { - return !data; - } - };//class ThreadMutexObject + bool operator !() + { + return !data; + } + };//class ThreadMutexObject - /** - * 线程排斥对像阵列 - */ - template class ThreadMutexObjectArray - { - protected: + /** + * 线程排斥对像阵列 + */ + template class ThreadMutexObjectArray + { + protected: - ThreadMutexObject *items; + ThreadMutexObject *items; - public: + public: - ThreadMutexObjectArray() - { - items=nullptr; - } + ThreadMutexObjectArray() + { + items=nullptr; + } - ThreadMutexObjectArray(int count) - { - if(count<=0) - { - items=nullptr; - } - else - { - items=new ThreadMutexObject[count]; - } - } + ThreadMutexObjectArray(int count) + { + if(count<=0) + { + items=nullptr; + } + else + { + items=new ThreadMutexObject[count]; + } + } - virtual ~ThreadMutexObjectArray() - { - delete[] items; //delete nullptr不是个错误 - } + virtual ~ThreadMutexObjectArray() + { + delete[] items; //delete nullptr不是个错误 + } - ThreadMutexObject &operator [](int index) - { - return items[index]; - } - };//class ThreadMutexObjectArray + ThreadMutexObject &operator [](int index) + { + return items[index]; + } + };//class ThreadMutexObjectArray - /** - * 智能自释放线程排斥锁 - */ - class ThreadMutexLock - { - ThreadMutex *tm; + /** + * 智能自释放线程排斥锁 + */ + class ThreadMutexLock + { + ThreadMutex *tm; - public: + public: - ThreadMutexLock(ThreadMutex *tm_lock) - { - tm=tm_lock; + ThreadMutexLock(ThreadMutex *tm_lock) + { + tm=tm_lock; - if(tm) - tm->Lock(); - } + if(tm) + tm->Lock(); + } - ~ThreadMutexLock() - { - if(tm) - tm->Unlock(); - } + ~ThreadMutexLock() + { + if(tm) + tm->Unlock(); + } - void Lock() - { - if(tm) - tm->Lock(); - } + void Lock() + { + if(tm) + tm->Lock(); + } - void Unlock() - { - if(tm) - tm->Unlock(); - } + void Unlock() + { + if(tm) + tm->Unlock(); + } - void Clear() - { - if(tm) - { - tm->Unlock(); - tm=nullptr; - } - } - };//class ThreadMutexLock + void Clear() + { + if(tm) + { + tm->Unlock(); + tm=nullptr; + } + } + };//class ThreadMutexLock }//namespace hgl #endif//HGL_THREAD_MUTEX_INCLUDE diff --git a/inc/hgl/thread/Workflow.h b/inc/hgl/thread/Workflow.h index b233eb31..59cdf989 100644 --- a/inc/hgl/thread/Workflow.h +++ b/inc/hgl/thread/Workflow.h @@ -8,231 +8,231 @@ #include namespace hgl { - /** - * 工作流名字空间
- * 工作流是一种对工作的多线安排管理机制,它适用于按业务分配多线程的工作环境
- * 开发者需要为每一种工作指定一定的线程数量,但每一种工作确只有一个工作分配入口和分发出口。
- * 由其它程序提交工作任务到入口,开发者可以自行重载分配入口的分配函数。 - */ - namespace workflow - { + /** + * 工作流名字空间
+ * 工作流是一种对工作的多线安排管理机制,它适用于按业务分配多线程的工作环境
+ * 开发者需要为每一种工作指定一定的线程数量,但每一种工作确只有一个工作分配入口和分发出口。
+ * 由其它程序提交工作任务到入口,开发者可以自行重载分配入口的分配函数。 + */ + namespace workflow + { /** * 工作处理基类模板 * @param W 工作对象 */ - template class WorkProc - { - public: + template class WorkProc + { + public: - virtual ~WorkProc()=default; + virtual ~WorkProc()=default; - public: //投递工作线程所需调用的方法 + public: //投递工作线程所需调用的方法 - virtual void Post(W *w)=0; ///<投递一个工作 - virtual void Post(W **w,int count)=0; ///<投递一批工作 + virtual void Post(W *w)=0; ///<投递一个工作 + virtual void Post(W **w,int count)=0; ///<投递一批工作 - public: //需用户重载实现的真正执行工作的方法 + public: //需用户重载实现的真正执行工作的方法 - /** - * 单个工作执行事件函数,此函数需用户重载实现 - */ - virtual void OnWork(const uint,W *)=0; + /** + * 单个工作执行事件函数,此函数需用户重载实现 + */ + virtual void OnWork(const uint,W *)=0; - public: //由工作线程调用的执行工作事件函数 + public: //由工作线程调用的执行工作事件函数 /** * 工作执行处理函数 */ - virtual bool OnExecuteWork(const uint)=0; - };//template class WorkProc + virtual bool OnExecuteWork(const uint)=0; + };//template class WorkProc - /** - * 单体工作处理
- * 该类可以由多个线程投递工作,但只能被一个工作线程获取工作 - */ - template class SingleWorkProc:public WorkProc - { - public: + /** + * 单体工作处理
+ * 该类可以由多个线程投递工作,但只能被一个工作线程获取工作 + */ + template class SingleWorkProc:public WorkProc + { + public: - using WorkList=List; + using WorkList=List; - private: + private: - SemSwapData work_list; ///<工程列表 + SemSwapData work_list; ///<工程列表 - protected: + protected: - double time_out; + double time_out; - public: + public: - SingleWorkProc() - { - time_out=5; - } + SingleWorkProc() + { + time_out=5; + } - virtual ~SingleWorkProc()=default; + virtual ~SingleWorkProc()=default; - void SetTimeOut(const double to) ///<设置超时时间 - { - if(to<=0)time_out=0; - else time_out=to; - } + void SetTimeOut(const double to) ///<设置超时时间 + { + if(to<=0)time_out=0; + else time_out=to; + } - virtual void Post(W *w) override ///<投递一个工作 - { - WorkList &wl=work_list.GetPost(); - wl.Add(w); - work_list.ReleasePost(); - } + virtual void Post(W *w) override ///<投递一个工作 + { + WorkList &wl=work_list.GetPost(); + wl.Add(w); + work_list.ReleasePost(); + } - virtual void Post(W **w,int count) override ///<投递一批工作 - { - WorkList &wl=work_list.GetPost(); - wl.Add(w,count); - work_list.ReleasePost(); - } + virtual void Post(W **w,int count) override ///<投递一批工作 + { + WorkList &wl=work_list.GetPost(); + wl.Add(w,count); + work_list.ReleasePost(); + } - virtual void ToWork() ///<将堆积的工作列表发送给工作线程 - { - work_list.PostSem(1); - } + virtual void ToWork() ///<将堆积的工作列表发送给工作线程 + { + work_list.PostSem(1); + } - public: + public: - /** - * 当前工作序列完成事件函数,如需使用请重载此函数 - */ - virtual void OnFinish(const uint wt_index) - { - } + /** + * 当前工作序列完成事件函数,如需使用请重载此函数 + */ + virtual void OnFinish(const uint wt_index) + { + } - /** - * 开始执行工作函数 - */ - virtual bool OnExecuteWork(const uint wt_index) override - { + /** + * 开始执行工作函数 + */ + virtual bool OnExecuteWork(const uint wt_index) override + { //为什么不使用TrySemSwap,使用TrySemSwap固然会立即返回结果,但会引起线程频繁刷新造成CPU的流费。 //使用WaitSemSwap目前唯一坏处是在退出时,需要等待超时时间。 - if(!work_list.WaitSemSwap(time_out)) - return(false); + if(!work_list.WaitSemSwap(time_out)) + return(false); - WorkList &wl=work_list.GetReceive(); + WorkList &wl=work_list.GetReceive(); - const int count=wl.GetCount(); + const int count=wl.GetCount(); - if(count>0) - { - W **p=wl.GetData(); + if(count>0) + { + W **p=wl.GetData(); - for(int i=0;iOnWork(wt_index,*p); - ++p; - } + for(int i=0;iOnWork(wt_index,*p); + ++p; + } - this->OnFinish(wt_index); + this->OnFinish(wt_index); - wl.ClearData(); - } + wl.ClearData(); + } - return(true); - } - };//template class SingleWorkProc:public WorkProc + return(true); + } + };//template class SingleWorkProc:public WorkProc - /** - * 多体工作处理
- * 该类可以由多个线程投递工作,也可以同时被多个工作线程获取工作 - */ - template class MultiWorkProc:public WorkProc - { - protected: + /** + * 多体工作处理
+ * 该类可以由多个线程投递工作,也可以同时被多个工作线程获取工作 + */ + template class MultiWorkProc:public WorkProc + { + protected: - SemDataPost work_list; ///<工程列表 + SemDataPost work_list; ///<工程列表 - protected: + protected: - double time_out; + double time_out; - public: + public: - MultiWorkProc() - { - time_out=5; - } + MultiWorkProc() + { + time_out=5; + } - virtual ~MultiWorkProc()=default; + virtual ~MultiWorkProc()=default; - void SetTimeOut(const double to) ///<设置超时时间 - { - if(to<=0)time_out=0; - else time_out=to; - } + void SetTimeOut(const double to) ///<设置超时时间 + { + if(to<=0)time_out=0; + else time_out=to; + } - virtual void Post(W *w) override ///<投递一个工作 - { - if(!w)return; + virtual void Post(W *w) override ///<投递一个工作 + { + if(!w)return; - work_list.Post(w); - work_list.PostSem(1); - } + work_list.Post(w); + work_list.PostSem(1); + } - virtual void Post(W **w,int count) override ///<投递一批工作 - { - if(!w||count<=0)return; + virtual void Post(W **w,int count) override ///<投递一批工作 + { + if(!w||count<=0)return; - work_list.Post(w,count); - work_list.PostSem(count); - } + work_list.Post(w,count); + work_list.PostSem(count); + } - public: + public: - /** - * 开始执行工作函数 - */ - virtual bool OnExecuteWork(const uint wt_index) override - { + /** + * 开始执行工作函数 + */ + virtual bool OnExecuteWork(const uint wt_index) override + { //为什么不使用TrySemReceive,使用TrySemReceive固然会立即返回结果,但会引起线程频繁刷新造成CPU的流费。 //使用WaitSemReceive目前唯一坏处是在退出时,需要等待超时时间。 - W *obj=work_list.WaitSemReceive(time_out); + W *obj=work_list.WaitSemReceive(time_out); - if(!obj) + if(!obj) return(false); - this->OnWork(wt_index,obj); + this->OnWork(wt_index,obj); - return(true); - } - };//template class MultiWorkProc:public WorkProc + return(true); + } + };//template class MultiWorkProc:public WorkProc - /** - * 工作线程,用于真正处理事务 - */ - template class WorkThread:public Thread - { - protected: + /** + * 工作线程,用于真正处理事务 + */ + template class WorkThread:public Thread + { + protected: - using WorkList=List; + using WorkList=List; - WorkProc *work_proc; + WorkProc *work_proc; - uint work_thread_index; + uint work_thread_index; bool force_close; - public: + public: - WorkThread(WorkProc *wp) - { - work_proc=wp; - work_thread_index=0; - force_close=false; - } + WorkThread(WorkProc *wp) + { + work_proc=wp; + work_thread_index=0; + force_close=false; + } #ifndef _DEBUG - virtual ~WorkThread()=default; + virtual ~WorkThread()=default; #else virtual ~WorkThread() { @@ -241,21 +241,21 @@ namespace hgl #endif//_DEBUG - bool IsExitDelete()const override{return false;} ///<返回在退出线程时,不删除本对象 + bool IsExitDelete()const override{return false;} ///<返回在退出线程时,不删除本对象 - void SetWorkThreadIndex(const uint index) - { - work_thread_index=index; - } + void SetWorkThreadIndex(const uint index) + { + work_thread_index=index; + } - void ExitWork(const bool fc) - { - force_close=fc; + void ExitWork(const bool fc) + { + force_close=fc; Thread::WaitExit(); - } + } - virtual void ProcEndThread() override - { + virtual void ProcEndThread() override + { if(!force_close) //不是强退 while(work_proc->OnExecuteWork(work_thread_index)); //把工作全部做完 @@ -266,106 +266,106 @@ namespace hgl #endif//_DEBUG } - virtual bool Execute() override - { - if(!work_proc) - RETURN_FALSE; + virtual bool Execute() override + { + if(!work_proc) + RETURN_FALSE; work_proc->OnExecuteWork(work_thread_index); return(true); - } - };//template class WorkThread:public Thread + } + };//template class WorkThread:public Thread - /** - * 工作组
- * 用于管理一组的工作线程以及投递器
- * 注:可以一组工作线程共用一个投递器,也可以每个工作线程配一个投递器。工作组管理只为方便统一清理 - */ - template class WorkGroup - { - ObjectList wp_list; ///<投递器列表 - ObjectList wt_list; ///<工作线程列表 + /** + * 工作组
+ * 用于管理一组的工作线程以及投递器
+ * 注:可以一组工作线程共用一个投递器,也可以每个工作线程配一个投递器。工作组管理只为方便统一清理 + */ + template class WorkGroup + { + ObjectList wp_list; ///<投递器列表 + ObjectList wt_list; ///<工作线程列表 - bool run=false; + bool run=false; - public: + public: - virtual ~WorkGroup() - { - Close(); - } + virtual ~WorkGroup() + { + Close(); + } - virtual bool Add(WP *wp) - { - if(!wp)return(false); + virtual bool Add(WP *wp) + { + if(!wp)return(false); - wp_list.Add(wp); - return(true); - } + wp_list.Add(wp); + return(true); + } - virtual bool Add(WP **wp,const int count) - { - if(!wp)return(false); + virtual bool Add(WP **wp,const int count) + { + if(!wp)return(false); - wp_list.Add(wp,count); - return(true); - } + wp_list.Add(wp,count); + return(true); + } - virtual bool Add(WT *wt) - { - if(!wt)return(false); + virtual bool Add(WT *wt) + { + if(!wt)return(false); - int index=wt_list.Add(wt); - wt->SetWorkThreadIndex(index); - return(true); - } + int index=wt_list.Add(wt); + wt->SetWorkThreadIndex(index); + return(true); + } - virtual bool Add(WT **wt,const int count) - { - if(!wt)return(false); + virtual bool Add(WT **wt,const int count) + { + if(!wt)return(false); - int index=wt_list.Add(wt,count); - for(int i=0;iSetWorkThreadIndex(index); - ++index; - ++wt; - } + int index=wt_list.Add(wt,count); + for(int i=0;iSetWorkThreadIndex(index); + ++index; + ++wt; + } - return(true); - } + return(true); + } - virtual bool Start() - { - int count=wt_list.GetCount(); + virtual bool Start() + { + int count=wt_list.GetCount(); - if(count<=0) - RETURN_FALSE; + if(count<=0) + RETURN_FALSE; WT **wt=wt_list.GetData(); - for(int i=0;iStart(); + for(int i=0;iStart(); run=true; - return(true); - } + return(true); + } - virtual void Close(bool force_close=false) - { + virtual void Close(bool force_close=false) + { if(!run)return; - int count=wt_list.GetCount(); + int count=wt_list.GetCount(); WT **wt=wt_list.GetData(); - for(int i=0;iExitWork(force_close); + for(int i=0;iExitWork(force_close); run=false; - } - };//template class WorkGroup - }//namespace workflow + } + };//template class WorkGroup + }//namespace workflow }//namespace hgl #endif//HGL_WORKFLOW_INCLUDE diff --git a/inc/hgl/thread/atomic/AtomicGNU.h b/inc/hgl/thread/atomic/AtomicGNU.h index 161bac20..db1a5522 100644 --- a/inc/hgl/thread/atomic/AtomicGNU.h +++ b/inc/hgl/thread/atomic/AtomicGNU.h @@ -3,46 +3,46 @@ namespace hgl { - template class atom - { - volatile T value; + template class atom + { + volatile T value; - public: + public: - atom(){value=0;} - atom(const volatile T new_value){operator=(new_value);} + atom(){value=0;} + atom(const volatile T new_value){operator=(new_value);} - inline T operator ->(){return value;} + inline T operator ->(){return value;} - inline T operator *= (const volatile T v) {return operator=(value *=v);} - inline T operator /= (const volatile T v) {return operator=(value /=v);} - inline T operator %= (const volatile T v) {return operator=(value %=v);} - inline T operator >>= (const volatile T v) {return operator=(value>>=v);} - inline T operator <<= (const volatile T v) {return operator=(value<<=v);} + inline T operator *= (const volatile T v) {return operator=(value *=v);} + inline T operator /= (const volatile T v) {return operator=(value /=v);} + inline T operator %= (const volatile T v) {return operator=(value %=v);} + inline T operator >>= (const volatile T v) {return operator=(value>>=v);} + inline T operator <<= (const volatile T v) {return operator=(value<<=v);} - inline T operator ! ()const {return !value;} - inline T operator ~ ()const {return ~value;} + inline T operator ! ()const {return !value;} + inline T operator ~ ()const {return ~value;} - inline operator T () {return value;} - inline operator const T ()const {return value;} - inline bool operator ! () {return !value;} + inline operator T () {return value;} + inline operator const T ()const {return value;} + inline bool operator ! () {return !value;} - inline bool operator == (const volatile T v)const {return value==v;} - inline bool operator != (const volatile T v)const {return value!=v;} + inline bool operator == (const volatile T v)const {return value==v;} + inline bool operator != (const volatile T v)const {return value!=v;} - inline T operator = (const volatile T new_value) {__sync_lock_test_and_set(&value,new_value); return value;} + inline T operator = (const volatile T new_value) {__sync_lock_test_and_set(&value,new_value); return value;} - inline T operator += (const volatile T add_value) {return __sync_add_and_fetch(&value,add_value);} - inline T operator -= (const volatile T sub_value) {return __sync_sub_and_fetch(&value,sub_value);} + inline T operator += (const volatile T add_value) {return __sync_add_and_fetch(&value,add_value);} + inline T operator -= (const volatile T sub_value) {return __sync_sub_and_fetch(&value,sub_value);} - inline T operator &= (const volatile T v) {return __sync_and_and_fetch(&value,v);} - inline T operator |= (const volatile T v) {return __sync_or_and_fetch (&value,v);} - inline T operator ^= (const volatile T v) {return __sync_xor_and_fetch(&value,v);} + inline T operator &= (const volatile T v) {return __sync_and_and_fetch(&value,v);} + inline T operator |= (const volatile T v) {return __sync_or_and_fetch (&value,v);} + inline T operator ^= (const volatile T v) {return __sync_xor_and_fetch(&value,v);} - inline T operator ++ () {return __sync_add_and_fetch(&value,1);} //前置++ - inline T operator ++ (int) {return __sync_fetch_and_add(&value,1);} //后置++ - inline T operator -- () {return __sync_sub_and_fetch(&value,1);} - inline T operator -- (int) {return __sync_fetch_and_sub(&value,1);} - };//template class atom + inline T operator ++ () {return __sync_add_and_fetch(&value,1);} //前置++ + inline T operator ++ (int) {return __sync_fetch_and_add(&value,1);} //后置++ + inline T operator -- () {return __sync_sub_and_fetch(&value,1);} + inline T operator -- (int) {return __sync_fetch_and_sub(&value,1);} + };//template class atom }//namespace hgl #endif//HGL_THREAD_ATOMIC_GNU_INCLUDE diff --git a/inc/hgl/thread/atomic/AtomicOSX.h b/inc/hgl/thread/atomic/AtomicOSX.h index 5fca01d3..1664a64c 100644 --- a/inc/hgl/thread/atomic/AtomicOSX.h +++ b/inc/hgl/thread/atomic/AtomicOSX.h @@ -5,46 +5,46 @@ namespace hgl { - typedef int aint; + typedef int aint; - typedef volatile aint avint; - typedef const avint cavint; + typedef volatile aint avint; + typedef const avint cavint; - template class atom ///原子数据 - { - avint value; + template class atom ///原子数据 + { + avint value; - public: + public: - atom(){value=0;} - atom(cavint new_value){operator=(new_value);} + atom(){value=0;} + atom(cavint new_value){operator=(new_value);} - inline aint operator *= (cavint v) {return operator=(value *=v);} - inline aint operator /= (cavint v) {return operator=(value /=v);} - inline aint operator %= (cavint v) {return operator=(value %=v);} - inline aint operator >>= (cavint v) {return operator=(value>>=v);} - inline aint operator <<= (cavint v) {return operator=(value<<=v);} + inline aint operator *= (cavint v) {return operator=(value *=v);} + inline aint operator /= (cavint v) {return operator=(value /=v);} + inline aint operator %= (cavint v) {return operator=(value %=v);} + inline aint operator >>= (cavint v) {return operator=(value>>=v);} + inline aint operator <<= (cavint v) {return operator=(value<<=v);} - inline aint operator ! ()const {return !value;} - inline aint operator ~ ()const {return ~value;} + inline aint operator ! ()const {return !value;} + inline aint operator ~ ()const {return ~value;} - inline operator const aint ()const {return value;} - inline bool operator ! () {return !value;} + inline operator const aint ()const {return value;} + inline bool operator ! () {return !value;} - inline bool operator == (cavint v)const {return value==v;} - inline bool operator != (cavint v)const {return value!=v;} - inline aint operator = (cavint new_value) {value=new_value;return value;} - inline aint operator ++ () {return OSAtomicIncrement32(&value);} - inline aint operator -- () {return OSAtomicDecrement32(&value);} - inline aint operator += (cavint add_value) {return;} - inline aint operator -= (cavint sub_value) {return;} + inline bool operator == (cavint v)const {return value==v;} + inline bool operator != (cavint v)const {return value!=v;} + inline aint operator = (cavint new_value) {value=new_value;return value;} + inline aint operator ++ () {return OSAtomicIncrement32(&value);} + inline aint operator -- () {return OSAtomicDecrement32(&value);} + inline aint operator += (cavint add_value) {return;} + inline aint operator -= (cavint sub_value) {return;} - inline aint operator &= (cavint v) {return operator=(value &=v);} - inline aint operator |= (cavint v) {return operator=(value |=v);} - inline aint operator ^= (cavint v) {return operator=(value ^=v);} + inline aint operator &= (cavint v) {return operator=(value &=v);} + inline aint operator |= (cavint v) {return operator=(value |=v);} + inline aint operator ^= (cavint v) {return operator=(value ^=v);} - inline aint operator ++ (int) {aint ret=value;operator++();return ret;}//后置++ - inline aint operator -- (int) {volatile T ret=value;operator--();return ret;}//后置-- - };//class atom + inline aint operator ++ (int) {aint ret=value;operator++();return ret;}//后置++ + inline aint operator -- (int) {volatile T ret=value;operator--();return ret;}//后置-- + };//class atom }//namespace hgl #endif//HGL_THREAD_ATOMIC_OSX_INCLUDE diff --git a/inc/hgl/thread/atomic/AtomicWin.h b/inc/hgl/thread/atomic/AtomicWin.h index 94c3b67f..7360cc44 100644 --- a/inc/hgl/thread/atomic/AtomicWin.h +++ b/inc/hgl/thread/atomic/AtomicWin.h @@ -4,88 +4,88 @@ #include namespace hgl { - //开发日志 2013-06-19 - //1.原本是一个模板,但将32/64中不一样的部分各自特例化,但不知为何VC2012中无法认出对应的操作符,所以改成2个模板 - //2.部分原子函数没有8/16位版本 + //开发日志 2013-06-19 + //1.原本是一个模板,但将32/64中不一样的部分各自特例化,但不知为何VC2012中无法认出对应的操作符,所以改成2个模板 + //2.部分原子函数没有8/16位版本 - //32位版 - template class atom_win32 - { - volatile T value; + //32位版 + template class atom_win32 + { + volatile T value; - public: + public: - atom_win32(){value=0;} - atom_win32(const volatile T new_value){operator=(new_value);} + atom_win32(){value=0;} + atom_win32(const volatile T new_value){operator=(new_value);} - T operator *= (const volatile T v) {return operator=(value *=v);} - T operator /= (const volatile T v) {return operator=(value /=v);} - T operator %= (const volatile T v) {return operator=(value %=v);} - T operator >>= (const volatile T v) {return operator=(value>>=v);} - T operator <<= (const volatile T v) {return operator=(value<<=v);} + T operator *= (const volatile T v) {return operator=(value *=v);} + T operator /= (const volatile T v) {return operator=(value /=v);} + T operator %= (const volatile T v) {return operator=(value %=v);} + T operator >>= (const volatile T v) {return operator=(value>>=v);} + T operator <<= (const volatile T v) {return operator=(value<<=v);} - T operator ! ()const {return !value;} - T operator ~ ()const {return ~value;} + T operator ! ()const {return !value;} + T operator ~ ()const {return ~value;} - operator T () {return value;} - operator const T ()const {return value;} - bool operator ! () {return !value;} + operator T () {return value;} + operator const T ()const {return value;} + bool operator ! () {return !value;} - bool operator == (const volatile T v)const {return value==v;} - bool operator != (const volatile T v)const {return value!=v;} + bool operator == (const volatile T v)const {return value==v;} + bool operator != (const volatile T v)const {return value!=v;} - T operator = (const volatile T nv) {return InterlockedExchange((unsigned long *)&value,(unsigned long)nv);} - T operator ++ () {return InterlockedIncrement((unsigned long *)&value);} - T operator -- () {return InterlockedDecrement((unsigned long *)&value);} - T operator += (const volatile T av) {return InterlockedExchangeAdd((unsigned long *)&value, av); } - T operator -= (const volatile T av) {return InterlockedExchangeAdd((unsigned long *)&value, -av); } + T operator = (const volatile T nv) {return InterlockedExchange((unsigned long *)&value,(unsigned long)nv);} + T operator ++ () {return InterlockedIncrement((unsigned long *)&value);} + T operator -- () {return InterlockedDecrement((unsigned long *)&value);} + T operator += (const volatile T av) {return InterlockedExchangeAdd((unsigned long *)&value, av); } + T operator -= (const volatile T av) {return InterlockedExchangeAdd((unsigned long *)&value, -av); } - volatile T operator &= (const volatile T v) {return InterlockedAnd((unsigned long *)&value,v);} - volatile T operator |= (const volatile T v) {return InterlockedOr((unsigned long *)&value, v); } - volatile T operator ^= (const volatile T v) {return InterlockedXor((unsigned long *)&value, v); } + volatile T operator &= (const volatile T v) {return InterlockedAnd((unsigned long *)&value,v);} + volatile T operator |= (const volatile T v) {return InterlockedOr((unsigned long *)&value, v); } + volatile T operator ^= (const volatile T v) {return InterlockedXor((unsigned long *)&value, v); } - volatile T operator ++ (int) {volatile T ret=value;operator++();return ret;}//后置++ - volatile T operator -- (int) {volatile T ret=value;operator--();return ret;}//后置-- - };//template class atom_win32 + volatile T operator ++ (int) {volatile T ret=value;operator++();return ret;}//后置++ + volatile T operator -- (int) {volatile T ret=value;operator--();return ret;}//后置-- + };//template class atom_win32 - //64位版 - template class atom_win64 - { - volatile T value; + //64位版 + template class atom_win64 + { + volatile T value; - public: + public: - atom_win64(){value=0;} - atom_win64(const volatile T new_value){operator=(new_value);} + atom_win64(){value=0;} + atom_win64(const volatile T new_value){operator=(new_value);} - T operator *= (const volatile T v) {return operator=(value *=v);} - T operator /= (const volatile T v) {return operator=(value /=v);} - T operator %= (const volatile T v) {return operator=(value %=v);} - T operator >>= (const volatile T v) {return operator=(value>>=v);} - T operator <<= (const volatile T v) {return operator=(value<<=v);} + T operator *= (const volatile T v) {return operator=(value *=v);} + T operator /= (const volatile T v) {return operator=(value /=v);} + T operator %= (const volatile T v) {return operator=(value %=v);} + T operator >>= (const volatile T v) {return operator=(value>>=v);} + T operator <<= (const volatile T v) {return operator=(value<<=v);} - T operator ! ()const {return !value;} - T operator ~ ()const {return ~value;} + T operator ! ()const {return !value;} + T operator ~ ()const {return ~value;} - operator T () {return value;} - operator const T ()const {return value;} - bool operator ! () {return !value;} + operator T () {return value;} + operator const T ()const {return value;} + bool operator ! () {return !value;} - bool operator == (const volatile T v)const {return value==v;} - bool operator != (const volatile T v)const {return value!=v;} + bool operator == (const volatile T v)const {return value==v;} + bool operator != (const volatile T v)const {return value!=v;} - T operator = (const volatile T nv) {return InterlockedExchange64((LONG64 *)&value, (LONG64)nv); } - T operator ++ () {return InterlockedIncrement64((LONG64 *)&value); } - T operator -- () {return InterlockedDecrement64((LONG64 *)&value); } - T operator += (const volatile T av) {return InterlockedExchangeAdd64((LONG64 *)&value, av); } - T operator -= (const volatile T av) {return InterlockedExchangeAdd64((LONG64 *)&value, -av); } + T operator = (const volatile T nv) {return InterlockedExchange64((LONG64 *)&value, (LONG64)nv); } + T operator ++ () {return InterlockedIncrement64((LONG64 *)&value); } + T operator -- () {return InterlockedDecrement64((LONG64 *)&value); } + T operator += (const volatile T av) {return InterlockedExchangeAdd64((LONG64 *)&value, av); } + T operator -= (const volatile T av) {return InterlockedExchangeAdd64((LONG64 *)&value, -av); } - volatile T operator &= (const volatile T v) {return InterlockedAnd64((LONG64 *)&value, v); } - volatile T operator |= (const volatile T v) {return InterlockedOr64((LONG64 *)&value, v); } - volatile T operator ^= (const volatile T v) {return InterlockedXor64((LONG64 *)&value, v); } + volatile T operator &= (const volatile T v) {return InterlockedAnd64((LONG64 *)&value, v); } + volatile T operator |= (const volatile T v) {return InterlockedOr64((LONG64 *)&value, v); } + volatile T operator ^= (const volatile T v) {return InterlockedXor64((LONG64 *)&value, v); } - volatile T operator ++ (int) {volatile T ret=value;operator++();return ret;}//后置++ - volatile T operator -- (int) {volatile T ret=value;operator--();return ret;}//后置-- - };//template class atom_win64 + volatile T operator ++ (int) {volatile T ret=value;operator++();return ret;}//后置++ + volatile T operator -- (int) {volatile T ret=value;operator--();return ret;}//后置-- + };//template class atom_win64 }//namespace hgl #endif//HGL_THREAD_ATOMIC_WINDOWS_INCLUDE diff --git a/inc/hgl/type/BaseString.h b/inc/hgl/type/BaseString.h index de893056..cc542829 100644 --- a/inc/hgl/type/BaseString.h +++ b/inc/hgl/type/BaseString.h @@ -11,7 +11,7 @@ namespace hgl /** * 字符串基类 */ - template class BaseString ///字符串基类 + template class BaseString ///字符串基类 { protected: @@ -19,7 +19,7 @@ namespace hgl using InstClass =StringInstance; using SharedClass =SharedPtr; - SharedClass data; ///<字符串数据实例 + SharedClass data; ///<字符串数据实例 public: @@ -30,7 +30,7 @@ namespace hgl Set(str); } - explicit BaseString(const T); + explicit BaseString(const T); static BaseString charOf(const T &ch) { @@ -132,21 +132,21 @@ namespace hgl return(data.valid()?data->GetBeginChar():0); } - const T GetEndChar()const ///<取得当前字符串最后一个字符 + const T GetEndChar()const ///<取得当前字符串最后一个字符 { - // if(!this)return(0); + // if(!this)return(0); return(data.valid()?data->GetEndChar():0); } - const int Length()const ///<当前字符串长度 + const int Length()const ///<当前字符串长度 { - // if(!this)return(0); + // if(!this)return(0); return(data.valid()?data->GetLength():0); } - const bool IsEmpty()const ///<当前字符串是否空的 + const bool IsEmpty()const ///<当前字符串是否空的 { - // if(!this)return(true); + // if(!this)return(true); return(data.valid()?data->GetLength()<=0:true); } @@ -155,7 +155,7 @@ namespace hgl */ T *c_str()const { - // if(!this)return(nullptr); + // if(!this)return(nullptr); return(data.valid()?data->c_str():nullptr); } @@ -164,7 +164,7 @@ namespace hgl */ T *strchr(T ch)const { - // if(!this)return(nullptr); + // if(!this)return(nullptr); if(!data.valid())return(nullptr); const int result=FindChar(ch); @@ -179,7 +179,7 @@ namespace hgl */ T *strrchr(T ch)const { - // if(!this)return(nullptr); + // if(!this)return(nullptr); if(!data.valid())return(nullptr); const int result=FindRightChar(ch); @@ -197,7 +197,7 @@ namespace hgl */ void Set(const T *str,int len=-1,bool one_instance=false) { - if(!str||!*str||!len) //len=-1为自检测,为0不处理 + if(!str||!*str||!len) //len=-1为自检测,为0不处理 { Clear(); return; @@ -288,7 +288,7 @@ namespace hgl if(!data.valid()) return(false); - if(data.only()) //自己独有 + if(data.only()) //自己独有 return(true); data=data->CreateCopy(); @@ -624,25 +624,25 @@ namespace hgl } public: - bool ToBool(bool &result)const ///<将本类中的字符串转换成布尔数值并返回 + bool ToBool(bool &result)const ///<将本类中的字符串转换成布尔数值并返回 { return data.valid()?stob(data->c_str(),result):false; } template - bool ToInt(I &result)const ///<将本类中的字符串转换成整型数值并返回 + bool ToInt(I &result)const ///<将本类中的字符串转换成整型数值并返回 { return data.valid()?etof(data->c_str(),result):false; } template - bool ToUint(U &result)const ///<将本类中的字符串转换成整型数值并返回 + bool ToUint(U &result)const ///<将本类中的字符串转换成整型数值并返回 { return data.valid()?etof(data->c_str(),result):false; } template - bool ToFloat(F &result)const ///<将本类中的字符串转换成浮点数值并返回 + bool ToFloat(F &result)const ///<将本类中的字符串转换成浮点数值并返回 { return data.valid()?etof(data->c_str(),result):false; } @@ -651,7 +651,7 @@ namespace hgl * 将当前字符串全部转为小写 * @return 转换后的当前字符串 */ - SelfClass &LowerCase() ///<将本类中的字母全部转为小写 + SelfClass &LowerCase() ///<将本类中的字母全部转为小写 { if(data.valid()&&Unlink()) tolower(data->c_str()); @@ -663,7 +663,7 @@ namespace hgl * 将当前字符串全部转为小写 * @return 转换后的字符串 */ - SelfClass ToLowerCase()const ///<将本类中的字母全部转为小写 + SelfClass ToLowerCase()const ///<将本类中的字母全部转为小写 { if(!data.valid()) return SelfClass(); @@ -675,7 +675,7 @@ namespace hgl * 将当前字符串全部转为大写 * @return 转换后的当前字符串 */ - SelfClass &UpperCase() ///<将本类中的字母全部转为大写 + SelfClass &UpperCase() ///<将本类中的字母全部转为大写 { if(data.valid()&&Unlink()) toupper(data->c_str()); @@ -687,7 +687,7 @@ namespace hgl * 将当前字符串全部转换为大写 * @return 转换后的字符串 */ - SelfClass ToUpperCase()const ///<将本类中的字母全部转为大写 + SelfClass ToUpperCase()const ///<将本类中的字母全部转为大写 { if(!data.valid()) return SelfClass(); @@ -746,16 +746,16 @@ namespace hgl public: - bool TrimLeft(){return StrConv(trimleft);} ///<删除字符串前端的空格、换行等不可视字符串 - bool TrimRight(){return StrConv(trimright);} ///<删除字符串后端的空格、换行等不可视字符串 - bool Trim(){return StrConv(trim);} ///<删除字符串两端的空格、换行等不可视字符串 + bool TrimLeft(){return StrConv(trimleft);} ///<删除字符串前端的空格、换行等不可视字符串 + bool TrimRight(){return StrConv(trimright);} ///<删除字符串后端的空格、换行等不可视字符串 + bool Trim(){return StrConv(trim);} ///<删除字符串两端的空格、换行等不可视字符串 - bool TrimLeft(int n){return Delete(0,n);} ///<删除字符串前端的指定个字符 - bool TrimRight(int n){return Unlink()?data->TrimRight(n):false;} ///<删除字符串后端的指定个字符 + bool TrimLeft(int n){return Delete(0,n);} ///<删除字符串前端的指定个字符 + bool TrimRight(int n){return Unlink()?data->TrimRight(n):false;} ///<删除字符串后端的指定个字符 - bool ClipLeft(int n){return Unlink()?data->ClipLeft(n):false;} ///<截取字符串前端的指定个字符,等同TrimRight(lengths-n)) - bool ClipRight(int n){return Delete(0,Length()-n);} ///<截取字符串后端的指定个字符,等同TrimLeft(length-n) - bool Clip(int pos,int num) ///<从指定位置删除指定个字符 + bool ClipLeft(int n){return Unlink()?data->ClipLeft(n):false;} ///<截取字符串前端的指定个字符,等同TrimRight(lengths-n)) + bool ClipRight(int n){return Delete(0,Length()-n);} ///<截取字符串后端的指定个字符,等同TrimLeft(length-n) + bool Clip(int pos,int num) ///<从指定位置删除指定个字符 { if(!Unlink()) return(false); @@ -769,7 +769,7 @@ namespace hgl * @param n 字符数量,-1表示全部 * @return 截取后的字符串 */ - SelfClass SubString(int start,int n=-1) const ///<取字符串指定段的字符 + SelfClass SubString(int start,int n=-1) const ///<取字符串指定段的字符 { if(n==0) return SelfClass(); @@ -789,7 +789,7 @@ namespace hgl * @param n 字符数量 * @return 成否成功 */ - bool SubString(SelfClass &sc,int start,int n) const ///<取字符串指定段的字符 + bool SubString(SelfClass &sc,int start,int n) const ///<取字符串指定段的字符 { if(Length()Resize(n); } - int StatChar(const T ch)const{return data.valid()?StatChar(data->c_str(),ch):-1;} ///<统计字符串中某个字符的个数 - int StatLine()const{return data.valid()?StatLine(data->c_str()):-1;} ///<统计字符串行数 + int StatChar(const T ch)const{return data.valid()?StatChar(data->c_str(),ch):-1;} ///<统计字符串中某个字符的个数 + int StatLine()const{return data.valid()?StatLine(data->c_str()):-1;} ///<统计字符串行数 - int FindChar(int pos,const T ch)const ///<返回当前字符串中指定字符开始的索引(从左至右) + int FindChar(int pos,const T ch)const ///<返回当前字符串中指定字符开始的索引(从左至右) { if(!data.valid()) return(-1); @@ -846,9 +846,9 @@ namespace hgl return(-1); } - int FindChar(const T ch)const{return FindChar(0,ch);} ///<返回当前字符串中指定字符开始的索引(从左至右) + int FindChar(const T ch)const{return FindChar(0,ch);} ///<返回当前字符串中指定字符开始的索引(从左至右) - int FindRightChar(const T ch)const ///<返回当前字符串中指定字符开始的索引(从右至左) + int FindRightChar(const T ch)const ///<返回当前字符串中指定字符开始的索引(从右至左) { if(!data.valid()) return(-1); @@ -861,7 +861,7 @@ namespace hgl return(-1); } - int FindRightChar(const BaseString &ch)const ///<返回当前字符串中指定字符开始的索引(从右至左) + int FindRightChar(const BaseString &ch)const ///<返回当前字符串中指定字符开始的索引(从右至左) { if(!data.valid()) return(-1); @@ -917,7 +917,7 @@ namespace hgl * @return 指定子串所在的偏移 * @return -1 出错 */ - int FindString(const SelfClass &str,int start=0)const ///<返回当前字符串中指定子串开始的索引 + int FindString(const SelfClass &str,int start=0)const ///<返回当前字符串中指定子串开始的索引 { if(!data.valid()) return(-1); @@ -942,7 +942,7 @@ namespace hgl * @return 总计清除的个数 * @return -1 出错 */ - int ClearString(const SelfClass &sub) ///<清除当前字符串中指定子串 + int ClearString(const SelfClass &sub) ///<清除当前字符串中指定子串 { if(!Unlink()) return(-1); @@ -992,7 +992,7 @@ namespace hgl * @return 总计替换个数 * @return <0 出错 */ - int Replace(const T tch,const T sch) ///<替换字符 + int Replace(const T tch,const T sch) ///<替换字符 { if(!Unlink()) return(-1); @@ -1000,7 +1000,7 @@ namespace hgl return replace(data->c_str(),tch,sch); } - public: //操作符重载 + public: //操作符重载 operator const InstClass &() { @@ -1015,16 +1015,16 @@ namespace hgl const static T zero_char=0; - return zero_char; //this is error + return zero_char; //this is error } - operator T *() {return c_str();} - operator const T *()const {return c_str();} + operator T *() {return c_str();} + operator const T *()const {return c_str();} - SelfClass &operator = (const T *str ){Set(str);return(*this);} - SelfClass &operator = (const SelfClass &str){Set(str);return(*this);} - SelfClass &operator += (const SelfClass &str){Strcat(str);return(*this);} - SelfClass &operator << (const SelfClass &str){return(operator+=(str));} + SelfClass &operator = (const T *str ){Set(str);return(*this);} + SelfClass &operator = (const SelfClass &str){Set(str);return(*this);} + SelfClass &operator += (const SelfClass &str){Strcat(str);return(*this);} + SelfClass &operator << (const SelfClass &str){return(operator+=(str));} static SelfClass ComboString(const T *str1,int len1,const T *str2,int len2) { @@ -1045,26 +1045,26 @@ namespace hgl T *ms=new T[new_len+1]; - memcpy(ms, str1,len1*sizeof(T)); - memcpy(ms+len1, str2,len2*sizeof(T)); + memcpy(ms, str1,len1*sizeof(T)); + memcpy(ms+len1, str2,len2*sizeof(T)); ms[new_len]=0; return(SelfClass(ms,new_len,true)); } - SelfClass operator + (const SelfClass &str) const + SelfClass operator + (const SelfClass &str) const { - if(str.Length()<=0) //如果对方为空 + if(str.Length()<=0) //如果对方为空 return(*this); - if(!data.valid()) //如果我方为空 + if(!data.valid()) //如果我方为空 return(str); return ComboString(data->c_str(),data->GetLength(),str.c_str(),str.Length()); } - SelfClass operator + (const T ch) const + SelfClass operator + (const T ch) const { if(!data.valid()) return(SelfClass(ch)); @@ -1072,7 +1072,7 @@ namespace hgl return ComboString(data->c_str(),data->GetLength(),&ch,1); } - SelfClass operator + (const T *str) const + SelfClass operator + (const T *str) const { if(!data.valid()) return(SelfClass(str)); @@ -1080,20 +1080,20 @@ namespace hgl return ComboString(data->c_str(),data->GetLength(),str,strlen(str)); } - #define BASE_STRING_NUMBER_OPERATOR_ADD(type,func) SelfClass operator + (const type &num) const \ - { \ - SharedPtr vstr=func(new T[8*sizeof(type)],8*sizeof(type),num); \ + #define BASE_STRING_NUMBER_OPERATOR_ADD(type,func) SelfClass operator + (const type &num) const \ + { \ + SharedPtr vstr=func(new T[8*sizeof(type)],8*sizeof(type),num); \ \ - return operator+(vstr->data); \ + return operator+(vstr->data); \ } - BASE_STRING_NUMBER_OPERATOR_ADD(int, itos); - BASE_STRING_NUMBER_OPERATOR_ADD(uint, utos); - BASE_STRING_NUMBER_OPERATOR_ADD(int64, itos); - BASE_STRING_NUMBER_OPERATOR_ADD(uint64, utos); + BASE_STRING_NUMBER_OPERATOR_ADD(int, itos); + BASE_STRING_NUMBER_OPERATOR_ADD(uint, utos); + BASE_STRING_NUMBER_OPERATOR_ADD(int64, itos); + BASE_STRING_NUMBER_OPERATOR_ADD(uint64, utos); - BASE_STRING_NUMBER_OPERATOR_ADD(float, ftos); - BASE_STRING_NUMBER_OPERATOR_ADD(double, dtos); + BASE_STRING_NUMBER_OPERATOR_ADD(float, ftos); + BASE_STRING_NUMBER_OPERATOR_ADD(double, dtos); #undef BASE_STRING_NUMBER_OPERATOR_ADD diff --git a/inc/hgl/type/Color.h b/inc/hgl/type/Color.h index f97b3340..79d139e8 100644 --- a/inc/hgl/type/Color.h +++ b/inc/hgl/type/Color.h @@ -1,244 +1,244 @@ -#ifndef HGL_COLOR_TABLE_INCLUDE -#define HGL_COLOR_TABLE_INCLUDE +#ifndef HGL_COLOR_TABLE_INCLUDE +#define HGL_COLOR_TABLE_INCLUDE #include namespace hgl { - /** - * 颜色数据定义 - */ - struct COLOR_DEF ///颜色数据定义 - { - int red,green,blue; ///<三原色 - int yum; ///<亮度 + /** + * 颜色数据定义 + */ + struct COLOR_DEF ///颜色数据定义 + { + int red,green,blue; ///<三原色 + int yum; ///<亮度 - float r,g,b; ///<三原色浮点值 + float r,g,b; ///<三原色浮点值 float y; ///<亮度浮点值 char eng_name[32]; ///<英文名称 - u16char chs_name[16]; ///<中文名称 - }; + u16char chs_name[16]; ///<中文名称 + }; - /** - * 颜色枚举 - */ - enum COLOR_ENUM ///颜色枚举 - { - ceNone, + /** + * 颜色枚举 + */ + enum COLOR_ENUM ///颜色枚举 + { + ceNone, - ceAliceBlue, ///<艾利斯兰 + ceAliceBlue, ///<艾利斯兰 ceAndroidGreen, ///<安卓绿 - ceAntiqueWhite, ///<古董白 + ceAntiqueWhite, ///<古董白 ceAppleGreen, ///<苹果绿 - ceAqua, ///<浅绿色 - ceAquaMarine, ///<碧绿色 + ceAqua, ///<浅绿色 + ceAquaMarine, ///<碧绿色 - ceArdenRed, ///<雅顿红(注:商业使用需获得Elizabeth Arden公司授权) + ceArdenRed, ///<雅顿红(注:商业使用需获得Elizabeth Arden公司授权) - ceAzure, ///<天蓝色 + ceAzure, ///<天蓝色 ceBananaMania, ///<香蕉黄(芯) ceBananaYellow, ///<香蕉黄(皮) - ceBeige, ///<米色 - ceBisque, ///<桔黄色 - ceBlack, ///<黑色 - ceBlanchedAlmond, ///<白杏色 - ceBlue, ///<蓝色 - ceBlueViolet, ///<紫罗兰色 - ceBrown, ///<褐色 - ceBurlyWood, ///<实木色 - ceCadetBlue, ///<军兰色 + ceBeige, ///<米色 + ceBisque, ///<桔黄色 + ceBlack, ///<黑色 + ceBlanchedAlmond, ///<白杏色 + ceBlue, ///<蓝色 + ceBlueViolet, ///<紫罗兰色 + ceBrown, ///<褐色 + ceBurlyWood, ///<实木色 + ceCadetBlue, ///<军兰色 ceCaribbeanGreen, ///<加勒比海绿 - ceChartreuse, ///<黄绿色 + ceChartreuse, ///<黄绿色 ceCherryBlossomPink, ///<樱桃花粉 - ceChocolate, ///<巧克力色 - ceCoral, ///<珊瑚色 - ceCornflowerBlue, ///<菊花兰 - ceCornsilk, ///<米绸色 - ceCrimson, ///<暗深红 - ceCyan, ///<青色 - ceDarkBlue, ///<暗蓝色 - ceDarkCyan, ///<暗青色 - ceDarkGoldenrod, ///<暗金黄 - ceDarkGray, ///<暗灰色 - ceDarkGreen, ///<暗绿色 - ceDarkGrey, ///<暗白色 - ceDarkKhaki, ///<暗黄褐色 - ceDarkMagenta, ///<暗洋红 - ceDarkOliveGreen, ///<暗橄榄绿 - ceDarkOrange, ///<暗桔黄 - ceDarkOrchid, ///<暗紫色 - ceDarkRed, ///<暗红色 - ceDarkSalmon, ///<暗肉色 - ceDarkSeaGreen, ///<暗海兰 - ceDarkSlateBlue, ///<暗灰兰 - ceDarkSlateGray, ///<墨绿色 - ceDarkSlateGrey, ///<暗灰绿 - ceDarkTurquoise, ///<暗宝石绿 - ceDarkViolet, ///<暗紫罗兰 - ceDeepPink, ///<深粉红 - ceDeepSkyBlue, ///<深天蓝 - ceDimGray, ///<暗灰色 - ceDimGrey, ///<暗灰白 - ceDodgerBlue, ///<闪兰色 - ceFireBrick, ///<火砖色 - ceFloralWhite, ///<花白色 - ceForestGreen, ///<森林绿 + ceChocolate, ///<巧克力色 + ceCoral, ///<珊瑚色 + ceCornflowerBlue, ///<菊花兰 + ceCornsilk, ///<米绸色 + ceCrimson, ///<暗深红 + ceCyan, ///<青色 + ceDarkBlue, ///<暗蓝色 + ceDarkCyan, ///<暗青色 + ceDarkGoldenrod, ///<暗金黄 + ceDarkGray, ///<暗灰色 + ceDarkGreen, ///<暗绿色 + ceDarkGrey, ///<暗白色 + ceDarkKhaki, ///<暗黄褐色 + ceDarkMagenta, ///<暗洋红 + ceDarkOliveGreen, ///<暗橄榄绿 + ceDarkOrange, ///<暗桔黄 + ceDarkOrchid, ///<暗紫色 + ceDarkRed, ///<暗红色 + ceDarkSalmon, ///<暗肉色 + ceDarkSeaGreen, ///<暗海兰 + ceDarkSlateBlue, ///<暗灰兰 + ceDarkSlateGray, ///<墨绿色 + ceDarkSlateGrey, ///<暗灰绿 + ceDarkTurquoise, ///<暗宝石绿 + ceDarkViolet, ///<暗紫罗兰 + ceDeepPink, ///<深粉红 + ceDeepSkyBlue, ///<深天蓝 + ceDimGray, ///<暗灰色 + ceDimGrey, ///<暗灰白 + ceDodgerBlue, ///<闪兰色 + ceFireBrick, ///<火砖色 + ceFloralWhite, ///<花白色 + ceForestGreen, ///<森林绿 ceFrenchBeige, ///<法国米色 ceFrenchBlue, ///<法国兰 ceFrenchLilac, ///<法国丁香色 - ceFuchsia, ///<紫红色 - ceGainsboro, ///<淡灰色 - ceGhostWhite, ///<幽灵白 - ceGold, ///<金色 - ceGoldenrod, ///<金麒麟色 + ceFuchsia, ///<紫红色 + ceGainsboro, ///<淡灰色 + ceGhostWhite, ///<幽灵白 + ceGold, ///<金色 + ceGoldenrod, ///<金麒麟色 ceGoldenYellow, ///<金黄 - ceGray, ///<灰色 - ceGreen, ///<绿色 - ceGreenYellow, ///<蓝绿色 - ceGrey, ///<灰白色 + ceGray, ///<灰色 + ceGreen, ///<绿色 + ceGreenYellow, ///<蓝绿色 + ceGrey, ///<灰白色 ceHollywoodCerise, ///<好莱坞樱桃红 - ceHoneydew, ///<蜜色 - ceHotPink, ///<火热粉 + ceHoneydew, ///<蜜色 + ceHotPink, ///<火热粉 ceHunterGreen, ///<猎人绿 ceIndianGreen, ///<印度绿 - ceIndianRed, ///<印度红 + ceIndianRed, ///<印度红 ceIndianYellow, ///<印度黄 - ceIndigo, ///<靛青色 - ceIvory, ///<象牙白 - ceKhaki, ///<黄褐色 - ceLavender, ///<淡紫色 - ceLavenderBlush, ///<淡紫红 - ceLawnGreen, ///<草绿色 + ceIndigo, ///<靛青色 + ceIvory, ///<象牙白 + ceKhaki, ///<黄褐色 + ceLavender, ///<淡紫色 + ceLavenderBlush, ///<淡紫红 + ceLawnGreen, ///<草绿色 ceLemon, ///<柠檬色 ceLemonYellow, ///<柠檬黄 - ceLemonChiffon, ///<柠檬绸 - ceLightBlue, ///<亮蓝色 - ceLightCoral, ///<亮珊瑚色 - ceLightCyan, ///<亮青色 - ceLightGoldenrodYellow, ///<亮金黄 - ceLightGray, ///<亮灰色 - ceLightGreen, ///<亮绿色 - ceLightGrey, ///<亮灰白 - ceLightPink, ///<亮粉红 - ceLightSalmon, ///<亮肉色 - ceLightSeaGreen, ///<亮海蓝 - ceLightSkyBlue, ///<亮天蓝 - ceLightSlateGray, ///<亮蓝灰 - ceLightSlateGrey, ///<亮蓝白 - ceLightSteelBlue, ///<亮钢兰 - ceLightYellow, ///<亮黄色 - ceLime, ///<酸橙色 - ceLimeGreen, ///<橙绿色 - ceLinen, ///<亚麻色 + ceLemonChiffon, ///<柠檬绸 + ceLightBlue, ///<亮蓝色 + ceLightCoral, ///<亮珊瑚色 + ceLightCyan, ///<亮青色 + ceLightGoldenrodYellow, ///<亮金黄 + ceLightGray, ///<亮灰色 + ceLightGreen, ///<亮绿色 + ceLightGrey, ///<亮灰白 + ceLightPink, ///<亮粉红 + ceLightSalmon, ///<亮肉色 + ceLightSeaGreen, ///<亮海蓝 + ceLightSkyBlue, ///<亮天蓝 + ceLightSlateGray, ///<亮蓝灰 + ceLightSlateGrey, ///<亮蓝白 + ceLightSteelBlue, ///<亮钢兰 + ceLightYellow, ///<亮黄色 + ceLime, ///<酸橙色 + ceLimeGreen, ///<橙绿色 + ceLinen, ///<亚麻色 ceLion, ///<獅子棕 - ceMagenta, ///<红紫色 - ceMaroon, ///<粟色 - ceMediumAquamarine, ///<间绿色 - ceMediumBlue, ///<间兰色 - ceMediumOrchid, ///<间淡紫 - ceMediumPurple, ///<间紫色 - ceMediumSeaGreen, ///<间海蓝 - ceMediumSlateBlue, ///<间暗蓝 - ceMediumSpringGreen, ///<间春绿 - ceMediumTurquoise, ///<间绿宝石 - ceMediumVioletRed, ///<间紫罗兰 - ceMidNightBlue, ///<中灰蓝 + ceMagenta, ///<红紫色 + ceMaroon, ///<粟色 + ceMediumAquamarine, ///<间绿色 + ceMediumBlue, ///<间兰色 + ceMediumOrchid, ///<间淡紫 + ceMediumPurple, ///<间紫色 + ceMediumSeaGreen, ///<间海蓝 + ceMediumSlateBlue, ///<间暗蓝 + ceMediumSpringGreen, ///<间春绿 + ceMediumTurquoise, ///<间绿宝石 + ceMediumVioletRed, ///<间紫罗兰 + ceMidNightBlue, ///<中灰蓝 ceMint, ///<薄荷色 - ceMintCream, ///<薄荷霜 + ceMintCream, ///<薄荷霜 ceMintGreen, ///<薄荷绿 - ceMistyRose, ///<浅玫瑰 - ceMoccasin, ///<鹿皮色 + ceMistyRose, ///<浅玫瑰 + ceMoccasin, ///<鹿皮色 - ceMozillaBlue, /// namespace hgl { - /** - * r,g,b三原色类 - */ - class Color3f ///RGB三原色类 - { - void Clamp(); + /** + * r,g,b三原色类 + */ + class Color3f ///RGB三原色类 + { + void Clamp(); - public: + public: - float r,g,b; ///1)One();else{r=g=b=v;}} ///<设置颜色 + void SetLum(float v){if(v<0)Zero();else if(v>1)One();else{r=g=b=v;}} ///<设置颜色 - void To(float,float,float,float); ///<转换到另一颜色 - void To(const float *rgb,float v){To(*rgb,*(rgb+1),*(rgb+2),v);} ///<转换到另一颜色 - void To(Color3f &c,float v){To(c.r,c.g,c.b,v);} ///<转换到另一颜色 + void To(float,float,float,float); ///<转换到另一颜色 + void To(const float *rgb,float v){To(*rgb,*(rgb+1),*(rgb+2),v);} ///<转换到另一颜色 + void To(Color3f &c,float v){To(c.r,c.g,c.b,v);} ///<转换到另一颜色 - void Black(){r=0,g=0,b=0;} ///<黑色 - void White(){r=1,g=1,b=1;} ///<白色 + void Black(){r=0,g=0,b=0;} ///<黑色 + void White(){r=1,g=1,b=1;} ///<白色 - void Red(){r=1,g=0,b=0;} ///<红色 - void Green(){r=0,g=1,b=0;} ///<绿色 - void Blue(){r=0,g=0,b=1;} ///<蓝色 + void Red(){r=1,g=0,b=0;} ///<红色 + void Green(){r=0,g=1,b=0;} ///<绿色 + void Blue(){r=0,g=0,b=1;} ///<蓝色 - void Yellow(){r=1,g=1,b=0;} ///<黄色 - void Purple(){r=1,g=0,b=1;} ///<紫色 + void Yellow(){r=1,g=1,b=0;} ///<黄色 + void Purple(){r=1,g=0,b=1;} ///<紫色 - void Grey(float v){r=v,g=v,b=v;Clamp();} ///<灰色 - void Grey(float,float,float); ///<指彩色变成灰色 - void Grey(); ///<将当前色彩变成灰色 + void Grey(float v){r=v,g=v,b=v;Clamp();} ///<灰色 + void Grey(float,float,float); ///<指彩色变成灰色 + void Grey(); ///<将当前色彩变成灰色 - //操作符重载 - void operator = (const float *v){r=*v++;g=*v++;b=*v;} - void operator = (COLOR_ENUM ce){Use(ce);} + //操作符重载 + void operator = (const float *v){r=*v++;g=*v++;b=*v;} + void operator = (COLOR_ENUM ce){Use(ce);} - bool operator == (const Color3f &); - bool operator != (const Color3f &); + bool operator == (const Color3f &); + bool operator != (const Color3f &); - void operator += (const Color3f &v){r+=v.r;g+=v.g;b+=v.b;Clamp();} - void operator -= (const Color3f &v){r-=v.r;g-=v.g;b-=v.b;Clamp();} - void operator *= (const Color3f &v){r*=v.r;g*=v.g;b*=v.b;Clamp();} - void operator /= (const Color3f &v){r/=v.r;g/=v.g;b/=v.b;Clamp();} + void operator += (const Color3f &v){r+=v.r;g+=v.g;b+=v.b;Clamp();} + void operator -= (const Color3f &v){r-=v.r;g-=v.g;b-=v.b;Clamp();} + void operator *= (const Color3f &v){r*=v.r;g*=v.g;b*=v.b;Clamp();} + void operator /= (const Color3f &v){r/=v.r;g/=v.g;b/=v.b;Clamp();} - void operator *= (float v){r*=v;g*=v;b*=v;Clamp();} - void operator /= (float v){r/=v;g/=v;b/=v;Clamp();} + void operator *= (float v){r*=v;g*=v;b*=v;Clamp();} + void operator /= (float v){r/=v;g/=v;b/=v;Clamp();} - Color3f operator + (const Color3f &v){return(Color3f(r+v.r,g+v.g,b+v.b));} - Color3f operator - (const Color3f &v){return(Color3f(r-v.r,g-v.g,b-v.b));} - Color3f operator * (const Color3f &v){return(Color3f(r*v.r,g*v.g,b*v.b));} - Color3f operator / (const Color3f &v){return(Color3f(r/v.r,g/v.g,b/v.b));} + Color3f operator + (const Color3f &v){return(Color3f(r+v.r,g+v.g,b+v.b));} + Color3f operator - (const Color3f &v){return(Color3f(r-v.r,g-v.g,b-v.b));} + Color3f operator * (const Color3f &v){return(Color3f(r*v.r,g*v.g,b*v.b));} + Color3f operator / (const Color3f &v){return(Color3f(r/v.r,g/v.g,b/v.b));} - Color3f operator * (float v){return(Color3f(r*v,g*v,b*v));} - Color3f operator / (float v){return(Color3f(r/v,g/v,b/v));} + Color3f operator * (float v){return(Color3f(r*v,g*v,b*v));} + Color3f operator / (float v){return(Color3f(r/v,g/v,b/v));} - operator float *() const {return((float *)this);} //使得本类可以直接当做float *使用 - operator const float *() const {return((const float *)this);} //使得本类可以直接当做const float *使用 - };//class Color3f + operator float *() const {return((float *)this);} //使得本类可以直接当做float *使用 + operator const float *() const {return((const float *)this);} //使得本类可以直接当做const float *使用 + };//class Color3f - const Color3f BlackColor3f(0,0,0); - const Color3f WhiteColor3f(1,1,1); - const Color3f RedColor3f(1,1,0); - const Color3f GreenColor3f(0,1,0); - const Color3f BlueColor3f(0,0,1); - const Color3f YellowColor3f(1,1,0); + const Color3f BlackColor3f(0,0,0); + const Color3f WhiteColor3f(1,1,1); + const Color3f RedColor3f(1,1,0); + const Color3f GreenColor3f(0,1,0); + const Color3f BlueColor3f(0,0,1); + const Color3f YellowColor3f(1,1,0); - #define DEF_RGB_U8_TO_COLOR3F(r,g,b) Color3f(float(r)/255.0f,float(g)/255.0f,float(b)/255.0f) + #define DEF_RGB_U8_TO_COLOR3F(r,g,b) Color3f(float(r)/255.0f,float(g)/255.0f,float(b)/255.0f) }//namespace hgl #endif//HGL_COLOR_3_FLOAT_INCLUDE diff --git a/inc/hgl/type/Color4f.h b/inc/hgl/type/Color4f.h index 1fa8d09a..2882f98c 100644 --- a/inc/hgl/type/Color4f.h +++ b/inc/hgl/type/Color4f.h @@ -4,97 +4,97 @@ #include namespace hgl { - #define HGL_FLOAT_TO_U32(c1,c2,c3,c4) uint32( \ - (uint8(c1 * 255) << 24 ) | \ - (uint8(c2 * 255) << 16 ) | \ - (uint8(c3 * 255) << 8 ) | \ - (uint8(c4 * 255) ) ) + #define HGL_FLOAT_TO_U32(c1,c2,c3,c4) uint32( \ + (uint8(c1 * 255) << 24 ) | \ + (uint8(c2 * 255) << 16 ) | \ + (uint8(c3 * 255) << 8 ) | \ + (uint8(c4 * 255) ) ) - #define HGL_FLOAT_TO_RGBA8(r,g,b,a) HGL_FLOAT_TO_U32(r,g,b,a) - #define HGL_FLOAT_TO_BGRA8(r,g,b,a) HGL_FLOAT_TO_U32(b,g,r,a) - #define HGL_FLOAT_TO_ARGB8(r,g,b,a) HGL_FLOAT_TO_U32(a,r,g,b) - #define HGL_FLOAT_TO_ABGR8(r,g,b,a) HGL_FLOAT_TO_U32(a,b,g,r) + #define HGL_FLOAT_TO_RGBA8(r,g,b,a) HGL_FLOAT_TO_U32(r,g,b,a) + #define HGL_FLOAT_TO_BGRA8(r,g,b,a) HGL_FLOAT_TO_U32(b,g,r,a) + #define HGL_FLOAT_TO_ARGB8(r,g,b,a) HGL_FLOAT_TO_U32(a,r,g,b) + #define HGL_FLOAT_TO_ABGR8(r,g,b,a) HGL_FLOAT_TO_U32(a,b,g,r) - /** - * r,g,b,a四原色类 - */ - class Color4f ///RGBA四原色类 - { - void Clamp(); + /** + * r,g,b,a四原色类 + */ + class Color4f ///RGBA四原色类 + { + void Clamp(); - public: + public: - float r,g,b,a; ///1)One();else{r=v;g=v;b=v;}} ///<设置颜色 + void SetLum(float v){if(v<0)Zero();else if(v>1)One();else{r=v;g=v;b=v;}} ///<设置颜色 - void To(float,float,float,float); ///<转换到另一颜色 - void To(const Color4f &c,float v){To(c.r,c.g,c.b,v);} ///<转换到另一颜色 - void To(const Color3f &c,float v){To(c.r,c.g,c.b,v);} ///<转换到另一颜色 + void To(float,float,float,float); ///<转换到另一颜色 + void To(const Color4f &c,float v){To(c.r,c.g,c.b,v);} ///<转换到另一颜色 + void To(const Color3f &c,float v){To(c.r,c.g,c.b,v);} ///<转换到另一颜色 - uint32 ToRGBA8()const{ return HGL_FLOAT_TO_RGBA8(r, g, b, a); } ///<输出一个rgba8格式的颜色数据 - uint32 ToBGRA8()const{ return HGL_FLOAT_TO_BGRA8(r, g, b, a); } ///<输出一个bgra8格式的颜色数据 - uint32 ToARGB8()const{ return HGL_FLOAT_TO_ARGB8(r, g, b, a); } ///<输出一个argb8格式的颜色数据 - uint32 ToABGR8()const{ return HGL_FLOAT_TO_ABGR8(r, g, b, a); } ///<输出一个abgr8格式的颜色数据 + uint32 ToRGBA8()const{ return HGL_FLOAT_TO_RGBA8(r, g, b, a); } ///<输出一个rgba8格式的颜色数据 + uint32 ToBGRA8()const{ return HGL_FLOAT_TO_BGRA8(r, g, b, a); } ///<输出一个bgra8格式的颜色数据 + uint32 ToARGB8()const{ return HGL_FLOAT_TO_ARGB8(r, g, b, a); } ///<输出一个argb8格式的颜色数据 + uint32 ToABGR8()const{ return HGL_FLOAT_TO_ABGR8(r, g, b, a); } ///<输出一个abgr8格式的颜色数据 - void Grey(); ///<将当前色彩变成灰色 + void Grey(); ///<将当前色彩变成灰色 - //操作符重载 - void operator = (const float *v){r=*v++;g=*v++;b=*v++;a=*v;} - void operator = (const Color3f &v){r=v.r;g=v.g;b=v.b;a=1;} - void operator = (const Color4f &v){r=v.r;g=v.g;b=v.b;a=v.a;} - void operator = (COLOR_ENUM ce){Use(ce,a);} + //操作符重载 + void operator = (const float *v){r=*v++;g=*v++;b=*v++;a=*v;} + void operator = (const Color3f &v){r=v.r;g=v.g;b=v.b;a=1;} + void operator = (const Color4f &v){r=v.r;g=v.g;b=v.b;a=v.a;} + void operator = (COLOR_ENUM ce){Use(ce,a);} - bool operator == (const Color4f &); - bool operator != (const Color4f &); + bool operator == (const Color4f &); + bool operator != (const Color4f &); - void operator += (const Color4f &v){r+=v.r;g+=v.g;b+=v.b;a+=v.a;Clamp();} - void operator -= (const Color4f &v){r-=v.r;g-=v.g;b-=v.b;a-=v.a;Clamp();} - void operator *= (const Color4f &v){r*=v.r;g*=v.g;b*=v.b;a*=v.a;Clamp();} - void operator /= (const Color4f &v){r/=v.r;g/=v.g;b/=v.b;a/=v.a;Clamp();} + void operator += (const Color4f &v){r+=v.r;g+=v.g;b+=v.b;a+=v.a;Clamp();} + void operator -= (const Color4f &v){r-=v.r;g-=v.g;b-=v.b;a-=v.a;Clamp();} + void operator *= (const Color4f &v){r*=v.r;g*=v.g;b*=v.b;a*=v.a;Clamp();} + void operator /= (const Color4f &v){r/=v.r;g/=v.g;b/=v.b;a/=v.a;Clamp();} - void operator += (const Color3f &v){r+=v.r;g+=v.g;b+=v.b;Clamp();} - void operator -= (const Color3f &v){r-=v.r;g-=v.g;b-=v.b;Clamp();} - void operator *= (const Color3f &v){r*=v.r;g*=v.g;b*=v.b;Clamp();} - void operator /= (const Color3f &v){r/=v.r;g/=v.g;b/=v.b;Clamp();} + void operator += (const Color3f &v){r+=v.r;g+=v.g;b+=v.b;Clamp();} + void operator -= (const Color3f &v){r-=v.r;g-=v.g;b-=v.b;Clamp();} + void operator *= (const Color3f &v){r*=v.r;g*=v.g;b*=v.b;Clamp();} + void operator /= (const Color3f &v){r/=v.r;g/=v.g;b/=v.b;Clamp();} - void operator *= (float v){r*=v;g*=v;b*=v;a*=v;Clamp();} - void operator /= (float v){r/=v;g/=v;b/=v;a/=v;Clamp();} + void operator *= (float v){r*=v;g*=v;b*=v;a*=v;Clamp();} + void operator /= (float v){r/=v;g/=v;b/=v;a/=v;Clamp();} - Color4f operator + (const Color4f &v){return(Color4f(r+v.r,g+v.g,b+v.b,a+v.a));} - Color4f operator - (const Color4f &v){return(Color4f(r-v.r,g-v.g,b-v.b,a-v.a));} - Color4f operator * (const Color4f &v){return(Color4f(r*v.r,g*v.g,b*v.b,a*v.a));} - Color4f operator / (const Color4f &v){return(Color4f(r/v.r,g/v.g,b/v.b,a/v.a));} + Color4f operator + (const Color4f &v){return(Color4f(r+v.r,g+v.g,b+v.b,a+v.a));} + Color4f operator - (const Color4f &v){return(Color4f(r-v.r,g-v.g,b-v.b,a-v.a));} + Color4f operator * (const Color4f &v){return(Color4f(r*v.r,g*v.g,b*v.b,a*v.a));} + Color4f operator / (const Color4f &v){return(Color4f(r/v.r,g/v.g,b/v.b,a/v.a));} - Color4f operator * (float v){return(Color4f(r*v,g*v,b*v,a*v));} - Color4f operator / (float v){return(Color4f(r/v,g/v,b/v,a/v));} + Color4f operator * (float v){return(Color4f(r*v,g*v,b*v,a*v));} + Color4f operator / (float v){return(Color4f(r/v,g/v,b/v,a/v));} - operator float *() const {return((float *)this);} //使得本类可以直接当做float *使用 - operator const float *() const {return((const float *)this);} //使得本类可以直接当做const float *使用 - };//class Color4f + operator float *() const {return((float *)this);} //使得本类可以直接当做float *使用 + operator const float *() const {return((const float *)this);} //使得本类可以直接当做const float *使用 + };//class Color4f - #define DEF_RGBA_U8_TO_COLOR4F(r,g,b,a) Color4f(float(r)/255.0f,float(g)/255.0f,float(b)/255.0f,float(a)/255.0f) + #define DEF_RGBA_U8_TO_COLOR4F(r,g,b,a) Color4f(float(r)/255.0f,float(g)/255.0f,float(b)/255.0f,float(a)/255.0f) }//namespace hgl #endif//HGL_COLOR_4_FLOAT_INCLUDE diff --git a/inc/hgl/type/DateTime.h b/inc/hgl/type/DateTime.h index 1cf26d39..d72de602 100644 --- a/inc/hgl/type/DateTime.h +++ b/inc/hgl/type/DateTime.h @@ -6,173 +6,173 @@ namespace hgl { - namespace io - { - class DataInputStream; - class DataOutputStream; - }//namespace io + namespace io + { + class DataInputStream; + class DataOutputStream; + }//namespace io - /** - * 时间类,这个类用来保存和计算时间。 - * - * 当您对Hours,Minutes,Seconds中任何一个值进行加减时,其它值都会自动计算。如:11:30这个值,使用Minutes+=55。会自动计算出55分钟后的时间,值为12:25 - */ - class Time ///时间类 - { - int32 gmt_off; ///<当前时区与UTC时间的差值 + /** + * 时间类,这个类用来保存和计算时间。 + * + * 当您对Hours,Minutes,Seconds中任何一个值进行加减时,其它值都会自动计算。如:11:30这个值,使用Minutes+=55。会自动计算出55分钟后的时间,值为12:25 + */ + class Time ///时间类 + { + int32 gmt_off; ///<当前时区与UTC时间的差值 - int8 hours; - int8 minutes; - int8 seconds; - int32 micro_seconds; - int8 week_day; ///<为了取这个星期过了多少秒用的 + int8 hours; + int8 minutes; + int8 seconds; + int32 micro_seconds; + int8 week_day; ///<为了取这个星期过了多少秒用的 - public: + public: - int GetGMT ()const{return gmt_off;} ///<取得当前时间与UTC时间的时差 + int GetGMT ()const{return gmt_off;} ///<取得当前时间与UTC时间的时差 - int GetHour ()const{return hours;} ///<时 - int GetMinute ()const{return minutes;} ///<分 - int GetSecond ()const{return seconds;} ///<秒 - int GetMicroSecond ()const{return micro_seconds;} ///<微秒(百万分之一秒) + int GetHour ()const{return hours;} ///<时 + int GetMinute ()const{return minutes;} ///<分 + int GetSecond ()const{return seconds;} ///<秒 + int GetMicroSecond ()const{return micro_seconds;} ///<微秒(百万分之一秒) - int GetDaySeconds ()const{return (hours*HGL_TIME_ONE_HOUR)+(minutes*HGL_TIME_ONE_MINUTE)+seconds;} ///<今天过了多少秒 + int GetDaySeconds ()const{return (hours*HGL_TIME_ONE_HOUR)+(minutes*HGL_TIME_ONE_MINUTE)+seconds;} ///<今天过了多少秒 - int GetWeekSeconds ()const ///<这个星期过了多少秒 - { - int week = week_day; - if(week_day > 0) - --week; + int GetWeekSeconds ()const ///<这个星期过了多少秒 + { + int week = week_day; + if(week_day > 0) + --week; - if(week_day == 0) - week=(HGL_DAY_ONE_WEEK-1); + if(week_day == 0) + week=(HGL_DAY_ONE_WEEK-1); - return ((week*HGL_HOUR_ONE_DAY+hours)*HGL_TIME_ONE_HOUR)+(minutes*HGL_TIME_ONE_MINUTE)+seconds; - } + return ((week*HGL_HOUR_ONE_DAY+hours)*HGL_TIME_ONE_HOUR)+(minutes*HGL_TIME_ONE_MINUTE)+seconds; + } -// int GetPastWeekSeconds()const{return int(GetDoubleTime()-GetWeekSeconds());} ///<过去那些年到这周一0点过了多少秒 +// int GetPastWeekSeconds()const{return int(GetDoubleTime()-GetWeekSeconds());} ///<过去那些年到这周一0点过了多少秒 - void SetGMT(int go){gmt_off=go;} - void SetHour(int); - void SetMinute(int); - void SetSecond(int); - void SetMicroSecond(int); + void SetGMT(int go){gmt_off=go;} + void SetHour(int); + void SetMinute(int); + void SetSecond(int); + void SetMicroSecond(int); - public: + public: - Time(); - Time(const double); - Time(int,int,int,int); - Time(const Time &); + Time(); + Time(const double); + Time(int,int,int,int); + Time(const Time &); - void Clear() - { - gmt_off=0; - hours=0; - minutes=0; - seconds=0; - micro_seconds=0; - week_day=0; - } + void Clear() + { + gmt_off=0; + hours=0; + minutes=0; + seconds=0; + micro_seconds=0; + week_day=0; + } - void Set(int h,int m=0,int s=0,int ms=0,int wd=0); + void Set(int h,int m=0,int s=0,int ms=0,int wd=0); - Time &operator = (const Time &); + Time &operator = (const Time &); - const int Comp(const Time &)const; + const int Comp(const Time &)const; - CompOperator(const Time &,Comp); + CompOperator(const Time &,Comp); - void Sync(const double=0); ///<和系统时间同步 + void Sync(const double=0); ///<和系统时间同步 - bool SaveToStream(io::DataOutputStream *) const; - bool LoadFromStream(io::DataInputStream *); - };//class Time + bool SaveToStream(io::DataOutputStream *) const; + bool LoadFromStream(io::DataInputStream *); + };//class Time - /** - * 日期类,这个类用来保存和计算日期。带有闰年的处理 - * - * 当您对Year,Month,Day中任意一个值进行修改时,其它值都会自动跟着计算。如1981-4-17,如果使用Day+=400,会自动计算出400天之后的日期,结果是1982-5-21 - */ - class Date ///日期类 - { - int32 year; - int8 month; - int8 day; + /** + * 日期类,这个类用来保存和计算日期。带有闰年的处理 + * + * 当您对Year,Month,Day中任意一个值进行修改时,其它值都会自动跟着计算。如1981-4-17,如果使用Day+=400,会自动计算出400天之后的日期,结果是1982-5-21 + */ + class Date ///日期类 + { + int32 year; + int8 month; + int8 day; - int8 week_day; - int16 year_day; + int8 week_day; + int16 year_day; - int8 max_days_per_month; + int8 max_days_per_month; - public: + public: - int GetYear ()const{return year;} ///<年 - int GetMonth ()const{return month;} ///<月 - int GetDay ()const{return day;} ///<日 - int GetMaxDays ()const{return max_days_per_month;} ///<本月天数 - int GetWeekDay ()const{return week_day;} ///<星期,0为星期天 - int GetYearDay ()const{return year_day;} ///<今天为这一年的第几天 + int GetYear ()const{return year;} ///<年 + int GetMonth ()const{return month;} ///<月 + int GetDay ()const{return day;} ///<日 + int GetMaxDays ()const{return max_days_per_month;} ///<本月天数 + int GetWeekDay ()const{return week_day;} ///<星期,0为星期天 + int GetYearDay ()const{return year_day;} ///<今天为这一年的第几天 - void SetYear (int y){year=y;} - void SetMonth (int); - void SetDay (int); + void SetYear (int y){year=y;} + void SetMonth (int); + void SetDay (int); - public: + public: - Date(); - Date(const double); - Date(int,int,int); - Date(const Date &); + Date(); + Date(const double); + Date(int,int,int); + Date(const Date &); - void Clear() - { - year=0; - month=0; - day=0; + void Clear() + { + year=0; + month=0; + day=0; - week_day=0; - year_day=0; + week_day=0; + year_day=0; - max_days_per_month=0; - } + max_days_per_month=0; + } - void Set(int y,int m,int d,int wd=0,int yd=0); + void Set(int y,int m,int d,int wd=0,int yd=0); - Date &operator = (const Date &); + Date &operator = (const Date &); - const int Comp(const Date &)const; + const int Comp(const Date &)const; - CompOperator(const Date &,Comp); + CompOperator(const Date &,Comp); - void Sync(const double=0); ///<和系统日期同步 + void Sync(const double=0); ///<和系统日期同步 - bool SaveToStream(io::DataOutputStream *) const; - bool LoadFromStream(io::DataInputStream *); - };//class Date + bool SaveToStream(io::DataOutputStream *) const; + bool LoadFromStream(io::DataInputStream *); + };//class Date - /** - * 根据当前时间,转换出Date/Time两个结构 - */ - void ToDateTime(Date &,Time &,const double cur_time=0); + /** + * 根据当前时间,转换出Date/Time两个结构 + */ + void ToDateTime(Date &,Time &,const double cur_time=0); - /** - * 根据日期和时间,转换出以秒为单位的时间值 - * @param year 年 - * @param month 月 - * @param day 日 - * @param hour 小时 - * @param minute 分 - * @param second 秒 - * @param micro_second 微秒(1/1000秒) - * @return 转换出的值 - */ - double FromDateTime(const int year,const int month,const int day, - const int hour,const int minute=0,const int second=0,const int micro_second=0,const int gmt_off=0); + /** + * 根据日期和时间,转换出以秒为单位的时间值 + * @param year 年 + * @param month 月 + * @param day 日 + * @param hour 小时 + * @param minute 分 + * @param second 秒 + * @param micro_second 微秒(1/1000秒) + * @return 转换出的值 + */ + double FromDateTime(const int year,const int month,const int day, + const int hour,const int minute=0,const int second=0,const int micro_second=0,const int gmt_off=0); - /** - * 根据Date/Time转换出以秒为单位的时间值 - */ - double FromDateTime(const Date &,const Time &); + /** + * 根据Date/Time转换出以秒为单位的时间值 + */ + double FromDateTime(const Date &,const Time &); }//namespace hgl #endif//HGL_DATETIME_INCLUDE diff --git a/inc/hgl/type/List.cpp b/inc/hgl/type/List.cpp index ee9c293b..4f192ec4 100644 --- a/inc/hgl/type/List.cpp +++ b/inc/hgl/type/List.cpp @@ -6,487 +6,487 @@ //-------------------------------------------------------------------------------------------------- namespace hgl { - template - bool List::Get(int index,T &ti)const - { - if(!items||index<0||index>=count) - return(false); + template + bool List::Get(int index,T &ti)const + { + if(!items||index<0||index>=count) + return(false); - memcpy(&ti,items+index,sizeof(T)); - return(true); - } + memcpy(&ti,items+index,sizeof(T)); + return(true); + } - template - bool List::Begin(T &ti)const - { - if(!items) - return(false); + template + bool List::Begin(T &ti)const + { + if(!items) + return(false); - memcpy(&ti,items,sizeof(T)); - return(true); - } + memcpy(&ti,items,sizeof(T)); + return(true); + } - template - bool List::End(T &ti)const - { - if(!items) - return(false); + template + bool List::End(T &ti)const + { + if(!items) + return(false); - memcpy(&ti,items+count-1,sizeof(T)); - return(true); - } + memcpy(&ti,items+count-1,sizeof(T)); + return(true); + } - template - bool List::Rand(T &ti)const - { - if(!items||count<=0) - return(false); + template + bool List::Rand(T &ti)const + { + if(!items||count<=0) + return(false); - memcpy(&ti,items+(lrand48()%count),sizeof(T)); - return(true); - } + memcpy(&ti,items+(lrand48()%count),sizeof(T)); + return(true); + } - template - void List::Set(int index,const T &val) - { - #ifdef _DEBUG - if(!items||index<0||index>=count) - { + template + void List::Set(int index,const T &val) + { + #ifdef _DEBUG + if(!items||index<0||index>=count) + { LOG_ERROR(OS_TEXT("List<>::Set(index=")+OSString(index)+OS_TEXT(",T &) error,DataCount=")+OSString(count)); - } - else - memcpy(items+index,&val,sizeof(T));//items[index]=val; - #else - if(index>=0&&index=0&&index - T *List::Add() - { - if(!items) - { - count=1; - max_count=1; + /** + * 向列表中添加一个空数据 + * @return 这个数据的指针 + */ + template + T *List::Add() + { + if(!items) + { + count=1; + max_count=1; items=hgl_aligned_malloc(1); - return items; - } - else - { - max_count=power_to_2(count+1); + return items; + } + else + { + max_count=power_to_2(count+1); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); + items=(T *)hgl_realloc(items,max_count*sizeof(T)); - ++count; - return(items+(count-1)); - } - } + ++count; + return(items+(count-1)); + } + } - /** - * 向列表中添加一个数据对象 - * @param data 要添加的数据对象 - * @return 这个数据的索引号 - */ - template - int List::Add(const T &data) - { - if(!items) - { - count=0; - max_count=1; + /** + * 向列表中添加一个数据对象 + * @param data 要添加的数据对象 + * @return 这个数据的索引号 + */ + template + int List::Add(const T &data) + { + if(!items) + { + count=0; + max_count=1; items=hgl_aligned_malloc(1); - } - else - { - max_count=power_to_2(count+1); + } + else + { + max_count=power_to_2(count+1); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); - } + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } - memcpy(items+count,&data,sizeof(T));//items[count]=data; - return(count++); - } + memcpy(items+count,&data,sizeof(T));//items[count]=data; + return(count++); + } - /** - * 重复向列表中添加一个数据对象 - * @param data 要添加的数据对象 - * @param n 要添加的数据个数 - * @return 这个数据的索引号 - * @return -1 出错 - */ - template - int List::Add(const T &data,int n) - { - if(n<=0)return(-1); + /** + * 重复向列表中添加一个数据对象 + * @param data 要添加的数据对象 + * @param n 要添加的数据个数 + * @return 这个数据的索引号 + * @return -1 出错 + */ + template + int List::Add(const T &data,int n) + { + if(n<=0)return(-1); - if(!items) - { - count=0; - max_count=power_to_2(n); + if(!items) + { + count=0; + max_count=power_to_2(n); items=hgl_aligned_malloc(max_count); - } - else - { - max_count=power_to_2(count+n); + } + else + { + max_count=power_to_2(count+n); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); - } + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } - T *p=items; - int result=count; + T *p=items; + int result=count; - for(int i=0;i - int List::Add(const T *data,int n) - { - if(!items) - { - count=0; - max_count=power_to_2(n); + /** + * 向列表中添加一批数据对象 + * @param data 要添加的数据对象 + * @param n 要添加的数据数量 + * @return 起始数据的索引号 + */ + template + int List::Add(const T *data,int n) + { + if(!items) + { + count=0; + max_count=power_to_2(n); items=hgl_aligned_malloc(max_count); - } - else - { - max_count=power_to_2(count+n); + } + else + { + max_count=power_to_2(count+n); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); - } + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } - memcpy(items+count,data,n*sizeof(T)); + memcpy(items+count,data,n*sizeof(T)); - int r=count; + int r=count; - count+=n; - return(r); - } + count+=n; + return(r); + } - /** - * 清除整个列表 - */ - template - void List::Clear() - { - count=0; - max_count=0; + /** + * 清除整个列表 + */ + template + void List::Clear() + { + count=0; + max_count=0; - if(items) - { - hgl_free(items); - items=0; - } - } + if(items) + { + hgl_free(items); + items=0; + } + } - /** - * 清除整个列表,但不清除缓冲区 - */ - template - void List::ClearData() - { - count=0; - } + /** + * 清除整个列表,但不清除缓冲区 + */ + template + void List::ClearData() + { + count=0; + } - template - void List::DeleteClear() - { - if(count) - { - T *p=items; + template + void List::DeleteClear() + { + if(count) + { + T *p=items; - while(count--) - { - delete *p; - ++p; - } + while(count--) + { + delete *p; + ++p; + } - count=0; - } - } + count=0; + } + } - /** - * 在列表中查找指定的数据项 - * @param data 要查找的数据项 - * @return 数据在列表中的位置 - */ - template - int List::Find(const T &data)const - { - int n=count; + /** + * 在列表中查找指定的数据项 + * @param data 要查找的数据项 + * @return 数据在列表中的位置 + */ + template + int List::Find(const T &data)const + { + int n=count; - while(n--) -// if(items[n]==data)return(n); - if(memcmp(items+n,&data,sizeof(T))==0) - return(n); + while(n--) +// if(items[n]==data)return(n); + if(memcmp(items+n,&data,sizeof(T))==0) + return(n); - return(-1); - } + return(-1); + } - /** - * 删除列表中的指定项,删除后将最后一个数据移到被删除的位置 - * @param index 要删除的数据项的索引值 - * @return 是否成功 - */ - template - bool List::Delete(int index) - { - if(count>0&&index>=0&&index + bool List::Delete(int index) + { + if(count>0&&index>=0&&index - bool List::DeleteMove(int index) - { - if(count>0&&index>=0&&index + bool List::DeleteMove(int index) + { + if(count>0&&index>=0&&index - bool List::Delete(int start,int number) - { - if(start>=count)return(false); + /** + * 删除列表中的指定项 + * @param start 要删除的数据项的索引起始值 + * @param number 要删除的数据项数量 + * @return 是否成功 + */ + template + bool List::Delete(int start,int number) + { + if(start>=count)return(false); - if(start<0) - { - number+=start; - start=0; - } + if(start<0) + { + number+=start; + start=0; + } - if(start+number>count) - number=count-start; + if(start+number>count) + number=count-start; - if(number<=0)return(false); + if(number<=0)return(false); - count-=number; + count-=number; - if(start - bool List::DeleteByValue(const T &dat) - { - int index=Find(dat); + /** + * 删除列表中的指定项 + * @param data 要删除的数据项 + * @return 是否成功 + */ + template + bool List::DeleteByValue(const T &dat) + { + int index=Find(dat); - if(index!=-1) - { - Delete(index); - return(true); - } - else - return(false); - } + if(index!=-1) + { + Delete(index); + return(true); + } + else + return(false); + } - /** - * 删除列表中的指定项 - * @param data 要删除的数据项 - * @param n 要删除的数据个数 - */ - template - void List::DeleteByValue(const T *data,int n) - { - while(n--) - { - int index=Find(*data); + /** + * 删除列表中的指定项 + * @param data 要删除的数据项 + * @param n 要删除的数据个数 + */ + template + void List::DeleteByValue(const T *data,int n) + { + while(n--) + { + int index=Find(*data); - ++data; + ++data; - if(index!=-1) - Delete(index); - } - } + if(index!=-1) + Delete(index); + } + } - /** - * 交换列表中两个数据的位置 - * @param a 第一个数据的位置 - * @param b 第二个数据的位置 - */ - template - void List::Exchange(int a,int b) - { - //T t; - char t[sizeof(T)]; + /** + * 交换列表中两个数据的位置 + * @param a 第一个数据的位置 + * @param b 第二个数据的位置 + */ + template + void List::Exchange(int a,int b) + { + //T t; + char t[sizeof(T)]; -// t=items[a]; +// t=items[a]; -// items[a]=items[b]; -// items[b]=t; +// items[a]=items[b]; +// items[b]=t; - memcpy(&t,items+a,sizeof(T)); - memcpy(items+a,items+b,sizeof(T)); - memcpy(items+b,&t,sizeof(T)); - } + memcpy(&t,items+a,sizeof(T)); + memcpy(items+a,items+b,sizeof(T)); + memcpy(items+b,&t,sizeof(T)); + } - /** - * 在列表的指定位置中插入一个数据 - * @param index 插入数据的位置 - * @param data 要插入的数据 - */ - template - void List::Insert(int index,const T &data) - { - if(index<0)index=0; + /** + * 在列表的指定位置中插入一个数据 + * @param index 插入数据的位置 + * @param data 要插入的数据 + */ + template + void List::Insert(int index,const T &data) + { + if(index<0)index=0; - if(index(max_count); - } - else - { + } + else + { max_count=power_to_2(count+1); - items=(T *)hgl_realloc(items,max_count*sizeof(T)); - } + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } - memmove(items+index+1,items+index,(count-index)*sizeof(T)); + memmove(items+index+1,items+index,(count-index)*sizeof(T)); - memcpy(items+index,&data,sizeof(T));//items[index]=data; + memcpy(items+index,&data,sizeof(T));//items[index]=data; - ++count; - } - else - Add(data); - } + ++count; + } + else + Add(data); + } - /** - * 移动列表中的某一项到新的位置 - * @param index 要移动的数据位置 - * @param newindex 新的位置 - */ - template - void List::Move(int index,int newindex) - { - if(index==newindex)return; - if(index<=0||index>=count)return; - if(newindex<0)newindex=0; - if(newindex>=count)newindex=count-1; + /** + * 移动列表中的某一项到新的位置 + * @param index 要移动的数据位置 + * @param newindex 新的位置 + */ + template + void List::Move(int index,int newindex) + { + if(index==newindex)return; + if(index<=0||index>=count)return; + if(newindex<0)newindex=0; + if(newindex>=count)newindex=count-1; - //T t; - char t[sizeof(T)]; + //T t; + char t[sizeof(T)]; - memcpy(&t,items+index,sizeof(T));//t=items[index]; + memcpy(&t,items+index,sizeof(T));//t=items[index]; - if(index - void List::PreMalloc(int new_count) - { - if(max_count>=new_count)return; + template + void List::PreMalloc(int new_count) + { + if(max_count>=new_count)return; - max_count=power_to_2(new_count); + max_count=power_to_2(new_count); - if(!items) + if(!items) items=hgl_aligned_malloc(max_count); - else - items=(T *)hgl_realloc(items,max_count*sizeof(T)); - } + else + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } - template - void List::SetCount(int new_count) - { - if(count==new_count)return; + template + void List::SetCount(int new_count) + { + if(count==new_count)return; - if(new_count<=0) - { - ClearData(); - return; - } + if(new_count<=0) + { + ClearData(); + return; + } - PreMalloc(new_count); + PreMalloc(new_count); - count=new_count; - } + count=new_count; + } - /** - * 复制整个列表 - * @param lt 列表 - */ - template - void List::operator = (const List <) - { - if(lt.count<=0) - { - count=0; - return; - } + /** + * 复制整个列表 + * @param lt 列表 + */ + template + void List::operator = (const List <) + { + if(lt.count<=0) + { + count=0; + return; + } - SetCount(lt.count); + SetCount(lt.count); - memcpy(items,lt.items,count*sizeof(T)); - } + memcpy(items,lt.items,count*sizeof(T)); + } - template - void List::operator = (const std::initializer_list &l) - { - ClearData(); + template + void List::operator = (const std::initializer_list &l) + { + ClearData(); - SetCount((int)l.size()); + SetCount((int)l.size()); - memcpy(items,l.begin(),count*sizeof(T)); - } + memcpy(items,l.begin(),count*sizeof(T)); + } }//namespace hgl #endif//HGL_LIST_CPP diff --git a/inc/hgl/type/List.h b/inc/hgl/type/List.h index 5fc22a34..3caf0105 100644 --- a/inc/hgl/type/List.h +++ b/inc/hgl/type/List.h @@ -7,69 +7,69 @@ namespace hgl { - /** - * List类用于保存数据列表。可以在列表中添加、删除、查找、访问和排序数据。 - */ + /** + * List类用于保存数据列表。可以在列表中添加、删除、查找、访问和排序数据。 + */ template class List ///列表处理类 - { - protected: + { + protected: - int count=0; - int max_count=0; - T *items=nullptr; + int count=0; + int max_count=0; + T *items=nullptr; - public: //属性 + public: //属性 - int GetCount ()const{return count;} ///<取得列表内数据数量 - virtual void SetCount (int); ///<设置列表内数据数量 - virtual void PreMalloc (int); ///<预分配指定数量的数据空间 - T * GetData ()const{return items;} ///<提供原始数据项 - int GetBytes ()const{return count*sizeof(T);} ///<取得原始数据总字节数 + int GetCount ()const{return count;} ///<取得列表内数据数量 + virtual void SetCount (int); ///<设置列表内数据数量 + virtual void PreMalloc (int); ///<预分配指定数量的数据空间 + T * GetData ()const{return items;} ///<提供原始数据项 + int GetBytes ()const{return count*sizeof(T);} ///<取得原始数据总字节数 - public: //方法 + public: //方法 List(){}; ///<本类构造函数 - List(const List <){operator=(lt);} ///<本类构造函数 - List(const std::initializer_list <){operator=(lt);} + List(const List <){operator=(lt);} ///<本类构造函数 + List(const std::initializer_list <){operator=(lt);} - virtual ~List(){Clear();} ///<本类析构函数 + virtual ~List(){Clear();} ///<本类析构函数 - virtual T * Add(); ///<添加一个空数据 - virtual int Add(const T &); ///<增加一个数据 - virtual int Add(const T &,int n); ///<重复增加一个数据 - virtual int Add(const T *,int n); ///<增加一批数据 - int Add(const List &l){return Add(l.items,l.count);} ///<增加一批数据 + virtual T * Add(); ///<添加一个空数据 + virtual int Add(const T &); ///<增加一个数据 + virtual int Add(const T &,int n); ///<重复增加一个数据 + virtual int Add(const T *,int n); ///<增加一批数据 + int Add(const List &l){return Add(l.items,l.count);} ///<增加一批数据 - virtual void Clear(); ///<清除所有数据 - virtual void ClearData(); ///<清除所有数据,但不清空缓冲区 - virtual int Find(const T &)const; ///<查找指定数据的索引 + virtual void Clear(); ///<清除所有数据 + virtual void ClearData(); ///<清除所有数据,但不清空缓冲区 + virtual int Find(const T &)const; ///<查找指定数据的索引 bool IsExist(const T &flag)const{return Find(flag)!=-1;} ///<确认数据项是否存在 - virtual bool Delete(int); ///<删除指定索引的数据 - virtual bool Delete(int,int); ///<删除指定索引的数据 - virtual bool DeleteMove(int); ///<删除指定索引的数据,将后面紧邻的数据前移 - virtual bool DeleteByValue(const T &); ///<删除一个指定数据 - virtual void DeleteByValue(const T *,int); ///<删除一批指定的数据 - virtual void Exchange(int,int); ///<根据索引交换两个数据 - virtual void Insert(int,const T &); ///<在指定索引处插入一个数据 - virtual void Move(int,int); ///<移动一个数据到移指索引处 + virtual bool Delete(int); ///<删除指定索引的数据 + virtual bool Delete(int,int); ///<删除指定索引的数据 + virtual bool DeleteMove(int); ///<删除指定索引的数据,将后面紧邻的数据前移 + virtual bool DeleteByValue(const T &); ///<删除一个指定数据 + virtual void DeleteByValue(const T *,int); ///<删除一批指定的数据 + virtual void Exchange(int,int); ///<根据索引交换两个数据 + virtual void Insert(int,const T &); ///<在指定索引处插入一个数据 + virtual void Move(int,int); ///<移动一个数据到移指索引处 - void DeleteClear(); ///<清除所有数据并全部调用delete + void DeleteClear(); ///<清除所有数据并全部调用delete - virtual void operator = (const List &); ///<操作符重载复制一个列表 - virtual void operator = (const std::initializer_list &l); + virtual void operator = (const List &); ///<操作符重载复制一个列表 + virtual void operator = (const std::initializer_list &l); - virtual void operator += (const T &obj){Add(obj);} ///<操作符重载添加一个数据 - virtual void operator << (const T &obj){Add(obj);} ///<操作符重载添加一个数据 - virtual void operator -= (const T &obj){DeleteByValue(obj);} ///<操作符重载删除一个数据 + virtual void operator += (const T &obj){Add(obj);} ///<操作符重载添加一个数据 + virtual void operator << (const T &obj){Add(obj);} ///<操作符重载添加一个数据 + virtual void operator -= (const T &obj){DeleteByValue(obj);} ///<操作符重载删除一个数据 - bool Get(int,T &)const; ///<取得指定索引处的数据 - void Set(int,const T &); ///<设置指定索引处的数据 - bool Rand(T &)const; ///<随机取得一个数据 + bool Get(int,T &)const; ///<取得指定索引处的数据 + void Set(int,const T &); ///<设置指定索引处的数据 + bool Rand(T &)const; ///<随机取得一个数据 - virtual bool Begin(T &)const; ///<取第一个数据 - virtual bool End(T &)const; ///<取最后一个数据 + virtual bool Begin(T &)const; ///<取第一个数据 + virtual bool End(T &)const; ///<取最后一个数据 - virtual void Enum(void (*enum_func)(T &)) ///<枚举所有数据成员 + virtual void Enum(void (*enum_func)(T &)) ///<枚举所有数据成员 { T *obj=items; @@ -79,77 +79,77 @@ namespace hgl ++obj; } } - };//template class List + };//template class List - template T *GetObject(const List &list,const int index) - { - T *obj; + template T *GetObject(const List &list,const int index) + { + T *obj; - if(list.Get(index,obj)) - return(obj); + if(list.Get(index,obj)) + return(obj); - return(nullptr); - } + return(nullptr); + } }//namespace hgl #include //-------------------------------------------------------------------------------------------------- namespace hgl { - /** - * 自定义对象列表处理类与标准列表处理类的区别在于它对数据生成/清除时会多调用虚拟函数Create/Delte - */ - template class CusObjectList:public List ///对象列表处理类 - { + /** + * 自定义对象列表处理类与标准列表处理类的区别在于它对数据生成/清除时会多调用虚拟函数Create/Delte + */ + template class CusObjectList:public List ///对象列表处理类 + { public: - virtual void SetCount(int); + virtual void SetCount(int); -// virtual T * CreateObject()=0; ///<创建一个数据,但不加入列表 - virtual void DeleteObject(T *)=0; ///<删除一个数据 +// virtual T * CreateObject()=0; ///<创建一个数据,但不加入列表 + virtual void DeleteObject(T *)=0; ///<删除一个数据 - public: + public: - typedef T *ItemPointer; + typedef T *ItemPointer; - public: //方法 + public: //方法 CusObjectList(){} - virtual ~CusObjectList(); + virtual ~CusObjectList(); - public: + public: -// virtual T * Append(); ///<追加一个数据 -// virtual T * Insert(int); ///<在指定索引处创建一个数据 - void Insert(int,const ItemPointer &); ///<在指定索引处插入一个数据 +// virtual T * Append(); ///<追加一个数据 +// virtual T * Insert(int); ///<在指定索引处创建一个数据 + void Insert(int,const ItemPointer &); ///<在指定索引处插入一个数据 - virtual void Clear(); ///<清除所有数据 - virtual void ClearData(); ///<清除所有数据,但不清空缓冲区 + virtual void Clear(); ///<清除所有数据 + virtual void ClearData(); ///<清除所有数据,但不清空缓冲区 - virtual bool Unlink(int index){return List::Delete(index);} ///<将指定索引处的数据与列表断开 - virtual bool UnlinkMove(int index){return List::DeleteMove(index);} ///<将指定索引处的数据与列表断开,将前移后面的数据 - virtual bool Unlink(int start,int number){return List::Delete(start,number);} ///<将指定索引处的数据与列表断开 - virtual bool UnlinkByValue(const ItemPointer &ip){return List::DeleteByValue(ip);} ///<将一个指定数据与列表断开 - virtual void UnlinkByValue(const ItemPointer *ip,int n){List::DeleteByValue(ip,n);} ///<将一批指定数据与列表断开 - virtual void UnlinkAll(){List::ClearData();} ///<断开所有数据 + virtual bool Unlink(int index){return List::Delete(index);} ///<将指定索引处的数据与列表断开 + virtual bool UnlinkMove(int index){return List::DeleteMove(index);} ///<将指定索引处的数据与列表断开,将前移后面的数据 + virtual bool Unlink(int start,int number){return List::Delete(start,number);} ///<将指定索引处的数据与列表断开 + virtual bool UnlinkByValue(const ItemPointer &ip){return List::DeleteByValue(ip);} ///<将一个指定数据与列表断开 + virtual void UnlinkByValue(const ItemPointer *ip,int n){List::DeleteByValue(ip,n);} ///<将一批指定数据与列表断开 + virtual void UnlinkAll(){List::ClearData();} ///<断开所有数据 - virtual bool Delete(int); ///<删除指定索引处的数据 - virtual bool DeleteMove(int); ///<删除指定索引处的数据 - virtual bool DeleteByValue(const ItemPointer &); ///<删除指定的一个数据 - virtual void DeleteByValue(const ItemPointer *,int); ///<删除指定的一批数据 - virtual void DeleteAll(); ///<删除所有数据 + virtual bool Delete(int); ///<删除指定索引处的数据 + virtual bool DeleteMove(int); ///<删除指定索引处的数据 + virtual bool DeleteByValue(const ItemPointer &); ///<删除指定的一个数据 + virtual void DeleteByValue(const ItemPointer *,int); ///<删除指定的一批数据 + virtual void DeleteAll(); ///<删除所有数据 - virtual ItemPointer &operator[](int n)const ///<操作符重载取得指定索引处的数据 - { - static T *null_pointer=nullptr; + virtual ItemPointer &operator[](int n)const ///<操作符重载取得指定索引处的数据 + { + static T *null_pointer=nullptr; - if(n<0||n>=this->count) - return(null_pointer); + if(n<0||n>=this->count) + return(null_pointer); - return this->items[n]; - } + return this->items[n]; + } - virtual void Enum(void (*enum_func)(T *)) ///<枚举所有数据成员 + virtual void Enum(void (*enum_func)(T *)) ///<枚举所有数据成员 { T **obj=this->items; @@ -160,26 +160,26 @@ namespace hgl ++obj; } } - };//template class CusObjectList + };//template class CusObjectList - /** - * 对象列表处理类
- * 将自定义对象列表中的Create重载为new,Delete重载为delete - */ - template class ObjectList:public CusObjectList - { - private: + /** + * 对象列表处理类
+ * 将自定义对象列表中的Create重载为new,Delete重载为delete + */ + template class ObjectList:public CusObjectList + { + private: -// virtual T * CreateObject(){return(new T);} ///<创建一个数据 - virtual void DeleteObject(T *obj){if(obj)delete obj;} ///<删除一个数据 +// virtual T * CreateObject(){return(new T);} ///<创建一个数据 + virtual void DeleteObject(T *obj){if(obj)delete obj;} ///<删除一个数据 - public: + public: - virtual ~ObjectList() - { - CusObjectList::Clear(); - } - };//class ObjectList + virtual ~ObjectList() + { + CusObjectList::Clear(); + } + };//class ObjectList }//namespace hgl #include #endif//HGL_LIST_INCLUDE diff --git a/inc/hgl/type/Map.cpp b/inc/hgl/type/Map.cpp index d3b569c2..bc963856 100644 --- a/inc/hgl/type/Map.cpp +++ b/inc/hgl/type/Map.cpp @@ -3,210 +3,210 @@ namespace hgl { - /** - * 查找数据是否存在 - * @param flag 数据标识 - * @return 数据所在索引,-1表示不存在 - */ - template - int _Map::Find(const F &flag)const - { - int left=0,right=data_list.GetCount()-1; //使用left,right而不使用min,max是为了让代码能够更好的阅读。 - int mid; + /** + * 查找数据是否存在 + * @param flag 数据标识 + * @return 数据所在索引,-1表示不存在 + */ + template + int _Map::Find(const F &flag)const + { + int left=0,right=data_list.GetCount()-1; //使用left,right而不使用min,max是为了让代码能够更好的阅读。 + int mid; - DataPair **data_array=data_list.GetData(); + DataPair **data_array=data_list.GetData(); - while(left<=right) - { - if(data_array[left ]->left==flag)return(left); - if(data_array[right]->left==flag)return(right); + while(left<=right) + { + if(data_array[left ]->left==flag)return(left); + if(data_array[right]->left==flag)return(right); - mid=(right+left)>>1; + mid=(right+left)>>1; - if(data_array[mid]->left==flag)return(mid); + if(data_array[mid]->left==flag)return(mid); - if(data_array[mid]->left>flag) - { - ++left; - right=mid-1; - } - else - { - --right; - left=mid+1; - } - } + if(data_array[mid]->left>flag) + { + ++left; + right=mid-1; + } + else + { + --right; + left=mid+1; + } + } -// LOG_PROBLEM(OS_TEXT("Map::Find,no result.")); - return(-1); - } +// LOG_PROBLEM(OS_TEXT("Map::Find,no result.")); + return(-1); + } - template - bool _Map::FindPos(const F &flag,int &pos)const - { - int left=0,right=data_list.GetCount()-1; - int mid; + template + bool _Map::FindPos(const F &flag,int &pos)const + { + int left=0,right=data_list.GetCount()-1; + int mid; - DataPair **data_array=data_list.GetData(); + DataPair **data_array=data_list.GetData(); - while(left<=right) - { - if(data_array[left ]->left>flag) - { - pos=left; - return(false); - } - else - if(data_array[left ]->left==flag) - { - pos=left; - return(true); - } + while(left<=right) + { + if(data_array[left ]->left>flag) + { + pos=left; + return(false); + } + else + if(data_array[left ]->left==flag) + { + pos=left; + return(true); + } - if(data_array[right]->leftleft==flag) - { - pos=right; - return(true); - } + if(data_array[right]->leftleft==flag) + { + pos=right; + return(true); + } - mid=(right+left)>>1; + mid=(right+left)>>1; - if(data_array[mid]->left==flag) - { - pos=mid; - return(true); - } + if(data_array[mid]->left==flag) + { + pos=mid; + return(true); + } - if(data_array[mid]->left>flag) - { - if(data_array[mid-1]->leftleft==flag) - { - pos=mid-1; - return(true); - } + if(data_array[mid]->left>flag) + { + if(data_array[mid-1]->leftleft==flag) + { + pos=mid-1; + return(true); + } - ++left; - right=mid-1; - } - else - { - if(data_array[mid+1]->left>flag) - { - pos=mid+1; - return(false); - } - else - if(data_array[mid+1]->left==flag) - { - pos=mid+1; - return(true); - } + ++left; + right=mid-1; + } + else + { + if(data_array[mid+1]->left>flag) + { + pos=mid+1; + return(false); + } + else + if(data_array[mid+1]->left==flag) + { + pos=mid+1; + return(true); + } - --right; - left=mid+1; - } - } + --right; + left=mid+1; + } + } -// LOG_PROBLEM(OS_TEXT("Map::FindPos,no result.")); - pos=-1; - return(false); - } +// LOG_PROBLEM(OS_TEXT("Map::FindPos,no result.")); + pos=-1; + return(false); + } - template - int _Map::FindByValue(const T &data)const - { - const int count=data_list.GetCount(); + template + int _Map::FindByValue(const T &data)const + { + const int count=data_list.GetCount(); - DataPair **data_array=data_list.GetData(); + DataPair **data_array=data_list.GetData(); - for(int i=0;iright==data) - return(i); + for(int i=0;iright==data) + return(i); - ++data_array; - } + ++data_array; + } - return -1; - } + return -1; + } - /** - * 添加一个数据 - * @param flag 数据标识 - * @param data 数据 + /** + * 添加一个数据 + * @param flag 数据标识 + * @param data 数据 * @return 新创建好的数据结构 - */ - template - DataPair *_Map::Add(const F &flag,const T &data) - { - DataPair *ds=data_pool.Acquire(); + */ + template + DataPair *_Map::Add(const F &flag,const T &data) + { + DataPair *ds=data_pool.Acquire(); - ds->left=flag; - ds->right=data; + ds->left=flag; + ds->right=data; - int pos; + int pos; if(FindPos(flag,pos)) return(nullptr); data_list.Insert(pos,ds); - return(ds); - } + return(ds); + } - /** - * 添加一个数据 - * @param flag 数据标识 - * @return 创建好的数据 - */ - template - T &_Map::Add(const F &flag) - { - DataPair *ds=data_pool.Acquire(); + /** + * 添加一个数据 + * @param flag 数据标识 + * @return 创建好的数据 + */ + template + T &_Map::Add(const F &flag) + { + DataPair *ds=data_pool.Acquire(); - ds->left=flag; + ds->left=flag; - int pos; + int pos; - FindPos(flag,pos); + FindPos(flag,pos); data_list.Insert(pos,ds); - return ds->right; - } + return ds->right; + } - /** - * 添加一个数据 - * @param obj 数据 - */ - template - void _Map::Add(DataPair *obj) - { - data_list.Insert(FindPos(obj->left),obj); - } + /** + * 添加一个数据 + * @param obj 数据 + */ + template + void _Map::Add(DataPair *obj) + { + data_list.Insert(FindPos(obj->left),obj); + } - /** - * 根据索引取得数据 - * @param flag 数据索引 - * @param data 数据存放处 - * @return 是否取得成功 - */ - template - bool _Map::Get(const F &flag,T &data) const - { - int index=Find(flag); + /** + * 根据索引取得数据 + * @param flag 数据索引 + * @param data 数据存放处 + * @return 是否取得成功 + */ + template + bool _Map::Get(const F &flag,T &data) const + { + int index=Find(flag); DataPair *obj=GetObject(data_list,index); @@ -215,8 +215,8 @@ namespace hgl data=obj->right; - return(true); - } + return(true); + } template bool _Map::Check(const F &key,const T &value) const @@ -231,224 +231,224 @@ namespace hgl return (value==obj->right); } - /** - * 根据序号取得数据 - * @param index 序号 - * @param f 数据索引存放处 - * @param t 数据存放处 - * @return 是否取得成功 - */ - template - bool _Map::Get(int index,F &f,T &t) const - { - if(index<0||index>=data_list.GetCount())return(false); + /** + * 根据序号取得数据 + * @param index 序号 + * @param f 数据索引存放处 + * @param t 数据存放处 + * @return 是否取得成功 + */ + template + bool _Map::Get(int index,F &f,T &t) const + { + if(index<0||index>=data_list.GetCount())return(false); - DataPair *ds=data_list[index]; + DataPair *ds=data_list[index]; - f=ds->left; - t=ds->right; + f=ds->left; + t=ds->right; - return(true); - } + return(true); + } - /** - * 根据序号取得索引 - * @param index 序号 - * @param f 数据索引存放处 - * @return 是否取得成功 - */ - template - bool _Map::GetKey(int index,F &f) - { - if(index<0||index>=data_list.GetCount())return(false); + /** + * 根据序号取得索引 + * @param index 序号 + * @param f 数据索引存放处 + * @return 是否取得成功 + */ + template + bool _Map::GetKey(int index,F &f) + { + if(index<0||index>=data_list.GetCount())return(false); - DataPair *ds=data_list[index]; + DataPair *ds=data_list[index]; - f=ds->left; + f=ds->left; - return(true); - } + return(true); + } - /** - * 根据序号取得数据 - * @param index 序号 - * @param t 数据存放处 - * @return 是否取得成功 - */ - template - bool _Map::GetValue(int index,T &t) - { - if(index<0||index>=data_list.GetCount())return(false); + /** + * 根据序号取得数据 + * @param index 序号 + * @param t 数据存放处 + * @return 是否取得成功 + */ + template + bool _Map::GetValue(int index,T &t) + { + if(index<0||index>=data_list.GetCount())return(false); - DataPair *ds=data_list[index]; + DataPair *ds=data_list[index]; - if(!ds)return(false); + if(!ds)return(false); - t=ds->right; + t=ds->right; - return(true); - } + return(true); + } - /** - * 根据序号设置数据 - * @param index 数据序号 - * @param t 数据 - */ - template - bool _Map::SetValueBySerial(int index,T &t) - { - if(index<0||index>=data_list.GetCount())return(false); + /** + * 根据序号设置数据 + * @param index 数据序号 + * @param t 数据 + */ + template + bool _Map::SetValueBySerial(int index,T &t) + { + if(index<0||index>=data_list.GetCount())return(false); - data_list[index]->right=t; + data_list[index]->right=t; - return(true); - } + return(true); + } - /** - * 将指定数据从列表中移除同时取得这个数据 - * @param flag 数据标识 - * @param data 数据存放位处 - * @return 是否成功 - */ - template - bool _Map::Delete(const F &flag,T &data) - { - int index=Find(flag); + /** + * 将指定数据从列表中移除同时取得这个数据 + * @param flag 数据标识 + * @param data 数据存放位处 + * @return 是否成功 + */ + template + bool _Map::Delete(const F &flag,T &data) + { + int index=Find(flag); - DataPair *dp=GetObject(data_list,index); + DataPair *dp=GetObject(data_list,index); if(!dp) return(false); - data=dp->right; + data=dp->right; - data_pool.Release(dp); - data_list.DeleteMove(index); + data_pool.Release(dp); + data_list.DeleteMove(index); - return(true); - } + return(true); + } - /** - * 根据索引将指定数据从列表中移除 - * @param flag 索引 - * @return 是否成功 - */ - template - bool _Map::DeleteByKey(const F &flag) - { + /** + * 根据索引将指定数据从列表中移除 + * @param flag 索引 + * @return 是否成功 + */ + template + bool _Map::DeleteByKey(const F &flag) + { return DeleteBySerial(Find(flag)); - } + } - /** - * 根据索引将指定数据从列表中批量移除 - * @param fp 索引列表 - * @param count 索引数量 - * @return 是否成功 - */ - template - int _Map::DeleteByKey(const F *fp,const int count) - { - if(!fp||count<=0)return(0); + /** + * 根据索引将指定数据从列表中批量移除 + * @param fp 索引列表 + * @param count 索引数量 + * @return 是否成功 + */ + template + int _Map::DeleteByKey(const F *fp,const int count) + { + if(!fp||count<=0)return(0); - int total=0; + int total=0; - for(int i=0;i - bool _Map::DeleteByValue(const T &data) - { + /** + * 根据数据将指定数据从列表中移除 + * @param data 数据 + * @return 是否成功 + */ + template + bool _Map::DeleteByValue(const T &data) + { return DeleteBySerial(FindByValue(data)); - } + } - /** - * 根据序号将指定数据从列表中移除 - * @param index 序号 - * @return 是否成功 - */ - template - bool _Map::DeleteBySerial(int index) - { - if(index<0 - ||index>=data_list.GetCount())return(false); + /** + * 根据序号将指定数据从列表中移除 + * @param index 序号 + * @return 是否成功 + */ + template + bool _Map::DeleteBySerial(int index) + { + if(index<0 + ||index>=data_list.GetCount())return(false); data_pool.Release(GetObject(data_list,index)); - data_list.DeleteMove(index); + data_list.DeleteMove(index); - return(true); - } + return(true); + } - /** - * 根据序号将指定数据从列表中移除 - * @param start 起始序号 - * @param number 数量 - * @return 是否成功 - */ - template - bool _Map::DeleteBySerial(int start,int number) - { - DataPair **dp=data_list.GetData()+start; + /** + * 根据序号将指定数据从列表中移除 + * @param start 起始序号 + * @param number 数量 + * @return 是否成功 + */ + template + bool _Map::DeleteBySerial(int start,int number) + { + DataPair **dp=data_list.GetData()+start; - for(int i=0;i - void _Map::Update(const F &flag,const T &data) - { - int result; + /** + * 更新一个数据,如果这个数据不存在,就添加一个新的 + * @param flag 数据标识 + * @param data 新的数据内容 + */ + template + void _Map::Update(const F &flag,const T &data) + { + int result; - if(FindPos(flag,result)) + if(FindPos(flag,result)) { DataPair *dp=GetObject(data_list,result); if(dp) dp->right=data; } - else - { - DataPair *ds=data_pool.Acquire(); + else + { + DataPair *ds=data_pool.Acquire(); - ds->left=flag; - ds->right=data; + ds->left=flag; + ds->right=data; - data_list.Insert(result,ds); - } - } + data_list.Insert(result,ds); + } + } - /** - * 更改数据,这个更改和Set不同,它要求指定标识的数据必须存在,则否就会更改失败 - * @param flag 数据标识 - * @param data 新的数据内容 - * @param return 是否更改成功 - */ - template - bool _Map::Change(const F &flag,const T &data) - { + /** + * 更改数据,这个更改和Set不同,它要求指定标识的数据必须存在,则否就会更改失败 + * @param flag 数据标识 + * @param data 新的数据内容 + * @param return 是否更改成功 + */ + template + bool _Map::Change(const F &flag,const T &data) + { DataPair *dp=GetObject(data_list,Find(flag)); if(!dp) @@ -456,32 +456,32 @@ namespace hgl dp->right=data; return(true); - } + } - /** - * 清除所有数据 - */ - template - void _Map::Clear() - { - data_pool.ClearAll(); - data_list.Clear(); - } + /** + * 清除所有数据 + */ + template + void _Map::Clear() + { + data_pool.ClearAll(); + data_list.Clear(); + } - /** - * 清除所有数据,但不释放内存 - */ - template - void _Map::ClearData() - { - data_pool.ReleaseAll(); - data_list.ClearData(); - } + /** + * 清除所有数据,但不释放内存 + */ + template + void _Map::ClearData() + { + data_pool.ReleaseAll(); + data_list.ClearData(); + } - template - void _Map::operator=(const _Map &ftd) - { - Clear(); + template + void _Map::operator=(const _Map &ftd) + { + Clear(); data_pool.ClearAll(); data_list.ClearData(); @@ -504,9 +504,9 @@ namespace hgl ++obj; } - } + } - template + template void _Map::Enum(void (*enum_func)(const F &,T)) { const int count=data_list.GetCount(); diff --git a/inc/hgl/type/Map.h b/inc/hgl/type/Map.h index bbcc0628..cff4bd87 100644 --- a/inc/hgl/type/Map.h +++ b/inc/hgl/type/Map.h @@ -7,283 +7,283 @@ #include namespace hgl { - /** - * 索引数据模板 - */ - template class _Map - { - public: + /** + * 索引数据模板 + */ + template class _Map + { + public: using IDItem=DataPair; - protected: + protected: - ObjectPool data_pool; - List data_list; + ObjectPool data_pool; + List data_list; using this_calss=_Map; - public: //方法 + public: //方法 - _Map()=default; - virtual ~_Map()=default; + _Map()=default; + virtual ~_Map()=default; - const int GetCount()const{return data_list.GetCount();} ///<取得数据总量 + const int GetCount()const{return data_list.GetCount();} ///<取得数据总量 - IDItem * Add(const F &,const T &); ///<添加一个数据,数果索引已存在,返回nullptr - T & Add(const F &); ///<添加一个数据 - void Add(IDItem *); ///<添加一个数据 - bool FindPos(const F &,int &)const; ///<查找数据如果插入后,会所在的位置,返回是否存在这个数据 - int FindPos(const F &flag)const{int pos;FindPos(flag,pos);return(pos);} ///<查找数据如果插入后,会所在的位置 - int Find(const F &)const; ///<查找数据是否存在,返回-1表示数据不存在 - int FindByValue(const T &)const; ///<查找数据是否存在,返回-1表示数据不存在 - bool KeyExist(const F &key)const{return(Find(key)!=-1);} ///<确认这个数据是否存在 - bool ValueExist(const T &value)const{return(FindByValue(value)!=-1);} ///<确认这个数据是否存在 + IDItem * Add(const F &,const T &); ///<添加一个数据,数果索引已存在,返回nullptr + T & Add(const F &); ///<添加一个数据 + void Add(IDItem *); ///<添加一个数据 + bool FindPos(const F &,int &)const; ///<查找数据如果插入后,会所在的位置,返回是否存在这个数据 + int FindPos(const F &flag)const{int pos;FindPos(flag,pos);return(pos);} ///<查找数据如果插入后,会所在的位置 + int Find(const F &)const; ///<查找数据是否存在,返回-1表示数据不存在 + int FindByValue(const T &)const; ///<查找数据是否存在,返回-1表示数据不存在 + bool KeyExist(const F &key)const{return(Find(key)!=-1);} ///<确认这个数据是否存在 + bool ValueExist(const T &value)const{return(FindByValue(value)!=-1);} ///<确认这个数据是否存在 bool Check(const F &key,const T &value)const; ///<确认数据是否是这个 - virtual bool Get(const F &,T &) const; ///<取得数据 - virtual bool Delete(const F &,T &); ///<将指定数据从列表中移除,并获得这个数据 - virtual bool DeleteByKey(const F &); ///<根据索引将指定数据从列表中移除 - virtual int DeleteByKey(const F *,const int); ///<根据索引将指定数据从列表中批量移除 - virtual bool DeleteByValue(const T &); ///<根据数据将指定数据从列表中移除 - virtual bool DeleteBySerial(int); ///<根据序号将指定数据从列表中移除 - virtual bool DeleteBySerial(int,int); ///<根据序号将指定数据从列表中移除 - virtual void Update(const F &,const T &); ///<更新一个数据的内容(如不存在则添加) - virtual bool Change(const F &,const T &); ///<更改一个数据的内容(如不存在则更改失效) - virtual void Clear(); ///<清除所有数据 - virtual void ClearData(); ///<清除所有数据,但不释放内存 + virtual bool Get(const F &,T &) const; ///<取得数据 + virtual bool Delete(const F &,T &); ///<将指定数据从列表中移除,并获得这个数据 + virtual bool DeleteByKey(const F &); ///<根据索引将指定数据从列表中移除 + virtual int DeleteByKey(const F *,const int); ///<根据索引将指定数据从列表中批量移除 + virtual bool DeleteByValue(const T &); ///<根据数据将指定数据从列表中移除 + virtual bool DeleteBySerial(int); ///<根据序号将指定数据从列表中移除 + virtual bool DeleteBySerial(int,int); ///<根据序号将指定数据从列表中移除 + virtual void Update(const F &,const T &); ///<更新一个数据的内容(如不存在则添加) + virtual bool Change(const F &,const T &); ///<更改一个数据的内容(如不存在则更改失效) + virtual void Clear(); ///<清除所有数据 + virtual void ClearData(); ///<清除所有数据,但不释放内存 - List &GetList(){return data_list;} ///<取得线性列表 - IDItem ** GetDataList()const{return data_list.GetData();} ///<取得纯数据线性列表 + List &GetList(){return data_list;} ///<取得线性列表 + IDItem ** GetDataList()const{return data_list.GetData();} ///<取得纯数据线性列表 template - int GetKey(IT &il_list) ///<取得所有索引合集 - { - const int count=data_list.GetCount(); + int GetKey(IT &il_list) ///<取得所有索引合集 + { + const int count=data_list.GetCount(); - if(count<=0) - return count; + if(count<=0) + return count; - IDItem **idp=data_list.GetData(); + IDItem **idp=data_list.GetData(); - for(int i=0;ileft); - ++idp; - } + for(int i=0;ileft); + ++idp; + } - return count; - } + return count; + } - template - int GetValue(IT &il_list) ///<取得所有数值合集 - { - const int count=data_list.GetCount(); + template + int GetValue(IT &il_list) ///<取得所有数值合集 + { + const int count=data_list.GetCount(); - if(count<=0) - return count; + if(count<=0) + return count; - IDItem **idp=data_list.GetData(); + IDItem **idp=data_list.GetData(); - for(int i=0;iright); - ++idp; - } + for(int i=0;iright); + ++idp; + } - return count; - } + return count; + } - IDItem *GetItem(int n){return GetObject(data_list,n);} ///<取指定序号的数据 - bool Get(int,F &,T &) const; ///<取指定序号的数据 - bool GetKey(int,F &); ///<取指定序号的索引 - bool GetValue(int,T &); ///<取指定序号的数据 + IDItem *GetItem(int n){return GetObject(data_list,n);} ///<取指定序号的数据 + bool Get(int,F &,T &) const; ///<取指定序号的数据 + bool GetKey(int,F &); ///<取指定序号的索引 + bool GetValue(int,T &); ///<取指定序号的数据 - bool SetValueBySerial(int,T &); ///<根据序号设置数据 + bool SetValueBySerial(int,T &); ///<根据序号设置数据 - void operator=(const _Map &); ///<操作符重载,复制一个列表 + void operator=(const _Map &); ///<操作符重载,复制一个列表 void Enum(void (*enum_func)(const F &,T)); ///<枚举所有数据项 void EnumKey(void (*enum_func)(const F &)); ///<枚举所有索引 void EnumValue(void (*enum_func)(T)); ///<枚举所有数值 void EnumValue(bool (*enum_func)(T)); ///<枚举所有数值 - };//class _Map + };//class _Map - template class Map:public _Map > - { - public: + template class Map:public _Map > + { + public: - Map()=default; - virtual ~Map()=default; - };//class Map + Map()=default; + virtual ~Map()=default; + };//class Map - template T_U *GetObject(Map &list,const T_ID &id) - { - T_U *result; + template T_U *GetObject(Map &list,const T_ID &id) + { + T_U *result; - if(!list.Get(id,result)) - return(nullptr); + if(!list.Get(id,result)) + return(nullptr); - return result; - } + return result; + } - template class _MapObject:public _Map - { - protected: + template class _MapObject:public _Map + { + protected: - typedef _Map SuperClass; + typedef _Map SuperClass; - virtual void DeleteObject(const F &,T *)=0; ///<删除一个数据 - void DeleteObject(DataPair *ds) + virtual void DeleteObject(const F &,T *)=0; ///<删除一个数据 + void DeleteObject(DataPair *ds) { if(!ds)return; DeleteObject(ds->left,ds->right); } - void DeleteObject(int index) + void DeleteObject(int index) { DeleteObject(GetObject(this->data_list,index)); } - public: + public: - _MapObject()=default; - virtual ~_MapObject() - { - if(SuperClass::GetCount()>0) - { - //LOG_ERROR(u"这是一个严重的程序设计错误,会产生纯虚函数调用,请在派生类的析构函数中调用Clear函数以清除数据。"); - //LOG_ERROR(OS_TEXT("This is a serious design errors, will produce the pure virtual function call, please call in the destructor of the derived class the function to clear the data.")); - } - } + _MapObject()=default; + virtual ~_MapObject() + { + if(SuperClass::GetCount()>0) + { + //LOG_ERROR(u"这是一个严重的程序设计错误,会产生纯虚函数调用,请在派生类的析构函数中调用Clear函数以清除数据。"); + //LOG_ERROR(OS_TEXT("This is a serious design errors, will produce the pure virtual function call, please call in the destructor of the derived class the function to clear the data.")); + } + } - /** - * 断开一个数据 - * @param flag 要断开的数据标识 - * @return 是否断开成功 - */ - bool UnlinkByKey(const F &flag) - { - return UnlinkBySerial(SuperClass::Find(flag)); - } + /** + * 断开一个数据 + * @param flag 要断开的数据标识 + * @return 是否断开成功 + */ + bool UnlinkByKey(const F &flag) + { + return UnlinkBySerial(SuperClass::Find(flag)); + } - /** - * 断开一个数据 - * @param tp 要断开的数据 - * @return 是否断开成功 - */ - bool UnlinkByValue(T *tp) - { - return UnlinkBySerial(this->FindByValue(tp)); - } + /** + * 断开一个数据 + * @param tp 要断开的数据 + * @return 是否断开成功 + */ + bool UnlinkByValue(T *tp) + { + return UnlinkBySerial(this->FindByValue(tp)); + } - /** - * 断开一个数据关联 - * @param index 要断开的数据的序列号 - * @return 是否断开成功 - */ - bool UnlinkBySerial(int index) - { - if(index<0||index>=this->data_list.GetCount())return(false); + /** + * 断开一个数据关联 + * @param index 要断开的数据的序列号 + * @return 是否断开成功 + */ + bool UnlinkBySerial(int index) + { + if(index<0||index>=this->data_list.GetCount())return(false); - SuperClass::DeleteBySerial(index); + SuperClass::DeleteBySerial(index); - return(true); - } + return(true); + } - /** - * 断开所有数据关联 - */ - void UnlinkAll() - { - SuperClass::Clear(); - } + /** + * 断开所有数据关联 + */ + void UnlinkAll() + { + SuperClass::Clear(); + } - /** - * 删除一个数据 - * @param flag 要删除的数据标识 - * @return 是否删除成功 - */ - bool DeleteByKey(const F &flag) - { - return DeleteBySerial(SuperClass::Find(flag)); - } + /** + * 删除一个数据 + * @param flag 要删除的数据标识 + * @return 是否删除成功 + */ + bool DeleteByKey(const F &flag) + { + return DeleteBySerial(SuperClass::Find(flag)); + } - /** - * 删除一个数据 - * @param tp 要删除的数据 - * @return 是否删除成功 - */ - bool DeleteByValue(T *tp) - { - return DeleteBySerial(this->FindByValue(tp)); - } + /** + * 删除一个数据 + * @param tp 要删除的数据 + * @return 是否删除成功 + */ + bool DeleteByValue(T *tp) + { + return DeleteBySerial(this->FindByValue(tp)); + } - /** - * 删除一个数据 - * @param index 要删除的数据的序列号 - * @return 是否删除成功 - */ - bool DeleteBySerial(int index) - { - if(index<0||index>=this->data_list.GetCount())return(false); + /** + * 删除一个数据 + * @param index 要删除的数据的序列号 + * @return 是否删除成功 + */ + bool DeleteBySerial(int index) + { + if(index<0||index>=this->data_list.GetCount())return(false); - DeleteObject(index); - SuperClass::DeleteBySerial(index); + DeleteObject(index); + SuperClass::DeleteBySerial(index); - return(true); - } + return(true); + } - /** - * 清除所有数据 - */ - void DeleteAll() - { - int n=this->data_list.GetCount(); + /** + * 清除所有数据 + */ + void DeleteAll() + { + int n=this->data_list.GetCount(); - while(n--) - DeleteObject(n); + while(n--) + DeleteObject(n); - _Map::Clear(); - } + _Map::Clear(); + } - /** - * 更新数据,如果数据不存在就添加一个新的 - * @param flag 数据标识 - * @param data 新的数据内容 - */ - void Update(const F &flag,T *data) - { - int index=this->Find(flag); + /** + * 更新数据,如果数据不存在就添加一个新的 + * @param flag 数据标识 + * @param data 新的数据内容 + */ + void Update(const F &flag,T *data) + { + int index=this->Find(flag); - if(index!=-1) - { - DeleteObject(index); + if(index!=-1) + { + DeleteObject(index); DataPair *dp=GetObject(this->data_list,index); if(dp) dp->right=data; - } - else - { - this->Add(flag,data); - } - } + } + else + { + this->Add(flag,data); + } + } - /** - * 更改数据,这个更改和Set不同,它要求指定标识的数据必须存在,则否就会更改失败 - * @param flag 数据标识 - * @param data 新的数据内容 - * @param return 是否更改成功 - */ - bool Change(const F &flag,T *data) - { - int index=this->Find(flag); + /** + * 更改数据,这个更改和Set不同,它要求指定标识的数据必须存在,则否就会更改失败 + * @param flag 数据标识 + * @param data 新的数据内容 + * @param return 是否更改成功 + */ + bool Change(const F &flag,T *data) + { + int index=this->Find(flag); - if(index!=-1) - { - DeleteObject(index); + if(index!=-1) + { + DeleteObject(index); DataPair *dp=GetObject(this->data_list,index); @@ -291,51 +291,51 @@ namespace hgl return(false); dp->right=data; - return(true); - } - else - return(false); - } + return(true); + } + else + return(false); + } - void Clear(){DeleteAll();} - };//class _MapObject + void Clear(){DeleteAll();} + };//class _MapObject - template class CusMapObject:public _MapObject - { - protected: + template class CusMapObject:public _MapObject + { + protected: - //virtual T * CreateObject(const F &){return(new T);} ///<创建一个数据 - virtual void DeleteObject(const F &,T *obj){delete obj;} ///<删除一个数据 + //virtual T * CreateObject(const F &){return(new T);} ///<创建一个数据 + virtual void DeleteObject(const F &,T *obj){delete obj;} ///<删除一个数据 - public: + public: - CusMapObject()=default; - virtual ~CusMapObject() - { - _MapObject::Clear(); - } - };//class CusMapObject + CusMapObject()=default; + virtual ~CusMapObject() + { + _MapObject::Clear(); + } + };//class CusMapObject - template class MapObject:public CusMapObject > - { - public: + template class MapObject:public CusMapObject > + { + public: - MapObject()=default; - virtual ~MapObject() - { - CusMapObject >::Clear(); - } + MapObject()=default; + virtual ~MapObject() + { + CusMapObject >::Clear(); + } - T *operator[](const F &index)const - { + T *operator[](const F &index)const + { auto *obj=GetObject(this->data_list,this->Find(index)); if(obj) return obj->right; else return nullptr; - }; - };//class MapObject + }; + };//class MapObject }//namespace hgl #include #endif//HGL_MAP_INCLUDE diff --git a/inc/hgl/type/ObjectList.cpp b/inc/hgl/type/ObjectList.cpp index 96f4b6bc..f670c510 100644 --- a/inc/hgl/type/ObjectList.cpp +++ b/inc/hgl/type/ObjectList.cpp @@ -4,212 +4,212 @@ #include namespace hgl { - /** - * 对象列表析构函数,会调用DeleteAll函数 - */ - template - CusObjectList::~CusObjectList() - { - Clear(); - } + /** + * 对象列表析构函数,会调用DeleteAll函数 + */ + template + CusObjectList::~CusObjectList() + { + Clear(); + } -// /** -// * 生成一个对象,并返回它的指针 -// */ -// template -// T *CusObjectList::Append() -// { -// if(!this->items) -// { -// this->max_count=1; -// this->items=(T **)hgl_aligned_malloc(1); -// } -// else -// { -// if(this->count>=this->max_count) -// this->max_count<<=1; +// /** +// * 生成一个对象,并返回它的指针 +// */ +// template +// T *CusObjectList::Append() +// { +// if(!this->items) +// { +// this->max_count=1; +// this->items=(T **)hgl_aligned_malloc(1); +// } +// else +// { +// if(this->count>=this->max_count) +// this->max_count<<=1; // -// this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); -// } +// this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); +// } // -// return(this->items[this->count++]=CreateObject()); -// } +// return(this->items[this->count++]=CreateObject()); +// } -// /** -// * 在指定位置插入一个对象 -// */ -// template -// T *CusObjectList::Insert(int index) -// { -// if(index<0)index=0; +// /** +// * 在指定位置插入一个对象 +// */ +// template +// T *CusObjectList::Insert(int index) +// { +// if(index<0)index=0; // -// if(indexcount) -// { -// if(this->count>=this->max_count) -// this->max_count<<=1; +// if(indexcount) +// { +// if(this->count>=this->max_count) +// this->max_count<<=1; // -// this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); +// this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); // -// memmove(this->items+index+1,this->items+index,(this->count-index)*sizeof(T *)); +// memmove(this->items+index+1,this->items+index,(this->count-index)*sizeof(T *)); // -// this->count++; +// this->count++; // -// return(this->items[index]=CreateObject()); -// } +// return(this->items[index]=CreateObject()); +// } // -// return(Append()); -// } +// return(Append()); +// } - /** - * 在指定位置插入一个对象 - */ - template - void CusObjectList::Insert(int index,const ItemPointer &obj) - { - List::Insert(index,obj); - } + /** + * 在指定位置插入一个对象 + */ + template + void CusObjectList::Insert(int index,const ItemPointer &obj) + { + List::Insert(index,obj); + } - /** - * 清除所有对象,作用和DeleteAll一样 - */ - template - void CusObjectList::Clear() - { - DeleteAll(); - List::Clear(); - } + /** + * 清除所有对象,作用和DeleteAll一样 + */ + template + void CusObjectList::Clear() + { + DeleteAll(); + List::Clear(); + } - /** - * 清除所有对象,但不释放内存 - */ - template - void CusObjectList::ClearData() - { - DeleteAll(); - List::ClearData(); - } + /** + * 清除所有对象,但不释放内存 + */ + template + void CusObjectList::ClearData() + { + DeleteAll(); + List::ClearData(); + } - /** - * 删除列表中的指定项 - * - * 这个函数在删除指定对象时,附加使用delete方法 - * @param index 要删除的对象的索引值 - * @return 是否成功 - */ - template - bool CusObjectList::Delete(int index) - { - if(index>=0&&indexcount) - { - DeleteObject(this->items[index]); + /** + * 删除列表中的指定项 + * + * 这个函数在删除指定对象时,附加使用delete方法 + * @param index 要删除的对象的索引值 + * @return 是否成功 + */ + template + bool CusObjectList::Delete(int index) + { + if(index>=0&&indexcount) + { + DeleteObject(this->items[index]); - this->count--; + this->count--; - if(indexcount) - memcpy(this->items+index,this->items+this->count,sizeof(T *)); + if(indexcount) + memcpy(this->items+index,this->items+this->count,sizeof(T *)); - return(true); - } - else - return(false); - } + return(true); + } + else + return(false); + } - /** - * 删除列表中的指定项 - * - * 这个函数在删除指定对象时,附加使用delete方法 - * @param index 要删除的对象的索引值 - * @return 是否成功 - */ - template - bool CusObjectList::DeleteMove(int index) - { - if(index>=0&&indexcount) - { - DeleteObject(this->items[index]); + /** + * 删除列表中的指定项 + * + * 这个函数在删除指定对象时,附加使用delete方法 + * @param index 要删除的对象的索引值 + * @return 是否成功 + */ + template + bool CusObjectList::DeleteMove(int index) + { + if(index>=0&&indexcount) + { + DeleteObject(this->items[index]); - this->count--; + this->count--; - if(indexcount) - memmove(this->items+index,this->items+index+1,(this->count-index)*sizeof(T *)); + if(indexcount) + memmove(this->items+index,this->items+index+1,(this->count-index)*sizeof(T *)); - return(true); - } - else - return(false); - } + return(true); + } + else + return(false); + } - /** - * 删除列表中的指定项 - * - * 这个函数在删除指定对象时,附加使用delete方法 - * @param obj 要删除的对象 - * @return 是否成功 - */ - template - bool CusObjectList::DeleteByValue(const ItemPointer &obj) - { - int n=this->count; + /** + * 删除列表中的指定项 + * + * 这个函数在删除指定对象时,附加使用delete方法 + * @param obj 要删除的对象 + * @return 是否成功 + */ + template + bool CusObjectList::DeleteByValue(const ItemPointer &obj) + { + int n=this->count; - while(n--) - { - if(this->items[n]==obj) - { - DeleteObject(this->items[n]); + while(n--) + { + if(this->items[n]==obj) + { + DeleteObject(this->items[n]); - this->count--; + this->count--; - if(ncount) - memmove(this->items+n,this->items+n+1,(this->count-n)*sizeof(T *)); + if(ncount) + memmove(this->items+n,this->items+n+1,(this->count-n)*sizeof(T *)); - return(true); - } - } + return(true); + } + } - return(false); - } + return(false); + } - /** - * 将一批对象从列表中删除 - * @param obj 要删除的对象 - * @param n 要删除的对象个数 - */ - template - void CusObjectList::DeleteByValue(const ItemPointer *obj,int n) - { - while(n--) - { - int index=List::Find(*obj); + /** + * 将一批对象从列表中删除 + * @param obj 要删除的对象 + * @param n 要删除的对象个数 + */ + template + void CusObjectList::DeleteByValue(const ItemPointer *obj,int n) + { + while(n--) + { + int index=List::Find(*obj); - obj++; + obj++; - if(index!=-1) - Delete(index); - } - } - /** - * 删除整个列表中的所有对象 - * - * 这个函数在删除每一个对象时,都会使用一次delete - */ - template - void CusObjectList::DeleteAll() - { - if(this->count) - { - int n=this->count; + if(index!=-1) + Delete(index); + } + } + /** + * 删除整个列表中的所有对象 + * + * 这个函数在删除每一个对象时,都会使用一次delete + */ + template + void CusObjectList::DeleteAll() + { + if(this->count) + { + int n=this->count; - while(n--) - DeleteObject(this->items[n]); + while(n--) + DeleteObject(this->items[n]); - this->count=0; - } - } + this->count=0; + } + } template void CusObjectList::SetCount(int new_count) { - if(this->count==new_count)return; + if(this->count==new_count)return; if(new_count<=0) { @@ -217,33 +217,33 @@ namespace hgl } else { - this->max_count=power_to_2(new_count); + this->max_count=power_to_2(new_count); - if(this->items) + if(this->items) { - if(new_count>this->count) + if(new_count>this->count) { - this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); + this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); -// for(;this->countcount++) -// this->items[this->count]=CreateObject(); +// for(;this->countcount++) +// this->items[this->count]=CreateObject(); } else { - while(this->count-->new_count) - DeleteObject(this->items[this->count]); + while(this->count-->new_count) + DeleteObject(this->items[this->count]); - this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); + this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); } } // else // { -// this->items=(T **)hgl_aligned_malloc(this->max_count); +// this->items=(T **)hgl_aligned_malloc(this->max_count); // // while(new_count--) -// this->items[this->count++]=CreateObject(); +// this->items[this->count++]=CreateObject(); // } } - } + } }//namespace hgl #endif//HGL_OBJECT_LIST_CPP diff --git a/inc/hgl/type/Pair.h b/inc/hgl/type/Pair.h index 4f1e7895..13d4cfa0 100644 --- a/inc/hgl/type/Pair.h +++ b/inc/hgl/type/Pair.h @@ -6,63 +6,63 @@ namespace hgl { - /** - * 成对数据模板 - */ - template struct Pair ///成对数据模板 - { - L left; - R right; + /** + * 成对数据模板 + */ + template struct Pair ///成对数据模板 + { + L left; + R right; - using SelfClass=Pair; + using SelfClass=Pair; - public: + public: - Pair()=default; + Pair()=default; - Pair(const L &l,const R &r) - { + Pair(const L &l,const R &r) + { left=l; right=r; - } + } - Pair(const SelfClass &p) - { - operator=(p); - } + Pair(const SelfClass &p) + { + operator=(p); + } - Pair(const SelfClass *p) - { - operator=(p); - } + Pair(const SelfClass *p) + { + operator=(p); + } - virtual ~Pair()=default; + virtual ~Pair()=default; - SelfClass &operator=(const SelfClass &p) - { + SelfClass &operator=(const SelfClass &p) + { left=p.left; right=p.right; - return(*this); - } + return(*this); + } - SelfClass &operator=(const SelfClass *p) - { - if(!p)return(*this); + SelfClass &operator=(const SelfClass *p) + { + if(!p)return(*this); left=p->left; right=p->right; - return(*this); - } + return(*this); + } - void Swap(Pair &p) - { - p.left=right; - p.right=left; - } + void Swap(Pair &p) + { + p.left=right; + p.right=left; + } - //不使用CompOperator是因为某些类型无法做+-处理 + //不使用CompOperator是因为某些类型无法做+-处理 const bool operator == (const SelfClass &p)const { @@ -77,6 +77,6 @@ namespace hgl return this->right!=p.right; } - };//template struct Pair + };//template struct Pair }//namespace hgl #endif//HGL_TYPE_PAIR_INCLUDE diff --git a/inc/hgl/type/Pool.cpp b/inc/hgl/type/Pool.cpp index 58bc7bf8..3c81dea5 100644 --- a/inc/hgl/type/Pool.cpp +++ b/inc/hgl/type/Pool.cpp @@ -5,140 +5,140 @@ #include namespace hgl { - template - void Pool::PreMalloc(int num) - { - if(num<=0)return; + template + void Pool::PreMalloc(int num) + { + if(num<=0)return; - for(int i=0;ihistory_max) - history_max=count; - } + count+=num; + if(count>history_max) + history_max=count; + } - template - T Pool::Acquire() - { - T result; + template + T Pool::Acquire() + { + T result; - if(!Inactive.Pop(result)) - { - result=Create(); + if(!Inactive.Pop(result)) + { + result=Create(); - count++; + count++; - if(count>history_max) - history_max=count; - } + if(count>history_max) + history_max=count; + } - Active.Add(result); + Active.Add(result); - return(result); - } + return(result); + } - template - void Pool::Append(T value) - { - T result; + template + void Pool::Append(T value) + { + T result; - if(!Inactive.Pop(result)) - { - count++; + if(!Inactive.Pop(result)) + { + count++; - if(count>history_max) - history_max=count; - } + if(count>history_max) + history_max=count; + } - Active.Add(result); + Active.Add(result); - result=value; - } + result=value; + } - template - bool Pool::Release(T value) - { - int index=Active.Find(value); + template + bool Pool::Release(T value) + { + int index=Active.Find(value); - if(index!=-1) - { - Active.Delete(index); + if(index!=-1) + { + Active.Delete(index); - Inactive.Push(value); + Inactive.Push(value); - return(true); - } + return(true); + } - return(false); - } + return(false); + } - template - int Pool::Release(T *vl,int count) - { - int total=0; + template + int Pool::Release(T *vl,int count) + { + int total=0; - for(int i=0;i - int Pool::ReleaseAll() - { - int count=Active.GetCount(); - T *p=Active.GetData(); + template + int Pool::ReleaseAll() + { + int count=Active.GetCount(); + T *p=Active.GetData(); - for(int i=0;i - void Pool::ClearInactive() - { - T result; + template + void Pool::ClearInactive() + { + T result; - count-=Inactive.GetCount(); + count-=Inactive.GetCount(); - while(Inactive.Pop(result)) - Clear(result); - } + while(Inactive.Pop(result)) + Clear(result); + } - template - void Pool::ClearAll() - { - { //不能直接调ClearInactive - T result; + template + void Pool::ClearAll() + { + { //不能直接调ClearInactive + T result; - count-=Inactive.GetCount(); + count-=Inactive.GetCount(); - while(Inactive.Pop(result)) - Clear(result); - } + while(Inactive.Pop(result)) + Clear(result); + } - { - int n=Active.GetCount(); + { + int n=Active.GetCount(); T *p=Active.GetData(); - while(n--) + while(n--) { - Clear(*p); + Clear(*p); ++p; } - Active.Clear(); - } + Active.Clear(); + } - count=0; - } + count=0; + } }//namespace hgl #endif//HGL_POOL_CPP diff --git a/inc/hgl/type/Pool.h b/inc/hgl/type/Pool.h index 92146352..df5b7f98 100644 --- a/inc/hgl/type/Pool.h +++ b/inc/hgl/type/Pool.h @@ -8,184 +8,184 @@ #include namespace hgl { - /** - * 数据池模板用于管于两个队列,一个在用的,一个空闲的 - */ - template class Pool ///数据池 - { - protected: + /** + * 数据池模板用于管于两个队列,一个在用的,一个空闲的 + */ + template class Pool ///数据池 + { + protected: - List Active; - Queue Inactive; + List Active; + Queue Inactive; - int count; - int history_max; + int count; + int history_max; - protected: + protected: - virtual T Create()=0; ///<创建数据 - virtual void Clear(T)=0; ///<清除数据 + virtual T Create()=0; ///<创建数据 + virtual void Clear(T)=0; ///<清除数据 - public: //属性 + public: //属性 - virtual int GetActiveCount() const{return Active.GetCount();} ///<取得活动数据数量 - virtual int GetInactiveCount() const{return Inactive.GetCount();} ///<取得非活动数据数量 - virtual int GetHistoryMaxCount()const{return history_max;} ///<取得历史性最大数据数量 + virtual int GetActiveCount() const{return Active.GetCount();} ///<取得活动数据数量 + virtual int GetInactiveCount() const{return Inactive.GetCount();} ///<取得非活动数据数量 + virtual int GetHistoryMaxCount()const{return history_max;} ///<取得历史性最大数据数量 - public: + public: - Pool(){count=0;history_max=0;} - virtual ~Pool()=default; + Pool(){count=0;history_max=0;} + virtual ~Pool()=default; - virtual void PreMalloc(int); ///<预分配空间 + virtual void PreMalloc(int); ///<预分配空间 - virtual T Acquire(); ///<申请一个数据 - virtual void Append(T); ///<添加一个数据 - virtual bool Release(T); ///<释放一个数据 - virtual int Release(T *,int); ///<释放一批数据 - virtual int ReleaseAll(); ///<释放所有数据 + virtual T Acquire(); ///<申请一个数据 + virtual void Append(T); ///<添加一个数据 + virtual bool Release(T); ///<释放一个数据 + virtual int Release(T *,int); ///<释放一批数据 + virtual int ReleaseAll(); ///<释放所有数据 - virtual void ClearInactive(); ///<清除所有空闲的 - virtual void ClearAll(); ///<清除所有的 - };//template class Pool + virtual void ClearInactive(); ///<清除所有空闲的 + virtual void ClearAll(); ///<清除所有的 + };//template class Pool - template class MTPool:public Pool ///多线程数据池 - { - RWLock lock; + template class MTPool:public Pool ///多线程数据池 + { + RWLock lock; - protected: + protected: - virtual T Create()=0; ///<创建数据 - virtual void Clear(T)=0; ///<清除数据 + virtual T Create()=0; ///<创建数据 + virtual void Clear(T)=0; ///<清除数据 - public: + public: - virtual int GetActiveCount() {OnlyReadLock rl(lock);return Pool::Active.GetCount(); } - virtual int GetInactiveCount() {OnlyReadLock rl(lock);return Pool::Inactive.GetCount(); } - virtual int GetHistoryMaxCount(){OnlyReadLock rl(lock);return Pool::history_max; } + virtual int GetActiveCount() {OnlyReadLock rl(lock);return Pool::Active.GetCount(); } + virtual int GetInactiveCount() {OnlyReadLock rl(lock);return Pool::Inactive.GetCount(); } + virtual int GetHistoryMaxCount(){OnlyReadLock rl(lock);return Pool::history_max; } - public: + public: - virtual ~MTPool()=default; + virtual ~MTPool()=default; - virtual T *ReadLock(int &c) ///<读列表锁定(用于访问整个列表) - { - lock.ReadLock(); + virtual T *ReadLock(int &c) ///<读列表锁定(用于访问整个列表) + { + lock.ReadLock(); - c=Pool::GetActiveCount(); - return(Pool::Active.GetData()); - } + c=Pool::GetActiveCount(); + return(Pool::Active.GetData()); + } - virtual T *WriteLock(int &c) ///<写列表锁定(用于访问整个列表) - { - lock.WriteLock(); + virtual T *WriteLock(int &c) ///<写列表锁定(用于访问整个列表) + { + lock.WriteLock(); - c=Pool::GetActiveCount(); - return(Pool::Active.GetData()); - } + c=Pool::GetActiveCount(); + return(Pool::Active.GetData()); + } - virtual T *TryReadLock(int &c) ///<尝试读列表锁定(用于访问整个列表) - { - if(!lock.TryReadLock()) - return(nullptr); + virtual T *TryReadLock(int &c) ///<尝试读列表锁定(用于访问整个列表) + { + if(!lock.TryReadLock()) + return(nullptr); - c=Pool::GetActiveCount(); - return(Pool::Active.GetData()); - } + c=Pool::GetActiveCount(); + return(Pool::Active.GetData()); + } - virtual T *TryWriteLock(int &c) ///<尝试写列表锁定(用于访问整个列表) - { - if(!lock.TryWriteLock()) - return(nullptr); + virtual T *TryWriteLock(int &c) ///<尝试写列表锁定(用于访问整个列表) + { + if(!lock.TryWriteLock()) + return(nullptr); - c=Pool::GetActiveCount(); - return(Pool::Active.GetData()); - } + c=Pool::GetActiveCount(); + return(Pool::Active.GetData()); + } - virtual void ReadLock(){lock.ReadLock();} - virtual void WriteLock(){lock.WriteLock();} - virtual bool TryReadLock(){return lock.TryReadLock();} - virtual bool TryWriteLock(){return lock.TryWriteLock();} + virtual void ReadLock(){lock.ReadLock();} + virtual void WriteLock(){lock.WriteLock();} + virtual bool TryReadLock(){return lock.TryReadLock();} + virtual bool TryWriteLock(){return lock.TryWriteLock();} - virtual void ReadUnlock(){lock.ReadUnlock();} ///<读访问解锁(用于访问整个列表) - virtual void WriteUnlock(){lock.WriteUnlock();} ///<写访问解锁(用于访问整个列表) + virtual void ReadUnlock(){lock.ReadUnlock();} ///<读访问解锁(用于访问整个列表) + virtual void WriteUnlock(){lock.WriteUnlock();} ///<写访问解锁(用于访问整个列表) - virtual T SafeAcquire() ///<安全申请一个数据 - { - T tmp; + virtual T SafeAcquire() ///<安全申请一个数据 + { + T tmp; - lock.WriteLock(); + lock.WriteLock(); tmp=Pool::Acquire(); - lock.WriteUnlock(); + lock.WriteUnlock(); - return tmp; - } + return tmp; + } - virtual void SafeAppend(T tmp) ///<安全添加一个数据 - { - lock.WriteLock(); - Pool::Append(tmp); - lock.WriteUnlock(); - } + virtual void SafeAppend(T tmp) ///<安全添加一个数据 + { + lock.WriteLock(); + Pool::Append(tmp); + lock.WriteUnlock(); + } - virtual bool SafeRelease(T tmp) ///<安全释放一个数据 - { - bool result; + virtual bool SafeRelease(T tmp) ///<安全释放一个数据 + { + bool result; - lock.WriteLock(); - result=Pool::Release(tmp); - lock.WriteUnlock(); + lock.WriteLock(); + result=Pool::Release(tmp); + lock.WriteUnlock(); - return result; - } + return result; + } - virtual int SafeRelease(T *tmp,int num) ///<安全释放一批数据 - { - int result; + virtual int SafeRelease(T *tmp,int num) ///<安全释放一批数据 + { + int result; - lock.WriteLock(); - result=Pool::Release(tmp,num); - lock.WriteUnlock(); + lock.WriteLock(); + result=Pool::Release(tmp,num); + lock.WriteUnlock(); - return result; - } + return result; + } - virtual int SafeReleaseAll() ///<安全释放所有数据 - { - int result; + virtual int SafeReleaseAll() ///<安全释放所有数据 + { + int result; - lock.WriteLock(); - result=Pool::ReleaseAll(); - lock.WriteUnlock(); + lock.WriteLock(); + result=Pool::ReleaseAll(); + lock.WriteUnlock(); - return(result); - } + return(result); + } - virtual void SafeClearInactive() ///<安全清除所有空闲数据 - { - lock.WriteLock(); - Pool::ClearInactive(); - lock.WriteUnlock(); - } + virtual void SafeClearInactive() ///<安全清除所有空闲数据 + { + lock.WriteLock(); + Pool::ClearInactive(); + lock.WriteUnlock(); + } - virtual void SafeClearAll() ///<安全清除所有的 - { - lock.WriteLock(); - Pool::ClearAll(); - lock.WriteUnlock(); - } - };//template class MTPool + virtual void SafeClearAll() ///<安全清除所有的 + { + lock.WriteLock(); + Pool::ClearAll(); + lock.WriteUnlock(); + } + };//template class MTPool - template class _ObjectPool:public Pool ///对象池 - { - virtual T *Create()=0; + template class _ObjectPool:public Pool ///对象池 + { + virtual T *Create()=0; - virtual void Clear(T *obj) { if(obj)delete obj; } + virtual void Clear(T *obj) { if(obj)delete obj; } - public: + public: - using Pool::Pool; - virtual ~_ObjectPool(){Pool::ClearAll();} + using Pool::Pool; + virtual ~_ObjectPool(){Pool::ClearAll();} virtual bool Release(T *obj) override ///<释放一个数据 { @@ -193,75 +193,75 @@ namespace hgl return Pool::Release(obj); } - };//template class _ObjectPool + };//template class _ObjectPool - template class ObjectPool:public _ObjectPool ///对象池 - { - virtual T *Create()override{return(new T());} + template class ObjectPool:public _ObjectPool ///对象池 + { + virtual T *Create()override{return(new T());} - public: + public: - using _ObjectPool::_ObjectPool; - virtual ~ObjectPool(){_ObjectPool::ClearAll();} - };//template class ObjectPool + using _ObjectPool::_ObjectPool; + virtual ~ObjectPool(){_ObjectPool::ClearAll();} + };//template class ObjectPool - template class _MTObjectPool:public MTPool ///多线程对象池 - { - virtual T *Create() override=0; + template class _MTObjectPool:public MTPool ///多线程对象池 + { + virtual T *Create() override=0; - virtual void Clear(T *obj) override { if(obj)delete obj; } + virtual void Clear(T *obj) override { if(obj)delete obj; } - public: + public: - using MTPool::MTPool; - virtual ~_MTObjectPool(){MTPool::ClearAll();} - };//template class MTObjectPool + using MTPool::MTPool; + virtual ~_MTObjectPool(){MTPool::ClearAll();} + };//template class MTObjectPool - template class MTObjectPool:public _MTObjectPool ///多线程对象池 - { - virtual T *Create() override {return(new T());} + template class MTObjectPool:public _MTObjectPool ///多线程对象池 + { + virtual T *Create() override {return(new T());} - virtual void Clear(T *obj) override { if(obj)delete obj; } + virtual void Clear(T *obj) override { if(obj)delete obj; } - public: + public: - using _MTObjectPool::_MTObjectPool; - virtual ~MTObjectPool(){_MTObjectPool::ClearAll();} - };//template class MTObjectPool + using _MTObjectPool::_MTObjectPool; + virtual ~MTObjectPool(){_MTObjectPool::ClearAll();} + };//template class MTObjectPool - template class MemBlockPool:public Pool ///内存块池 - { - int memblock_size; + template class MemBlockPool:public Pool ///内存块池 + { + int memblock_size; - T *Create() { return(new T[memblock_size]); } + T *Create() { return(new T[memblock_size]); } - void Clear(T *obj) { if(obj)delete[] obj; } + void Clear(T *obj) { if(obj)delete[] obj; } - public: + public: - MemBlockPool(int size){SetSize(size);} - virtual ~MemBlockPool(){Pool::ClearAll();} + MemBlockPool(int size){SetSize(size);} + virtual ~MemBlockPool(){Pool::ClearAll();} - void SetSize(int size){memblock_size=size;} - int GetSize(){return memblock_size;} - };//template class MemBlockPool + void SetSize(int size){memblock_size=size;} + int GetSize(){return memblock_size;} + };//template class MemBlockPool - template class MTMemBlockPool:public MTPool ///多线程内存块池 - { - int memblock_size; + template class MTMemBlockPool:public MTPool ///多线程内存块池 + { + int memblock_size; - T *Create() { return(new T[memblock_size]); } + T *Create() { return(new T[memblock_size]); } - void Clear(T *obj) { if(obj)delete[] obj; } + void Clear(T *obj) { if(obj)delete[] obj; } - public: + public: - MTMemBlockPool(int size){SetSize(size);} - virtual ~MTMemBlockPool(){MTPool::ClearAll();} + MTMemBlockPool(int size){SetSize(size);} + virtual ~MTMemBlockPool(){MTPool::ClearAll();} - void SetSize(int size){memblock_size=size;} - int GetSize(){return memblock_size;} - };//template class MTMemBlockPool + void SetSize(int size){memblock_size=size;} + int GetSize(){return memblock_size;} + };//template class MTMemBlockPool }//namespace hgl #include #endif//HGL_POOL_INCLUDE diff --git a/inc/hgl/type/Queue.cpp b/inc/hgl/type/Queue.cpp index fe23ac1a..9998e2a3 100644 --- a/inc/hgl/type/Queue.cpp +++ b/inc/hgl/type/Queue.cpp @@ -3,237 +3,237 @@ namespace hgl { - /** - * 本类构造函数 - * @param m 如果m的值不为0,则表示使用固定的队列大小。固定大小的队列会在一开始即分配好内存。 - */ - template - Queue::Queue(int m) - { - count=0; + /** + * 本类构造函数 + * @param m 如果m的值不为0,则表示使用固定的队列大小。固定大小的队列会在一开始即分配好内存。 + */ + template + Queue::Queue(int m) + { + count=0; - if(m) - { - max_count=m; + if(m) + { + max_count=m; items=hgl_aligned_malloc(max_count); - } - else max_count=0; + } + else max_count=0; - mem_count=max_count; - } + mem_count=max_count; + } - template - Queue::~Queue() - { - if(count||max_count)hgl_free(items); - } + template + Queue::~Queue() + { + if(count||max_count)hgl_free(items); + } - /** - * 修改队列的最大值 - */ - template - void Queue::SetMax(int m) - { - if(max_count||(!max_count&&count)) - items=(T *)hgl_realloc(items,m*sizeof(T)); - else + /** + * 修改队列的最大值 + */ + template + void Queue::SetMax(int m) + { + if(max_count||(!max_count&&count)) + items=(T *)hgl_realloc(items,m*sizeof(T)); + else items=hgl_aligned_malloc(m); - max_count=m; - mem_count=m; + max_count=m; + mem_count=m; - if(count>=max_count)count=max_count-1; - } + if(count>=max_count)count=max_count-1; + } - /** - * 清除队列中的所有数据 - */ - template - void Queue::Clear() - { - if(max_count==0) - if(count) - { - hgl_free(items); - mem_count=0; - } + /** + * 清除队列中的所有数据 + */ + template + void Queue::Clear() + { + if(max_count==0) + if(count) + { + hgl_free(items); + mem_count=0; + } - count=0; - } + count=0; + } - /** - * 清除队列中的所有数据,但不释放内存 - */ - template - void Queue::ClearData() - { - count=0; - } + /** + * 清除队列中的所有数据,但不释放内存 + */ + template + void Queue::ClearData() + { + count=0; + } - /** - * 访问队列中的一个数据,但不清除它 - * @param t 取出的数据保存地 - * @return 是否成功取出数据 - */ - template - bool Queue::Peek(T &t) - { - if(count) - { -// t=items[0]; - memcpy(&t,items,sizeof(T)); - return(true); - } - else - return(false); - } + /** + * 访问队列中的一个数据,但不清除它 + * @param t 取出的数据保存地 + * @return 是否成功取出数据 + */ + template + bool Queue::Peek(T &t) + { + if(count) + { +// t=items[0]; + memcpy(&t,items,sizeof(T)); + return(true); + } + else + return(false); + } - /** - * 删除队列中的指定数据 - * @param index 索引 - */ - template - bool Queue::Delete(int index) - { - if(index<0||index>=count) - return(false); + /** + * 删除队列中的指定数据 + * @param index 索引 + */ + template + bool Queue::Delete(int index) + { + if(index<0||index>=count) + return(false); - count--; + count--; - if(count) - { - if(index - bool Queue::Pop(T &t) - { - if(count) - { -// t=items[0]; - memcpy(&t,items,sizeof(T)); + /** + * 从队列中取出一个数据 + * @param t 取出的数据保存地 + * @return 是否成功取出数据 + */ + template + bool Queue::Pop(T &t) + { + if(count) + { +// t=items[0]; + memcpy(&t,items,sizeof(T)); - count--; + count--; - if(max_count==0) - { - if(count) - { - //memcpy(items,items+1,count*sizeof(T)); - memmove(items,items+1,count*sizeof(T)); + if(max_count==0) + { + if(count) + { + //memcpy(items,items+1,count*sizeof(T)); + memmove(items,items+1,count*sizeof(T)); // items=(T *)hgl_realloc(items,count*sizeof(T)); - } - } - else - { - memcpy(items,items+1,count*sizeof(T)); - } + } + } + else + { + memcpy(items,items+1,count*sizeof(T)); + } - return(true); - } - else - return(false); - } + return(true); + } + else + return(false); + } - /** - * 向队列中放入一个数据 - * @param data 要放入的数据指针 - * @return true 放入数据成功 - * @return false 放入数据失败 - */ - template - bool Queue::Push(const T &data) - { - if(max_count) - { - if(count>=max_count)return(false); - } - else - { - if(count) - { - if(count+1>mem_count) - { - mem_count=power_to_2(count+1); + /** + * 向队列中放入一个数据 + * @param data 要放入的数据指针 + * @return true 放入数据成功 + * @return false 放入数据失败 + */ + template + bool Queue::Push(const T &data) + { + if(max_count) + { + if(count>=max_count)return(false); + } + else + { + if(count) + { + if(count+1>mem_count) + { + mem_count=power_to_2(count+1); - items=(T *)hgl_realloc(items,mem_count*sizeof(T)); - } - } - else - { + items=(T *)hgl_realloc(items,mem_count*sizeof(T)); + } + } + else + { items=hgl_aligned_malloc(1); - mem_count=1; - } - } + mem_count=1; + } + } -// items[count++]=data; - memcpy(items+count,&data,sizeof(T)); - count++; +// items[count++]=data; + memcpy(items+count,&data,sizeof(T)); + count++; - return(true); - } + return(true); + } - template - int Queue::Find(const T &data) - { - if(count<=0) - return(-1); + template + int Queue::Find(const T &data) + { + if(count<=0) + return(-1); - T *p=items; - for(int i=0;i - void Queue::operator =(const Queue &ori) - { - if(ori.count==0)return; + template + void Queue::operator =(const Queue &ori) + { + if(ori.count==0)return; - Clear(); + Clear(); - max_count=ori.count; - count=ori.count; + max_count=ori.count; + count=ori.count; - if(max_count==0) - mem_count=count; - else - mem_count=max_count; + if(max_count==0) + mem_count=count; + else + mem_count=max_count; items=hgl_aligned_malloc(mem_count); - memcpy(items,ori.items,mem_count*sizeof(T)); - } + memcpy(items,ori.items,mem_count*sizeof(T)); + } } namespace hgl { - template - void QueueObject::Clear() - { - int n=Queue::count; + template + void QueueObject::Clear() + { + int n=Queue::count; - while(n--) - delete Queue::items[n]; + while(n--) + delete Queue::items[n]; - Queue::Clear(); - } + Queue::Clear(); + } } #endif//HGL_QUEUE_CPP diff --git a/inc/hgl/type/Queue.h b/inc/hgl/type/Queue.h index 88a5d020..cf71ecb7 100644 --- a/inc/hgl/type/Queue.h +++ b/inc/hgl/type/Queue.h @@ -4,72 +4,72 @@ #include namespace hgl { - /** - * Queue模板类用于保存一个先进先出、后进后出的数据队列 - * - * 注:这个类还在测试中,请不要修改这个类的源代码,如有修改意见,请致电作者。 - */ - template class Queue ///队列顺序数据访问类 - { - protected: + /** + * Queue模板类用于保存一个先进先出、后进后出的数据队列 + * + * 注:这个类还在测试中,请不要修改这个类的源代码,如有修改意见,请致电作者。 + */ + template class Queue ///队列顺序数据访问类 + { + protected: - int max_count; - int mem_count; - int count; - T *items; + int max_count; + int mem_count; + int count; + T *items; - public: //属性 + public: //属性 - T *GetData()const{return items;} ///<取得原始数据 - int GetCount()const{return count;} ///<取得数据数量 + T *GetData()const{return items;} ///<取得原始数据 + int GetCount()const{return count;} ///<取得数据数量 - int GetMax()const{return max_count;} ///<取得最大数量 - void SetMax(int); ///<设置最大数量 + int GetMax()const{return max_count;} ///<取得最大数量 + void SetMax(int); ///<设置最大数量 T *GetData(){return items;} ///<取得原始数据 - public: //方法 + public: //方法 - Queue(int=0); - virtual ~Queue(); + Queue(int=0); + virtual ~Queue(); - bool Peek(T &); ///<尝试访问一个数据 - bool Pop(T &); ///<弹出一个数据 - bool Push(const T &); ///<压入一个数据 + bool Peek(T &); ///<尝试访问一个数据 + bool Pop(T &); ///<弹出一个数据 + bool Push(const T &); ///<压入一个数据 - int Find(const T &); ///<查找队列中这个数据的编号 - bool Delete(int); ///<删除队列中指定编号的数据 + int Find(const T &); ///<查找队列中这个数据的编号 + bool Delete(int); ///<删除队列中指定编号的数据 - void Clear(); ///<清除所有数据 - void ClearData(); ///<清除所有数据,但不释放内存 + void Clear(); ///<清除所有数据 + void ClearData(); ///<清除所有数据,但不释放内存 - bool GetItem(int n,T &ti) ///<取得指定项数据 - { - if(n<0||n>=count)return(false); + bool GetItem(int n,T &ti) ///<取得指定项数据 + { + if(n<0||n>=count)return(false); - ti=items[n]; - return(true); - } + ti=items[n]; + return(true); + } - virtual void operator =(const Queue &); - };//template class Queue + virtual void operator =(const Queue &); + };//template class Queue - template class QueueObject:public Queue ///堆栈对象 - { - public: + template class QueueObject:public Queue ///堆栈对象 + { + public: - using Queue::Queue; - virtual ~QueueObject(){Clear();} + using Queue::Queue; + virtual ~QueueObject(){Clear();} - void Clear(); + void Clear(); - T *operator[](int n)const - { - if(n<0||n>=Queue::count)return(nullptr); + T *operator[](int n)const + { + if(n<0||n>=Queue::count)return(nullptr); - return Queue::items[n]; - } - };//template class QueueObject + return Queue::items[n]; + } + };//template class QueueObject }//namespace hgl #include #endif//HGL_QUEUE_INCLUDE diff --git a/inc/hgl/type/RectScope.cpp b/inc/hgl/type/RectScope.cpp index 5c707d2c..8c615eb8 100644 --- a/inc/hgl/type/RectScope.cpp +++ b/inc/hgl/type/RectScope.cpp @@ -4,77 +4,77 @@ #include namespace hgl { - /** - * 本类构造函数 - */ - template - RectScope2::RectScope2() - { - Left=0; - Top=0; - Width=0; - Height=0; - } + /** + * 本类构造函数 + */ + template + RectScope2::RectScope2() + { + Left=0; + Top=0; + Width=0; + Height=0; + } - /** - * 本类构造函数 - * @param l 矩形最左边的坐标值 - * @param t 矩形最上边的坐标值 - * @param w 矩形的宽度 - * @param h 矩形的高度 - */ - template - RectScope2::RectScope2(T l,T t,T w,T h) - { - Left=l; - Top=t; - Width=w; - Height=h; - } + /** + * 本类构造函数 + * @param l 矩形最左边的坐标值 + * @param t 矩形最上边的坐标值 + * @param w 矩形的宽度 + * @param h 矩形的高度 + */ + template + RectScope2::RectScope2(T l,T t,T w,T h) + { + Left=l; + Top=t; + Width=w; + Height=h; + } - template template - RectScope2::RectScope2(const RectScope2 &rs) - { - Left =rs.Left; - Top =rs.Top; - Width =rs.Width; - Height =rs.Height; - } + template template + RectScope2::RectScope2(const RectScope2 &rs) + { + Left =rs.Left; + Top =rs.Top; + Width =rs.Width; + Height =rs.Height; + } - /** - * 设置数据 - * @param l 矩形最左边的坐标值 - * @param t 矩形最上边的坐标值 - * @param w 矩形的宽度 - * @param h 矩形的高度 - */ - template - void RectScope2::Set(T l,T t,T w,T h) - { - Left=l; - Top=t; - Width=w; - Height=h; - } + /** + * 设置数据 + * @param l 矩形最左边的坐标值 + * @param t 矩形最上边的坐标值 + * @param w 矩形的宽度 + * @param h 矩形的高度 + */ + template + void RectScope2::Set(T l,T t,T w,T h) + { + Left=l; + Top=t; + Width=w; + Height=h; + } - /** - * 求坐标点x,y,z是否在这个矩形内 - * @param x X坐标 - * @param y Y坐标 - * @return 点x,y,z是否在矩形范围内 - */ - template - bool RectScope2::PointIn(T x,T y) const - { - T pos; + /** + * 求坐标点x,y,z是否在这个矩形内 + * @param x X坐标 + * @param y Y坐标 + * @return 点x,y,z是否在矩形范围内 + */ + template + bool RectScope2::PointIn(T x,T y) const + { + T pos; - pos=x-Left; - if(pos<0||pos>=Width)return(false); + pos=x-Left; + if(pos<0||pos>=Width)return(false); - pos=y-Top; - if(pos<0||pos>=Height)return(false); + pos=y-Top; + if(pos<0||pos>=Height)return(false); - return(true); - } + return(true); + } }//namespace hgl #endif//HGL_RECT_SCOPE_CPP diff --git a/inc/hgl/type/RectScope.h b/inc/hgl/type/RectScope.h index 02495f10..777d062a 100644 --- a/inc/hgl/type/RectScope.h +++ b/inc/hgl/type/RectScope.h @@ -4,124 +4,124 @@ #include namespace hgl { - /** - * 这个类用于描述和处理一个矩形范围 - */ - template class RectScope2 ///矩形范围类 - { - public: + /** + * 这个类用于描述和处理一个矩形范围 + */ + template class RectScope2 ///矩形范围类 + { + public: - T Left; ///<矩形左边所在的坐标 - T Top; ///<矩形上边所在的坐标 - T Width; ///<矩形的宽度 - T Height; ///<矩形的高度 + T Left; ///<矩形左边所在的坐标 + T Top; ///<矩形上边所在的坐标 + T Width; ///<矩形的宽度 + T Height; ///<矩形的高度 - public: + public: - T GetBottom()const{return Height+Top;} - T GetRight()const{return Width+Left;} - void SetBottom(T v){Top=v-Height;} - void SetRight(T v){Left=v-Width;} + T GetBottom()const{return Height+Top;} + T GetRight()const{return Width+Left;} + void SetBottom(T v){Top=v-Height;} + void SetRight(T v){Left=v-Width;} - T GetCenterX()const{return Left+(Width/2);} - T GetCenterY()const{return Top+(Height/2);} + T GetCenterX()const{return Left+(Width/2);} + T GetCenterY()const{return Top+(Height/2);} - public: + public: - RectScope2(); - RectScope2(T,T,T,T); - template RectScope2(const RectScope2 &); + RectScope2(); + RectScope2(T,T,T,T); + template RectScope2(const RectScope2 &); - void Clear() - { - Left=0; - Top=0; - Width=0; - Height=0; - } + void Clear() + { + Left=0; + Top=0; + Width=0; + Height=0; + } - void Set(T,T,T,T); + void Set(T,T,T,T); - bool PointIn(T,T)const; + bool PointIn(T,T)const; - bool PointIn(const Vector2f &v)const{return PointIn(v.x,v.y);} - bool PointIn(const Vector3f &v)const{return PointIn(v.x,v.y);} + bool PointIn(const Vector2f &v)const{return PointIn(v.x,v.y);} + bool PointIn(const Vector3f &v)const{return PointIn(v.x,v.y);} - template - void operator = (const RectScope2 &rs) - { - Left =rs.Left; - Top =rs.Top; - Width =rs.Width; - Height =rs.Height; - } + template + void operator = (const RectScope2 &rs) + { + Left =rs.Left; + Top =rs.Top; + Width =rs.Width; + Height =rs.Height; + } - template - bool operator == (const RectScope2 &rs) const - { - if((Left ==rs.Left ) - &&(Top ==rs.Top ) - &&(Width ==rs.Width ) - &&(Height ==rs.Height ))return(true); - else - return(false); - } + template + bool operator == (const RectScope2 &rs) const + { + if((Left ==rs.Left ) + &&(Top ==rs.Top ) + &&(Width ==rs.Width ) + &&(Height ==rs.Height ))return(true); + else + return(false); + } - template - bool operator != (const RectScope2 &rs) const - { - return(!operator==(rs)); - } + template + bool operator != (const RectScope2 &rs) const + { + return(!operator==(rs)); + } - template - void operator += (const RectScope2 &rs) - { - const T r=hgl_max(GetRight(),rs.GetRight()); - const T b=hgl_max(GetBottom(),rs.GetBottom()); + template + void operator += (const RectScope2 &rs) + { + const T r=hgl_max(GetRight(),rs.GetRight()); + const T b=hgl_max(GetBottom(),rs.GetBottom()); - Left=hgl_min(Left,rs.Left); - Top =hgl_max(Top,rs.Top); - Width=r-Left; - Height=b-Top; - } + Left=hgl_min(Left,rs.Left); + Top =hgl_max(Top,rs.Top); + Width=r-Left; + Height=b-Top; + } - template - RectScope2 operator + (const N &v) const - { - return RectScope2(Left+v[0],Top+v[1],Width,Height); - } + template + RectScope2 operator + (const N &v) const + { + return RectScope2(Left+v[0],Top+v[1],Width,Height); + } - template - RectScope2 operator - (const N &v) const - { - return RectScope2(Left+v[0],Top+v[1],Width,Height); - } + template + RectScope2 operator - (const N &v) const + { + return RectScope2(Left+v[0],Top+v[1],Width,Height); + } - template - RectScope2 &operator += (const N &v) - { - Left+=v[0]; - Top+=v[1]; + template + RectScope2 &operator += (const N &v) + { + Left+=v[0]; + Top+=v[1]; - return(*this); - } + return(*this); + } - template - RectScope2 &operator -= (const N &v) - { - Left-=v[0]; - Top-=v[1]; + template + RectScope2 &operator -= (const N &v) + { + Left-=v[0]; + Top-=v[1]; - return(*this); - } - };//class RectScope2 + return(*this); + } + };//class RectScope2 - typedef RectScope2 RectScope2d; - typedef RectScope2 RectScope2f; - typedef RectScope2 RectScope2i; - typedef RectScope2 RectScope2ui; - typedef RectScope2 RectScope2s; - typedef RectScope2 RectScope2us; + typedef RectScope2 RectScope2d; + typedef RectScope2 RectScope2f; + typedef RectScope2 RectScope2i; + typedef RectScope2 RectScope2ui; + typedef RectScope2 RectScope2s; + typedef RectScope2 RectScope2us; }//namespace hgl #include #endif//HGL_RECTSCOPE_INCLUDE diff --git a/inc/hgl/type/ResManage.cpp b/inc/hgl/type/ResManage.cpp index 74210a5a..ee61e8d2 100644 --- a/inc/hgl/type/ResManage.cpp +++ b/inc/hgl/type/ResManage.cpp @@ -4,131 +4,131 @@ #include namespace hgl { - template - ResManage::~ResManage() - { - Clear(); - } + template + ResManage::~ResManage() + { + Clear(); + } - template - void ResManage::Clear() - { - int n=items.GetCount(); + template + void ResManage::Clear() + { + int n=items.GetCount(); - while(n--) - { - ResItem *obj=items.GetItem(n); + while(n--) + { + ResItem *obj=items.GetItem(n); - Clear(obj->right); - } + Clear(obj->right); + } - items.Clear(); - } + items.Clear(); + } - template - void ResManage::ClearZero() - { - int n=items.GetCount(); + template + void ResManage::ClearZero() + { + int n=items.GetCount(); - while(n--) - { - ResItem *obj=items.GetItem(n); + while(n--) + { + ResItem *obj=items.GetItem(n); if(obj->count<=0) { Clear(obj->right); items.DeleteBySerial(n); } - } - } + } + } - template - bool ResManage::Add(const F &flag,T *obj) - { - if(!obj)return(false); + template + bool ResManage::Add(const F &flag,T *obj) + { + if(!obj)return(false); - if(items.KeyExist(flag)) - return(false); + if(items.KeyExist(flag)) + return(false); - items.Add(flag,obj); - return(true); - } + items.Add(flag,obj); + return(true); + } - template - T *ResManage::Find(const F &flag) - { - int index=items.Find(flag); + template + T *ResManage::Find(const F &flag) + { + int index=items.Find(flag); - if(index==-1) - return(nullptr); + if(index==-1) + return(nullptr); - T *result; + T *result; - if(items.Get(flag,result)) - return(result); + if(items.Get(flag,result)) + return(result); - return(nullptr); - } + return(nullptr); + } - template - T *ResManage::Get(const F &flag) - { - int index=items.Find(flag); + template + T *ResManage::Get(const F &flag) + { + int index=items.Find(flag); - if(index!=-1) - { - ResItem *obj=items.GetItem(index); + if(index!=-1) + { + ResItem *obj=items.GetItem(index); - //items[index]->count++; - obj->count++; + //items[index]->count++; + obj->count++; -// return(items[index]->data); - return obj->right; - } +// return(items[index]->data); + return obj->right; + } - T *data=Create(flag); + T *data=Create(flag); - if(data) - { - items.Add(flag,data); + if(data) + { + items.Add(flag,data); - return(data); - } + return(data); + } - return(nullptr); - } + return(nullptr); + } - template - void ResManage::ReleaseBySerial(int index,bool zero_clear) - { - if(index==-1) - { -// ErrorHint(u"所释放的资源不存在"); - return; - } + template + void ResManage::ReleaseBySerial(int index,bool zero_clear) + { + if(index==-1) + { +// ErrorHint(u"所释放的资源不存在"); + return; + } - ResItem *obj=items.GetItem(index); + ResItem *obj=items.GetItem(index); --obj->count; if(zero_clear&&obj->count==0) - { - Clear(obj->right); + { + Clear(obj->right); - items.DeleteBySerial(index); - } - } + items.DeleteBySerial(index); + } + } - template - void ResManage::Release(const F &flag,bool zero_clear) - { - ReleaseBySerial(items.Find(flag),zero_clear); - } + template + void ResManage::Release(const F &flag,bool zero_clear) + { + ReleaseBySerial(items.Find(flag),zero_clear); + } - template - void ResManage::Release(T *td,bool zero_clear) - { - ReleaseBySerial(items.FindByValue(td),zero_clear); - } + template + void ResManage::Release(T *td,bool zero_clear) + { + ReleaseBySerial(items.FindByValue(td),zero_clear); + } }//namespace hgl #endif//HGL_RES_MANAGE_CPP diff --git a/inc/hgl/type/ResManage.h b/inc/hgl/type/ResManage.h index 8b94083f..59850933 100644 --- a/inc/hgl/type/ResManage.h +++ b/inc/hgl/type/ResManage.h @@ -4,52 +4,52 @@ #include namespace hgl { - template struct RefFlagData:public Pair - { - uint count; + template struct RefFlagData:public Pair + { + uint count; - public: + public: - RefFlagData():Pair() - { - count=1; - } - }; + RefFlagData():Pair() + { + count=1; + } + }; - /** - * 资源管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载 - */ - template class ResManage - { - protected: + /** + * 资源管理器,它没有缓冲管理,仅仅是管理数据,并保证不会被重复加载 + */ + template class ResManage + { + protected: - typedef RefFlagData ResItem; + typedef RefFlagData ResItem; - _Map items; + _Map items; - void ReleaseBySerial(int,bool); + void ReleaseBySerial(int,bool); - protected: + protected: - virtual T *Create(const F &)=0; ///<资源创建虚拟函数无实现,请特例化实现 - virtual void Clear(T *obj){delete obj;} ///<资源释放虚拟函数(缺省为直接delete对象) + virtual T *Create(const F &)=0; ///<资源创建虚拟函数无实现,请特例化实现 + virtual void Clear(T *obj){delete obj;} ///<资源释放虚拟函数(缺省为直接delete对象) - public: + public: - virtual ~ResManage(); + virtual ~ResManage(); - virtual void Clear(); ///<清除所有数据 + virtual void Clear(); ///<清除所有数据 virtual void ClearZero(); ///<清除所有没有用到的数据 - const int GetCount()const{return items.GetCount();} ///<取得数据数量 + const int GetCount()const{return items.GetCount();} ///<取得数据数量 - virtual bool Add(const F &,T *); ///<添加一个数据 - virtual T * Find(const F &); ///<查找一个数据 - virtual T * Get(const F &); ///<取得一个数据,如不存在则创建 + virtual bool Add(const F &,T *); ///<添加一个数据 + virtual T * Find(const F &); ///<查找一个数据 + virtual T * Get(const F &); ///<取得一个数据,如不存在则创建 - virtual void Release(const F &,bool zero_clear=false); ///<释放一个数据 - virtual void Release(T *,bool zero_clear=false); ///<释放一个数据 - };//template class ResManage + virtual void Release(const F &,bool zero_clear=false); ///<释放一个数据 + virtual void Release(T *,bool zero_clear=false); ///<释放一个数据 + };//template class ResManage }//namespace hgl #include #endif//HGL_RES_MANAGE_INCLUDE diff --git a/inc/hgl/type/ResPoolManage.h b/inc/hgl/type/ResPoolManage.h index 9a282de9..b6eb7c43 100644 --- a/inc/hgl/type/ResPoolManage.h +++ b/inc/hgl/type/ResPoolManage.h @@ -6,29 +6,29 @@ namespace hgl { - /** - * 资源池是Pool/ResManage两个模板的组合应用 - */ - template class ResPoolManage:public ResManage - { - protected: + /** + * 资源池是Pool/ResManage两个模板的组合应用 + */ + template class ResPoolManage:public ResManage + { + protected: - ObjectPool data_pool; + ObjectPool data_pool; - public: + public: - virtual T *Create(const I &flag){return data_pool.Acquire();} - virtual void Clear(T *obj){data_pool.Release(obj);} + virtual T *Create(const I &flag){return data_pool.Acquire();} + virtual void Clear(T *obj){data_pool.Release(obj);} - public: + public: - virtual ~ResPoolManage()=default; + virtual ~ResPoolManage()=default; - virtual void Clear() - { - this->items.Clear(); - data_pool.ClearAll(); - } - };//template class ResPoolManage:public ResManage + virtual void Clear() + { + this->items.Clear(); + data_pool.ClearAll(); + } + };//template class ResPoolManage:public ResManage }//namespace hgl #endif//HGL_RES_POOL_MANAGE_INCLUDE diff --git a/inc/hgl/type/Set.cpp b/inc/hgl/type/Set.cpp index 8de586ff..35cdffc9 100644 --- a/inc/hgl/type/Set.cpp +++ b/inc/hgl/type/Set.cpp @@ -4,281 +4,281 @@ #include namespace hgl { - /** - * 查找数据是否存在 - * @param flag 数据 - * @return 数据所在索引,-1表示不存在 - */ - template - const int Set::Find(const T &flag)const - { - int left=0,right=data_list.GetCount()-1; //使用left,right而不使用min,max是为了让代码能够更好的阅读。 - int mid; + /** + * 查找数据是否存在 + * @param flag 数据 + * @return 数据所在索引,-1表示不存在 + */ + template + const int Set::Find(const T &flag)const + { + int left=0,right=data_list.GetCount()-1; //使用left,right而不使用min,max是为了让代码能够更好的阅读。 + int mid; - T *data_array=data_list.GetData(); + T *data_array=data_list.GetData(); - while(left<=right) - { - if(data_array[left ]==flag)return(left); - if(data_array[right]==flag)return(right); + while(left<=right) + { + if(data_array[left ]==flag)return(left); + if(data_array[right]==flag)return(right); - mid=(right+left)>>1; + mid=(right+left)>>1; - if(data_array[mid]==flag)return(mid); + if(data_array[mid]==flag)return(mid); - if(data_array[mid]>flag) - { - left++; - right=mid-1; - } - else - { - right--; - left=mid+1; - } - } + if(data_array[mid]>flag) + { + left++; + right=mid-1; + } + else + { + right--; + left=mid+1; + } + } - return(-1); - } + return(-1); + } - template - bool Set::FindPos(const T &flag,int &pos)const - { - int left=0,right=data_list.GetCount()-1; - int mid; + template + bool Set::FindPos(const T &flag,int &pos)const + { + int left=0,right=data_list.GetCount()-1; + int mid; - T *data_array=data_list.GetData(); + T *data_array=data_list.GetData(); - while(left<=right) - { - if(data_array[left]>flag) - { - pos=left; - return(false); - } - else - if(data_array[left]==flag) - { - pos=left; - return(true); - } + while(left<=right) + { + if(data_array[left]>flag) + { + pos=left; + return(false); + } + else + if(data_array[left]==flag) + { + pos=left; + return(true); + } - if(data_array[right]>1; + mid=(right+left)>>1; - if(data_array[mid]==flag) - { - pos=mid; - return(true); - } + if(data_array[mid]==flag) + { + pos=mid; + return(true); + } - if(data_array[mid]>flag) - { - if(data_array[mid-1]flag) + { + if(data_array[mid-1]flag) - { - pos=mid+1; - return(false); - } - else - if(data_array[mid+1]==flag) - { - pos=mid+1; - return(true); - } + ++left; + right=mid-1; + } + else + { + if(data_array[mid+1]>flag) + { + pos=mid+1; + return(false); + } + else + if(data_array[mid+1]==flag) + { + pos=mid+1; + return(true); + } - --right; - left=mid+1; - } - } + --right; + left=mid+1; + } + } - return(false); - } + return(false); + } - /** - * 添加一个数据 - * @param data 数据 - * @return 位置 - */ - template - int Set::Add(const T &data) - { - if(data_list.GetCount()<=0) - { - data_list.Add(data); + /** + * 添加一个数据 + * @param data 数据 + * @return 位置 + */ + template + int Set::Add(const T &data) + { + if(data_list.GetCount()<=0) + { + data_list.Add(data); - return 0; - } - else - { - int pos; + return 0; + } + else + { + int pos; - if(FindPos(data,pos)) - return(-1); //数据已存在 + if(FindPos(data,pos)) + return(-1); //数据已存在 - data_list.Insert(pos,data); + data_list.Insert(pos,data); - return(pos); - } - } + return(pos); + } + } - /** - * 添加一批数据 - * @param dp 数据指针 - * @param count 数据个数 - * @return 成功加入的数据个数 - */ - template - int Set::Add(const T *dp,const int count) - { - int total=0; + /** + * 添加一批数据 + * @param dp 数据指针 + * @param count 数据个数 + * @return 成功加入的数据个数 + */ + template + int Set::Add(const T *dp,const int count) + { + int total=0; - for(int i=0;i - bool Set::Update(const T &data) - { - if(data_list.GetCount()<=0) - return(false); + /** + * 更新一个数据 + * @param data 数据 + * @return 是否成功 + */ + template + bool Set::Update(const T &data) + { + if(data_list.GetCount()<=0) + return(false); - int pos; + int pos; - if(!FindPos(data,pos)) - return(false); + if(!FindPos(data,pos)) + return(false); - data_list.Set(pos,data); - return(true); - } + data_list.Set(pos,data); + return(true); + } - /** - * 删除一个数据 - * @param pos 索引编号 - */ - template - bool Set::DeleteBySerial(int pos) - { - if(pos<0||pos>=data_list.GetCount())return(false); + /** + * 删除一个数据 + * @param pos 索引编号 + */ + template + bool Set::DeleteBySerial(int pos) + { + if(pos<0||pos>=data_list.GetCount())return(false); - return data_list.DeleteMove(pos); - } + return data_list.DeleteMove(pos); + } - /** - * 删除一个数据 - * @param data 数据 - */ - template - bool Set::Delete(const T &data) - { - int pos=Find(data); + /** + * 删除一个数据 + * @param data 数据 + */ + template + bool Set::Delete(const T &data) + { + int pos=Find(data); - if(pos==-1)return(false); + if(pos==-1)return(false); - return DeleteBySerial(pos); - } + return DeleteBySerial(pos); + } - /** - * 删除一批数据 - * @param dp 数据指针 - * @param count 数据个数 - * @return 成功删除的数据个数 - */ - template - int Set::Delete(T *dp,const int count) - { - int total=0; - int pos; + /** + * 删除一批数据 + * @param dp 数据指针 + * @param count 数据个数 + * @return 成功删除的数据个数 + */ + template + int Set::Delete(T *dp,const int count) + { + int total=0; + int pos; - for(int i=0;i - void Set::Clear() - { - data_list.Clear(); - } + /** + * 清除所有数据 + */ + template + void Set::Clear() + { + data_list.Clear(); + } - /** - * 清除所有数据,但不释放内存 - */ - template - void Set::ClearData() - { - data_list.ClearData(); - } + /** + * 清除所有数据,但不释放内存 + */ + template + void Set::ClearData() + { + data_list.ClearData(); + } - /** - * 随机取得一个数据 - */ - template - bool Set::Rand(T &result)const - { - return data_list.Rand(result); - } + /** + * 随机取得一个数据 + */ + template + bool Set::Rand(T &result)const + { + return data_list.Rand(result); + } - /** + /** * 求当前合集与另一个数据集的交集 * @param result 结果存放合集 * @param list 要计算交集的数据集 * @return 交集数量 */ - template - int Set::Intersection(Set &result,const Set &list) + template + int Set::Intersection(Set &result,const Set &list) { if(data_list.GetCount()<=0) return(0); diff --git a/inc/hgl/type/Set.h b/inc/hgl/type/Set.h index d6e9b8a2..e355ef05 100644 --- a/inc/hgl/type/Set.h +++ b/inc/hgl/type/Set.h @@ -4,46 +4,46 @@ #include namespace hgl { - /** - * 集合数据列表中不允许数据出现重复性,同时它会将数据排序,所以也可以当做有序列表使用 - */ - template class Set - { - protected: + /** + * 集合数据列表中不允许数据出现重复性,同时它会将数据排序,所以也可以当做有序列表使用 + */ + template class Set + { + protected: - List data_list; + List data_list; - bool FindPos(const T &,int &)const; ///<查找数据如果插入后,会所在的位置,返回是否存在这个数据 - int FindPos(const T &flag)const{int pos;FindPos(flag,pos);return(pos);} ///<查找数据如果插入后,会所在的位置 + bool FindPos(const T &,int &)const; ///<查找数据如果插入后,会所在的位置,返回是否存在这个数据 + int FindPos(const T &flag)const{int pos;FindPos(flag,pos);return(pos);} ///<查找数据如果插入后,会所在的位置 - public: //属性 + public: //属性 - List & GetList ()const{return data_list;} ///<取得数据原始列表 - T * GetData ()const{return data_list.GetData();} ///<取得数据指针 - int GetCount ()const{return data_list.GetCount();} ///<取得数据总量 + List & GetList ()const{return data_list;} ///<取得数据原始列表 + T * GetData ()const{return data_list.GetData();} ///<取得数据指针 + int GetCount ()const{return data_list.GetCount();} ///<取得数据总量 - public: + public: - Set()=default; - virtual ~Set()=default; + Set()=default; + virtual ~Set()=default; - void SetCount (int count){data_list.SetCount(count);} ///<指定数据数量,一般用于批量加载前的处理 - void PreMalloc (int count){data_list.PreMalloc(count);} ///<预分配指定数量的数据空间 + void SetCount (int count){data_list.SetCount(count);} ///<指定数据数量,一般用于批量加载前的处理 + void PreMalloc (int count){data_list.PreMalloc(count);} ///<预分配指定数量的数据空间 - const int Find (const T &)const; ///<查找数据位置,不存在返回-1 - const bool IsMember (const T &v)const{return(Find(v)!=-1);} ///<确认是否成员 - int Add (const T &); ///<添加一个数据,返回索引号,返回-1表示数据已存在 - int Add (const T *,const int); ///<添加一批数据 - int Add (const Set &s){return Add(s.GetData(),s.GetCount());} ///<添加一批数据 - bool Update (const T &); ///<更新一个数据 - bool Delete (const T &); ///<删除一个数据 - int Delete (T *,const int); ///<删除一批数据 - bool DeleteBySerial (int); ///<删除一个数据,使用序号 - void Clear (); ///<清除数据 - void ClearData (); ///<清除数据,但不释放内存 - void DeleteClear (){data_list.DeleteClear();} ///<清除所有数据并全部调用delete + const int Find (const T &)const; ///<查找数据位置,不存在返回-1 + const bool IsMember (const T &v)const{return(Find(v)!=-1);} ///<确认是否成员 + int Add (const T &); ///<添加一个数据,返回索引号,返回-1表示数据已存在 + int Add (const T *,const int); ///<添加一批数据 + int Add (const Set &s){return Add(s.GetData(),s.GetCount());} ///<添加一批数据 + bool Update (const T &); ///<更新一个数据 + bool Delete (const T &); ///<删除一个数据 + int Delete (T *,const int); ///<删除一批数据 + bool DeleteBySerial (int); ///<删除一个数据,使用序号 + void Clear (); ///<清除数据 + void ClearData (); ///<清除数据,但不释放内存 + void DeleteClear (){data_list.DeleteClear();} ///<清除所有数据并全部调用delete - bool Get (const int index,T &data) + bool Get (const int index,T &data) { if(index<0||index>=data_list.GetCount()) return(false); @@ -68,10 +68,10 @@ namespace hgl void operator =(const Set &set){data_list=set.data_list;} ///<等号操作符重载 - bool Rand (T &)const; ///<随机取得一个 + bool Rand (T &)const; ///<随机取得一个 virtual void Enum (void (*enum_func)(T &)){data_list.Enum(enum_func);} ///<枚举所有数据成员 - };//template class Set + };//template class Set }//namespace hgl #include #endif//HGL_TYPE_SET_INCLUDE diff --git a/inc/hgl/type/Smart.h b/inc/hgl/type/Smart.h index 1bf07eb8..dcdec534 100644 --- a/inc/hgl/type/Smart.h +++ b/inc/hgl/type/Smart.h @@ -5,456 +5,456 @@ #include namespace hgl { - struct RefCount - { - atom_int count; - atom_int weak; - - public: - - RefCount() - { - count=1; - weak=0; - } - - virtual ~RefCount()=default; + struct RefCount + { + atom_int count; + atom_int weak; + + public: + + RefCount() + { + count=1; + weak=0; + } + + virtual ~RefCount()=default; - virtual void Delete()=0; + virtual void Delete()=0; - int inc_ref() - { - return ++count; - } + int inc_ref() + { + return ++count; + } - virtual int unref() - { - count--; + virtual int unref() + { + count--; - if(count<=0) - { - Delete(); - - if(weak<=0) - delete this; - - return 0; - } - - return count; - } + if(count<=0) + { + Delete(); + + if(weak<=0) + delete this; + + return 0; + } + + return count; + } - int inc_ref_weak() - { - return ++weak; - } - - int unref_weak() - { - weak--; - - if(weak<=0) - { - if(count<=0) - { - delete this; - return 0; - } - } + int inc_ref_weak() + { + return ++weak; + } + + int unref_weak() + { + weak--; + + if(weak<=0) + { + if(count<=0) + { + delete this; + return 0; + } + } - return weak; - } - };//struct RefCount + return weak; + } + };//struct RefCount - template struct SmartData:public RefCount - { - T *data; + template struct SmartData:public RefCount + { + T *data; - public: + public: - SmartData(T *ptr) - { - data=ptr; - } + SmartData(T *ptr) + { + data=ptr; + } - ~SmartData() - { - Delete(); - } + ~SmartData() + { + Delete(); + } - void Delete() - { - SAFE_CLEAR(data); - } - };//struct template struct SmartData + void Delete() + { + SAFE_CLEAR(data); + } + };//struct template struct SmartData - template struct SmartArrayData:public RefCount - { - T *data; + template struct SmartArrayData:public RefCount + { + T *data; - public: + public: - SmartArrayData(T *ptr) - { - data=ptr; - } + SmartArrayData(T *ptr) + { + data=ptr; + } - ~SmartArrayData() - { - Delete(); - } + ~SmartArrayData() + { + Delete(); + } - void Delete() - { - SAFE_CLEAR_ARRAY(data); - } + void Delete() + { + SAFE_CLEAR_ARRAY(data); + } - const T &operator *() const {return data;} - const bool operator!() const{return !data;} - };//struct template struct SmartArrayData + const T &operator *() const {return data;} + const bool operator!() const{return !data;} + };//struct template struct SmartArrayData - template class _Smart - { - protected: + template class _Smart + { + protected: - typedef _Smart SelfClass; + typedef _Smart SelfClass; - SD *sd; + SD *sd; - public: + public: - _Smart() - { - sd=0; - } + _Smart() + { + sd=0; + } - _Smart(T *ptr) - { - if(ptr) - sd=new SD(ptr); - else - sd=0; - } + _Smart(T *ptr) + { + if(ptr) + sd=new SD(ptr); + else + sd=0; + } - _Smart(const SelfClass &st) - { - sd=0; + _Smart(const SelfClass &st) + { + sd=0; - set(st); - } + set(st); + } - virtual ~_Smart()=default; - - T *set(T *ptr) - { - if(sd) - sd->unref(); - - if(!ptr) - { - sd=0; - return(0); - } - - sd=new SD(ptr); + virtual ~_Smart()=default; + + T *set(T *ptr) + { + if(sd) + sd->unref(); + + if(!ptr) + { + sd=0; + return(0); + } + + sd=new SD(ptr); - return ptr; - } + return ptr; + } - void inc_ref(const SelfClass &sc) - { - if(sd==sc.sd)return; + void inc_ref(const SelfClass &sc) + { + if(sd==sc.sd)return; - unref(); + unref(); - sd=sc.sd; + sd=sc.sd; - if(sd) - sd->inc_ref(); - } - - void unref() - { - if(sd) - { - sd->unref(); - sd=0; - } - } + if(sd) + sd->inc_ref(); + } + + void unref() + { + if(sd) + { + sd->unref(); + sd=0; + } + } - void inc_ref_weak(const SelfClass &sc) - { - if(sd==sc.sd)return; + void inc_ref_weak(const SelfClass &sc) + { + if(sd==sc.sd)return; - unref(); + unref(); - sd=sc.sd; + sd=sc.sd; - if(sd) - sd->inc_ref_weak(); - } + if(sd) + sd->inc_ref_weak(); + } - void unref_weak() - { - if(sd) - { - sd->unref_weak(); - sd=0; - } - } + void unref_weak() + { + if(sd) + { + sd->unref_weak(); + sd=0; + } + } - T *get()const{return sd?sd->data:0;} - virtual bool valid()const{return sd;} - int use_count()const{return sd?sd->count:-1;} - bool only()const{return sd?sd->count==1:true;} + T *get()const{return sd?sd->data:0;} + virtual bool valid()const{return sd;} + int use_count()const{return sd?sd->count:-1;} + bool only()const{return sd?sd->count==1:true;} - public: + public: - const T &operator *() const {return *(sd->data);} - const bool operator!() const{return sd?!(sd->data):true;} + const T &operator *() const {return *(sd->data);} + const bool operator!() const{return sd?!(sd->data):true;} - operator T *()const{return(sd?sd->data:0);} - T *operator ->()const{return(sd?sd->data:0);} + operator T *()const{return(sd?sd->data:0);} + T *operator ->()const{return(sd?sd->data:0);} - bool operator == (const SelfClass & rp)const{return(get()==rp.get()); } - bool operator == (const T * rp)const{return(get()==rp); } + bool operator == (const SelfClass & rp)const{return(get()==rp.get()); } + bool operator == (const T * rp)const{return(get()==rp); } - bool operator != (const SelfClass & rp)const{return !(operator==(rp)); } - bool operator != (const T * rp)const{return !(operator==(rp)); } - };//template class _Smart + bool operator != (const SelfClass & rp)const{return !(operator==(rp)); } + bool operator != (const T * rp)const{return !(operator==(rp)); } + };//template class _Smart - template class WeakPtr; - - /** - * 共享指针数据类
- * 用于自动释放超出作用域的指针 - */ - template class SharedPtr:public _Smart,T> ///共享指针数据类 - { - friend class WeakPtr; + template class WeakPtr; + + /** + * 共享指针数据类
+ * 用于自动释放超出作用域的指针 + */ + template class SharedPtr:public _Smart,T> ///共享指针数据类 + { + friend class WeakPtr; - public: + public: - typedef _Smart,T> SuperClass; - typedef SharedPtr SelfClass; + typedef _Smart,T> SuperClass; + typedef SharedPtr SelfClass; - public: + public: - SharedPtr():SuperClass(){} - SharedPtr(T *ptr):SuperClass(ptr){} - SharedPtr(const SelfClass &sp):SuperClass() - { - operator=(sp); - } - - SharedPtr(const WeakPtr &wp):SuperClass() - { - operator=(wp); - } + SharedPtr():SuperClass(){} + SharedPtr(T *ptr):SuperClass(ptr){} + SharedPtr(const SelfClass &sp):SuperClass() + { + operator=(sp); + } + + SharedPtr(const WeakPtr &wp):SuperClass() + { + operator=(wp); + } - ~SharedPtr() - { - SuperClass::unref(); - } + ~SharedPtr() + { + SuperClass::unref(); + } - operator T *() - { - return SuperClass::get(); - } + operator T *() + { + return SuperClass::get(); + } - operator const T *()const - { - return SuperClass::get(); - } + operator const T *()const + { + return SuperClass::get(); + } - T *operator =(T *ptr) - { - return SuperClass::set(ptr); - } - - SelfClass &operator =(const SelfClass &sp) - { - SuperClass::inc_ref(sp); - - return(*this); - } + T *operator =(T *ptr) + { + return SuperClass::set(ptr); + } + + SelfClass &operator =(const SelfClass &sp) + { + SuperClass::inc_ref(sp); + + return(*this); + } - SelfClass &operator =(const WeakPtr &wp) - { - SuperClass::inc_ref(wp); - - return(*this); - } + SelfClass &operator =(const WeakPtr &wp) + { + SuperClass::inc_ref(wp); + + return(*this); + } - bool valid()const override{return this->sd?(this->sd->data?true:false):false;} - };//template class SharedPtr - - template class WeakArray; - - /** - * 共享阵列数据类,它在SharedPtr的基础上增加了[]操作符访问,以及在删除时使用delete[] - */ - template class SharedArray:public _Smart,T> ///共享阵列数据类 - { - friend class WeakArray; - - public: + bool valid()const override{return this->sd?(this->sd->data?true:false):false;} + };//template class SharedPtr + + template class WeakArray; + + /** + * 共享阵列数据类,它在SharedPtr的基础上增加了[]操作符访问,以及在删除时使用delete[] + */ + template class SharedArray:public _Smart,T> ///共享阵列数据类 + { + friend class WeakArray; + + public: - typedef _Smart,T> SuperClass; - typedef SharedArray SelfClass; + typedef _Smart,T> SuperClass; + typedef SharedArray SelfClass; - public: + public: - SharedArray():SuperClass(){} - SharedArray(T *ptr):SuperClass(ptr){} - SharedArray(const SelfClass &sa):SuperClass() - { - operator=(sa); - } + SharedArray():SuperClass(){} + SharedArray(T *ptr):SuperClass(ptr){} + SharedArray(const SelfClass &sa):SuperClass() + { + operator=(sa); + } - SharedArray(const WeakArray &wa):SuperClass() - { - operator=(wa); - } + SharedArray(const WeakArray &wa):SuperClass() + { + operator=(wa); + } - ~SharedArray() - { - SuperClass::unref(); - } + ~SharedArray() + { + SuperClass::unref(); + } - operator T *() - { - return SuperClass::get(); - } - - operator const T *()const - { - return SuperClass::get(); - } + operator T *() + { + return SuperClass::get(); + } + + operator const T *()const + { + return SuperClass::get(); + } - T &operator [](int n) - { - return SuperClass::sd->data[n]; - } - - SelfClass &operator =(const SelfClass &sap) - { - SuperClass::inc_ref(sap); - - return(*this); - } - - SelfClass &operator =(const WeakPtr &wp) - { - SuperClass::inc_ref(wp); - - return(*this); - } - };//template class SharedArray + T &operator [](int n) + { + return SuperClass::sd->data[n]; + } + + SelfClass &operator =(const SelfClass &sap) + { + SuperClass::inc_ref(sap); + + return(*this); + } + + SelfClass &operator =(const WeakPtr &wp) + { + SuperClass::inc_ref(wp); + + return(*this); + } + };//template class SharedArray - template class WeakPtr:public _Smart,T> - { - friend class SharedPtr; - - public: - - typedef _Smart,T> SuperClass; - typedef WeakPtr SelfClass; + template class WeakPtr:public _Smart,T> + { + friend class SharedPtr; + + public: + + typedef _Smart,T> SuperClass; + typedef WeakPtr SelfClass; - public: - - WeakPtr():SuperClass(){} - WeakPtr(const SharedPtr &sp):SuperClass() - { - operator=(sp); - } + public: + + WeakPtr():SuperClass(){} + WeakPtr(const SharedPtr &sp):SuperClass() + { + operator=(sp); + } - WeakPtr(const SelfClass &wp):SuperClass() - { - operator=(wp); - } + WeakPtr(const SelfClass &wp):SuperClass() + { + operator=(wp); + } - ~WeakPtr() - { - SuperClass::unref_weak(); - } + ~WeakPtr() + { + SuperClass::unref_weak(); + } - operator T *() - { - return SuperClass::get(); - } - - operator const T *()const - { - return SuperClass::get(); - } + operator T *() + { + return SuperClass::get(); + } + + operator const T *()const + { + return SuperClass::get(); + } - virtual SuperClass &operator =(const SharedPtr &sp) - { - SuperClass::inc_ref_weak(sp); - - return(*this); - } - - virtual SelfClass &operator =(const SelfClass &wp) - { - SuperClass::inc_ref_weak(wp); - - return(*this); - } - };//template class WeakPtr - - template class WeakArray:public _Smart,T> - { - friend class SharedArray; - - public: - - typedef _Smart,T> SuperClass; - typedef WeakArray SelfClass; - - public: - - WeakArray():SuperClass(){} - WeakArray(const SharedArray &sap):SuperClass() - { - operator=(sap); - } + virtual SuperClass &operator =(const SharedPtr &sp) + { + SuperClass::inc_ref_weak(sp); + + return(*this); + } + + virtual SelfClass &operator =(const SelfClass &wp) + { + SuperClass::inc_ref_weak(wp); + + return(*this); + } + };//template class WeakPtr + + template class WeakArray:public _Smart,T> + { + friend class SharedArray; + + public: + + typedef _Smart,T> SuperClass; + typedef WeakArray SelfClass; + + public: + + WeakArray():SuperClass(){} + WeakArray(const SharedArray &sap):SuperClass() + { + operator=(sap); + } - WeakArray(const WeakArray &wap):SuperClass() - { - operator=(wap); - } + WeakArray(const WeakArray &wap):SuperClass() + { + operator=(wap); + } - ~WeakArray() - { - SuperClass::unref_weak(); - } + ~WeakArray() + { + SuperClass::unref_weak(); + } - operator T *() - { - return SuperClass::get(); - } + operator T *() + { + return SuperClass::get(); + } - operator const T *()const - { - return SuperClass::get(); - } + operator const T *()const + { + return SuperClass::get(); + } - virtual SuperClass &operator =(const SharedArray &sap) - { - SuperClass::inc_ref_weak(sap); + virtual SuperClass &operator =(const SharedArray &sap) + { + SuperClass::inc_ref_weak(sap); - return(*this); - } + return(*this); + } - virtual SelfClass &operator =(const SelfClass &wap) - { - SuperClass::inc_ref_weak(wap); + virtual SelfClass &operator =(const SelfClass &wap) + { + SuperClass::inc_ref_weak(wap); - return(*this); - } - };//template class WeakArray + return(*this); + } + };//template class WeakArray template class AutoDelete { diff --git a/inc/hgl/type/Stack.cpp b/inc/hgl/type/Stack.cpp index 67923dd3..b4a2d0c0 100644 --- a/inc/hgl/type/Stack.cpp +++ b/inc/hgl/type/Stack.cpp @@ -4,39 +4,39 @@ #include namespace hgl { - /** - * 本类构造函数 - * @param m 如果m的值不为0,则表示使用固定的堆栈大小。固定大小的堆栈会在一开始即分配好内存。 - */ - template - Stack::Stack(int m) - { - count=0; + /** + * 本类构造函数 + * @param m 如果m的值不为0,则表示使用固定的堆栈大小。固定大小的堆栈会在一开始即分配好内存。 + */ + template + Stack::Stack(int m) + { + count=0; - if(m) - { - max_count=m; + if(m) + { + max_count=m; items=hgl_aligned_malloc(max_count); - } - else - max_count=0; + } + else + max_count=0; - mem_count=max_count; - } + mem_count=max_count; + } - template - Stack::~Stack() - { - if(count||max_count)hgl_free(items); - } + template + Stack::~Stack() + { + if(count||max_count)hgl_free(items); + } - /** - * 修改堆栈的最大值 - */ - template - void Stack::SetMax(int m) - { + /** + * 修改堆栈的最大值 + */ + template + void Stack::SetMax(int m) + { if(m<=0)return; if(mem_count==0) @@ -51,13 +51,13 @@ namespace hgl items=(T *)hgl_realloc(items,mem_count*sizeof(T)); } - max_count=m; + max_count=m; - if(count>=max_count)count=max_count-1; - } + if(count>=max_count)count=max_count-1; + } - template - bool Stack::SetCount(int c) + template + bool Stack::SetCount(int c) { if(c<0)return(false); @@ -68,194 +68,194 @@ namespace hgl return(true); } - /** - * 清除堆栈中的所有数据 - */ - template - void Stack::Clear() - { - if(max_count==0) - if(count) - { - hgl_free(items); - mem_count=0; - } + /** + * 清除堆栈中的所有数据 + */ + template + void Stack::Clear() + { + if(max_count==0) + if(count) + { + hgl_free(items); + mem_count=0; + } - count=0; - } + count=0; + } - template - bool Stack::GetItem(int n,T &data) - { - if(n<0||n>=count) - { - LOG_ERROR(OS_TEXT("从堆栈中按索引<") + OSString(n) + OS_TEXT(">取数据,超出正常范围<")+OSString(count) + OS_TEXT(">")); + template + bool Stack::GetItem(int n,T &data) + { + if(n<0||n>=count) + { + LOG_ERROR(OS_TEXT("从堆栈中按索引<") + OSString(n) + OS_TEXT(">取数据,超出正常范围<")+OSString(count) + OS_TEXT(">")); - return(false); - } + return(false); + } - data=items[n]; + data=items[n]; return(true); - } + } - /** - * 访问堆栈中的一个数据,但不清除它 - * @param t 取出的数据保存地 - * @return 是否成功取出数据 - */ - template - bool Stack::Peek(T &t) - { - if(count) - { - memcpy(&t,items+(count-1),sizeof(T)); -// t=items[count-1]; - return(true); - } - else - return(false); - } + /** + * 访问堆栈中的一个数据,但不清除它 + * @param t 取出的数据保存地 + * @return 是否成功取出数据 + */ + template + bool Stack::Peek(T &t) + { + if(count) + { + memcpy(&t,items+(count-1),sizeof(T)); +// t=items[count-1]; + return(true); + } + else + return(false); + } - /** - * 从堆栈中取出一个数据 - * @param t 取出的数据保存地 - * @return 是否成功取出数据 - */ - template - bool Stack::Pop(T &t) - { - if(count) - { -// t=items[--count]; - count--; - memcpy(&t,items+count,sizeof(T)); + /** + * 从堆栈中取出一个数据 + * @param t 取出的数据保存地 + * @return 是否成功取出数据 + */ + template + bool Stack::Pop(T &t) + { + if(count) + { +// t=items[--count]; + count--; + memcpy(&t,items+count,sizeof(T)); - if(max_count==0) - { - if(count==0) - { - hgl_free(items); + if(max_count==0) + { + if(count==0) + { + hgl_free(items); - mem_count=0; - } - //else - //items=(T *)hgl_realloc(items,count*sizeof(T)); - } + mem_count=0; + } + //else + //items=(T *)hgl_realloc(items,count*sizeof(T)); + } - return(true); - } - else - return(false); - } + return(true); + } + else + return(false); + } - /** - * 向堆栈中放入一个数据 - * @param data 要放入的数据指针 - * @return true 放入数据成功 - * @return false 放入数据失败 - */ - template - bool Stack::Push(T &data) - { - if(max_count) - { - if(count>=max_count)return(false); - } - else - { - if(count) - { - if(count+1>mem_count) - { - mem_count=power_to_2(count+1); + /** + * 向堆栈中放入一个数据 + * @param data 要放入的数据指针 + * @return true 放入数据成功 + * @return false 放入数据失败 + */ + template + bool Stack::Push(T &data) + { + if(max_count) + { + if(count>=max_count)return(false); + } + else + { + if(count) + { + if(count+1>mem_count) + { + mem_count=power_to_2(count+1); - items=(T *)hgl_realloc(items,mem_count*sizeof(T)); - } - } - else - { + items=(T *)hgl_realloc(items,mem_count*sizeof(T)); + } + } + else + { items=hgl_aligned_malloc(1); - mem_count=1; - } - } + mem_count=1; + } + } -// items[count++]=data; - memcpy(items+count,&data,sizeof(T)); - count++; +// items[count++]=data; + memcpy(items+count,&data,sizeof(T)); + count++; - return(true); - } + return(true); + } - /** - * 向堆栈中放入多个数据 - * @param data 要放入的数据 - * @param data_count 要放入的数据个数 - * @return true 放入数据成功 - * @return false 放入数据失败 - */ - template - bool Stack::MultiPush(T *data,int data_count) - { - if(max_count) - { - if(count>=max_count)return(false); - } - else - { - if(count) - { - if(count+data_count>mem_count) - { - mem_count=power_to_2(count+data_count); + /** + * 向堆栈中放入多个数据 + * @param data 要放入的数据 + * @param data_count 要放入的数据个数 + * @return true 放入数据成功 + * @return false 放入数据失败 + */ + template + bool Stack::MultiPush(T *data,int data_count) + { + if(max_count) + { + if(count>=max_count)return(false); + } + else + { + if(count) + { + if(count+data_count>mem_count) + { + mem_count=power_to_2(count+data_count); - items=(T *)hgl_realloc(items,mem_count*sizeof(T)); - } - } - else - { + items=(T *)hgl_realloc(items,mem_count*sizeof(T)); + } + } + else + { items=hgl_aligned_malloc(data_count); - mem_count=data_count; - } - } + mem_count=data_count; + } + } - memcpy(items+count,data,data_count*sizeof(T)); - count+=data_count; + memcpy(items+count,data,data_count*sizeof(T)); + count+=data_count; - return(true); - } + return(true); + } - template - void Stack::operator =(const Stack &ori) - { - if(ori.count==0)return; + template + void Stack::operator =(const Stack &ori) + { + if(ori.count==0)return; - Clear(); + Clear(); - max_count=ori.count; - count=ori.count; + max_count=ori.count; + count=ori.count; - if(max_count==0) - mem_count=count; - else - mem_count=max_count; + if(max_count==0) + mem_count=count; + else + mem_count=max_count; items=hgl_aligned_malloc(mem_count); - memcpy(items,ori.items,mem_count*sizeof(T)); - } + memcpy(items,ori.items,mem_count*sizeof(T)); + } }//namespace hgl namespace hgl { - template - void StackObject::Clear() - { - for(int i=0;icount;i++) - delete this->items[i]; + template + void StackObject::Clear() + { + for(int i=0;icount;i++) + delete this->items[i]; - Stack::Clear(); - } + Stack::Clear(); + } }//namespace hgl #endif//HGL_STACK_CPP diff --git a/inc/hgl/type/Stack.h b/inc/hgl/type/Stack.h index f9d85e53..a12363cf 100644 --- a/inc/hgl/type/Stack.h +++ b/inc/hgl/type/Stack.h @@ -4,56 +4,56 @@ #include namespace hgl { - /** - * Stack模板类用于保存一个先进后出、后进先出的数据堆栈 - * - * 注:这个类还在测试中,请不要修改这个类的源代码,如有修改意见,请致电作者。 - */ - template class Stack ///堆栈顺序数据访问类 - { - protected: + /** + * Stack模板类用于保存一个先进后出、后进先出的数据堆栈 + * + * 注:这个类还在测试中,请不要修改这个类的源代码,如有修改意见,请致电作者。 + */ + template class Stack ///堆栈顺序数据访问类 + { + protected: - int max_count; - int mem_count; - int count; - T *items; + int max_count; + int mem_count; + int count; + T *items; - public: //属性 + public: //属性 - int GetCount() const{return count;} ///<取得堆栈中数据的个数 - bool SetCount(int c); ///<直接设置堆栈中数据的个数 + int GetCount() const{return count;} ///<取得堆栈中数据的个数 + bool SetCount(int c); ///<直接设置堆栈中数据的个数 - int GetMax () const{return max_count;} ///<取得堆栈中的最大数据个数 - void SetMax (int); ///<设置堆栈中的最大数据个数 + int GetMax () const{return max_count;} ///<取得堆栈中的最大数据个数 + void SetMax (int); ///<设置堆栈中的最大数据个数 T * GetData () {return items;} ///<取得原始数据 - public: //方法 + public: //方法 - Stack(int=0); - virtual ~Stack(); + Stack(int=0); + virtual ~Stack(); - bool Peek(T &); ///<尝试访问一个数据 - virtual bool Pop(T &); ///<弹出一个数据 - bool Push(T &); ///<压入一个数据 - bool MultiPush(T *,int); ///<放入多个数据 + bool Peek(T &); ///<尝试访问一个数据 + virtual bool Pop(T &); ///<弹出一个数据 + bool Push(T &); ///<压入一个数据 + bool MultiPush(T *,int); ///<放入多个数据 - virtual void Clear(); ///<清除所有数据 + virtual void Clear(); ///<清除所有数据 bool GetItem(int,T &); - virtual void operator =(const Stack &); - };//template class Stack + virtual void operator =(const Stack &); + };//template class Stack - template class StackObject:public Stack ///堆栈对象 - { - public: + template class StackObject:public Stack ///堆栈对象 + { + public: - using Stack::Stack; - virtual ~StackObject(){Clear();}; + using Stack::Stack; + virtual ~StackObject(){Clear();}; - void Clear(); - };//template class StackObject + void Clear(); + };//template class StackObject }//namespace hgl #include #endif//HGL_STACK_INCLUDE diff --git a/inc/hgl/type/StringInstance.h b/inc/hgl/type/StringInstance.h index 74dbfcdc..d267485b 100644 --- a/inc/hgl/type/StringInstance.h +++ b/inc/hgl/type/StringInstance.h @@ -9,14 +9,14 @@ namespace hgl /** * 字符串实例类 */ - template class StringInstance ///字符串实例类 + template class StringInstance ///字符串实例类 { protected: typedef StringInstance SelfClass; - int length; ///<字符串长度 - int malloc_length; ///<空间实际分配长度 + int length; ///<字符串长度 + int malloc_length; ///<空间实际分配长度 T *buffer; @@ -50,7 +50,7 @@ namespace hgl { length=hgl::strlen(str,len); - while(length&&!str[length-1]) //清除后面的0 + while(length&&!str[length-1]) //清除后面的0 length--; } @@ -64,7 +64,7 @@ namespace hgl return; } - if(one_instance&&str[length]==0) //如果最后不是0,则需要重新分配内存创建带0结尾的字串 + if(one_instance&&str[length]==0) //如果最后不是0,则需要重新分配内存创建带0结尾的字串 { malloc_length=len; buffer=(T *)str; @@ -103,15 +103,15 @@ namespace hgl virtual ~StringInstance() { - delete[] buffer; //delete[] NULL; don't is error + delete[] buffer; //delete[] NULL; don't is error } - const bool isEmpty()const ///<是否为空 + const bool isEmpty()const ///<是否为空 { return !buffer; } - SelfClass *CreateCopy() ///<创建一份自身的拷贝 + SelfClass *CreateCopy() ///<创建一份自身的拷贝 { if(!buffer)return(0); @@ -136,12 +136,12 @@ namespace hgl return result; } - T * c_str() ///<取得字符串C指针 + T * c_str() ///<取得字符串C指针 { return buffer; } - const int GetLength()const ///<取得字符串长度 + const int GetLength()const ///<取得字符串长度 { return length; } @@ -324,7 +324,7 @@ namespace hgl return hgl::stricmp(buffer,length,str,num); } - bool Insert(int pos,const T *istr,int len) ///<插入一个字符串 + bool Insert(int pos,const T *istr,int len) ///<插入一个字符串 { if(!istr||!*istr) return(false); @@ -370,16 +370,16 @@ namespace hgl return(true); } - bool Insert(int pos,const T &ch ){return Insert(pos, &ch, 1 );} - bool Insert(int pos,const T *str ){return Insert(pos, str, hgl::strlen(str));} - bool Insert(int pos,const SelfClass &str){return Insert(pos, str.c_str(),str.GetLength() );} + bool Insert(int pos,const T &ch ){return Insert(pos, &ch, 1 );} + bool Insert(int pos,const T *str ){return Insert(pos, str, hgl::strlen(str));} + bool Insert(int pos,const SelfClass &str){return Insert(pos, str.c_str(),str.GetLength() );} - bool Append(const T &ch ){return Insert(length, &ch, 1 );} - bool Append(const T *str,const int len ){return Insert(length, str, len );} - bool Append(const T *str ){return Insert(length, str, hgl::strlen(str));} - bool Append(const SelfClass &str ){return Insert(length, str.c_str(),str.GetLength() );} + bool Append(const T &ch ){return Insert(length, &ch, 1 );} + bool Append(const T *str,const int len ){return Insert(length, str, len );} + bool Append(const T *str ){return Insert(length, str, hgl::strlen(str));} + bool Append(const SelfClass &str ){return Insert(length, str.c_str(),str.GetLength() );} - bool Delete(int pos,int num) ///<删除指定字符 + bool Delete(int pos,int num) ///<删除指定字符 { if(pos<0||pos>=length||num<0)return(false); diff --git a/inc/hgl/type/StringList.h b/inc/hgl/type/StringList.h index 8f0121aa..70cffa00 100644 --- a/inc/hgl/type/StringList.h +++ b/inc/hgl/type/StringList.h @@ -14,7 +14,7 @@ namespace hgl /** * 字符串列表处理类,用于保存字符串列表。 */ - template class StringList ///字符串列表处理类 + template class StringList ///字符串列表处理类 { static T NullString; @@ -22,12 +22,12 @@ namespace hgl ObjectList Items; - public: //属性 + public: //属性 - T **GetDataList()const{return Items.GetData();} ///<取得字符串列表指针数据 - const int GetCount()const{return Items.GetCount();} ///<字符串列表行数虚拟变量 + T **GetDataList()const{return Items.GetData();} ///<取得字符串列表指针数据 + const int GetCount()const{return Items.GetCount();} ///<字符串列表行数虚拟变量 - public: //操作符重载 + public: //操作符重载 T &operator[](int n)const { @@ -60,22 +60,22 @@ namespace hgl public: //方法 - StringList()=default; ///<本类构造函数 + StringList()=default; ///<本类构造函数 //注:这里不要实现StringList(T &)或StringList(T *)之类 - virtual ~StringList(){Clear();} ///<本类析构函数 + virtual ~StringList(){Clear();} ///<本类析构函数 /** * 增加一行字符串到当前列表中 * @param str 要增加的字符串 * @return 增加字符串成功后的索引 */ - int Add(const T &str){return Items.Add(new T(str));} ///<添加字符串 + int Add(const T &str){return Items.Add(new T(str));} ///<添加字符串 /** * 增加一个字符串列表到当前字符串列表中 * @param sl 要增加的字符串列表 */ - int Add(const StringList &sl) ///<添加字符串 + int Add(const StringList &sl) ///<添加字符串 { const int count=sl.GetCount(); @@ -88,13 +88,13 @@ namespace hgl /** * 清除所有字符串 */ - void Clear(){Items.Clear();} ///<删除列表中的所有字符串 + void Clear(){Items.Clear();} ///<删除列表中的所有字符串 /** * 删除指定的字符串 * @param index 要删除的字符串索引 */ - void Delete(int index){Items.Delete(index);} ///<删除列表中指定的字符串 + void Delete(int index){Items.Delete(index);} ///<删除列表中指定的字符串 /** * 删除指定字符串 @@ -123,7 +123,7 @@ namespace hgl * @param str 要查找的字符串 * @return 查找到的字符串的索引,未找到返回-1 */ - int Find(const T &str) const ///<查找字符串,未找到返回-1 + int Find(const T &str) const ///<查找字符串,未找到返回-1 { const int count=Items.GetCount(); @@ -139,7 +139,7 @@ namespace hgl * @param str 要指找的字符串 * @return 查找到的字符串的索引,未找到返回-1 */ - int CaseFind(const T &str) const ///<查找字符串,英文无视大小写,未找到返回-1 + int CaseFind(const T &str) const ///<查找字符串,英文无视大小写,未找到返回-1 { const int count=Items.GetCount(); @@ -189,7 +189,7 @@ namespace hgl * @param index 要插入字符串的位置 * @param str 要插入的字符串 */ - void Insert(int index,const T &str) ///<在指定位置插入一个字符串 + void Insert(int index,const T &str) ///<在指定位置插入一个字符串 { if(index::Insert(index,new T(str)); @@ -200,12 +200,12 @@ namespace hgl * @param index1 第一个字符串的位置 * @param index2 第二个字符串的位置 */ - void Exchange(int index1,int index2) ///<交换两行字符串 + void Exchange(int index1,int index2) ///<交换两行字符串 { Items.Exchange(index1,index2); } - T &GetString(int n)const{return *(Items[n]);} ///<取得指定行字符串 + T &GetString(int n)const{return *(Items[n]);} ///<取得指定行字符串 };//template class StringList template T StringList::NullString; ///<空字符串实例 @@ -427,7 +427,7 @@ namespace hgl return count; } - if(*p==0x0D) // \r + if(*p==0x0D) // \r { sl.Add(BaseString(sp,p-sp)); ++count; @@ -435,7 +435,7 @@ namespace hgl ++p; --size; - if(*p==0x0A) // \r\n,Windows下的断行是(\r\n)0x0D+0x0A,而其它系统是仅有一个 + if(*p==0x0A) // \r\n,Windows下的断行是(\r\n)0x0D+0x0A,而其它系统是仅有一个 { ++p; --size; @@ -444,7 +444,7 @@ namespace hgl sp=p; } else - if(*p==0x0A) // \n + if(*p==0x0A) // \n { sl.Add(BaseString(sp,p-sp)); ++count; @@ -509,7 +509,7 @@ namespace hgl return count; } - if(*p==0x0D) // \r + if(*p==0x0D) // \r { sl[index]->Add(BaseString(sp,p-sp)); if(++index==slc)index=0; @@ -519,7 +519,7 @@ namespace hgl ++p; --size; - if(*p==0x0A) // \r\n,Windows下的断行是(\r\n)0x0D+0x0A,而其它系统是仅有一个 + if(*p==0x0A) // \r\n,Windows下的断行是(\r\n)0x0D+0x0A,而其它系统是仅有一个 { ++p; --size; @@ -528,7 +528,7 @@ namespace hgl sp=p; } else - if(*p==0x0A) // \n + if(*p==0x0A) // \n { sl[index]->Add(BaseString(sp,p-sp)); if(++index==slc)index=0; @@ -566,12 +566,12 @@ namespace hgl return SplitToMultiStringList(sl,slc,str.c_str(),str.Length()); } - using UTF8StringList =StringList; - using UTF16StringList =StringList; - using UTF32StringList =StringList; - using AnsiStringList =StringList; - using WideStringList =StringList; - using OSStringList =StringList; + using UTF8StringList =StringList; + using UTF16StringList =StringList; + using UTF32StringList =StringList; + using AnsiStringList =StringList; + using WideStringList =StringList; + using OSStringList =StringList; template struct ReadStringFromDIS { diff --git a/inc/hgl/type/Vertex2.cpp b/inc/hgl/type/Vertex2.cpp index b732e548..3737eae3 100644 --- a/inc/hgl/type/Vertex2.cpp +++ b/inc/hgl/type/Vertex2.cpp @@ -6,115 +6,115 @@ namespace hgl { - template - void Vertex2::Scale(double vx,double vy,double scale) - { - x=vx+(x-vx)*scale; - y=vy+(y-vy)*scale; - } + template + void Vertex2::Scale(double vx,double vy,double scale) + { + x=vx+(x-vx)*scale; + y=vy+(y-vy)*scale; + } - template - void Vertex2::Scale(double vx,double vy,double sx,double sy) - { - x=vx+(x-vx)*sx; - y=vy+(y-vy)*sy; - } - - template template - void Vertex2::Scale(const Vertex2 &v,double scale) - { - x=v.x+(x-v.x)*scale; - y=v.y+(y-v.y)*scale; - } + template + void Vertex2::Scale(double vx,double vy,double sx,double sy) + { + x=vx+(x-vx)*sx; + y=vy+(y-vy)*sy; + } - template template - void Vertex2::Scale(const Vertex2 &v,double sx,double sy) - { - x=v.x+(x-v.x)*sx; - y=v.y+(y-v.y)*sy; - } - - template - void Vertex2::Rotate(double vx,double vy,double ang) - { - double as,ac; - double nx,ny; + template template + void Vertex2::Scale(const Vertex2 &v,double scale) + { + x=v.x+(x-v.x)*scale; + y=v.y+(y-v.y)*scale; + } + + template template + void Vertex2::Scale(const Vertex2 &v,double sx,double sy) + { + x=v.x+(x-v.x)*sx; + y=v.y+(y-v.y)*sy; + } + + template + void Vertex2::Rotate(double vx,double vy,double ang) + { + double as,ac; + double nx,ny; as=sin(ang*HGL_PI/180.0f); - ac=cos(ang*HGL_PI/180.0f); + ac=cos(ang*HGL_PI/180.0f); - nx=vx+((x-vx)*ac-(y-vy)*as); - ny=vy+((x-vx)*as+(y-vy)*ac); + nx=vx+((x-vx)*ac-(y-vy)*as); + ny=vy+((x-vx)*as+(y-vy)*ac); - x=nx;y=ny; - } - - template - float Vertex2::Length() const - { - return sqrt(x*x+y*y); + x=nx;y=ny; } - template - float Vertex2::Length(float vx,float vy) const - { - float w=x-vx; + template + float Vertex2::Length() const + { + return sqrt(x*x+y*y); + } + + template + float Vertex2::Length(float vx,float vy) const + { + float w=x-vx; float h=y-vy; - return float(sqrt(double(w*w+h*h))); + return float(sqrt(double(w*w+h*h))); } - - template - float Vertex2::Angle(float vx,float vy) const - { - if(vx==x) - { - if(vy>y)return(90); - else return(270); - } - if(vy==y) - { - if(vx>x)return(0); - else return(180); - } + template + float Vertex2::Angle(float vx,float vy) const + { + if(vx==x) + { + if(vy>y)return(90); + else return(270); + } - // asin 参数范围必须在 -1 到 +1 之间,否则会报错 - // + if(vy==y) + { + if(vx>x)return(0); + else return(180); + } - double xx=vx-x; - double yy=vy-y; + // asin 参数范围必须在 -1 到 +1 之间,否则会报错 + // - double ang = atan(yy/xx)/HGL_PI*180.0f; + double xx=vx-x; + double yy=vy-y; - if(xx<0) - return(ang+180); - else - { - if(yy<0) - return(ang+360); - else - return(ang); - } - } - - template template - Vertex2 Vertex2::To(const Vertex2 &target,double pos)const - { - if(pos==0)return(*this);else - if(pos==1)return(target);else - { - Vertex2 result; + double ang = atan(yy/xx)/HGL_PI*180.0f; - result.x=x+(target.x-x)*pos; - result.y=y+(target.y-y)*pos; + if(xx<0) + return(ang+180); + else + { + if(yy<0) + return(ang+360); + else + return(ang); + } + } - return(result); - } - } - - template - void Vertex2::Normalize() + template template + Vertex2 Vertex2::To(const Vertex2 &target,double pos)const + { + if(pos==0)return(*this);else + if(pos==1)return(target);else + { + Vertex2 result; + + result.x=x+(target.x-x)*pos; + result.y=y+(target.y-y)*pos; + + return(result); + } + } + + template + void Vertex2::Normalize() { double size=sqrt(x*x+y*y); @@ -125,23 +125,23 @@ namespace hgl } x=0;y=0; - } - - template template - Vertex2 Vertex2::ResizeLength(const Vertex2 &target,double scale)const - { - Vertex2 result; + } - result.x=target.x+(x-target.x)*scale; - result.y=target.y+(y-target.y)*scale; + template template + Vertex2 Vertex2::ResizeLength(const Vertex2 &target,double scale)const + { + Vertex2 result; - return(result); - } - - template template - Vertex2 Vertex2::ToLength(const Vertex2 &target,double new_length)const - { - return ResizeLength(target,new_length/Length(target)); - } + result.x=target.x+(x-target.x)*scale; + result.y=target.y+(y-target.y)*scale; + + return(result); + } + + template template + Vertex2 Vertex2::ToLength(const Vertex2 &target,double new_length)const + { + return ResizeLength(target,new_length/Length(target)); + } }//namespace hgl #endif//HGL_VERTEX_2_CPP diff --git a/inc/hgl/type/Vertex2.h b/inc/hgl/type/Vertex2.h index bdd94560..6185dc77 100644 --- a/inc/hgl/type/Vertex2.h +++ b/inc/hgl/type/Vertex2.h @@ -5,109 +5,109 @@ #include namespace hgl { - /** - * 2D顶点坐标类模板 - */ - template class Vertex2 ///XY 2D顶点坐标类模板 - { - public: + /** + * 2D顶点坐标类模板 + */ + template class Vertex2 ///XY 2D顶点坐标类模板 + { + public: - T x,y; ///<坐标值 + T x,y; ///<坐标值 - public: + public: - Vertex2(){x=y=0;} ///<本类构造函数 - Vertex2(T nx,T ny){x=nx,y=ny;} ///<本类构造函数 - template - Vertex2(const Vertex2 &v){x=v.x,y=v.y;} ///<本类构造函数 + Vertex2(){x=y=0;} ///<本类构造函数 + Vertex2(T nx,T ny){x=nx,y=ny;} ///<本类构造函数 + template + Vertex2(const Vertex2 &v){x=v.x,y=v.y;} ///<本类构造函数 - void Zero(){x=0,y=0;} ///<将x和y全清为0 - void One(){x=1,y=1;} ///<将x和y全清为1 + void Zero(){x=0,y=0;} ///<将x和y全清为0 + void One(){x=1,y=1;} ///<将x和y全清为1 - void Set(float nx,float ny){x=nx,y=ny;} ///<设置坐标值为nx,ny - - void Flip(float vx,float vy){FlipX(vy);FlipY(vx);} ///<对应vx,vy做翻转 - template void Flip(const Vertex2 &v){Flip(v.x,v.y);} ///<对应v做翻转 + void Set(float nx,float ny){x=nx,y=ny;} ///<设置坐标值为nx,ny - void FlipX(){y=-y;} ///<对应X轴翻转 - void FlipY(){x=-x;} ///<对应Y轴翻转 - void FlipX(float v){y=v-(y-v);} ///<对应指定X轴做翻转 - void FlipY(float v){x=v-(x-v);} ///<对应指定Y轴做翻转 + void Flip(float vx,float vy){FlipX(vy);FlipY(vx);} ///<对应vx,vy做翻转 + template void Flip(const Vertex2 &v){Flip(v.x,v.y);} ///<对应v做翻转 - void Scale(double,double,double); ///<相对某一点做缩放 - void Scale(double,double,double,double); ///<相对某一点做缩放 - template void Scale(const Vertex2 &,double); ///<相对某一点做缩放 - template void Scale(const Vertex2 &,double,double); ///<相对某一点做指定比例的缩放 + void FlipX(){y=-y;} ///<对应X轴翻转 + void FlipY(){x=-x;} ///<对应Y轴翻转 + void FlipX(float v){y=v-(y-v);} ///<对应指定X轴做翻转 + void FlipY(float v){x=v-(x-v);} ///<对应指定Y轴做翻转 - void Rotate(double,double,double); ///<绕指定坐标旋转指定角度 - template void Rotate(const Vertex2 &v,double ang){Rotate(v.x,v.y,ang);} ///<绕指定坐标旋转指定角度 + void Scale(double,double,double); ///<相对某一点做缩放 + void Scale(double,double,double,double); ///<相对某一点做缩放 + template void Scale(const Vertex2 &,double); ///<相对某一点做缩放 + template void Scale(const Vertex2 &,double,double); ///<相对某一点做指定比例的缩放 - void Move(float vx,float vy){x+=vx;y+=vy;} ///<移动 - template - void Scale(const Vertex2 &v,const Vertex2 &l){Scale(v,l.x,l.y);} ///<相对某一点做指定比例的缩放 + void Rotate(double,double,double); ///<绕指定坐标旋转指定角度 + template void Rotate(const Vertex2 &v,double ang){Rotate(v.x,v.y,ang);} ///<绕指定坐标旋转指定角度 - float Length()const; ///<计算当前点与零点之间的距离 - float Length(float vx,float vy)const; ///<计算当前点与指定点之间的距离 - float Angle(float vx,float vy)const; ///<计算当前点与指定点之间的角度 + void Move(float vx,float vy){x+=vx;y+=vy;} ///<移动 + template + void Scale(const Vertex2 &v,const Vertex2 &l){Scale(v,l.x,l.y);} ///<相对某一点做指定比例的缩放 - template float Length(const Vertex2 &v)const{return(Length(v.x,v.y));} ///<计算当前点与指定点之间的距离 - template float Angle(const Vertex2 &v)const{return(Angle(v.x,v.y));} ///<计算当前点与指定点之间的角度 - void Normalize(); + float Length()const; ///<计算当前点与零点之间的距离 + float Length(float vx,float vy)const; ///<计算当前点与指定点之间的距离 + float Angle(float vx,float vy)const; ///<计算当前点与指定点之间的角度 - template Vertex2 ResizeLength(const Vertex2 &,double)const; ///<调整这个方向上两点的距离 - template Vertex2 ToLength(const Vertex2 &,double)const; ///<按这个方向将两点的距离调到指定长度 + template float Length(const Vertex2 &v)const{return(Length(v.x,v.y));} ///<计算当前点与指定点之间的距离 + template float Angle(const Vertex2 &v)const{return(Angle(v.x,v.y));} ///<计算当前点与指定点之间的角度 + void Normalize(); - template Vertex2 To(const Vertex2 &,double)const; + template Vertex2 ResizeLength(const Vertex2 &,double)const; ///<调整这个方向上两点的距离 + template Vertex2 ToLength(const Vertex2 &,double)const; ///<按这个方向将两点的距离调到指定长度 - public: //操作符重载 + template Vertex2 To(const Vertex2 &,double)const; - template bool operator == (const Vertex2 &v)const - { - if(x!=v.x)return(false); - if(y!=v.y)return(false); - return(true); - } + public: //操作符重载 - template bool operator != (const Vertex2 &v)const - { - if(x!=v.x)return(true); - if(y!=v.y)return(true); - return(false); - } + template bool operator == (const Vertex2 &v)const + { + if(x!=v.x)return(false); + if(y!=v.y)return(false); + return(true); + } - Vertex2 &operator = (const float *v){x=*v++;y=*v;return *this;} + template bool operator != (const Vertex2 &v)const + { + if(x!=v.x)return(true); + if(y!=v.y)return(true); + return(false); + } - template Vertex2 &operator +=(const Vertex2 &v){x+=v.x;y+=v.y;return *this;} - template Vertex2 &operator -=(const Vertex2 &v){x-=v.x;y-=v.y;return *this;} - template Vertex2 &operator *=(const Vertex2 &v){x*=v.x;y*=v.y;return *this;} - template Vertex2 &operator /=(const Vertex2 &v){x/=v.x;y/=v.y;return *this;} + Vertex2 &operator = (const float *v){x=*v++;y=*v;return *this;} - Vertex2 &operator *=(const float v){x*=v;y*=v;return *this;} - Vertex2 &operator /=(const float v){x/=v;y/=v;return *this;} + template Vertex2 &operator +=(const Vertex2 &v){x+=v.x;y+=v.y;return *this;} + template Vertex2 &operator -=(const Vertex2 &v){x-=v.x;y-=v.y;return *this;} + template Vertex2 &operator *=(const Vertex2 &v){x*=v.x;y*=v.y;return *this;} + template Vertex2 &operator /=(const Vertex2 &v){x/=v.x;y/=v.y;return *this;} - template Vertex2 operator + (const Vertex2 &v)const{return(Vertex2(x+v.x,y+v.y));} - template Vertex2 operator - (const Vertex2 &v)const{return(Vertex2(x-v.x,y-v.y));} - template Vertex2 operator * (const Vertex2 &v)const{return(Vertex2(x*v.x,y*v.y));} - template Vertex2 operator / (const Vertex2 &v)const{return(Vertex2(x/v.x,y/v.y));} + Vertex2 &operator *=(const float v){x*=v;y*=v;return *this;} + Vertex2 &operator /=(const float v){x/=v;y/=v;return *this;} - Vertex2 operator * (const float v)const{return(Vertex2(x*v,y*v));} - Vertex2 operator / (const float v)const{return(Vertex2(x/v,y/v));} + template Vertex2 operator + (const Vertex2 &v)const{return(Vertex2(x+v.x,y+v.y));} + template Vertex2 operator - (const Vertex2 &v)const{return(Vertex2(x-v.x,y-v.y));} + template Vertex2 operator * (const Vertex2 &v)const{return(Vertex2(x*v.x,y*v.y));} + template Vertex2 operator / (const Vertex2 &v)const{return(Vertex2(x/v.x,y/v.y));} - Vertex2 operator - () const {return(Vertex2(-x,-y));} + Vertex2 operator * (const float v)const{return(Vertex2(x*v,y*v));} + Vertex2 operator / (const float v)const{return(Vertex2(x/v,y/v));} - operator T *() const {return(( T *)this);} ///<使得本类可以直接当做T *使用 - operator const T *() const {return((const T *)this);} ///<使得本类可以直接当做const T *使用 - };//class Vertex2 + Vertex2 operator - () const {return(Vertex2(-x,-y));} - typedef Vertex2 Vertex2i; - typedef Vertex2 Vertex2ui; - typedef Vertex2 Vertex2f; - typedef Vertex2 Vertex2d; + operator T *() const {return(( T *)this);} ///<使得本类可以直接当做T *使用 + operator const T *() const {return((const T *)this);} ///<使得本类可以直接当做const T *使用 + };//class Vertex2 - typedef Vertex2 Vertex2b; - typedef Vertex2 Vertex2ub; - typedef Vertex2 Vertex2s; - typedef Vertex2 Vertex2us; + typedef Vertex2 Vertex2i; + typedef Vertex2 Vertex2ui; + typedef Vertex2 Vertex2f; + typedef Vertex2 Vertex2d; + + typedef Vertex2 Vertex2b; + typedef Vertex2 Vertex2ub; + typedef Vertex2 Vertex2s; + typedef Vertex2 Vertex2us; }//namespace hgl #include #endif//HGL_VERTEX_2_INCLUDE diff --git a/inc/hgl/type/_Object.h b/inc/hgl/type/_Object.h index 3c2e963a..2fa958b2 100644 --- a/inc/hgl/type/_Object.h +++ b/inc/hgl/type/_Object.h @@ -3,34 +3,34 @@ namespace hgl { - /** - * 最终基类 - */ - class _Object ///最终基类 - { - public: + /** + * 最终基类 + */ + class _Object ///最终基类 + { + public: - virtual ~_Object()=default; ///<本类析构函数 - };//class _Object + virtual ~_Object()=default; ///<本类析构函数 + };//class _Object - typedef void (_Object::*ObjectMemberFunc)(); + typedef void (_Object::*ObjectMemberFunc)(); - //此代码取自AngelScript,感谢 - #define GetMemberFuncPointer(c,m) MethodPtr::Convert((void (c::*)())(&c::m)) + //此代码取自AngelScript,感谢 + #define GetMemberFuncPointer(c,m) MethodPtr::Convert((void (c::*)())(&c::m)) - template struct MethodPtr - { - template - static void *Convert(M Mthd) - { - union u - { - M m; - void *v; - }; + template struct MethodPtr + { + template + static void *Convert(M Mthd) + { + union u + { + M m; + void *v; + }; - return ((u *)(&Mthd))->v; - } - };//struct MethodPtr + return ((u *)(&Mthd))->v; + } + };//struct MethodPtr }//namespace hgl #endif//HGL__OBJECT_INCLUDE diff --git a/src/Base/DataType/Color.cpp b/src/Base/DataType/Color.cpp index 707a3f2a..dbec16f4 100644 --- a/src/Base/DataType/Color.cpp +++ b/src/Base/DataType/Color.cpp @@ -145,15 +145,15 @@ namespace hgl DEF_COLOR(MistyRose, 255,228,225,"浅玫瑰") DEF_COLOR(Moccasin, 255,228,181,"鹿皮色") - DEF_COLOR(MozillaBlue, 0, 83,159,"火狐蓝") + DEF_COLOR(MozillaBlue, 0, 83,159,"火狐蓝") DEF_COLOR(MozillaCharcoal, 77, 78, 83,"谋智炭") - DEF_COLOR(MozillaLightBlue, 0,150,221,"火狐亮蓝") - DEF_COLOR(MozillaLightOrange, 255,149, 0,"火狐亮橙") - DEF_COLOR(MoziilaNightBlue, 0, 33, 71,"谋智暗夜蓝") - DEF_COLOR(MozillaOrange, 230, 96, 0,"火狐橙") + DEF_COLOR(MozillaLightBlue, 0,150,221,"火狐亮蓝") + DEF_COLOR(MozillaLightOrange, 255,149, 0,"火狐亮橙") + DEF_COLOR(MoziilaNightBlue, 0, 33, 71,"谋智暗夜蓝") + DEF_COLOR(MozillaOrange, 230, 96, 0,"火狐橙") DEF_COLOR(MozillaRed, 193, 56, 50,"谋智红") DEF_COLOR(MozillaSand, 215,211,200,"谋智沙") - DEF_COLOR(MozillaYellow, 255,203, 0,"火狐黄") + DEF_COLOR(MozillaYellow, 255,203, 0,"火狐黄") DEF_COLOR(NavajoWhite, 255,222,173,"纳瓦白") DEF_COLOR(Navy, 0, 0,128,"海军色") diff --git a/src/Base/FileSystem/FileSystem.cpp b/src/Base/FileSystem/FileSystem.cpp index 2e4c5e36..20c368ce 100644 --- a/src/Base/FileSystem/FileSystem.cpp +++ b/src/Base/FileSystem/FileSystem.cpp @@ -115,32 +115,32 @@ namespace hgl * @param filename 要保存的文件名称 * @param buf_list 要保存的内存块指针列表 * @param buf_size 要保存的内存块数据长度列表 - * @param buf_count 要保存的内存块数量 + * @param buf_count 要保存的内存块数量 * @return 成功写入的字节数 * @return -1 失败 */ - int64 SaveMemoryToFile(const OSString &filename,void **buf_list,const int64 *buf_size,const int &buf_count) - { + int64 SaveMemoryToFile(const OSString &filename,void **buf_list,const int64 *buf_size,const int &buf_count) + { io::FileOutputStream fs; if(!fs.CreateTrunc(filename)) return(-1); - int64 total=0; - int64 result; + int64 total=0; + int64 result; - for(int i=0;i=str_len) { if(dis->ReadArrays(buf,str_len)!=str_len) @@ -64,25 +64,25 @@ namespace hgl return(true); } - + if(dis->ReadArrays(buf,max_len)!=max_len) return(false); dis->Skip(str_len-max_len); return(true); - } + } bool DataInputStream::ReadUTF8String (char *u8str,uint max_len){return hgl::io::ReadUTF8String(u8str,max_len,this);} bool DataInputStream::ReadUTF8ShortString (char *u8str,uint max_len){return hgl::io::ReadUTF8String(u8str,max_len,this);} bool DataInputStream::ReadUTF8TinyString (char *u8str,uint max_len){return hgl::io::ReadUTF8String(u8str,max_len,this);} - + template bool ReadUTF8String(UTF8String &u8str,uint max_len,DataInputStream *dis) { if(!max_len||!dis) return(false); - + T str_len; if(!dis->ReadNumber(str_len)) @@ -111,13 +111,13 @@ namespace hgl bool DataInputStream::ReadUTF8String (UTF8String &u8str,uint max_len){return hgl::io::ReadUTF8String(u8str,max_len,this);} bool DataInputStream::ReadUTF8ShortString (UTF8String &u8str,uint max_len){return hgl::io::ReadUTF8String(u8str,max_len,this);} bool DataInputStream::ReadUTF8TinyString (UTF8String &u8str,uint max_len){return hgl::io::ReadUTF8String(u8str,max_len,this);} - + template bool ReadUTF8String(UTF16String &u16str,uint max_len,DataInputStream *dis) { if(!max_len||!dis) return(false); - + T str_len; if(!dis->ReadNumber(str_len)) @@ -206,7 +206,7 @@ namespace hgl if(!dis||!max_len)return(false); T count; - + if(!dis->ReadNumber(count)) return(false); diff --git a/src/Base/IO/FileInputStream.cpp b/src/Base/IO/FileInputStream.cpp index f1d1077a..26fda0a0 100644 --- a/src/Base/IO/FileInputStream.cpp +++ b/src/Base/IO/FileInputStream.cpp @@ -44,7 +44,7 @@ namespace hgl } } - int64 FileInputStream::Read (void *buf,int64 size) {return file?file->Read(buf,size):-1;} + int64 FileInputStream::Read (void *buf,int64 size) {return file?file->Read(buf,size):-1;} int64 FileInputStream::Peek (void *buf,int64 size) {return file?file->Peek(buf,size):-1;} bool FileInputStream::CanSeek ()const {return file?file->CanSeek():false;} @@ -58,8 +58,8 @@ namespace hgl bool FileInputStream::Restart () {return file?file->Restart():false;} int64 FileInputStream::Seek (int64 off,SeekOrigin so) {return file?file->Seek(off,so):-1;} - int64 FileInputStream::Available ()const {return file?file->AvailableRead():-1;} + int64 FileInputStream::Available ()const {return file?file->AvailableRead():-1;} - int64 FileInputStream::Read (int64 off,void *buf,int64 size){return file?file->Read(off,buf,size):-1;} + int64 FileInputStream::Read (int64 off,void *buf,int64 size){return file?file->Read(off,buf,size):-1;} }//namespace io }//namespace hgl diff --git a/src/Base/IO/FileOutputStream.cpp b/src/Base/IO/FileOutputStream.cpp index 5bccd910..128084e6 100644 --- a/src/Base/IO/FileOutputStream.cpp +++ b/src/Base/IO/FileOutputStream.cpp @@ -44,7 +44,7 @@ namespace hgl } } - int64 FileOutputStream::Write (const void *buf,int64 size) {return file?file->Write(buf,size):-1;} + int64 FileOutputStream::Write (const void *buf,int64 size) {return file?file->Write(buf,size):-1;} bool FileOutputStream::CanSeek ()const {return file?file->CanSeek():false;} bool FileOutputStream::CanRestart()const {return file?file->CanRestart():false;} @@ -55,6 +55,6 @@ namespace hgl bool FileOutputStream::Restart () {return file?file->Restart():false;} int64 FileOutputStream::Seek (int64 off,SeekOrigin so) {return file?file->Seek(off,so):-1;} - int64 FileOutputStream::Write (int64 off,const void *buf,int64 size) {return file?file->Write(off,buf,size):-1;} + int64 FileOutputStream::Write (int64 off,const void *buf,int64 size) {return file?file->Write(off,buf,size):-1;} }//namespace io }//namespace hgl diff --git a/src/Base/Other/ThreadFunc.cpp b/src/Base/Other/ThreadFunc.cpp index bede6325..137ae117 100644 --- a/src/Base/Other/ThreadFunc.cpp +++ b/src/Base/Other/ThreadFunc.cpp @@ -4,7 +4,7 @@ namespace hgl { /** - * tips: PTHREAD_CREATE_DETACHED 方式创建的线程,在退出时,自动清除线程。无法使用pthread_join函数获取运行状态,pthread_join会返回22号错误 + * tips: PTHREAD_CREATE_DETACHED 方式创建的线程,在退出时,自动清除线程。无法使用pthread_join函数获取运行状态,pthread_join会返回22号错误 * PTHREAD_CREATE_JOINABLE 方式创建的线程,在退出时,不会清除线程,必使使用pthread_join函数获取。或是在退出时使用pthread_detach(pthread_self())。 */ diff --git a/src/Base/Other/TimeVal.cpp b/src/Base/Other/TimeVal.cpp index 8205769b..7187e446 100644 --- a/src/Base/Other/TimeVal.cpp +++ b/src/Base/Other/TimeVal.cpp @@ -6,9 +6,9 @@ namespace hgl { - void SetTimeVal(timeval &tv,const double t_sec) - { - tv.tv_sec=t_sec; - tv.tv_usec=(t_sec-(double)tv.tv_sec)*HGL_MICRO_SEC_PER_SEC; - } + void SetTimeVal(timeval &tv,const double t_sec) + { + tv.tv_sec=t_sec; + tv.tv_usec=(t_sec-(double)tv.tv_sec)*HGL_MICRO_SEC_PER_SEC; + } }//namespace hgl diff --git a/src/Platform/Android/AndroidMain.cpp b/src/Platform/Android/AndroidMain.cpp index 4a281536..3b53b6a8 100644 --- a/src/Platform/Android/AndroidMain.cpp +++ b/src/Platform/Android/AndroidMain.cpp @@ -23,9 +23,9 @@ */ struct saved_state { - float angle; - int32_t x; - int32_t y; + float angle; + int32_t x; + int32_t y; }; /** @@ -33,20 +33,20 @@ struct saved_state */ struct engine { - struct android_app* app; + struct android_app* app; - ASensorManager* sensorManager; - const ASensor* accelerometerSensor; - ASensorEventQueue* sensorEventQueue; + ASensorManager* sensorManager; + const ASensor* accelerometerSensor; + ASensorEventQueue* sensorEventQueue; ASensorEvent acceleration_event; - int animating; - EGLDisplay display; - EGLSurface surface; - EGLContext context; - int32_t width; - int32_t height; - struct saved_state state; + int animating; + EGLDisplay display; + EGLSurface surface; + EGLContext context; + int32_t width; + int32_t height; + struct saved_state state; }; /** @@ -54,15 +54,15 @@ struct engine */ static int engine_init_display(struct engine* engine) { - // initialize OpenGL ES and EGL + // initialize OpenGL ES and EGL - /* - * Here specify the attributes of the desired configuration. - * Below, we select an EGLConfig with at least 8 bits per color - * component compatible with on-screen windows - */ - const EGLint config_attrs[] = - { + /* + * Here specify the attributes of the desired configuration. + * Below, we select an EGLConfig with at least 8 bits per color + * component compatible with on-screen windows + */ + const EGLint config_attrs[] = + { EGL_DEPTH_SIZE, 0, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, @@ -72,7 +72,7 @@ static int engine_init_display(struct engine* engine) EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, //即使是用es3这里也写es2 EGL_NONE - }; + }; const EGLint context_attrs[] = { @@ -80,15 +80,15 @@ static int engine_init_display(struct engine* engine) EGL_NONE }; - EGLint w, h, format; - EGLint numConfigs; - EGLConfig config; - EGLSurface surface; - EGLContext context; + EGLint w, h, format; + EGLint numConfigs; + EGLConfig config; + EGLSurface surface; + EGLContext context; - EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); + EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); - eglInitialize(display, 0, 0); + eglInitialize(display, 0, 0); EGLint config_count; bool depth_check=false; @@ -109,38 +109,38 @@ static int engine_init_display(struct engine* engine) if(!depth_check) return(-1); - /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is - * guaranteed to be accepted by ANativeWindow_setBuffersGeometry(). - * As soon as we picked a EGLConfig, we can safely reconfigure the - * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */ - eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format); + /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is + * guaranteed to be accepted by ANativeWindow_setBuffersGeometry(). + * As soon as we picked a EGLConfig, we can safely reconfigure the + * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */ + eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format); - ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format); + ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format); - surface = eglCreateWindowSurface(display, config, engine->app->window, nullptr); + surface = eglCreateWindowSurface(display, config, engine->app->window, nullptr); - context = eglCreateContext(display, config, nullptr, context_attrs); + context = eglCreateContext(display, config, nullptr, context_attrs); - if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) + if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { - LOGW("Unable to eglMakeCurrent"); - return -1; - } + LOGW("Unable to eglMakeCurrent"); + return -1; + } - eglQuerySurface(display, surface, EGL_WIDTH, &w); - eglQuerySurface(display, surface, EGL_HEIGHT, &h); + eglQuerySurface(display, surface, EGL_WIDTH, &w); + eglQuerySurface(display, surface, EGL_HEIGHT, &h); - engine->display = display; - engine->context = context; - engine->surface = surface; - engine->width = w; - engine->height = h; - engine->state.angle = 0; + engine->display = display; + engine->context = context; + engine->surface = surface; + engine->width = w; + engine->height = h; + engine->state.angle = 0; - // Initialize GL state. - Cube_setupGL(w, h); + // Initialize GL state. + Cube_setupGL(w, h); - return 0; + return 0; } /** @@ -149,16 +149,16 @@ static int engine_init_display(struct engine* engine) static void engine_draw_frame(struct engine* engine) { - if (engine->display == nullptr) { - // No display. - return; - } + if (engine->display == nullptr) { + // No display. + return; + } - Cube_prepare(); + Cube_prepare(); - Cube_draw(); + Cube_draw(); - eglSwapBuffers(engine->display, engine->surface); + eglSwapBuffers(engine->display, engine->surface); } @@ -167,29 +167,29 @@ static void engine_draw_frame(struct engine* engine) */ static void engine_term_display(struct engine* engine) { - if (engine->display != EGL_NO_DISPLAY) + if (engine->display != EGL_NO_DISPLAY) { - eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); - if (engine->context != EGL_NO_CONTEXT) + if (engine->context != EGL_NO_CONTEXT) { - eglDestroyContext(engine->display, engine->context); - } - - if (engine->surface != EGL_NO_SURFACE) + eglDestroyContext(engine->display, engine->context); + } + + if (engine->surface != EGL_NO_SURFACE) { - eglDestroySurface(engine->display, engine->surface); - } - - eglTerminate(engine->display); - } - - engine->animating = 0; - engine->display = EGL_NO_DISPLAY; - engine->context = EGL_NO_CONTEXT; - engine->surface = EGL_NO_SURFACE; + eglDestroySurface(engine->display, engine->surface); + } + + eglTerminate(engine->display); + } + + engine->animating = 0; + engine->display = EGL_NO_DISPLAY; + engine->context = EGL_NO_CONTEXT; + engine->surface = EGL_NO_SURFACE; - Cube_tearDownGL(); + Cube_tearDownGL(); } /** @@ -197,16 +197,16 @@ static void engine_term_display(struct engine* engine) */ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) { - struct engine* engine = (struct engine*)app->userData; + struct engine* engine = (struct engine*)app->userData; - if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) + if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { - engine->state.x = AMotionEvent_getX(event, 0); - engine->state.y = AMotionEvent_getY(event, 0); - return 1; - } - - return 0; + engine->state.x = AMotionEvent_getX(event, 0); + engine->state.y = AMotionEvent_getY(event, 0); + return 1; + } + + return 0; } /** @@ -214,9 +214,9 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) */ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { - struct engine* engine = (struct engine*)app->userData; + struct engine* engine = (struct engine*)app->userData; - switch (cmd) + switch (cmd) { case APP_CMD_SAVE_STATE: // The system has asked us to save our current state. Do so. engine->app->savedState = malloc(sizeof(struct saved_state)); @@ -255,7 +255,7 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) engine->animating = 0; engine_draw_frame(engine); break; - } + } } /** @@ -265,80 +265,80 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) */ void android_main(struct android_app* state) { - struct engine engine; + struct engine engine; - memset(&engine, 0, sizeof(engine)); - state->userData = &engine; - state->onAppCmd = engine_handle_cmd; - state->onInputEvent = engine_handle_input; - engine.app = state; + memset(&engine, 0, sizeof(engine)); + state->userData = &engine; + state->onAppCmd = engine_handle_cmd; + state->onInputEvent = engine_handle_input; + engine.app = state; - // Prepare to monitor accelerometer - engine.sensorManager = ASensorManager_getInstance(); - engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,ASENSOR_TYPE_ACCELEROMETER); - engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager,state->looper, LOOPER_ID_USER, nullptr, nullptr); + // Prepare to monitor accelerometer + engine.sensorManager = ASensorManager_getInstance(); + engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,ASENSOR_TYPE_ACCELEROMETER); + engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager,state->looper, LOOPER_ID_USER, nullptr, nullptr); - if (state->savedState != nullptr) + if (state->savedState != nullptr) { - // We are starting with a previous saved state; restore from it. - engine.state = *(struct saved_state*)state->savedState; - } + // We are starting with a previous saved state; restore from it. + engine.state = *(struct saved_state*)state->savedState; + } - engine.animating = 1; + engine.animating = 1; - // loop waiting for stuff to do. - while (1) + // loop waiting for stuff to do. + while (1) { - // Read all pending events. - int ident; - int events; - struct android_poll_source* source; + // Read all pending events. + int ident; + int events; + struct android_poll_source* source; - // If not animating, we will block forever waiting for events. - // If animating, we loop until all events are read, then continue - // to draw the next frame of animation. - while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, nullptr, &events,(void**)&source)) >= 0) + // If not animating, we will block forever waiting for events. + // If animating, we loop until all events are read, then continue + // to draw the next frame of animation. + while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, nullptr, &events,(void**)&source)) >= 0) { - // Process this event. - if (source != nullptr) + // Process this event. + if (source != nullptr) { - source->process(state, source); - } + source->process(state, source); + } - // If a sensor has data, process it now. - if (ident == LOOPER_ID_USER) + // If a sensor has data, process it now. + if (ident == LOOPER_ID_USER) { - if (engine.accelerometerSensor != nullptr) + if (engine.accelerometerSensor != nullptr) { - while (ASensorEventQueue_getEvents(engine.sensorEventQueue,&engine->acceleration_event;, 1) > 0) + while (ASensorEventQueue_getEvents(engine.sensorEventQueue,&engine->acceleration_event;, 1) > 0) { - LOGI("accelerometer: x=%f y=%f z=%f", - engine->acceleration_event.acceleration.x, + LOGI("accelerometer: x=%f y=%f z=%f", + engine->acceleration_event.acceleration.x, engine->acceleration_event.acceleration.y, - engine->acceleration_event.acceleration.z); - } - } - } + engine->acceleration_event.acceleration.z); + } + } + } - // Check if we are exiting. - if (state->destroyRequested != 0) + // Check if we are exiting. + if (state->destroyRequested != 0) { - engine_term_display(&engine); - return; - } - } + engine_term_display(&engine); + return; + } + } - if (engine.animating) + if (engine.animating) { - // Done with events; draw next animation frame. - Cube_update(); - //if (engine.state.angle > 1) { - // engine.state.angle = 0; - //} + // Done with events; draw next animation frame. + Cube_update(); + //if (engine.state.angle > 1) { + // engine.state.angle = 0; + //} - // Drawing is throttled to the screen update rate, so there - // is no need to do timing here. - engine_draw_frame(&engine); - } - } + // Drawing is throttled to the screen update rate, so there + // is no need to do timing here. + engine_draw_frame(&engine); + } + } } diff --git a/src/Platform/Android/JNISupport.cpp b/src/Platform/Android/JNISupport.cpp index 0428ae5b..1552be8e 100644 --- a/src/Platform/Android/JNISupport.cpp +++ b/src/Platform/Android/JNISupport.cpp @@ -3,8 +3,8 @@ namespace hgl { - namespace logger - { + namespace logger + { void SetLocalAppdataPath(const char *fn); }//namespace logger diff --git a/src/Platform/Android/LogConsole.cpp b/src/Platform/Android/LogConsole.cpp index 106da24d..7b282c39 100644 --- a/src/Platform/Android/LogConsole.cpp +++ b/src/Platform/Android/LogConsole.cpp @@ -12,8 +12,8 @@ namespace hgl { - namespace logger - { + namespace logger + { constexpr uint LOG_BUF_SIZE=4096; constexpr android_LogPriority android_priority[]={ ANDROID_LOG_ERROR, //llError @@ -21,126 +21,126 @@ namespace hgl ANDROID_LOG_INFO, //llHint ANDROID_LOG_VERBOSE}; //llLog - /** - * Android控制台日志插件接口 - */ - class LogAndroidConsole:public Logger - { - char endline; + /** + * Android控制台日志插件接口 + */ + class LogAndroidConsole:public Logger + { + char endline; char thread_string[256]; char time_string[256]; - char log_buf[LOG_BUF_SIZE]; + char log_buf[LOG_BUF_SIZE]; #ifdef LOGINFO_THREAD_MUTEX - ThreadMutex mutex; + ThreadMutex mutex; #endif//LOGINFO_THREAD_MUTEX android_LogPriority prio; - public: + public: - LogAndroidConsole(LogLevel ll):Logger(ll) - { + LogAndroidConsole(LogLevel ll):Logger(ll) + { prio=android_priority[ll]; - endline='\n'; - } + endline='\n'; + } - bool Create(const OSString &) - { - return(true); - } + bool Create(const OSString &) + { + return(true); + } - void Close(){} + void Close(){} #ifdef LOG_INFO_THREAD - void WriteThreadID() - { - memcpy(thread_string,"[Thread:",8); + void WriteThreadID() + { + memcpy(thread_string,"[Thread:",8); - htos(thread_string+8,128-9,pthread_self()); - strcat(thread_string,LOG_BUF_SIZE,']'); - } + htos(thread_string+8,128-9,pthread_self()); + strcat(thread_string,LOG_BUF_SIZE,']'); + } #endif//LOG_INFO_THREAD #ifdef LOG_INFO_TIME - void WriteTime() - { - memcpy(time_string,"[Time:",6); + void WriteTime() + { + memcpy(time_string,"[Time:",6); - ftos(time_string+6,128-strlen(time_string),GetDoubleTime()); - strcat(time_string,LOG_BUF_SIZE,']'); - } + ftos(time_string+6,128-strlen(time_string),GetDoubleTime()); + strcat(time_string,LOG_BUF_SIZE,']'); + } #endif//LOG_INFO_TIME - void Write(const u16char *str,int size) - { - #ifdef LOGINFO_THREAD_MUTEX - mutex.Lock(); - #endif//LOGINFO_THREAD_MUTEX + void Write(const u16char *str,int size) + { + #ifdef LOGINFO_THREAD_MUTEX + mutex.Lock(); + #endif//LOGINFO_THREAD_MUTEX log_buf[0]=0; - #ifdef LOG_INFO_THREAD - WriteThreadID(); + #ifdef LOG_INFO_THREAD + WriteThreadID(); strcpy(log_buf,LOG_BUF_SIZE,thread_string); - #endif//LOG_INFO_THREAD + #endif//LOG_INFO_THREAD - #ifdef LOG_INFO_TIME - WriteTime(); + #ifdef LOG_INFO_TIME + WriteTime(); strcat(log_buf,LOG_BUF_SIZE,time_string); - #endif//LOG_INFO_TIME + #endif//LOG_INFO_TIME - int len; + int len; - len=u16_to_u8(log_buf+hgl::strlen(log_buf),LOG_BUF_SIZE,str,size); + len=u16_to_u8(log_buf+hgl::strlen(log_buf),LOG_BUF_SIZE,str,size); - if(len>0) - { - log_buf[len++]=0; + if(len>0) + { + log_buf[len++]=0; __android_log_write(prio,"",log_buf); - } - #ifdef LOGINFO_THREAD_MUTEX - mutex.Unlock(); - #endif//LOGINFO_THREAD_MUTEX - } + } + #ifdef LOGINFO_THREAD_MUTEX + mutex.Unlock(); + #endif//LOGINFO_THREAD_MUTEX + } - void Write(const char *str,int size) - { - #ifdef LOGINFO_THREAD_MUTEX - mutex.Lock(); - #endif//LOGINFO_THREAD_MUTEX + void Write(const char *str,int size) + { + #ifdef LOGINFO_THREAD_MUTEX + mutex.Lock(); + #endif//LOGINFO_THREAD_MUTEX log_buf[0]=0; - #ifdef LOG_INFO_THREAD - WriteThreadID(); + #ifdef LOG_INFO_THREAD + WriteThreadID(); strcpy(log_buf,LOG_BUF_SIZE,thread_string); - #endif//LOG_INFO_THREAD + #endif//LOG_INFO_THREAD - #ifdef LOG_INFO_TIME - WriteTime(); + #ifdef LOG_INFO_TIME + WriteTime(); strcat(log_buf,LOG_BUF_SIZE,time_string); - #endif//LOG_INFO_TIME + #endif//LOG_INFO_TIME strcpy(log_buf,LOG_BUF_SIZE,str,size); __android_log_write(prio,"",log_buf); - #ifdef LOGINFO_THREAD_MUTEX - mutex.Unlock(); - #endif//LOGINFO_THREAD_MUTEX - } - };//class LogAndroidConsole:public Logger + #ifdef LOGINFO_THREAD_MUTEX + mutex.Unlock(); + #endif//LOGINFO_THREAD_MUTEX + } + };//class LogAndroidConsole:public Logger - Logger *CreateLoggerConsole(const OSString &,LogLevel ll) - { - if(ll #include namespace hgl { - template - int CharSetConv(T **out_buf,const CharSetName &out_charset, - const S *in_str,int in_str_size,const CharSetName &in_charset) - { - if(!out_buf||!out_charset||!*out_charset - ||!in_str||!*in_str||in_str_size==0||!in_charset||!*in_charset) - return(-1); + template + int CharSetConv(T **out_buf,const CharSetName &out_charset, + const S *in_str,int in_str_size,const CharSetName &in_charset) + { + if(!out_buf||!out_charset||!*out_charset + ||!in_str||!*in_str||in_str_size==0||!in_charset||!*in_charset) + return(-1); - iconv_t cd=iconv_open(out_charset,in_charset); + iconv_t cd=iconv_open(out_charset,in_charset); - if(cd==(iconv_t)-1) - return(-1); + if(cd==(iconv_t)-1) + return(-1); - if(in_str_size<0) - in_str_size=strlen(in_str); + if(in_str_size<0) + in_str_size=strlen(in_str); - size_t result; + size_t result; - char *in; - char *out; - T *out_str; - T *out_new; + char *in; + char *out; + T *out_str; + T *out_new; - size_t in_left; - size_t out_left; + size_t in_left; + size_t out_left; - int out_malloc_size=in_str_size; - int last_left=-1; + int out_malloc_size=in_str_size; + int last_left=-1; - out_str=new T[out_malloc_size+1]; + out_str=new T[out_malloc_size+1]; - in=(char *)in_str; - out=(char *)out_str; - in_left=in_str_size; - out_left=out_malloc_size*sizeof(T); + in=(char *)in_str; + out=(char *)out_str; + in_left=in_str_size; + out_left=out_malloc_size*sizeof(T); - while(true) - { - size_t res=iconv(cd,&in,&in_left,&out,&out_left); + while(true) + { + size_t res=iconv(cd,&in,&in_left,&out,&out_left); - if(res==(size_t)(-1)) - { - if(errno==E2BIG) - { - out_new=new T[out_malloc_size*2]; + if(res==(size_t)(-1)) + { + if(errno==E2BIG) + { + out_new=new T[out_malloc_size*2]; - if(!out_new) - return(false); + if(!out_new) + return(false); - result=out-(char *)out_str; + result=out-(char *)out_str; - memcpy(out_new,out_str,result); + memcpy(out_new,out_str,result); - out_malloc_size*=2; + out_malloc_size*=2; - delete[] out_str; - out_str=out_new; + delete[] out_str; + out_str=out_new; - out=(char *)out_str; - out+=result; + out=(char *)out_str; + out+=result; - //in_left已经被减了,所以不用处理 - out_left=(out_malloc_size*sizeof(T))-result; - continue; - } - else if(errno==EILSEQ) //有非法字符,跳过 - { - in++; - in_left--; - continue; - } - } + //in_left已经被减了,所以不用处理 + out_left=(out_malloc_size*sizeof(T))-result; + continue; + } + else if(errno==EILSEQ) //有非法字符,跳过 + { + in++; + in_left--; + continue; + } + } - break; - } + break; + } - //result=(out_malloc_size-(out_left/sizeof(T)); - result=((T *)out)-out_str; // 同上一行 + //result=(out_malloc_size-(out_left/sizeof(T)); + result=((T *)out)-out_str; // 同上一行 - out_str[result]=0; - iconv_close(cd); + out_str[result]=0; + iconv_close(cd); - *out_buf=out_str; - return(result); - } + *out_buf=out_str; + return(result); + } - int to_utf16(const CharSet &cs,u16char **dst,const char *src,const int src_size) - { - return CharSetConv(dst,endian::GetCharSet(),src,src_size,cs.charset); - } + int to_utf16(const CharSet &cs,u16char **dst,const char *src,const int src_size) + { + return CharSetConv(dst,endian::GetCharSet(),src,src_size,cs.charset); + } - int utf16_to(const CharSet &cs,char **dst,const u16char *src,const int src_size) - { - return CharSetConv(dst,cs.charset,src,src_size,endian::GetCharSet()); - } + int utf16_to(const CharSet &cs,char **dst,const u16char *src,const int src_size) + { + return CharSetConv(dst,cs.charset,src,src_size,endian::GetCharSet()); + } - int to_utf8(const CharSet &cs,char **dst,const char *src,const int src_size) + int to_utf8(const CharSet &cs,char **dst,const char *src,const int src_size) { return CharSetConv(dst,utf8_charset,src,src_size,cs.charset); } diff --git a/src/Platform/UNIX/CondVar.cpp b/src/Platform/UNIX/CondVar.cpp index 8b6fef36..395133c0 100644 --- a/src/Platform/UNIX/CondVar.cpp +++ b/src/Platform/UNIX/CondVar.cpp @@ -4,44 +4,44 @@ namespace hgl { - void GetWaitTime(struct timespec &,double); + void GetWaitTime(struct timespec &,double); - CondVar::CondVar() - { - cond_var=new pthread_cond_t; + CondVar::CondVar() + { + cond_var=new pthread_cond_t; - pthread_cond_init((pthread_cond_t *)cond_var,nullptr); - } + pthread_cond_init((pthread_cond_t *)cond_var,nullptr); + } - CondVar::~CondVar() - { - pthread_cond_destroy((pthread_cond_t *)cond_var); - delete (pthread_cond_t *)cond_var; - } + CondVar::~CondVar() + { + pthread_cond_destroy((pthread_cond_t *)cond_var); + delete (pthread_cond_t *)cond_var; + } - bool CondVar::Wait(ThreadMutex *tm,double t) - { - if(t>0) - { - struct timespec abstime; + bool CondVar::Wait(ThreadMutex *tm,double t) + { + if(t>0) + { + struct timespec abstime; - GetWaitTime(abstime,t); + GetWaitTime(abstime,t); - return(!pthread_cond_timedwait((pthread_cond_t *)cond_var,tm->GetThreadMutex(),&abstime)); - } - else - { - return(!pthread_cond_wait((pthread_cond_t *)cond_var,tm->GetThreadMutex())); - } - } + return(!pthread_cond_timedwait((pthread_cond_t *)cond_var,tm->GetThreadMutex(),&abstime)); + } + else + { + return(!pthread_cond_wait((pthread_cond_t *)cond_var,tm->GetThreadMutex())); + } + } - void CondVar::Signal() - { - pthread_cond_signal((pthread_cond_t *)cond_var); - } + void CondVar::Signal() + { + pthread_cond_signal((pthread_cond_t *)cond_var); + } - void CondVar::Broadcast() - { - pthread_cond_broadcast((pthread_cond_t *)cond_var); - } + void CondVar::Broadcast() + { + pthread_cond_broadcast((pthread_cond_t *)cond_var); + } }//namespace hgl diff --git a/src/Platform/UNIX/DateTime.cpp b/src/Platform/UNIX/DateTime.cpp index 5d0b74c5..ef09d930 100644 --- a/src/Platform/UNIX/DateTime.cpp +++ b/src/Platform/UNIX/DateTime.cpp @@ -11,115 +11,115 @@ namespace hgl { - //和系统时间同步 - void Time::Sync(const double cur_time) - { - tm m; - time_t tt; + //和系统时间同步 + void Time::Sync(const double cur_time) + { + tm m; + time_t tt; - if(cur_time<=0) - { - struct timeval tv; - gettimeofday(&tv, nullptr); + if(cur_time<=0) + { + struct timeval tv; + gettimeofday(&tv, nullptr); - tt=tv.tv_sec; - micro_seconds=tv.tv_usec; - } - else - { - tt=cur_time; - micro_seconds=(cur_time-tt)*HGL_MICRO_SEC_PER_SEC; - } + tt=tv.tv_sec; + micro_seconds=tv.tv_usec; + } + else + { + tt=cur_time; + micro_seconds=(cur_time-tt)*HGL_MICRO_SEC_PER_SEC; + } - localtime_r(&tt,&m); + localtime_r(&tt,&m); - hours =m.tm_hour; - minutes =m.tm_min; - seconds =m.tm_sec; - week_day =m.tm_wday; + hours =m.tm_hour; + minutes =m.tm_min; + seconds =m.tm_sec; + week_day =m.tm_wday; - gmt_off =m.tm_gmtoff; - } + gmt_off =m.tm_gmtoff; + } }//namespace hgl //-------------------------------------------------------------------------------------------------- namespace hgl { - void Date::Sync(const double cur_time) - { - tm m; - time_t tt; + void Date::Sync(const double cur_time) + { + tm m; + time_t tt; - if(cur_time<=0) - { - struct timeval tv; - gettimeofday(&tv, nullptr); + if(cur_time<=0) + { + struct timeval tv; + gettimeofday(&tv, nullptr); - tt=tv.tv_sec; - } - else - { - tt=cur_time; - } + tt=tv.tv_sec; + } + else + { + tt=cur_time; + } - localtime_r(&tt,&m); + localtime_r(&tt,&m); - year =m.tm_year+1900; - SetMonth(m.tm_mon+1); - SetDay (m.tm_mday); - week_day=m.tm_wday; - year_day=m.tm_yday; - } + year =m.tm_year+1900; + SetMonth(m.tm_mon+1); + SetDay (m.tm_mday); + week_day=m.tm_wday; + year_day=m.tm_yday; + } }//namespace hgl namespace hgl { - void ToDateTime(Date &d,Time &t,const double cur_time) - { - tm m; - time_t tt; - uint micro_sec; + void ToDateTime(Date &d,Time &t,const double cur_time) + { + tm m; + time_t tt; + uint micro_sec; - if(cur_time<=0) - { - struct timeval tv; - gettimeofday(&tv, nullptr); + if(cur_time<=0) + { + struct timeval tv; + gettimeofday(&tv, nullptr); - tt=tv.tv_sec; - micro_sec=tv.tv_usec; - } - else - { - tt=cur_time; - micro_sec=(cur_time-tt)*HGL_MICRO_SEC_PER_SEC; - } + tt=tv.tv_sec; + micro_sec=tv.tv_usec; + } + else + { + tt=cur_time; + micro_sec=(cur_time-tt)*HGL_MICRO_SEC_PER_SEC; + } - localtime_r(&tt,&m); + localtime_r(&tt,&m); - d.Set(m.tm_year+1900,m.tm_mon+1,m.tm_mday,m.tm_wday,m.tm_yday); - t.Set(m.tm_hour,m.tm_min,m.tm_sec,micro_sec,m.tm_wday); - t.SetGMT(m.tm_gmtoff); - } + d.Set(m.tm_year+1900,m.tm_mon+1,m.tm_mday,m.tm_wday,m.tm_yday); + t.Set(m.tm_hour,m.tm_min,m.tm_sec,micro_sec,m.tm_wday); + t.SetGMT(m.tm_gmtoff); + } - double FromDateTime(const int year,const int month,const int day, - const int hour,const int minute,const int second,const int micro_second, - const int gmt_off) - { - tm at; + double FromDateTime(const int year,const int month,const int day, + const int hour,const int minute,const int second,const int micro_second, + const int gmt_off) + { + tm at; - hgl_zero(at); + hgl_zero(at); - at.tm_year =year-1900; - at.tm_mon =month-1; - at.tm_mday =day; + at.tm_year =year-1900; + at.tm_mon =month-1; + at.tm_mday =day; - at.tm_hour =hour; - at.tm_min =minute; - at.tm_sec =second; + at.tm_hour =hour; + at.tm_min =minute; + at.tm_sec =second; - at.tm_gmtoff=gmt_off; + at.tm_gmtoff=gmt_off; - double result=mktime(&at); + double result=mktime(&at); - return(result + (double(micro_second) / HGL_MICRO_SEC_PER_SEC)); - } + return(result + (double(micro_second) / HGL_MICRO_SEC_PER_SEC)); + } }//namespace hgl diff --git a/src/Platform/UNIX/EnumFile.cpp b/src/Platform/UNIX/EnumFile.cpp index bf66abeb..67c6b5c7 100644 --- a/src/Platform/UNIX/EnumFile.cpp +++ b/src/Platform/UNIX/EnumFile.cpp @@ -76,7 +76,7 @@ namespace hgl memset(&fi,0,sizeof(FileInfo)); fi.size=statbuf.st_size; - fi.can_read =statbuf.st_mode&S_IROTH; + fi.can_read =statbuf.st_mode&S_IROTH; fi.can_write=statbuf.st_mode&S_IWOTH; fi.is_hiddle=(entry->d_name[0]=='.'); diff --git a/src/Platform/UNIX/ExternalModule.cpp b/src/Platform/UNIX/ExternalModule.cpp index 1febf005..2120a103 100644 --- a/src/Platform/UNIX/ExternalModule.cpp +++ b/src/Platform/UNIX/ExternalModule.cpp @@ -8,36 +8,36 @@ namespace hgl { ExternalModule *LoadExternalModule(const os_char *filename) { - ExternalModulePointer fp=dlopen(filename,RTLD_LAZY); + ExternalModulePointer fp=dlopen(filename,RTLD_LAZY); - if(!fp) - { - LOG_ERROR(OS_TEXT("Load Module <")+OSString(filename)+OS_TEXT("> error! os info: ")+OSString(dlerror())); + if(!fp) + { + LOG_ERROR(OS_TEXT("Load Module <")+OSString(filename)+OS_TEXT("> error! os info: ")+OSString(dlerror())); - return(nullptr); - } + return(nullptr); + } - return(new ExternalModule(fp)); + return(new ExternalModule(fp)); } - /** - * 加载一个外部模块 - * @param name 模块文件名称 - * @return 是否加载成功 - */ - bool ExternalModule::Load(const os_char *name) - { - Clear(); + /** + * 加载一个外部模块 + * @param name 模块文件名称 + * @return 是否加载成功 + */ + bool ExternalModule::Load(const os_char *name) + { + Clear(); - fp=dlopen(name,RTLD_LAZY); + fp=dlopen(name,RTLD_LAZY); - if(!fp) - { - LOG_ERROR(OS_TEXT("Load Module <")+OSString(name)+OS_TEXT("> error! os info: ")+OSString(dlerror())); + if(!fp) + { + LOG_ERROR(OS_TEXT("Load Module <")+OSString(name)+OS_TEXT("> error! os info: ")+OSString(dlerror())); - return(false); - } + return(false); + } - return(true); - } + return(true); + } }//namespace hgl diff --git a/src/Platform/UNIX/Fifo.cpp b/src/Platform/UNIX/Fifo.cpp index 012cabd0..5ab161d7 100644 --- a/src/Platform/UNIX/Fifo.cpp +++ b/src/Platform/UNIX/Fifo.cpp @@ -4,21 +4,21 @@ namespace hgl { - /** - * 创建一个管道通信文件 - * @param fn 简略文件名(请不要带路径) - * @return 是否创建成功 - */ - bool Fifo::Create(const char *fn) - { - if(!fn||!(*fn)) - return(false); + /** + * 创建一个管道通信文件 + * @param fn 简略文件名(请不要带路径) + * @return 是否创建成功 + */ + bool Fifo::Create(const char *fn) + { + if(!fn||!(*fn)) + return(false); - hgl::strcpy(filename,HGL_MAX_PATH-5,"/tmp/"); - hgl::strcpy(filename+5,HGL_MAX_PATH-5,fn); + hgl::strcpy(filename,HGL_MAX_PATH-5,"/tmp/"); + hgl::strcpy(filename+5,HGL_MAX_PATH-5,fn); - fd=mkfifo(filename,0777); + fd=mkfifo(filename,0777); - return(!fd); - } + return(!fd); + } }//namespace hgl diff --git a/src/Platform/UNIX/File.cpp b/src/Platform/UNIX/File.cpp index 46349951..78b09101 100644 --- a/src/Platform/UNIX/File.cpp +++ b/src/Platform/UNIX/File.cpp @@ -146,7 +146,7 @@ namespace hgl memset(&buf,0,sizeof(struct_stat64)); if(hgl_lstat64(name,&buf)==-1) - return(false); //错误,有可能是不能访问 + return(false); //错误,有可能是不能访问 return S_ISDIR(buf.st_mode); } @@ -161,7 +161,7 @@ namespace hgl memset(&buf,0,sizeof(struct_stat64)); if(hgl_lstat64(name,&buf)==-1) - return(false); //错误,有可能是不能访问 + return(false); //错误,有可能是不能访问 return S_ISLNK(buf.st_mode); } diff --git a/src/Platform/UNIX/FileAccess.cpp b/src/Platform/UNIX/FileAccess.cpp index e01c5c4e..f6952225 100644 --- a/src/Platform/UNIX/FileAccess.cpp +++ b/src/Platform/UNIX/FileAccess.cpp @@ -7,33 +7,33 @@ namespace hgl { - namespace io - { - bool FileAccess::CreateTemp() - { - char template_filename[HGL_MAX_PATH]="/tmp/cm/XXXXXX"; + namespace io + { + bool FileAccess::CreateTemp() + { + char template_filename[HGL_MAX_PATH]="/tmp/cm/XXXXXX"; - fp=mkstemps(template_filename,HGL_MAX_PATH); + fp=mkstemps(template_filename,HGL_MAX_PATH); - if(fp==-1) - return(false); + if(fp==-1) + return(false); - filename=template_filename; - mode=fomCreate; + filename=template_filename; + mode=fomCreate; - return(true); + return(true); } - int OpenFile(const os_char *fn,FileOpenMode fom) + int OpenFile(const os_char *fn,FileOpenMode fom) { int fp; - if(fom==fomCreate )fp=hgl_open64(fn,O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);else - if(fom==fomCreateTrunc )fp=hgl_open64(fn,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);else - if(fom==fomOnlyRead )fp=hgl_open64(fn,O_RDONLY );else - if(fom==fomOnlyWrite )fp=hgl_open64(fn,O_WRONLY );else - if(fom==fomReadWrite )fp=hgl_open64(fn,O_RDWR );else - if(fom==fomAppend )fp=hgl_open64(fn,O_APPEND );else + if(fom==fomCreate )fp=hgl_open64(fn,O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);else + if(fom==fomCreateTrunc )fp=hgl_open64(fn,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);else + if(fom==fomOnlyRead )fp=hgl_open64(fn,O_RDONLY );else + if(fom==fomOnlyWrite )fp=hgl_open64(fn,O_WRONLY );else + if(fom==fomReadWrite )fp=hgl_open64(fn,O_RDWR );else + if(fom==fomAppend )fp=hgl_open64(fn,O_APPEND );else { LOG_ERROR(OS_TEXT("UNIX,FileAccess,OpenFile(")+OSString(fn)+OS_TEXT(" mode error: "+OSString(fom))); RETURN_ERROR(-1); @@ -49,21 +49,21 @@ namespace hgl void CloseFile(int fp) { - close(fp); + close(fp); } - int64 FileAccess::Read(int64 offset,void *buf,int64 size) - { - if(!CanRead())return(-1); + int64 FileAccess::Read(int64 offset,void *buf,int64 size) + { + if(!CanRead())return(-1); - return hgl_pread64(fp,buf,size,offset); - } + return hgl_pread64(fp,buf,size,offset); + } - int64 FileAccess::Write(int64 offset,const void *buf,int64 size) - { - if(!CanWrite())return(-1); + int64 FileAccess::Write(int64 offset,const void *buf,int64 size) + { + if(!CanWrite())return(-1); - return hgl_pwrite64(fp,buf,size,offset); - } - }//namespace io + return hgl_pwrite64(fp,buf,size,offset); + } + }//namespace io }//namespace hgl diff --git a/src/Platform/UNIX/Process.cpp b/src/Platform/UNIX/Process.cpp index 0a5ee189..5cbeae78 100644 --- a/src/Platform/UNIX/Process.cpp +++ b/src/Platform/UNIX/Process.cpp @@ -8,78 +8,78 @@ namespace hgl { using namespace filesystem; - bool Process::SetWorkPath(const OSString &wp) - { - if(!IsDirectory(wp)) - return(false); + bool Process::SetWorkPath(const OSString &wp) + { + if(!IsDirectory(wp)) + return(false); - work_path=wp; - return(true); - } + work_path=wp; + return(true); + } - bool Process::SetExecFile(const OSString &ef) - { - if(!FileCanExec(ef)) - return(false); + bool Process::SetExecFile(const OSString &ef) + { + if(!FileCanExec(ef)) + return(false); - filename=ef; - return(true); - } + filename=ef; + return(true); + } - bool Process::Execute() - { - pid=fork(); + bool Process::Execute() + { + pid=fork(); - if(pid<0) - _exit(72); //为啥是72,我也不清楚,反正是要退出,多少无所谓。抄的poco + if(pid<0) + _exit(72); //为啥是72,我也不清楚,反正是要退出,多少无所谓。抄的poco - if(pid==0) - { //fork会复制出一个子进程并从这里开始执行 - if(chdir(work_path.c_str())!=0) - _exit(72); + if(pid==0) + { //fork会复制出一个子进程并从这里开始执行 + if(chdir(work_path.c_str())!=0) + _exit(72); - char **argv=new char *[args.GetCount()+2]; + char **argv=new char *[args.GetCount()+2]; - argv[0]=filename; - for(int i=0;ipw_dir); - strcat(home_path,HGL_MAX_PATH,"/cmgdk",6); + strcpy(home_path,HGL_MAX_PATH,pwd->pw_dir); + strcat(home_path,HGL_MAX_PATH,"/cmgdk",6); - if(IsDirectory(home_path)) - { - cmgdk_path=home_path; - return(true); - } + if(IsDirectory(home_path)) + { + cmgdk_path=home_path; + return(true); + } - for(int i=0;i= HGL_NANO_SEC_PER_SEC) - { - abstime.tv_nsec -= HGL_NANO_SEC_PER_SEC; - ++abstime.tv_sec; - } - } + if (abstime.tv_nsec >= HGL_NANO_SEC_PER_SEC) + { + abstime.tv_nsec -= HGL_NANO_SEC_PER_SEC; + ++abstime.tv_sec; + } + } }//namespace hgl namespace hgl @@ -39,64 +39,64 @@ namespace hgl return timeinfo->tm_gmtoff; } - /** - * 取得当前时间 - * @return 当前时间(单位:千分之一秒) - */ - uint64 GetTime() - { -// struct timeval tv; -// gettimeofday(&tv, nullptr); -// return ((tv.tv_sec) * HGL_MILLI_SEC_PRE_SEC) + (tv.tv_usec/1000); + /** + * 取得当前时间 + * @return 当前时间(单位:千分之一秒) + */ + uint64 GetTime() + { +// struct timeval tv; +// gettimeofday(&tv, nullptr); +// return ((tv.tv_sec) * HGL_MILLI_SEC_PRE_SEC) + (tv.tv_usec/1000); struct timespec ts; clock_gettime(CLOCK_REALTIME,&ts); return (ts.tv_sec*HGL_MILLI_SEC_PRE_SEC)+(ts.tv_nsec/1000000); - } + } - /** - * 取得当前时间 - * @return 当前时间(单位:百万分之一秒) - */ - uint64 GetMicroTime() - { -// struct timeval tv; -// gettimeofday(&tv, nullptr); -// return (tv.tv_sec) * HGL_MICRO_SEC_PER_SEC + tv.tv_usec; + /** + * 取得当前时间 + * @return 当前时间(单位:百万分之一秒) + */ + uint64 GetMicroTime() + { +// struct timeval tv; +// gettimeofday(&tv, nullptr); +// return (tv.tv_sec) * HGL_MICRO_SEC_PER_SEC + tv.tv_usec; struct timespec ts; clock_gettime(CLOCK_REALTIME,&ts); return (ts.tv_sec*HGL_MICRO_SEC_PER_SEC)+(ts.tv_nsec/1000); - } + } - /** - * 取得当前时间(双精度) - * @return 当前时间(单位:秒) - */ - double GetDoubleTime() ///<取得当前时间(双精度,单位秒) - { -// struct timeval tv; -// gettimeofday(&tv, nullptr); -// return double(tv.tv_sec) + (double(tv.tv_usec)/HGL_MICRO_SEC_PER_SEC); + /** + * 取得当前时间(双精度) + * @return 当前时间(单位:秒) + */ + double GetDoubleTime() ///<取得当前时间(双精度,单位秒) + { +// struct timeval tv; +// gettimeofday(&tv, nullptr); +// return double(tv.tv_sec) + (double(tv.tv_usec)/HGL_MICRO_SEC_PER_SEC); struct timespec ts; clock_gettime(CLOCK_REALTIME,&ts); return double(ts.tv_sec)+double(ts.tv_nsec)/HGL_NANO_SEC_PER_SEC; - } + } - /** - * 等待指定时间 - * @param time 时间(单位:秒) - */ - void WaitTime(double t) - { - if(t<=0)return; + /** + * 等待指定时间 + * @param time 时间(单位:秒) + */ + void WaitTime(double t) + { + if(t<=0)return; - struct timeval tv; - tv.tv_sec = t; - tv.tv_usec = (t-tv.tv_sec)*HGL_MICRO_SEC_PER_SEC; - select(0, nullptr, nullptr, nullptr, &tv); + struct timeval tv; + tv.tv_sec = t; + tv.tv_usec = (t-tv.tv_sec)*HGL_MICRO_SEC_PER_SEC; + select(0, nullptr, nullptr, nullptr, &tv); //注:不要使用sleep/nanosleep/clock_nanosleep来代替select,因为它们是真正的暂停执行线程。而sleep是挂起等待,可以被信号打断 - } + } }//namespace hgl diff --git a/src/Platform/Win/Clipboard.cpp b/src/Platform/Win/Clipboard.cpp index f7117864..67820c62 100644 --- a/src/Platform/Win/Clipboard.cpp +++ b/src/Platform/Win/Clipboard.cpp @@ -3,41 +3,41 @@ namespace hgl { - namespace os - { - void CopyTextToClipboard(const u16char *str) - { - if(!str||!(*str))return; + namespace os + { + void CopyTextToClipboard(const u16char *str) + { + if(!str||!(*str))return; - if (!OpenClipboard(nullptr))return; + if (!OpenClipboard(nullptr))return; - EmptyClipboard(); + EmptyClipboard(); - HGLOBAL clipbuffer; - u16char * buffer; + HGLOBAL clipbuffer; + u16char * buffer; - clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(str)+1); - buffer = (u16char *)GlobalLock(clipbuffer); + clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(str)+1); + buffer = (u16char *)GlobalLock(clipbuffer); - strcpy(buffer, str); + strcpy(buffer, str); - GlobalUnlock(clipbuffer); - SetClipboardData(CF_UNICODETEXT, clipbuffer); - CloseClipboard(); - } + GlobalUnlock(clipbuffer); + SetClipboardData(CF_UNICODETEXT, clipbuffer); + CloseClipboard(); + } - const u16char *GetTextFromClipboard() - { - if (!OpenClipboard(nullptr)) - return 0; + const u16char *GetTextFromClipboard() + { + if (!OpenClipboard(nullptr)) + return 0; - u16char * buffer = 0; + u16char * buffer = 0; - HANDLE hData = GetClipboardData( CF_UNICODETEXT ); - buffer = (u16char *)GlobalLock( hData ); - GlobalUnlock( hData ); - CloseClipboard(); - return buffer; - } - }//namespace os + HANDLE hData = GetClipboardData( CF_UNICODETEXT ); + buffer = (u16char *)GlobalLock( hData ); + GlobalUnlock( hData ); + CloseClipboard(); + return buffer; + } + }//namespace os }//namespace hgl diff --git a/src/Platform/Win/EnumFile.cpp b/src/Platform/Win/EnumFile.cpp index 93c6ca10..fa3018a2 100644 --- a/src/Platform/Win/EnumFile.cpp +++ b/src/Platform/Win/EnumFile.cpp @@ -29,7 +29,7 @@ namespace hgl } else { - full_findname=MergeFilename(config->folder_name,config->find_name); + full_findname=MergeFilename(config->folder_name,config->find_name); } WIN32_FIND_DATAW FindFileData; diff --git a/src/Platform/Win/FileAccess.cpp b/src/Platform/Win/FileAccess.cpp index 6326b724..ad750388 100644 --- a/src/Platform/Win/FileAccess.cpp +++ b/src/Platform/Win/FileAccess.cpp @@ -28,13 +28,13 @@ namespace hgl errno_t result; - if(fom==fomCreate )result=_wsopen_s(&fp,fn,_O_BINARY|_O_WRONLY|_O_CREAT ,_SH_DENYNO,S_IREAD|_S_IWRITE);else - if(fom==fomCreateTrunc )result=_wsopen_s(&fp,fn,_O_BINARY|_O_WRONLY|_O_CREAT|_O_TRUNC ,_SH_DENYNO,S_IREAD|_S_IWRITE);else -// if(fom==fomCreateTemp )result=_wsopen_s(&fp,fn,_O_BINARY|_O_WRONLY|_O_CREAT|_O_TEMPORARY ,_SH_DENYNO,S_IREAD|_S_IWRITE);else //某些平台不支持,所以全都不使用fomCreateTemp,统一使用CreateTemp - if(fom==fomOnlyRead )result=_wsopen_s(&fp,fn,_O_BINARY|_O_RDONLY ,_SH_DENYNO,S_IREAD|_S_IWRITE);else - if(fom==fomOnlyWrite )result=_wsopen_s(&fp,fn,_O_BINARY|_O_WRONLY ,_SH_DENYNO,S_IREAD|_S_IWRITE);else - if(fom==fomReadWrite )result=_wsopen_s(&fp,fn,_O_BINARY|_O_RDWR ,_SH_DENYNO,S_IREAD|_S_IWRITE);else - if(fom==fomAppend )result=_wsopen_s(&fp,fn,_O_BINARY|_O_APPEND ,_SH_DENYNO,S_IREAD|_S_IWRITE);else + if(fom==fomCreate )result=_wsopen_s(&fp,fn,_O_BINARY|_O_WRONLY|_O_CREAT ,_SH_DENYNO,S_IREAD|_S_IWRITE);else + if(fom==fomCreateTrunc )result=_wsopen_s(&fp,fn,_O_BINARY|_O_WRONLY|_O_CREAT|_O_TRUNC ,_SH_DENYNO,S_IREAD|_S_IWRITE);else +// if(fom==fomCreateTemp )result=_wsopen_s(&fp,fn,_O_BINARY|_O_WRONLY|_O_CREAT|_O_TEMPORARY ,_SH_DENYNO,S_IREAD|_S_IWRITE);else //某些平台不支持,所以全都不使用fomCreateTemp,统一使用CreateTemp + if(fom==fomOnlyRead )result=_wsopen_s(&fp,fn,_O_BINARY|_O_RDONLY ,_SH_DENYNO,S_IREAD|_S_IWRITE);else + if(fom==fomOnlyWrite )result=_wsopen_s(&fp,fn,_O_BINARY|_O_WRONLY ,_SH_DENYNO,S_IREAD|_S_IWRITE);else + if(fom==fomReadWrite )result=_wsopen_s(&fp,fn,_O_BINARY|_O_RDWR ,_SH_DENYNO,S_IREAD|_S_IWRITE);else + if(fom==fomAppend )result=_wsopen_s(&fp,fn,_O_BINARY|_O_APPEND ,_SH_DENYNO,S_IREAD|_S_IWRITE);else RETURN_ERROR(-1); return(fp); diff --git a/src/Platform/Win/Pipe.cpp b/src/Platform/Win/Pipe.cpp index c8e36e4a..f8bfd199 100644 --- a/src/Platform/Win/Pipe.cpp +++ b/src/Platform/Win/Pipe.cpp @@ -4,13 +4,13 @@ namespace hgl { bool CreatePipe(pipe_pair &pp) { - SECURITY_ATTRIBUTES sa; + SECURITY_ATTRIBUTES sa; - sa.nLength=sizeof(SECURITY_ATTRIBUTES); - sa.bInheritHandle=TRUE; - sa.lpSecurityDescriptor=nullptr; + sa.nLength=sizeof(SECURITY_ATTRIBUTES); + sa.bInheritHandle=TRUE; + sa.lpSecurityDescriptor=nullptr; - return CreatePipe(&(pp[0]),&(pp[1]),&sa,0); + return CreatePipe(&(pp[0]),&(pp[1]),&sa,0); } }//namespace hgl diff --git a/src/Platform/Win/ProgramPath.cpp b/src/Platform/Win/ProgramPath.cpp index a73a8b3b..6947a22b 100644 --- a/src/Platform/Win/ProgramPath.cpp +++ b/src/Platform/Win/ProgramPath.cpp @@ -6,10 +6,10 @@ namespace hgl { namespace filesystem { - void GetLocalAppdataPath(os_char fn[HGL_MAX_PATH]) - { - SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, fn); - } + void GetLocalAppdataPath(os_char fn[HGL_MAX_PATH]) + { + SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, fn); + } /** * 取得当前程序完整路径名称 diff --git a/src/Platform/Win/WinMessage.cpp b/src/Platform/Win/WinMessage.cpp index 1a0777fa..4f947f67 100644 --- a/src/Platform/Win/WinMessage.cpp +++ b/src/Platform/Win/WinMessage.cpp @@ -10,8 +10,8 @@ namespace hgl { namespace { - static KeyboardButton KeyConvert[256]; - static void (*WMProc[2048])(WinWindow *,uint32,uint32); //消息处理队列 + static KeyboardButton KeyConvert[256]; + static void (*WMProc[2048])(WinWindow *,uint32,uint32); //消息处理队列 uint32 GetKeyFlags(uint32 wflags) { @@ -27,178 +27,178 @@ namespace hgl return(flag); } - void InitKeyConvert() - { - int i; + void InitKeyConvert() + { + int i; - memset(KeyConvert,0,sizeof(KeyConvert)); + memset(KeyConvert,0,sizeof(KeyConvert)); - KeyConvert[VK_ESCAPE ]=kbEsc; - for(i=VK_F1;i<=VK_F12;i++)KeyConvert[i]=(KeyboardButton)(kbF1+i-VK_F1); + KeyConvert[VK_ESCAPE ]=kbEsc; + for(i=VK_F1;i<=VK_F12;i++)KeyConvert[i]=(KeyboardButton)(kbF1+i-VK_F1); - KeyConvert['`' ]=kbGrave; - for(i='0';i<='9';i++)KeyConvert[i]=(KeyboardButton)(kb0+i-'0'); - KeyConvert['-' ]=kbMinus; - KeyConvert['=' ]=kbEquals; - KeyConvert['\\' ]=kbBackSlash; - KeyConvert[VK_BACK ]=kbBackSpace; + KeyConvert['`' ]=kbGrave; + for(i='0';i<='9';i++)KeyConvert[i]=(KeyboardButton)(kb0+i-'0'); + KeyConvert['-' ]=kbMinus; + KeyConvert['=' ]=kbEquals; + KeyConvert['\\' ]=kbBackSlash; + KeyConvert[VK_BACK ]=kbBackSpace; - KeyConvert[VK_TAB ]=kbTab; - KeyConvert['Q' ]=kbQ; - KeyConvert['W' ]=kbW; - KeyConvert['E' ]=kbE; - KeyConvert['R' ]=kbR; - KeyConvert['T' ]=kbT; - KeyConvert['Y' ]=kbY; - KeyConvert['U' ]=kbU; - KeyConvert['I' ]=kbI; - KeyConvert['O' ]=kbO; - KeyConvert['P' ]=kbP; - KeyConvert['[' ]=kbLeftBracket; - KeyConvert[']' ]=kbRightBracket; + KeyConvert[VK_TAB ]=kbTab; + KeyConvert['Q' ]=kbQ; + KeyConvert['W' ]=kbW; + KeyConvert['E' ]=kbE; + KeyConvert['R' ]=kbR; + KeyConvert['T' ]=kbT; + KeyConvert['Y' ]=kbY; + KeyConvert['U' ]=kbU; + KeyConvert['I' ]=kbI; + KeyConvert['O' ]=kbO; + KeyConvert['P' ]=kbP; + KeyConvert['[' ]=kbLeftBracket; + KeyConvert[']' ]=kbRightBracket; - KeyConvert[VK_CAPITAL ]=kbCapsLock; - KeyConvert['A' ]=kbA; - KeyConvert['S' ]=kbS; - KeyConvert['D' ]=kbD; - KeyConvert['F' ]=kbF; - KeyConvert['G' ]=kbG; - KeyConvert['H' ]=kbH; - KeyConvert['J' ]=kbJ; - KeyConvert['K' ]=kbK; - KeyConvert['L' ]=kbL; - KeyConvert[';' ]=kbSemicolon; - KeyConvert['\'' ]=kbApostrophe; - KeyConvert[VK_RETURN ]=kbEnter; + KeyConvert[VK_CAPITAL ]=kbCapsLock; + KeyConvert['A' ]=kbA; + KeyConvert['S' ]=kbS; + KeyConvert['D' ]=kbD; + KeyConvert['F' ]=kbF; + KeyConvert['G' ]=kbG; + KeyConvert['H' ]=kbH; + KeyConvert['J' ]=kbJ; + KeyConvert['K' ]=kbK; + KeyConvert['L' ]=kbL; + KeyConvert[';' ]=kbSemicolon; + KeyConvert['\'' ]=kbApostrophe; + KeyConvert[VK_RETURN ]=kbEnter; - KeyConvert[VK_LSHIFT ]=kbLeftShift; - KeyConvert['Z' ]=kbZ; - KeyConvert['X' ]=kbX; - KeyConvert['C' ]=kbC; - KeyConvert['V' ]=kbV; - KeyConvert['B' ]=kbB; - KeyConvert['N' ]=kbN; - KeyConvert['M' ]=kbM; - KeyConvert[',' ]=kbComma; - KeyConvert['.' ]=kbPeriod; - KeyConvert['/' ]=kbSlash; - KeyConvert[VK_RSHIFT ]=kbRightShift; + KeyConvert[VK_LSHIFT ]=kbLeftShift; + KeyConvert['Z' ]=kbZ; + KeyConvert['X' ]=kbX; + KeyConvert['C' ]=kbC; + KeyConvert['V' ]=kbV; + KeyConvert['B' ]=kbB; + KeyConvert['N' ]=kbN; + KeyConvert['M' ]=kbM; + KeyConvert[',' ]=kbComma; + KeyConvert['.' ]=kbPeriod; + KeyConvert['/' ]=kbSlash; + KeyConvert[VK_RSHIFT ]=kbRightShift; - KeyConvert[VK_LCONTROL ]=kbLeftCtrl; - KeyConvert[VK_LWIN ]=kbLeftOS; - KeyConvert[VK_LMENU ]=kbLeftAlt; - KeyConvert[VK_SPACE ]=kbSpace; - KeyConvert[VK_RMENU ]=kbRightAlt; - KeyConvert[VK_RWIN ]=kbRightOS; - KeyConvert[VK_RCONTROL ]=kbRightCtrl; + KeyConvert[VK_LCONTROL ]=kbLeftCtrl; + KeyConvert[VK_LWIN ]=kbLeftOS; + KeyConvert[VK_LMENU ]=kbLeftAlt; + KeyConvert[VK_SPACE ]=kbSpace; + KeyConvert[VK_RMENU ]=kbRightAlt; + KeyConvert[VK_RWIN ]=kbRightOS; + KeyConvert[VK_RCONTROL ]=kbRightCtrl; - KeyConvert[VK_PAUSE ]=kbPause; - // KeyConvert[VK_CLEAR ]=kbClear; + KeyConvert[VK_PAUSE ]=kbPause; + // KeyConvert[VK_CLEAR ]=kbClear; - KeyConvert[VK_NUMPAD0 ]=kbNum0; - KeyConvert[VK_NUMPAD1 ]=kbNum1; - KeyConvert[VK_NUMPAD2 ]=kbNum2; - KeyConvert[VK_NUMPAD3 ]=kbNum3; - KeyConvert[VK_NUMPAD4 ]=kbNum4; - KeyConvert[VK_NUMPAD5 ]=kbNum5; - KeyConvert[VK_NUMPAD6 ]=kbNum6; - KeyConvert[VK_NUMPAD7 ]=kbNum7; - KeyConvert[VK_NUMPAD8 ]=kbNum8; - KeyConvert[VK_NUMPAD9 ]=kbNum9; + KeyConvert[VK_NUMPAD0 ]=kbNum0; + KeyConvert[VK_NUMPAD1 ]=kbNum1; + KeyConvert[VK_NUMPAD2 ]=kbNum2; + KeyConvert[VK_NUMPAD3 ]=kbNum3; + KeyConvert[VK_NUMPAD4 ]=kbNum4; + KeyConvert[VK_NUMPAD5 ]=kbNum5; + KeyConvert[VK_NUMPAD6 ]=kbNum6; + KeyConvert[VK_NUMPAD7 ]=kbNum7; + KeyConvert[VK_NUMPAD8 ]=kbNum8; + KeyConvert[VK_NUMPAD9 ]=kbNum9; - KeyConvert[VK_DECIMAL ]=kbNumDecimal; - KeyConvert[VK_DIVIDE ]=kbNumDivide; - KeyConvert[VK_MULTIPLY ]=kbNumMultiply; - KeyConvert[VK_SUBTRACT ]=kbNumSubtract; - KeyConvert[VK_ADD ]=kbNumAdd; + KeyConvert[VK_DECIMAL ]=kbNumDecimal; + KeyConvert[VK_DIVIDE ]=kbNumDivide; + KeyConvert[VK_MULTIPLY ]=kbNumMultiply; + KeyConvert[VK_SUBTRACT ]=kbNumSubtract; + KeyConvert[VK_ADD ]=kbNumAdd; - KeyConvert[VK_UP ]=kbUp; - KeyConvert[VK_DOWN ]=kbDown; - KeyConvert[VK_LEFT ]=kbLeft; - KeyConvert[VK_RIGHT ]=kbRight; + KeyConvert[VK_UP ]=kbUp; + KeyConvert[VK_DOWN ]=kbDown; + KeyConvert[VK_LEFT ]=kbLeft; + KeyConvert[VK_RIGHT ]=kbRight; - KeyConvert[VK_INSERT ]=kbInsert; - KeyConvert[VK_DELETE ]=kbDelete; - KeyConvert[VK_HOME ]=kbHome; - KeyConvert[VK_END ]=kbEnd; - KeyConvert[VK_PRIOR ]=kbPageUp; - KeyConvert[VK_NEXT ]=kbPageDown; + KeyConvert[VK_INSERT ]=kbInsert; + KeyConvert[VK_DELETE ]=kbDelete; + KeyConvert[VK_HOME ]=kbHome; + KeyConvert[VK_END ]=kbEnd; + KeyConvert[VK_PRIOR ]=kbPageUp; + KeyConvert[VK_NEXT ]=kbPageDown; - KeyConvert[VK_NUMLOCK ]=kbNumLock; - KeyConvert[VK_SCROLL ]=kbScrollLock; + KeyConvert[VK_NUMLOCK ]=kbNumLock; + KeyConvert[VK_SCROLL ]=kbScrollLock; - //KeyConvert[VK_SHIFT ]=kbLeftShift; - //KeyConvert[VK_CONTROL ]=kbLeftCtrl; - //KeyConvert[VK_MENU ]=kbLeftAlt; + //KeyConvert[VK_SHIFT ]=kbLeftShift; + //KeyConvert[VK_CONTROL ]=kbLeftCtrl; + //KeyConvert[VK_MENU ]=kbLeftAlt; - KeyConvert[VK_OEM_1 ]=kbSemicolon; - KeyConvert[VK_OEM_PLUS ]=kbEquals; - KeyConvert[VK_OEM_COMMA ]=kbComma; - KeyConvert[VK_OEM_MINUS ]=kbMinus; - KeyConvert[VK_OEM_PERIOD]=kbPeriod; - KeyConvert[VK_OEM_2 ]=kbSlash; - KeyConvert[VK_OEM_3 ]=kbGrave; - KeyConvert[VK_OEM_4 ]=kbLeftBracket; - KeyConvert[VK_OEM_5 ]=kbBackSlash; - KeyConvert[VK_OEM_6 ]=kbRightBracket; - KeyConvert[VK_OEM_7 ]=kbApostrophe; - } + KeyConvert[VK_OEM_1 ]=kbSemicolon; + KeyConvert[VK_OEM_PLUS ]=kbEquals; + KeyConvert[VK_OEM_COMMA ]=kbComma; + KeyConvert[VK_OEM_MINUS ]=kbMinus; + KeyConvert[VK_OEM_PERIOD]=kbPeriod; + KeyConvert[VK_OEM_2 ]=kbSlash; + KeyConvert[VK_OEM_3 ]=kbGrave; + KeyConvert[VK_OEM_4 ]=kbLeftBracket; + KeyConvert[VK_OEM_5 ]=kbBackSlash; + KeyConvert[VK_OEM_6 ]=kbRightBracket; + KeyConvert[VK_OEM_7 ]=kbApostrophe; + } - KeyboardButton ConvertOSKey(uint key) - { - if(key=kbEndRange)return(kbBeginRange); + KeyboardButton ConvertOSKey(uint key) + { + if(key=kbEndRange)return(kbBeginRange); - if(key==VK_SHIFT) - { - if((GetAsyncKeyState(VK_LSHIFT)>>15)&1) - return kbLeftShift; - else - return kbRightShift; - } - else - if(key==VK_CONTROL) - { - if((GetAsyncKeyState(VK_LCONTROL)>>15)&1) - return kbLeftCtrl; - else - return kbRightCtrl; - } - if(key==VK_MENU) - { - if((GetAsyncKeyState(VK_LMENU)>>15)&1) - return kbLeftAlt; - else - return kbRightAlt; - } + if(key==VK_SHIFT) + { + if((GetAsyncKeyState(VK_LSHIFT)>>15)&1) + return kbLeftShift; + else + return kbRightShift; + } + else + if(key==VK_CONTROL) + { + if((GetAsyncKeyState(VK_LCONTROL)>>15)&1) + return kbLeftCtrl; + else + return kbRightCtrl; + } + if(key==VK_MENU) + { + if((GetAsyncKeyState(VK_LMENU)>>15)&1) + return kbLeftAlt; + else + return kbRightAlt; + } #ifdef _DEBUG - if(KeyConvert[key]==0) - { - wchar_t name[64]; + if(KeyConvert[key]==0) + { + wchar_t name[64]; - ::GetKeyNameTextW(key,name,64); + ::GetKeyNameTextW(key,name,64); - LOG_INFO(WideString(L"Unknow Key: " )+WideString(key) + LOG_INFO(WideString(L"Unknow Key: " )+WideString(key) +WideString(L" ,name: " )+WideString(name)); - } + } #endif _DEBUG - return KeyConvert[key]; - } + return KeyConvert[key]; + } void WMProcDestroy(WinWindow *,uint32,uint32) { PostQuitMessage(0); } - #define WMEF_MOUSE(button,action) void WMProcMouse##button##action(WinWindow *win,uint32 wParam,uint32 lParam) \ - { \ + #define WMEF_MOUSE(button,action) void WMProcMouse##button##action(WinWindow *win,uint32 wParam,uint32 lParam) \ + { \ const int x=LOWORD(lParam); \ const int y=HIWORD(lParam); \ \ win->OnMouseMove(x,y); \ - win->OnMouse##action(x,y,mb##button|GetKeyFlags(wParam)); \ - } + win->OnMouse##action(x,y,mb##button|GetKeyFlags(wParam)); \ + } WMEF_MOUSE(Left,Down); WMEF_MOUSE(Left,Up); @@ -213,9 +213,9 @@ namespace hgl WMEF_MOUSE(Right,DoubleClick); void WMProcMouseMove(WinWindow *win,uint32 wParam,uint32 lParam) - { + { win->OnMouseMove(LOWORD(lParam),HIWORD(lParam)); - } + } #undef WMEF_MOUSE #define WMEF2(name) void name(WinWindow *win,uint32 wParam,uint32 lParam) @@ -268,28 +268,28 @@ namespace hgl //if(joy) // LoadJoystick(win->hInstance,win->hWnd); - #define WM_MAP(wm,func) WMProc[wm]=func; + #define WM_MAP(wm,func) WMProc[wm]=func; - WM_MAP(WM_CLOSE ,WMProcDestroy); - WM_MAP(WM_LBUTTONDOWN ,WMProcMouseLeftDown); - WM_MAP(WM_LBUTTONUP ,WMProcMouseLeftUp); - WM_MAP(WM_LBUTTONDBLCLK ,WMProcMouseLeftDoubleClick); - WM_MAP(WM_MBUTTONDOWN ,WMProcMouseMidDown); - WM_MAP(WM_MBUTTONUP ,WMProcMouseMidUp); - WM_MAP(WM_MBUTTONDBLCLK ,WMProcMouseMidDoubleClick); - WM_MAP(WM_RBUTTONDOWN ,WMProcMouseRightDown); - WM_MAP(WM_RBUTTONUP ,WMProcMouseRightUp); - WM_MAP(WM_RBUTTONDBLCLK ,WMProcMouseRightDoubleClick); - WM_MAP(WM_MOUSEWHEEL ,WMProcMouseWheel); - WM_MAP(WM_MOUSEMOVE ,WMProcMouseMove); - WM_MAP(WM_KEYDOWN ,WMProcKeyDown); - WM_MAP(WM_KEYUP ,WMProcKeyUp); - WM_MAP(WM_SYSKEYDOWN ,WMProcKeyDown); - WM_MAP(WM_SYSKEYUP ,WMProcKeyUp); - WM_MAP(WM_CHAR ,WMProcChar); - WM_MAP(WM_SYSCHAR ,WMProcChar); - WM_MAP(WM_ACTIVATE ,WMProcActive); - WM_MAP(WM_SIZE ,WMProcSize); + WM_MAP(WM_CLOSE ,WMProcDestroy); + WM_MAP(WM_LBUTTONDOWN ,WMProcMouseLeftDown); + WM_MAP(WM_LBUTTONUP ,WMProcMouseLeftUp); + WM_MAP(WM_LBUTTONDBLCLK ,WMProcMouseLeftDoubleClick); + WM_MAP(WM_MBUTTONDOWN ,WMProcMouseMidDown); + WM_MAP(WM_MBUTTONUP ,WMProcMouseMidUp); + WM_MAP(WM_MBUTTONDBLCLK ,WMProcMouseMidDoubleClick); + WM_MAP(WM_RBUTTONDOWN ,WMProcMouseRightDown); + WM_MAP(WM_RBUTTONUP ,WMProcMouseRightUp); + WM_MAP(WM_RBUTTONDBLCLK ,WMProcMouseRightDoubleClick); + WM_MAP(WM_MOUSEWHEEL ,WMProcMouseWheel); + WM_MAP(WM_MOUSEMOVE ,WMProcMouseMove); + WM_MAP(WM_KEYDOWN ,WMProcKeyDown); + WM_MAP(WM_KEYUP ,WMProcKeyUp); + WM_MAP(WM_SYSKEYDOWN ,WMProcKeyDown); + WM_MAP(WM_SYSKEYUP ,WMProcKeyUp); + WM_MAP(WM_CHAR ,WMProcChar); + WM_MAP(WM_SYSCHAR ,WMProcChar); + WM_MAP(WM_ACTIVATE ,WMProcActive); + WM_MAP(WM_SIZE ,WMProcSize); #undef WM_MAP } diff --git a/src/Platform/Win/WinWindow.cpp b/src/Platform/Win/WinWindow.cpp index 9e98121f..1fb1c2b0 100644 --- a/src/Platform/Win/WinWindow.cpp +++ b/src/Platform/Win/WinWindow.cpp @@ -4,9 +4,9 @@ namespace hgl { LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); - namespace - { - constexpr wchar_t WIN_CLASS_NAME[] = L"CMGameEngine/ULRE Window Class"; + namespace + { + constexpr wchar_t WIN_CLASS_NAME[] = L"CMGameEngine/ULRE Window Class"; bool RegistryWinClass(HINSTANCE hInstance) { @@ -29,80 +29,80 @@ namespace hgl return RegisterClassExW(&win_class); } - }//namespace + }//namespace - bool WinWindow::Create() - { - constexpr DWORD win_style = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU ; + bool WinWindow::Create() + { + constexpr DWORD win_style = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU ; - int win_left, win_top; - int win_width, win_height; + int win_left, win_top; + int win_width, win_height; - { - RECT win_rect; + { + RECT win_rect; - win_rect.left = 0; - win_rect.right = width; - win_rect.top = 0; - win_rect.bottom = height; + win_rect.left = 0; + win_rect.right = width; + win_rect.top = 0; + win_rect.bottom = height; - AdjustWindowRectEx(&win_rect, win_style, false, 0); //计算窗口坐标 + AdjustWindowRectEx(&win_rect, win_style, false, 0); //计算窗口坐标 - win_width = win_rect.right - win_rect.left; - win_height = win_rect.bottom - win_rect.top; - } + win_width = win_rect.right - win_rect.left; + win_height = win_rect.bottom - win_rect.top; + } - if (width && height) - { - win_left = (GetSystemMetrics(SM_CXSCREEN) - win_width) / 2; - win_top = (GetSystemMetrics(SM_CYSCREEN) - win_height) / 2; - } - else - { - win_left = CW_USEDEFAULT; - win_top = CW_USEDEFAULT; - } + if (width && height) + { + win_left = (GetSystemMetrics(SM_CXSCREEN) - win_width) / 2; + win_top = (GetSystemMetrics(SM_CYSCREEN) - win_height) / 2; + } + else + { + win_left = CW_USEDEFAULT; + win_top = CW_USEDEFAULT; + } - win_hwnd = CreateWindowExW(0, - WIN_CLASS_NAME, // class name - win_name.c_str(), // app name - win_style, // window style - win_left,win_top, // x/y coords - win_width, // width - win_height, // height - nullptr, // handle to parent - nullptr, // handle to menu - hInstance, // hInstance - nullptr); // no extra parameters + win_hwnd = CreateWindowExW(0, + WIN_CLASS_NAME, // class name + win_name.c_str(), // app name + win_style, // window style + win_left,win_top, // x/y coords + win_width, // width + win_height, // height + nullptr, // handle to parent + nullptr, // handle to menu + hInstance, // hInstance + nullptr); // no extra parameters - if (!win_hwnd) - { - UnregisterClassW(WIN_CLASS_NAME, hInstance); - return(false); - } + if (!win_hwnd) + { + UnregisterClassW(WIN_CLASS_NAME, hInstance); + return(false); + } - win_dc = GetDC(win_hwnd); - SetWindowLongPtrW(win_hwnd, GWLP_USERDATA, (LONG_PTR)this); - return(true); - } + win_dc = GetDC(win_hwnd); + SetWindowLongPtrW(win_hwnd, GWLP_USERDATA, (LONG_PTR)this); + return(true); + } WinWindow::~WinWindow() - { - Close(); - } + { + Close(); + } - bool WinWindow::Create(uint w, uint h) - { + bool WinWindow::Create(uint w, uint h) + { full_screen=false; - width = w; - height = h; + width = w; + height = h; - hInstance = GetModuleHandleW(nullptr); + hInstance = GetModuleHandleW(nullptr); - if (!RegistryWinClass(hInstance)) - return(false); + if (!RegistryWinClass(hInstance)) + return(false); - if(!Create()) + if(!Create()) { is_close=true; return(false); @@ -112,24 +112,24 @@ namespace hgl is_close=false; return(true); } - } + } - bool WinWindow::Create(uint, uint, uint) - { + bool WinWindow::Create(uint, uint, uint) + { full_screen=true; - return(false); - } + return(false); + } - void WinWindow::Close() - { - ReleaseDC(win_hwnd, win_dc); - DestroyWindow(win_hwnd); - UnregisterClassW(WIN_CLASS_NAME,hInstance); + void WinWindow::Close() + { + ReleaseDC(win_hwnd, win_dc); + DestroyWindow(win_hwnd); + UnregisterClassW(WIN_CLASS_NAME,hInstance); - win_dc = nullptr; - win_hwnd = nullptr; + win_dc = nullptr; + win_hwnd = nullptr; is_close = true; - } + } void WinWindow::SetCaption(const OSString &caption) { @@ -137,20 +137,20 @@ namespace hgl SetWindowTextW(win_hwnd,caption.c_str()); } - void WinWindow::Show() - { - ShowWindow(win_hwnd, SW_SHOW); - SetForegroundWindow(win_hwnd); - SetFocus(win_hwnd); + void WinWindow::Show() + { + ShowWindow(win_hwnd, SW_SHOW); + SetForegroundWindow(win_hwnd); + SetFocus(win_hwnd); - UpdateWindow(win_hwnd); - } + UpdateWindow(win_hwnd); + } - void WinWindow::Hide() - { - ShowWindow(win_hwnd, SW_HIDE); - UpdateWindow(win_hwnd); - } + void WinWindow::Hide() + { + ShowWindow(win_hwnd, SW_HIDE); + UpdateWindow(win_hwnd); + } void WinWindow::ToMinWindow() { @@ -197,8 +197,8 @@ namespace hgl return ::WaitMessage(); } - Window *CreateRenderWindow(const WideString& win_name) - { - return(new WinWindow(win_name)); - } + Window *CreateRenderWindow(const WideString& win_name) + { + return(new WinWindow(win_name)); + } }//namespace hgl diff --git a/src/RenderDevice/Vulkan/VKCommandBuffer.cpp b/src/RenderDevice/Vulkan/VKCommandBuffer.cpp index 569a252a..51328317 100644 --- a/src/RenderDevice/Vulkan/VKCommandBuffer.cpp +++ b/src/RenderDevice/Vulkan/VKCommandBuffer.cpp @@ -86,7 +86,7 @@ bool CommandBuffer::Bind(Material *mat) return(false); const uint32_t count=mat->GetDescriptorSetCount(); - + if(count>0) vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,mat->GetPipelineLayout(),0,count,mat->GetDescriptorSets(),0,nullptr); @@ -107,7 +107,7 @@ bool CommandBuffer::Bind(Renderable *render_obj) IndexBuffer *indices_buffer=render_obj->GetIndexBuffer(); - if(indices_buffer) + if(indices_buffer) vkCmdBindIndexBuffer(cmd_buf,*indices_buffer,render_obj->GetIndexOffset(),indices_buffer->GetType()); return(true); @@ -147,7 +147,7 @@ void CommandBuffer::SetLineWidth(float line_width) { vkCmdSetLineWidth(cmd_buf,line_width); } - + void CommandBuffer::Draw(const uint32_t vertex_count) { vkCmdDraw(cmd_buf,vertex_count,1,0,0); diff --git a/src/RenderDevice/Vulkan/VKDescriptorSets.cpp b/src/RenderDevice/Vulkan/VKDescriptorSets.cpp index 735acdfe..78b3d71e 100644 --- a/src/RenderDevice/Vulkan/VKDescriptorSets.cpp +++ b/src/RenderDevice/Vulkan/VKDescriptorSets.cpp @@ -8,7 +8,7 @@ namespace { if(count<=0) return; - + for(int i=0;iCreateSem(); @@ -83,11 +83,11 @@ RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format subpass.pipelineBindPoint=VK_PIPELINE_BIND_POINT_GRAPHICS; subpass.flags=0; subpass.inputAttachmentCount=0; - subpass.pInputAttachments=nullptr; + subpass.pInputAttachments=nullptr; subpass.pResolveAttachments=nullptr; subpass.preserveAttachmentCount=0; subpass.pPreserveAttachments=nullptr; - + int att_count=0; if(color_format!=VK_FORMAT_UNDEFINED) diff --git a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp index cf38d5be..cdc9adfb 100644 --- a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp @@ -124,7 +124,7 @@ namespace } else { - swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE; + swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE; } VkSwapchainKHR swap_chain; @@ -176,7 +176,7 @@ namespace return(true); } - + bool CreateDepthBuffer(DeviceAttribute *rsa) { VkImageCreateInfo image_info={}; @@ -278,7 +278,7 @@ namespace pipelineCache.initialDataSize = 0; pipelineCache.pInitialData = nullptr; pipelineCache.flags = 0; - + VkPipelineCache cache; if(vkCreatePipelineCache(device, &pipelineCache, nullptr, &cache)!=VK_SUCCESS) @@ -298,7 +298,7 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device DeviceAttribute *attr=new DeviceAttribute(inst,physical_device,surface); AutoDelete auto_delete(attr); - + attr->swapchain_extent=GetSwapchainExtent(attr->surface_caps,win->GetWidth(),win->GetHeight()); if(attr->graphics_family==ERROR_FAMILY_INDEX) diff --git a/src/RenderDevice/Vulkan/VKFormat.cpp b/src/RenderDevice/Vulkan/VKFormat.cpp index ffb73d51..6742fa31 100644 --- a/src/RenderDevice/Vulkan/VKFormat.cpp +++ b/src/RenderDevice/Vulkan/VKFormat.cpp @@ -15,7 +15,7 @@ namespace constexpr StrideByFormat stride_list[]= #else - + #define STRIDE_BY_FORMAT(vktype,size) size constexpr uint32_t stride_list[]= @@ -172,7 +172,7 @@ namespace STRIDE_BY_FORMAT(D16_UNORM_S8_UINT, 3), STRIDE_BY_FORMAT(D24_UNORM_S8_UINT, 4), STRIDE_BY_FORMAT(D32_SFLOAT_S8_UINT, 5) - }; + }; #ifdef _DEBUG constexpr uint32_t STRIDE_FORMAT_COUNT=sizeof(stride_list)/sizeof(StrideByFormat); diff --git a/src/RenderDevice/Vulkan/VKMaterial.cpp b/src/RenderDevice/Vulkan/VKMaterial.cpp index 51a28136..a82b8cbf 100644 --- a/src/RenderDevice/Vulkan/VKMaterial.cpp +++ b/src/RenderDevice/Vulkan/VKMaterial.cpp @@ -13,7 +13,7 @@ Material *CreateMaterial(Device *dev,ShaderModuleMap *shader_maps) const int shader_count=shader_maps->GetCount(); List *shader_stage_list=new List; - + shader_stage_list->SetCount(shader_count); VkPipelineShaderStageCreateInfo *p=shader_stage_list->GetData(); @@ -148,7 +148,7 @@ bool Material::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *bu VkWriteDescriptorSet writeDescriptorSet = {}; - writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSet.dstSet = set; writeDescriptorSet.descriptorCount = 1; writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; diff --git a/src/RenderDevice/Vulkan/VKPipeline.cpp b/src/RenderDevice/Vulkan/VKPipeline.cpp index b65347d9..888d5abc 100644 --- a/src/RenderDevice/Vulkan/VKPipeline.cpp +++ b/src/RenderDevice/Vulkan/VKPipeline.cpp @@ -82,7 +82,7 @@ PipelineCreater::PipelineCreater(Device *dev,const Material *material,RenderPass rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizer.pNext = nullptr; rasterizer.flags = 0; - rasterizer.depthClampEnable = VK_FALSE; + rasterizer.depthClampEnable = VK_FALSE; rasterizer.rasterizerDiscardEnable = VK_FALSE; rasterizer.polygonMode = VK_POLYGON_MODE_FILL; rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; diff --git a/src/RenderDevice/Vulkan/VKRenderable.cpp b/src/RenderDevice/Vulkan/VKRenderable.cpp index ecd94c67..fdba4b37 100644 --- a/src/RenderDevice/Vulkan/VKRenderable.cpp +++ b/src/RenderDevice/Vulkan/VKRenderable.cpp @@ -23,14 +23,14 @@ bool Renderable::Set(const int binding,VertexBuffer *vbo,VkDeviceSize offset) { if(binding<0||binding>=buf_count||!vbo)return(false); - const VkVertexInputBindingDescription *desc=vertex_sm->GetDesc(binding); + const VkVertexInputBindingDescription *desc=vertex_sm->GetDesc(binding); const VkVertexInputAttributeDescription *attr=vertex_sm->GetAttr(binding); - if(vbo->GetFormat()!=attr->format)return(false); + if(vbo->GetFormat()!=attr->format)return(false); if(vbo->GetStride()!=desc->stride)return(false); //format信息来自于shader,实际中可以不一样。但那样需要为每一个格式产生一个同样shader的material instance,不同的格式又需要不同的pipeline,我们不支持这种行为 - + buf_list[binding]=*vbo; buf_offset[binding]=offset; diff --git a/src/RenderDevice/Vulkan/VKShaderModuleManage.cpp b/src/RenderDevice/Vulkan/VKShaderModuleManage.cpp index 7144017b..6f6e3fa5 100644 --- a/src/RenderDevice/Vulkan/VKShaderModuleManage.cpp +++ b/src/RenderDevice/Vulkan/VKShaderModuleManage.cpp @@ -83,7 +83,7 @@ const ShaderModule *ShaderModuleManage::GetShader(int id) if(!shader_list.Get(id,sm)) return nullptr; - + sm->IncRef(); return sm; } @@ -97,10 +97,10 @@ bool ShaderModuleManage::ReleaseShader(const ShaderModule *const_sm) if(!shader_list.Get(const_sm->GetID(),sm)) return(false); - + if(sm!=const_sm) return(false); - + sm->DecRef(); return(true); }