-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaCheck.cpp
More file actions
75 lines (65 loc) · 1.74 KB
/
Copy pathLambdaCheck.cpp
File metadata and controls
75 lines (65 loc) · 1.74 KB
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
#include<string> // std::string
#include<iostream> // std::cout
struct LambdaError
{
bool value = false;
// false = no error
// true = error
auto what()
{
return string_;
}
LambdaError(std::string s)
{
string_ = s;
}
LambdaError()
{
}
std::string string_{"nothing to see here."};
};
template<typename ErrorType>
struct LambdaCheck
{
auto add(auto lambda)
{
try
{
lambda();
}
catch(ErrorType& e)
{
error = e.what();
HasErrorHappened = true;
}
}
auto check(LambdaError& error_)
{
error_.value = HasErrorHappened;
if(HasErrorHappened)
{
std::cout << "---------------------------------------------------------------------\n"
"The function call LambdaCheck<ErrorType>{}(lambda){lambda()} failed\n"
"error.what() = " << error <<
"\n---------------------------------------------------------------------\n";
}
else
{
std::cout << "---------------------------------------------------------------------\n"
"The function call LambdaCheck<ErrorType>{}(lambda){lambda()} passed\n"
"With no errors\n"
"---------------------------------------------------------------------\n";
}
}
// member variables.
std::string error = {};
bool HasErrorHappened = false;
};
int main()
{
auto lambda_error = LambdaError{};
auto lambda_throw = [](){/*throw LambdaError{};*/};
LambdaCheck<LambdaError> l_e{};
l_e.add(lambda_throw);
l_e.check(lambda_error);
}