-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
89 lines (77 loc) · 1.86 KB
/
test.cpp
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
#include <string>
#include <iomanip>
#include <iostream>
#include "lex_compare.hpp"
#include "lex_compare_functor.hpp"
class Person;
bool testPredicate(const Person& lhs, const Person& rhs);
class Person
{
public:
Person(std::string surname, int age, char initial, int income)
:surname(std::move(surname))
,age(age)
,initial(initial)
,income(income)
{
}
const std::string& getSurname() const
{
return surname;
}
int getAge() const
{
return age;
}
char getInitial() const
{
return initial;
}
int getIncome() const
{
return income;
}
bool operator<(const Person& rhs) const
{
return lco::LessThan(*this, rhs,
&Person::surname, // member
&Person::getAge, // method
[](const Person& p) -> char {return p.getInitial();}, // functor
LCOPRED(Person, testPredicate) // predicate functor
);
}
private:
std::string surname;
int age;
char initial;
int income;
friend std::ostream& operator<<(std::ostream& os, const Person& p);
};
bool testPredicate(const Person& lhs, const Person& rhs)
{
return lhs.getIncome() < rhs.getIncome();
}
std::ostream& operator<<(std::ostream& os, const Person& p)
{
os << "{" << p.surname << ", " << p.age << ", " << p.initial << ", " << p.income << "}";
return os;
}
int main()
{
const Person people[5] = {
Person("Smith", 43, 'B', 45000),
Person("Smith", 43, 'J', 52000),
Person("Smith", 9, 'B', 0),
Person("Doe", 19, 'J', 20000),
Person("Doe", 43, 'J', 60000)
};
auto compFields = lco::Functor<Person>::make(&Person::getSurname, &Person::getAge, &Person::getInitial, &Person::getIncome);
for (const Person& lhs : people)
{
for (const Person& rhs : people)
{
std::cout << lhs << " < " << rhs << " ? " << std::boolalpha << (lhs < rhs) << compFields(lhs, rhs) << std::endl;
}
}
return 0;
}