-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaller.cpp
More file actions
100 lines (86 loc) · 2.7 KB
/
caller.cpp
File metadata and controls
100 lines (86 loc) · 2.7 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
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
#include <iostream>
#include <cmath>
#include <future>
#include <type_traits>
#include <utility>
#include <memory>
//------------------------------------------------------------------------------
//useful to derive from base class when putting instances inside
//e.g. a command queue for deferred execution: add to queue and return
//future then call Invoke when execution required
struct ICaller {
virtual void Invoke() = 0;
virtual ~ICaller() {}
};
template < typename ResultType >
class Caller : ICaller {
public:
template < typename U, typename... Args >
Caller(U f, Args...args) : f_(std::bind(f, args...)), empty_(false) {}
Caller() : empty_(true) {}
std::future< ResultType > GetFuture() {
return p_.get_future();
}
void Invoke() {
try {
ResultType r = ResultType(f_());
p_.set_value(r);
} catch(...) {
p_.set_exception(std::current_exception());
}
}
bool Empty() const { return empty_; }
private:
std::promise< ResultType > p_;
std::function< ResultType () > f_;
bool empty_;
};
template <>
class Caller<void> : ICaller {
public:
template < typename U, typename... Args >
Caller(U f, Args...args) : f_(std::bind(f, args...)), empty_(false) {}
Caller() : empty_(true) {}
std::future< void > GetFuture() {
return p_.get_future();
}
void Invoke() {
try {
f_();
p_.set_value();
} catch(...) {
p_.set_exception(std::current_exception());
}
}
bool Empty() const { return empty_; }
private:
std::promise< void > p_;
std::function< void () > f_;
bool empty_;
};
template <typename F, typename... Args >
auto MakeCaller(F f, Args... args) -> Caller< decltype(f(args...)) >* {
return new Caller< decltype(f(args...)) >(f, args...);
}
//------------------------------------------------------------------------------
void foo() { std::cout << "foo\n";}
//consider returning std::unique_ptr from MakeCaller
int main(int, char**) {
typedef std::remove_pointer< decltype(MakeCaller(sqrt, 2.0)) >::type C1;
std::unique_ptr< C1 > caller(MakeCaller(sqrt, 2.0));
auto f = caller->GetFuture();
caller->Invoke();
std::cout << f.get() << std::endl;
typedef std::remove_pointer<
decltype(MakeCaller(printf, "message %d\n", 1)) >::type C2;
std::unique_ptr< C2 > printer(MakeCaller(printf, "message %d\n", 1));
auto f2 = printer->GetFuture();
printer->Invoke();
f2.wait();
typedef std::remove_pointer< decltype(MakeCaller(foo)) >::type C3;
std::unique_ptr< C3 > v(MakeCaller(foo));
auto f3 = v->GetFuture();
v->Invoke();
f3.wait();
return 0;
}