-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTestTuples.cxx
145 lines (115 loc) · 3.49 KB
/
TestTuples.cxx
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
/*
* TestTuples.cxx
*
* Created on: Sep 25, 2021
* Author: <a href="mailto:[email protected]">Damir Ljubic</a>
*/
#include <string>
#include <vector>
#include <algorithm>
#include "TestTuples.hxx"
#include "Setter.hxx"
#define CHECK_ENUM(x) case (x) : return #x
namespace test::tuples
{
using gender_t = enum class Gender
{
female = 0,
male
};
std::string printEnum(gender_t gender)
{
switch(gender)
{
CHECK_ENUM(gender_t::female);
CHECK_ENUM(gender_t::male);
}
return "n/a";
}
class Person
{
public:
Person(int _age, std::string _name, gender_t _gender):
age(_age)
, name(std::move(_name))
, gender(_gender)
{}
std::string getName() const { return name;}
int getAge() const { return age;}
gender_t getGender() const { return gender;}
private:
int age;
std::string name;
gender_t gender;
};
std::ostream& operator << (std::ostream& out, const Person& p)
{
return out << "Name=" << p.getName()
<< ", Age=" << p.getAge()
<< ", gender=" << printEnum(p.getGender())
;
}
bool operator < (const Person& p1, const Person& p2)
{
/*
* Lexicographic ordering
*
* It's relying on the std::tuple internal implementation of the
* comparison operator ("less than"), instead of writing your own tedious,
* and error-prone code
*
*/
return std::make_tuple(p1.getAge(), p1.getName(), p1.getGender()) <
std::make_tuple(p2.getAge(), p2.getName(), p2.getGender());
}
template <typename T>
struct Optional
{
T value;
bool valid;
// Optional(T& t) : value(t), valid(true) // implicit convertible
// {}
//
// Optional(T&& t) : value(std::move(t)), valid(true)
// {}
};
template <typename T>
std::ostream& operator << (std::ostream& out, const Optional<T>& optional)
{
return out << "Valid=" << std::boolalpha << optional.valid
<< ", Value=" << optional.value;
}
int testTuples()
{
int age;
std::string name;
std::vector<int> arr;
Optional<std::string> shoolClass;
// Optional<std::string> shoolClass {""}; // with proper c-tor
std::cout << "Universal setter:\n\n";
{
Setter setter {age, name, arr, shoolClass};
setter.set<0>(7)
.set<1>("Alex")
.set<2>(std::vector<int>{1, 2, 3})
.set<3>(Optional<std::string>{.value = std::string("1D")})
//.set<3>(std::string("1D")) //with proper c-tor
;
setter.print2Console();
}
std::cout << "\nSetting by references:\n\n";
std::cout << "age=" << age << '\n';
std::cout << "name=" << name << '\n';
std::cout << "arr=\n";
print(arr);
std::cout << "shoolClass=" << shoolClass << '\n';
std::cout << "\nLexicographical ordering:\n\n";
std::vector<Person> persons { {45, "Steven", gender_t::male},
{7, "Alex", gender_t::male},
{45, "Mary", gender_t::female}
};
std::sort(persons.begin(), persons.end());
print(persons);
return 0;
}
}