-
Notifications
You must be signed in to change notification settings - Fork 14
/
member_func.cpp
116 lines (95 loc) · 3.36 KB
/
member_func.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
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
// practical example of modern C++ decorators
// view the full tutorial at https://github.com/TheMaverickProgrammer/C-Python-like-Decorators
#include <iostream>
#include <memory>
#include <cassert>
#include <chrono>
#include <ctime>
#include <stdexcept>
#include <functional>
#include <type_traits>
#include <string>
#include <variant>
using namespace std::placeholders;
using namespace std;
///////////////////////////////////
// weak optional value structure //
///////////////////////////////////
template<typename T>
struct optional_type {
T value;
bool OK;
bool BAD;
std::string msg;
optional_type(T&& t) : value(std::move(t)) { OK = true; BAD = false; }
optional_type(bool ok, std::string msg="") : msg(std::move(msg)) { OK = ok; BAD = !ok; }
};
////////////////////////////////////
// decorators //
////////////////////////////////////
// exception decorator for optional return types
template<typename F>
auto exception_fail_safe(const F& func) {
return [func](auto&&... args)
-> optional_type<decltype(func(std::forward<decltype(args)>(args)...))> {
using R = optional_type<decltype(func(std::forward<decltype(args)>(args)...))>;
try {
return R(func(std::forward<decltype(args)>(args)...));
} catch(std::iostream::failure& e) {
return R(false, e.what());
} catch(std::exception& e) {
return R(false, e.what());
} catch(...) {
// This ... catch clause will capture any exception thrown
return R(false, std::string("Exception caught: default exception"));
}
};
}
template<typename F>
auto output(const F& func) {
return [func](auto&&... args) {
std::cout << func(std::forward<decltype(args)>(args)...) << std::endl;
};
}
template<typename F>
auto log_time(const F& func) {
return [func](auto&&... args) {
auto now = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(now);
func(std::forward<decltype(args)>(args)...);
std::cout << "> Logged at " << std::ctime(&time) << std::endl;
};
}
/////////////////////////////////////////////
// an example class with a member function //
/////////////////////////////////////////////
struct apples {
apples(double cost_per_apple) : cost_per_apple(cost_per_apple) { }
double calculate_cost(int count, double weight) {
if(count <= 0)
throw std::runtime_error("must have 1 or more apples");
if(weight <= 0)
throw std::runtime_error("apples must weigh more than 0 ounces");
return count*weight*cost_per_apple;
}
double cost_per_apple;
};
int main() {
// $1.09 per apple
apples groceries(1.09);
// we must bind the object and member function in scope
auto get_cost = exception_fail_safe(std::bind(&apples::calculate_cost, &groceries, _1, _2));
// create a vector of optional result values
auto vec = { get_cost(4, 0), get_cost(2, 1.1), get_cost(5, 1.3), get_cost(0, 2.45) };
// step through the vector and print values
int idx = 0;
for(auto& opt : vec) {
std::cout << "[" << ++idx << "] ";
if(opt.BAD) {
std::cout << "There was an error: " << opt.msg << std::endl;
} else {
std::cout << "Bag cost $" << opt.value << std::endl;
}
}
return 0;
}