From 6e1f32f38eca4cdc1088e737abf58ffec5e2592b Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 5 Dec 2024 13:38:29 +0800 Subject: [PATCH] use newly Comparator<> --- inc/hgl/CodePage.h | 8 ++- inc/hgl/CompOperator.h | 103 +++++++++------------------------- inc/hgl/type/ConstStringSet.h | 12 ++-- inc/hgl/type/DateTime.h | 12 ++-- inc/hgl/type/IDName.h | 11 +--- inc/hgl/type/String.h | 8 ++- src/Time/DateTime.cpp | 4 +- 7 files changed, 48 insertions(+), 110 deletions(-) diff --git a/inc/hgl/CodePage.h b/inc/hgl/CodePage.h index 29198a8..10ee69b 100644 --- a/inc/hgl/CodePage.h +++ b/inc/hgl/CodePage.h @@ -132,7 +132,7 @@ namespace hgl return (uint16)CharCodePage::NONE; } - struct CharSet + struct CharSet:public Comparator { uint16 codepage; CharSetName charset; @@ -160,8 +160,10 @@ namespace hgl strcpy(charset,CHAR_SET_NAME_MAX_LENGTH,cs.charset); } - int _Comp(const CharSet &data)const{return (size_t)codepage-(size_t)data.codepage;} \ - CompOperator(const CharSet &,_Comp) + const int compare(const CharSet &other)const override + { + return (size_t)codepage-(size_t)other.codepage; + } };//struct CharacterSet inline CharSet::CharSet(const uint16 ccp) diff --git a/inc/hgl/CompOperator.h b/inc/hgl/CompOperator.h index 3c9f1db..b27333e 100644 --- a/inc/hgl/CompOperator.h +++ b/inc/hgl/CompOperator.h @@ -1,89 +1,36 @@ -#ifndef HGL_COMP_OPERATOR_INCLUDE -#define HGL_COMP_OPERATOR_INCLUDE +#pragma once + +#include -#include 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 CompOperatorMemcmp(name) int _Comp(name data)const{return memcmp(this,&data,sizeof(name));} \ - CompOperator(name,_Comp) - - #define CompOperatorMemcmpPointer(name) int _Comp(const name *data)const{return memcmp(this,data,sizeof(name));} \ - CompOperator(const name *,_Comp) -}//namespace hgl - -/** -* 比较处理模板基类 -*/ -template class Comparator ///比较处理模板基类 -{ -public: - /** - * 比较函数,需被特例化或派生实现. 正确情况下此函数不应该会被调用 + * 比较处理模板基类 */ - virtual int compare(const T &a,const T &b)const; - //{ - // return 0; // 如 return(a-b); - //} - - /** - * 交换函数 - */ - virtual void exchange(T &a,T &b) + template class Comparator ///比较处理模板基类 { - hgl::hgl_swap(a,b); - } + public: - /** - * 复制数据 + /** + * 比较函数,需被特例化或派生实现. 正确情况下此函数不应该会被调用 */ - virtual void cpy(T *t,T *s) - { - memcpy(t,s,sizeof(T)); - } -};//class Comparator + virtual const int compare(const T &other)const=0; -//针对原生类型的特例化版本,做适当加速 -#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;} \ - }; + const bool operator > (const T &i)const{return compare(i)>0;} + const bool operator < (const T &i)const{return compare(i)<0;} + const bool operator >=(const T &i)const{return compare(i)>=0;} + const bool operator <=(const T &i)const{return compare(i)<=0;} + const bool operator ==(const T &i)const{return compare(i)==0;} + const bool operator !=(const T &i)const{return compare(i)!=0;} + };//class Comparator - COMPARATOR_ORIGIN_TYPE(hgl::int8) - COMPARATOR_ORIGIN_TYPE(hgl::int16) - COMPARATOR_ORIGIN_TYPE(hgl::int32) - COMPARATOR_ORIGIN_TYPE(hgl::int64) + template class ComparatorData:public Comparator + { + public: - 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) - -#if __cpp_char8_t - COMPARATOR_ORIGIN_TYPE(char8_t) -#endif// - - COMPARATOR_ORIGIN_TYPE(char) - COMPARATOR_ORIGIN_TYPE(wchar_t) - -#if HGL_OS != HGL_OS_Windows - COMPARATOR_ORIGIN_TYPE(u16char) -#endif//HGL_OS != HGL_OS_Windows - - COMPARATOR_ORIGIN_TYPE(char32_t) -#undef COMPARATOR_ORIGIN_TYPE - -#endif//HGL_COMP_OPERATOR_INCLUDE + const int compare(const T &other)const override final + { + return memcmp(this,&other,sizeof(T)); + } + };//class ComparatorData +}//namespace hgl diff --git a/inc/hgl/type/ConstStringSet.h b/inc/hgl/type/ConstStringSet.h index a06cf49..e85c0ec 100644 --- a/inc/hgl/type/ConstStringSet.h +++ b/inc/hgl/type/ConstStringSet.h @@ -6,7 +6,7 @@ namespace hgl { - template class ConstString + template class ConstString:public Comparator> { const SC *str; int length; @@ -24,18 +24,16 @@ namespace hgl const SC *GetString()const{return str;} const int GetLength()const{return length;} - const int Comp(const ConstString &cs)const + const int Comp(const ConstString &cs)const override { if(lengthcs.length)return( 1); return memcmp(str,cs.str,length); } - - CompOperator(const ConstString &,Comp) }; - template struct ConstStringView + template struct ConstStringView:public Comparator> { DataArray *str_data; @@ -58,14 +56,12 @@ namespace hgl return str_data->GetData()+offset; } - int Comp(const ConstStringView &csv)const + const int compare(const ConstStringView &csv)const override { if(length!=csv.length)return(length-csv.length); return hgl::strcmp(GetString(),csv.GetString(),length); } - - CompOperator(const ConstStringView &,Comp) }; template class ConstStringSet diff --git a/inc/hgl/type/DateTime.h b/inc/hgl/type/DateTime.h index 80b014c..2528c7e 100644 --- a/inc/hgl/type/DateTime.h +++ b/inc/hgl/type/DateTime.h @@ -17,7 +17,7 @@ namespace hgl * * 当您对Hours,Minutes,Seconds中任何一个值进行加减时,其它值都会自动计算。如:11:30这个值,使用Minutes+=55。会自动计算出55分钟后的时间,值为12:25 */ - class Time ///时间类 + class Time:public Comparator