use newly Comparator<>
This commit is contained in:
parent
680b7717fc
commit
6e1f32f38e
@ -132,7 +132,7 @@ namespace hgl
|
||||
return (uint16)CharCodePage::NONE;
|
||||
}
|
||||
|
||||
struct CharSet
|
||||
struct CharSet:public Comparator<CharSet>
|
||||
{
|
||||
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)
|
||||
|
@ -1,89 +1,36 @@
|
||||
#ifndef HGL_COMP_OPERATOR_INCLUDE
|
||||
#define HGL_COMP_OPERATOR_INCLUDE
|
||||
#pragma once
|
||||
|
||||
#include<string.h>
|
||||
|
||||
#include<hgl/TypeFunc.h>
|
||||
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<typename T> 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<typename T> 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<type> \
|
||||
{ \
|
||||
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<typename T> class ComparatorData:public Comparator<T>
|
||||
{
|
||||
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
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
template<typename SC> class ConstString
|
||||
template<typename SC> class ConstString:public Comparator<ConstString<SC>>
|
||||
{
|
||||
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(length<cs.length)return(-1);
|
||||
if(length>cs.length)return( 1);
|
||||
|
||||
return memcmp(str,cs.str,length);
|
||||
}
|
||||
|
||||
CompOperator(const ConstString &,Comp)
|
||||
};
|
||||
|
||||
template<typename SC> struct ConstStringView
|
||||
template<typename SC> struct ConstStringView:public Comparator<ConstStringView<SC>>
|
||||
{
|
||||
DataArray<SC> *str_data;
|
||||
|
||||
@ -58,14 +56,12 @@ namespace hgl
|
||||
return str_data->GetData()+offset;
|
||||
}
|
||||
|
||||
int Comp(const ConstStringView<SC> &csv)const
|
||||
const int compare(const ConstStringView<SC> &csv)const override
|
||||
{
|
||||
if(length!=csv.length)return(length-csv.length);
|
||||
|
||||
return hgl::strcmp(GetString(),csv.GetString(),length);
|
||||
}
|
||||
|
||||
CompOperator(const ConstStringView &,Comp)
|
||||
};
|
||||
|
||||
template<typename SC> class ConstStringSet
|
||||
|
@ -17,7 +17,7 @@ namespace hgl
|
||||
*
|
||||
* 当您对Hours,Minutes,Seconds中任何一个值进行加减时,其它值都会自动计算。如:11:30这个值,使用Minutes+=55。会自动计算出55分钟后的时间,值为12:25
|
||||
*/
|
||||
class Time ///时间类
|
||||
class Time:public Comparator<Time> ///时间类
|
||||
{
|
||||
int32 gmt_off; ///<当前时区与UTC时间的差值
|
||||
|
||||
@ -79,9 +79,7 @@ namespace hgl
|
||||
|
||||
Time &operator = (const Time &);
|
||||
|
||||
const int Comp(const Time &)const;
|
||||
|
||||
CompOperator(const Time &,Comp);
|
||||
const int compare(const Time &)const override;
|
||||
|
||||
void Sync(const double=0); ///<和系统时间同步
|
||||
|
||||
@ -94,7 +92,7 @@ namespace hgl
|
||||
*
|
||||
* 当您对Year,Month,Day中任意一个值进行修改时,其它值都会自动跟着计算。如1981-4-17,如果使用Day+=400,会自动计算出400天之后的日期,结果是1982-5-21
|
||||
*/
|
||||
class Date ///日期类
|
||||
class Date:public Comparator<Date> ///日期类
|
||||
{
|
||||
int32 year;
|
||||
int8 month;
|
||||
@ -141,9 +139,7 @@ namespace hgl
|
||||
|
||||
Date &operator = (const Date &);
|
||||
|
||||
const int Comp(const Date &)const;
|
||||
|
||||
CompOperator(const Date &,Comp);
|
||||
const int compare(const Date &)const override;
|
||||
|
||||
void Sync(const double=0); ///<和系统日期同步
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/ConstStringSet.h>
|
||||
#include<typeinfo>
|
||||
@ -12,7 +12,7 @@ namespace hgl
|
||||
* 顺序ID+名称数据结构模板<br>
|
||||
* 按添加进来的名字先后顺序一个个产生连续的序号,所有数据只可读不可写
|
||||
*/
|
||||
template<typename SC,int CLASS_COUNTER> class OrderedIDName
|
||||
template<typename SC,int CLASS_COUNTER> class OrderedIDName:public Comparator<OrderedIDName<SC,CLASS_COUNTER>>
|
||||
{
|
||||
public:
|
||||
|
||||
@ -85,12 +85,7 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
const int Comp(const OrderedIDName &oin)const{return GetID()-oin.GetID();}
|
||||
|
||||
const int Comp(const OrderedIDName *oin)const{return GetID()-oin->GetID();}
|
||||
|
||||
CompOperator(const OrderedIDName &,Comp)
|
||||
CompOperator(const OrderedIDName *,Comp)
|
||||
const int compare(const OrderedIDName &oin)const override{return GetID()-oin.GetID();}
|
||||
};//class IDName
|
||||
|
||||
#define DefineIDName(name,type) using name=OrderedIDName<type,__COUNTER__>; //使用__COUNTER__是为了让typeid()不同
|
||||
|
@ -11,7 +11,7 @@ namespace hgl
|
||||
/**
|
||||
* 字符串类
|
||||
*/
|
||||
template<typename T> class String ///字符串基类
|
||||
template<typename T> class String:public Comparator<String<T>> ///字符串基类
|
||||
{
|
||||
protected:
|
||||
|
||||
@ -1312,8 +1312,10 @@ namespace hgl
|
||||
|
||||
#undef BASE_STRING_NUMBER_OPERATOR_ADD
|
||||
|
||||
CompOperator(const T *,Comp);
|
||||
CompOperator(const SelfClass &,Comp);
|
||||
const int compare(const SelfClass &str)const override
|
||||
{
|
||||
return Comp(str);
|
||||
}
|
||||
};//template<typename T> class String
|
||||
|
||||
//这种重载用于value+str的情况
|
||||
|
@ -90,7 +90,7 @@ namespace hgl
|
||||
return(*this);
|
||||
}
|
||||
|
||||
const int Time::Comp(const Time &t)const
|
||||
const int Time::compare(const Time &t)const
|
||||
{
|
||||
if(hours!=t.hours)
|
||||
return hours-t.hours;
|
||||
@ -220,7 +220,7 @@ namespace hgl
|
||||
return(*this);
|
||||
}
|
||||
|
||||
const int Date::Comp(const Date &t)const
|
||||
const int Date::compare(const Date &t)const
|
||||
{
|
||||
if(year!=t.year)
|
||||
return year-t.year;
|
||||
|
Loading…
x
Reference in New Issue
Block a user