-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_comparable.h++
177 lines (160 loc) · 6.11 KB
/
make_comparable.h++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once
#include <utility>
namespace make_comparable {
template <typename T>
concept bool ComparableLess = requires ( const T &x, const T &y ) { {x < y} -> bool; };
template <typename T>
concept bool ComparableLessEqual = requires ( const T &x, const T &y ) { {x <= y} -> bool; };
template <typename T>
concept bool ComparableMore = requires ( const T &x, const T &y ) { {x > y} -> bool; };
template <typename T>
concept bool ComparableMoreEqual = requires ( const T &x, const T &y ) { {x >= y} -> bool; };
template <typename T>
concept bool ComparableEqual = requires ( const T &x, const T &y ) { {x == y} -> bool; };
template <typename T>
concept bool ComparableNotEqual = requires ( const T &x, const T &y ) { {x != y} -> bool; };
}
namespace make_comparable_private {
template <typename T>
concept bool ComparableBase = make_comparable::ComparableLess <T>;
template <typename T>
concept bool ComparableLessEqualOk = ComparableBase <T> && make_comparable::ComparableLessEqual <T>;
template <typename T>
concept bool ComparableMoreOk = ComparableBase <T> && make_comparable::ComparableMore <T>;
template <typename T>
concept bool ComparableMoreEqualOk = ComparableBase <T> && make_comparable::ComparableMoreEqual <T>;
template <typename T>
concept bool ComparableEqualOk = ComparableBase <T> && make_comparable::ComparableEqual <T>;
template <typename T>
concept bool ComparableNotEqualOk = ComparableBase <T> && make_comparable::ComparableNotEqual <T>;
}
// <=
namespace make_comparable_private {
template <ComparableLessEqualOk T>
class SkipComparableLessEqual : public T {
public:
template <typename ...Args>
SkipComparableLessEqual ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
};
template <ComparableBase T>
class ForceComparableLessEqual : public T {
public:
template <typename ...Args>
ForceComparableLessEqual ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
bool operator <= ( const ForceComparableLessEqual <T> &y ) const {
return !(y < *this);
}
};
template <ComparableLessEqualOk T>
SkipComparableLessEqual <T> makeComparableLessEqual ( T x );
template <ComparableBase T>
ForceComparableLessEqual <T> makeComparableLessEqual ( T x );
template <typename T>
using MakeComparableLessEqual = decltype (makeComparableLessEqual (std::declval<T>()));
}
// >
namespace make_comparable_private {
template <ComparableMoreOk T>
class SkipComparableMore : public T {
public:
template <typename ...Args>
SkipComparableMore ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
};
template <ComparableBase T>
class ForceComparableMore : public T {
public:
template <typename ...Args>
ForceComparableMore ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
bool operator > ( const ForceComparableMore <T> &y ) const {
return (y < *this);
}
};
template <ComparableMoreOk T>
SkipComparableMore <T> makeComparableMore ( T x );
template <ComparableBase T>
ForceComparableMore <T> makeComparableMore ( T x );
template <typename T>
using MakeComparableMore = decltype (makeComparableMore (std::declval<T>()));
}
// >=
namespace make_comparable_private {
template <ComparableMoreEqualOk T>
class SkipComparableMoreEqual : public T {
public:
template <typename ...Args>
SkipComparableMoreEqual ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
};
template <ComparableBase T>
class ForceComparableMoreEqual : public T {
public:
template <typename ...Args>
ForceComparableMoreEqual ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
bool operator >= ( const ForceComparableMoreEqual <T> &y ) const {
return !(*this < y);
}
};
template <ComparableMoreEqualOk T>
SkipComparableMoreEqual <T> makeComparableMoreEqual ( T x );
template <ComparableBase T>
ForceComparableMoreEqual <T> makeComparableMoreEqual ( T x );
template <typename T>
using MakeComparableMoreEqual = decltype (makeComparableMoreEqual (std::declval<T>()));
}
// ==
namespace make_comparable_private {
template <ComparableEqualOk T>
class SkipComparableEqual : public T {
public:
template <typename ...Args>
SkipComparableEqual ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
};
template <ComparableBase T>
class ForceComparableEqual : public T {
public:
template <typename ...Args>
ForceComparableEqual ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
bool operator == ( const ForceComparableEqual <T> &y ) const {
return !(*this < y) && !(y < *this);
}
};
template <ComparableEqualOk T>
SkipComparableEqual <T> makeComparableEqual ( T x );
template <ComparableBase T>
ForceComparableEqual <T> makeComparableEqual ( T x );
template <typename T>
using MakeComparableEqual = decltype (makeComparableEqual (std::declval<T>()));
}
// !=
namespace make_comparable_private {
template <ComparableNotEqualOk T>
class SkipComparableNotEqual : public T {
public:
template <typename ...Args>
SkipComparableNotEqual ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
};
template <ComparableBase T>
class ForceComparableNotEqual : public T {
public:
template <typename ...Args>
ForceComparableNotEqual ( Args&& ... args ) : T (std::forward<Args>(args)...) {}
bool operator != ( const ForceComparableNotEqual <T> &y ) const {
return *this < y || y < *this;
}
};
template <ComparableNotEqualOk T>
SkipComparableNotEqual <T> makeComparableNotEqual ( T x );
template <ComparableBase T>
ForceComparableNotEqual <T> makeComparableNotEqual ( T x );
template <typename T>
using MakeComparableNotEqual = decltype (makeComparableNotEqual (std::declval<T>()));
}
namespace make_comparable {
template <typename T>
using MakeComparable =
make_comparable_private::MakeComparableLessEqual <
make_comparable_private::MakeComparableMore <
make_comparable_private::MakeComparableMoreEqual <
make_comparable_private::MakeComparableEqual <
make_comparable_private::MakeComparableNotEqual <
T>>>>>;
}