-
Notifications
You must be signed in to change notification settings - Fork 0
/
StandardFunctors.h
50 lines (43 loc) · 1.44 KB
/
StandardFunctors.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
#pragma once
class IsLess {
public:
template<class T>
static bool compare(const T& a, const T& b) { return a < b; }
};
class IsGreater {
public:
template<class T>
static bool compare(const T& a, const T& b) { return a > b; }
};
class IsLessDeref {
public:
template<class T>
static bool compare(const T& a, const T& b) {
if (a == NULL) return false; // CONVENTION: NULL pointer is "greater" than other pointers
else if (b == NULL) return true; // NOTE: returns false is both pointers are NULL
else return *a < *b; // for non NULL pointers we compare objects they point at
}
};
class IsGreaterDeref {
public:
template<class T>
static bool compare(const T& a, const T& b) {
if (b == NULL) return false; // CONVENTION: NULL pointer is "greater" than other pointers
else if (a == NULL) return true; // NOTE: returns false is both pointers are NULL
else return *a > *b; // for non NULL pointers we compare objects they point at
}
};
class IsEqual {
public:
template<class T>
static bool compare(const T& a, const T& b) { return a == b; }
};
class IsEqualDeref {
public:
template<class T>
static bool compare(const T& a, const T& b) {
if (a == NULL || b == NULL) return false; // CONVENTION: if at least one of the pointers is NULL
// then the "objects" they point at are not equal
else return (*a) == (*b); // for non NULL pointers we compare objects they point at
}
};