1 // (C) Copyright 2013 Ruslan Baratov
2 // Copyright (C) 2014 Vicente J. Botet Escriba
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 // See www.boost.org/libs/thread for documentation.
8
9 #define BOOST_THREAD_VERSION 4
10
11 #include <boost/detail/lightweight_test.hpp> // BOOST_TEST
12
13 #include <boost/thread/mutex.hpp>
14 #include <boost/thread/with_lock_guard.hpp>
15 #include <boost/bind/bind.hpp>
16
17 class Foo {
18 public:
Foo(int value)19 Foo(int value): value_(value) {
20 }
21
func(int a,int b) const22 int func(int a, int b) const {
23 BOOST_TEST(a == 1);
24 BOOST_TEST(b == 31);
25 return a + b + value_;
26 }
27
func_ref(int & a) const28 int func_ref(int& a) const {
29 a = 133;
30 return 36;
31 }
32
func_ref(int & a,int & b,int * c) const33 void func_ref(int& a, int& b, int* c) const {
34 BOOST_TEST(value_ == 3);
35 a = 567;
36 b = 897;
37 *c = 345;
38 }
39
40 private:
41 int value_;
42 };
43
test_bind()44 void test_bind() {
45 boost::mutex m;
46
47 Foo foo(2);
48
49 int res_bind = boost::with_lock_guard(
50 m,
51 boost::bind(&Foo::func, foo, 1, 31)
52 );
53 BOOST_TEST(res_bind == 34);
54
55 int a = 0;
56 int res_bind_ref = boost::with_lock_guard(
57 m,
58 boost::bind(&Foo::func_ref, foo, boost::ref(a))
59 );
60 BOOST_TEST(res_bind_ref == 36);
61 BOOST_TEST(a == 133);
62
63 a = 0;
64 int b = 0;
65 int c = 0;
66 Foo boo(3);
67 boost::with_lock_guard(
68 m, boost::bind(&Foo::func_ref, boo, boost::ref(a), boost::ref(b), &c)
69 );
70 BOOST_TEST(a == 567);
71 BOOST_TEST(b == 897);
72 BOOST_TEST(c == 345);
73 }
74
75 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
test_bind_non_const()76 void test_bind_non_const() {
77 std::cout << "c++11 variadic templates disabled" << std::endl;
78 }
79 #else
80
81 // calling non-const bind methods supported only with c++11 variadic templates
82 class Boo {
83 public:
Boo(int value)84 Boo(int value): value_(value) {
85 }
86
func(int a,int b)87 int func(int a, int b) {
88 BOOST_TEST(a == 7);
89 BOOST_TEST(b == 3);
90 return a - b + value_;
91 }
92
func_ref(int & a)93 int func_ref(int& a) {
94 a = 598;
95 return 23;
96 }
97
func_ref(int & a,int & b,int * c)98 void func_ref(int& a, int& b, int* c) {
99 BOOST_TEST(value_ == 67);
100 a = 111;
101 b = 222;
102 *c = 333;
103 }
104
105 private:
106 int value_;
107 };
108
test_bind_non_const()109 void test_bind_non_const() {
110 boost::mutex m;
111
112 Boo boo(20);
113
114 int res_bind = boost::with_lock_guard(
115 m,
116 boost::bind(&Boo::func, boo, 7, 3)
117 );
118 BOOST_TEST(res_bind == 24);
119
120 int a = 0;
121 int res_bind_ref = boost::with_lock_guard(
122 m,
123 boost::bind(&Boo::func_ref, boo, boost::ref(a))
124 );
125 BOOST_TEST(res_bind_ref == 23);
126 BOOST_TEST(a == 598);
127
128 a = 0;
129 int b = 0;
130 int c = 0;
131 Boo foo(67);
132 boost::with_lock_guard(
133 m, boost::bind(&Boo::func_ref, foo, boost::ref(a), boost::ref(b), &c)
134 );
135 BOOST_TEST(a == 111);
136 BOOST_TEST(b == 222);
137 BOOST_TEST(c == 333);
138 }
139 #endif
140
main()141 int main() {
142 test_bind();
143 test_bind_non_const();
144 return boost::report_errors();
145 }
146