boost::bind実践
26644 ワード
第一部分のソースコードを基礎として実践する:
第2部のソースコードは実際の応用である.
1 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/
2 /*bind */
3
4 #include <iostream>
5 #include <algorithm>
6 #include <functional>
7 #include <vector>
8
9 #include <boost/bind/bind.hpp>
10 #include <boost/bind/apply.hpp>
11 #include <boost/smart_ptr/shared_ptr.hpp>
12
13 using namespace std;
14 using namespace boost;
15
16 int f(int a, int b)
17 {
18 return a + b;
19 }
20
21 int g(int a, int b, int c)
22 {
23 return a + b + c;
24 }
25
26 bool preFind(int a, int b)
27 {
28 return a<b;
29 }
30 bool preFind2(int a, int b, int c)
31 {
32 return a<b && a<c;
33 }
34 struct predicate
35 {
36 typedef void result_type;
37 void operator() (int a, int b)
38 {
39 cout << a << "+" << b << "=" << a+b << endl;
40 }
41 };
42 struct X
43 {
44 void f(int a)
45 {
46 cout << "a = " << a << endl;
47 }
48 void f2(int a, int b)
49 {
50 cout << a << "+" << b << "=" << a+b << endl;
51 }
52 predicate pre;
53 int arrElem;
54 int *parrElem;
55 };
56
57 void main()
58 {
59 int arr[5] = {1, 2, 3, 4, 5};
60 //int ret1 = std::count_if(arr, arr+5, boost::bind(preFind, _1, 2));
61 //cout << ret1 << endl;
62 //int ret2 = std::count_if(arr, arr+5, boost::bind(preFind, 2, _2));
63 //cout << ret2 << endl;
64 //std::for_each (arr, arr+5, boost::bind(preFind, _1, 2));
65 //std::for_each (arr, arr+5, boost::bind(predicate(), _1, 2));
66 //std::for_each (arr, arr+5, boost::bind(boost::type<void>(), predicate(), _1, 2));
67 //std::for_each (arr, arr+5, boost::bind(preFind2, _1, 2, 3));
68 //std::for_each (arr, arr+5, boost::bind(preFind2, _1, 2, _1));
69 //boost::bind(preFind2, 5, _2, 3);
70 //std::for_each (arr, arr+5, boost::bind(preFind2, 5, _2, 3));
71
72 //boost::bind(preFind, _1, 2);
73 //boost::bind(preFind, _2, 2);
74 //boost::bind(preFind, _1, _2);
75 //boost::bind(preFind, _2, _1);
76 //boost::bind(preFind2, _1, _2, _3);
77 //boost::bind(predicate(), _1, _2);
78 //boost::bind(boost::type<void>(), predicate(), _1, _2);
79
80 //X x;
81 //boost::bind(&X::f, boost::ref(x), _1);
82 //boost::bind(&X::f, _1, 1);
83 //boost::bind(&X::f, _1, _2);
84 //boost::bind(&X::pre, _1);
85 //std::for_each (arr, arr+5, boost::bind(&X::f, x, _1));
86 //std::for_each (arr, arr+5, boost::bind(&X::f2, boost::ref(x), _1, 2));
87 //std::for_each (arr, arr+5, boost::bind(&X::f2, &x, _1, 2));
88 //boost::shared_ptr<X> p(new X);
89 //std::for_each (arr, arr+5, boost::bind(&X::f2, p, _1, 2));
90 std::vector<X*> arrs;
91 for (int i=0 ; i!=5 ; ++i)
92 {
93 arrs.push_back(new X);
94 }
95 std::for_each (arrs.begin(), arrs.end(), boost::bind(&X::arrElem, _1));
96 std::for_each (arrs.begin(), arrs.end(), boost::bind(&X::f, _1, 2));
97 }
第2部のソースコードは実際の応用である.
1 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/
2 /*bind */
3
4 #include <iostream>
5 #include <algorithm>
6 #include <functional>
7 #include <vector>
8
9 #include <boost/bind/bind.hpp>
10 #include <boost/smart_ptr/shared_ptr.hpp>
11
12 using namespace std;
13 using namespace boost;
14
15 class status
16 {
17 public:
18 std::string name_;
19 bool ok_;
20 public:
21 status(const std::string& name):name_(name),ok_(true) {}
22 void break_it() { ok_=false; }
23 bool is_broken() const { return ok_; }
24 void report() const
25 {
26 std::cout << name_.c_str() << " is " << (ok_ ? "working nominally":"terribly broken") << std::endl;
27 }
28 };
29
30 void nine_arguments( int i1,int i2,int i3,int i4, int i5,int i6,int i7,int i8, int i9)
31 {
32 std::cout << i1 << i2 << i3 << i4 << i5 << i6 << i7 << i8 << i9 << '
';
33 }
34 int main()
35 {
36 int i1=1,i2=2,i3=3,i4=4,i5=5,i6=6,i7=7,i8=8,i9=9;
37 (boost::bind(&nine_arguments,_9,_2,_1,_6,_3,_8,_4,_5,_7))(i1,i2,i3,i4,i5,i6,i7,i8,i9);
38
39
40 //std::vector<status> statuses;
41 //statuses.push_back(status("status 1"));
42 //statuses.push_back(status("status 2"));
43 //statuses.push_back(status("status 3"));
44 //statuses.push_back(status("status 4"));
45 //statuses[1].break_it();
46 //statuses[2].break_it();
47 ////method 1:
48 //for (std::vector<status>::iterator it=statuses.begin(); it!=statuses.end();++it)
49 //{
50 // it->report();
51 //}
52 ////method 2: ok
53 //std::for_each( statuses.begin(), statuses.end(), std::mem_fun_ref(&status::report));
54 ////method 3: ok
55 //std::for_each( statuses.begin(), statuses.end(), boost::bind(&status::report, _1));
56 ////others ok
57 //std::for_each( statuses.begin(), statuses.end(), boost::bind(&status::name_, _1));
58
59 //std::vector<status*> p_statuses;
60 //p_statuses.push_back(new status("status 1"));
61 //p_statuses.push_back(new status("status 2"));
62 //p_statuses.push_back(new status("status 3"));
63 //p_statuses.push_back(new status("status 4"));
64 //p_statuses[1]->break_it();
65 //p_statuses[2]->break_it();
66 ////method 2: ok
67 //std::for_each( p_statuses.begin(), p_statuses.end(), std::mem_fun(&status::report));
68 ////method 3: ok
69 //std::for_each( p_statuses.begin(), p_statuses.end(), boost::bind(&status::report, _1));
70
71 std::vector<boost::shared_ptr<status> > s_statuses;
72 s_statuses.push_back( boost::shared_ptr<status>(new status("status 1")));
73 s_statuses.push_back( boost::shared_ptr<status>(new status("status 2")));
74 s_statuses.push_back( boost::shared_ptr<status>(new status("status 3")));
75 s_statuses.push_back( boost::shared_ptr<status>(new status("status 4")));
76 s_statuses[1]->break_it();
77 s_statuses[2]->break_it();
78 //method 2: error
79 //std::for_each( s_statuses.begin(), s_statuses.end(), std::mem_fun(&status::report));
80 //method 3: ok
81 std::for_each( s_statuses.begin(), s_statuses.end(), boost::bind(&status::report, _1));
82
83
84 }