forked from chronolaw/boost_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction2.cpp
More file actions
126 lines (99 loc) · 1.92 KB
/
Copy pathfunction2.cpp
File metadata and controls
126 lines (99 loc) · 1.92 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2015
// Author: Chrono Law
#include <std.hpp>
//using namespace std;
#include <boost/bind.hpp>
#include <boost/function.hpp>
using namespace boost;
//////////////////////////////////////////
class demo_class
{
private:
typedef function<void(int)> func_t;
func_t func;
int n;
public:
demo_class(int i):n(i){}
template<typename CallBack>
void accept(CallBack f)
{ func = f; }
void run()
{ func(n); }
};
void call_back_func(int i)
{
using namespace std;
cout << "call_back_func:";
cout << i * 2 << endl;
}
void case1()
{
demo_class dc(10);
dc.accept(call_back_func);
dc.run();
}
//////////////////////////////////////////
class call_back_obj
{
private:
int x;
public:
call_back_obj(int i):x(i){}
void operator()(int i)
{
using namespace std;
cout << "call_back_obj:";
cout << i * x++ << endl;
}
};
void case2()
{
demo_class dc(10);
call_back_obj cbo(2);
dc.accept(ref(cbo));
dc.run();
dc.run();
}
//////////////////////////////////////////
class call_back_factory
{
public:
void call_back_func1(int i)
{
using namespace std;
cout << "call_back_factory1:";
cout << i * 2 << endl;
}
void call_back_func2(int i, int j)
{
using namespace std;
cout << "call_back_factory2:";
cout << i *j * 2 << endl;
}
};
void case3()
{
demo_class dc(10);
call_back_factory cbf;
dc.accept(bind(&call_back_factory::call_back_func1, cbf, _1));
dc.run();
dc.accept(bind(&call_back_factory::call_back_func2, cbf, _1, 5));
dc.run();
}
//////////////////////////////////////////
bool case4()
{
std::function<void(int)> func;
func = call_back_func;
//func.clear();
//func.empty();
return !!func;
}
//////////////////////////////////////////
int main()
{
case1();
case2();
case3();
case4();
}