-
Notifications
You must be signed in to change notification settings - Fork 4
/
rest.cc
145 lines (110 loc) · 2.88 KB
/
rest.cc
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
#include <iostream>
#include <limits>
#include <fstream>
#include <regex>
#include <optional>
using namespace std;
struct Event
{};
struct Policy
{
bool check() { return false; }
};
struct Channel
{
enum class Priority { Low, Medium, High};
void sendEvent(const Event& e);
void sendEvent(const Event& e, Priority p);
void sendEvent2(const Event& e, std::optional<Priority> p={})
{
if(!p)
cout << "No priority passed" << endl;
switch(*p) {
case Priority::Low:
cout << "Low!" << endl; break;
case Priority::Medium:
cout << "Medium!" << endl; break;
case Priority::High:
cout << "High!" << endl; break;
;
}
}
std::optional<Policy> getPolicy(const Event& e)
{
return std::nullopt;
}
};
#include <type_traits>
template<typename T>
bool isSet(const T& t)
{
static_assert(std::is_pointer<T>(), "isSet requires a pointer");
return t != nullptr;
}
int main(int argc, char** argv)
{
static_assert(sizeof(int) == 4, "Int must be 4 bytes");
if(isSet(argv))
cout << "Yes, is set"<<endl;
int i;
cout << std::numeric_limits<int>::min() << endl;
cout << std::numeric_limits<decltype(i)>::max() << endl;
ifstream in("/etc/passwd");
if (!in.is_open()) {
std::cout << "failed to open\n";
} else {
string line;
int count = 0;
while(getline(in, line))
++count;
cout << "Read " << count << " lines\n";
}
vector<int> vi{1, 2, 3, 4};
vector<string> vs{"foo", "bar", "baz"};
auto printAverages = [](int i){};
for(auto a : { 1, 10, 15} )
printAverages(a);
auto doAverage = [](vector<double> v) {};
vector<double> x, y, z;
for(auto& a : {x, y, z})
doAverage(a);
struct Coordinate
{
double x, y;
};
Coordinate coord{-1.0, 1.0};
auto [xc, yc] = coord;
xc = yc = 0;
std::string s = R"foo(Some people, when confronted with a problem, think:
"I know, I'll use regular expressions."
Now they have two problems.)foo";
cout << s << endl;
std::regex word_regex("(\\S+)");
auto words_begin =
std::sregex_iterator(s.begin(), s.end(), word_regex);
auto words_end = std::sregex_iterator();
std::cout << "Found "
<< std::distance(words_begin, words_end)
<< " words\n";
for_each(words_begin, words_end, [](const auto& w) {
cout << "Word: " << w.str() << endl;
});
Channel c;
Event e1, e2;
c.sendEvent2(e1);
c.sendEvent2(e2, Channel::Priority::High);
std::optional<Channel::Priority> p;
if(!p)
cout << "Priority not set" << endl;
p = Channel::Priority::High;
if(*p == Channel::Priority::High)
cout << "Priority is now set to High" << endl;
if(auto policy = c.getPolicy(e1)) {
if(policy->check())
c.sendEvent(e1);
}
else
cout << "No policy was set" <<endl;
if(auto policy = c.getPolicy(e1); policy->check())
c.sendEvent(e1);
}