-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmp.h
66 lines (49 loc) · 1.21 KB
/
cmp.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
#include <algorithm>
#include <initializer_list>
// requires C++20
namespace cmp {
template <typename _Type>
struct operand
{
operand(std::initializer_list<_Type> && p)
: params_(std::move(p))
{
}
virtual bool check(_Type val) const = 0;
const std::initializer_list<_Type> params_;
};
template <typename T, class U>
concept Derived = std::is_base_of<U, T>::value;
template <typename _Type>
struct in : public operand<_Type>
{
using operand<_Type>::params_;
in(std::initializer_list<_Type> list)
: operand<_Type>(std::move(list))
{
}
bool check(_Type val) const override { return std::find(params_.begin(), params_.end(), val) != params_.end(); }
};
template <typename _Type>
struct not_in : public operand<_Type>
{
using operand<_Type>::params_;
not_in(std::initializer_list<_Type> list)
: operand<_Type>(std::move(list))
{
}
bool check(_Type val) const override { return std::find(params_.begin(), params_.end(), val) == params_.end(); }
};
template <typename _Type, Derived<operand<_Type>> _Op>
struct is
{
is(_Type v, _Op op)
: v{v}
, op{std::move(op)} {};
_Type v;
_Op op;
operator bool() { return op.check(v); }
};
} // namespace cmp
// usage:
// if (is(123, in{1, 2, 3, 123, 100})) {}