SimpleWindow
载入中...
搜索中...
未找到
IComparable.h
1#pragma once
2
3#include <type_traits>
4
5namespace sw
6{
10 template <
11 typename TDerived,
12 typename TOther = const TDerived &>
14 {
15 public:
19 bool Equals(TOther other) const
20 {
21 static_assert(
22 !std::is_same<
24 decltype(&TDerived::Equals)>::value,
25 "Derived class must implement Equals method.");
26
27 return static_cast<const TDerived *>(this)->Equals(other);
28 }
29
33 bool operator==(TOther other) const
34 {
35 return Equals(other);
36 }
37
41 bool operator!=(TOther other) const
42 {
43 return !Equals(other);
44 }
45 };
46
50 template <
51 typename TDerived,
52 typename TOther = const TDerived &>
53 class IComparable : public IEqualityComparable<TDerived, TOther>
54 {
55 public:
60 int CompareTo(TOther other) const
61 {
62 static_assert(
63 !std::is_same<
65 decltype(&TDerived::CompareTo)>::value,
66 "Derived class must implement CompareTo method.");
67
68 return static_cast<const TDerived *>(this)->CompareTo(other);
69 }
70
74 bool Equals(TOther other) const
75 {
76 return CompareTo(other) == 0;
77 }
78
82 bool operator<(TOther other) const
83 {
84 return CompareTo(other) < 0;
85 }
86
90 bool operator<=(TOther other) const
91 {
92 return CompareTo(other) <= 0;
93 }
94
98 bool operator>(TOther other) const
99 {
100 return CompareTo(other) > 0;
101 }
102
106 bool operator>=(TOther other) const
107 {
108 return CompareTo(other) >= 0;
109 }
110 };
111}
全序比较接口
Definition IComparable.h:54
bool Equals(TOther other) const
判断当前对象与另一个对象是否相等
Definition IComparable.h:74
bool operator<=(TOther other) const
判断当前对象是否小于或等于另一个对象
Definition IComparable.h:90
int CompareTo(TOther other) const
比较当前对象与另一个对象的大小关系
Definition IComparable.h:60
bool operator<(TOther other) const
判断当前对象是否小于另一个对象
Definition IComparable.h:82
bool operator>(TOther other) const
判断当前对象是否大于另一个对象
Definition IComparable.h:98
bool operator>=(TOther other) const
判断当前对象是否大于或等于另一个对象
Definition IComparable.h:106
相等性比较接口
Definition IComparable.h:14
bool operator==(TOther other) const
判断当前对象是否与另一个对象相等
Definition IComparable.h:33
bool operator!=(TOther other) const
判断当前对象是否与另一个对象不相等
Definition IComparable.h:41
bool Equals(TOther other) const
判断当前对象与另一个对象是否相等
Definition IComparable.h:19