• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // Copyright (C) 2014 Vicente J. Botet Escriba
11 //
12 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
13 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14 
15 // <boost/thread/detail/invoker.hpp>
16 
17 #if ! defined  BOOST_NO_CXX11_DECLTYPE
18 //#define BOOST_RESULT_OF_USE_DECLTYPE
19 #endif
20 
21 #include <boost/thread/detail/invoker.hpp>
22 #include <boost/detail/lightweight_test.hpp>
23 
24 int count = 0;
25 
26 // 1 arg, return void
27 
f_void_1(int i)28 void f_void_1(int i)
29 {
30     count += i;
31 }
32 
33 struct A_void_1
34 {
35     typedef void result_type;
operator ()A_void_136     void operator()(int i)
37     {
38         count += i;
39     }
40 
mem1A_void_141     void mem1() {++count;}
mem2A_void_142     void mem2() const {count += 2;}
43 };
44 
45 void
test_void_1()46 test_void_1()
47 {
48     int save_count = count;
49     // function
50     {
51     boost::detail::invoker<void(*)(int), int>(f_void_1, 2)();
52     BOOST_TEST(count == save_count + 2);
53     save_count = count;
54     }
55     // function pointer
56     {
57     void (*fp)(int) = f_void_1;
58     boost::detail::invoker<void(*)(int), int>(fp, 3)();
59     BOOST_TEST(count == save_count+3);
60     save_count = count;
61     }
62     // functor
63     {
64     A_void_1 a0;
65 #if defined BOOST_THREAD_PROVIDES_INVOKE
66     boost::detail::invoker<A_void_1, int>(a0, 4)();
67     BOOST_TEST(count == save_count+4);
68     save_count = count;
69 #endif
70 //    boost::detail::invoke<void>(a0, 4);
71 //    BOOST_TEST(count == save_count+4);
72 //    save_count = count;
73     }
74     // member function pointer
75     {
76 #if defined BOOST_THREAD_PROVIDES_INVOKE
77     void (A_void_1::*fp)() = &A_void_1::mem1;
78     boost::detail::invoker<void (A_void_1::*)(), A_void_1>(fp, A_void_1())();
79     BOOST_TEST(count == save_count+1);
80     save_count = count;
81 
82 #endif
83     //    boost::detail::invoke<void>(fp, A_void_1());
84     //    BOOST_TEST(count == save_count+1);
85     //    save_count = count;
86 #if defined BOOST_THREAD_PROVIDES_INVOKE
87     A_void_1 a;
88     boost::detail::invoker<void (A_void_1::*)(), A_void_1*>(fp, &a)();
89     BOOST_TEST(count == save_count+1);
90     save_count = count;
91 
92 #endif
93     //    boost::detail::invoke<int>(fp, &a);
94     //    BOOST_TEST(count == save_count+1);
95     //    save_count = count;
96     }
97     // const member function pointer
98     {
99     void (A_void_1::*fp)() const = &A_void_1::mem2;
100     boost::detail::invoker<void (A_void_1::*)() const, A_void_1>(fp, A_void_1())();
101     BOOST_TEST(count == save_count+2);
102     save_count = count;
103     A_void_1 a;
104     boost::detail::invoker<void (A_void_1::*)() const, A_void_1*>(fp, &a)();
105     BOOST_TEST(count == save_count+2);
106     save_count = count;
107     }
108 }
109 
110 // 1 arg, return int
111 
f_int_1(int i)112 int f_int_1(int i)
113 {
114     return i + 1;
115 }
116 
117 struct A_int_1
118 {
A_int_1A_int_1119     A_int_1() : data_(5) {}
operator ()A_int_1120     int operator()(int i)
121     {
122         return i - 1;
123     }
124 
mem1A_int_1125     int mem1() {return 3;}
mem2A_int_1126     int mem2() const {return 4;}
127     int data_;
128 };
129 
130 void
test_int_1()131 test_int_1()
132 {
133     // function
134     {
135     BOOST_TEST((boost::detail::invoker<int (*)(int), int>(f_int_1, 2)() == 3));
136     }
137     // function pointer
138     {
139     int (*fp)(int) = f_int_1;
140     BOOST_TEST((boost::detail::invoker<int (*)(int), int>(fp, 3)() == 4));
141     }
142     // functor
143     {
144 #if defined BOOST_THREAD_PROVIDES_INVOKE
145     BOOST_TEST((boost::detail::invoker<A_int_1, int>(A_int_1(), 4)() == 3));
146 //    BOOST_TEST(boost::detail::invoke<int>(A_int_1(), 4) == 3);
147 #endif
148     }
149     // member function pointer
150     {
151 #if defined BOOST_THREAD_PROVIDES_INVOKE
152     BOOST_TEST((boost::detail::invoker<int (A_int_1::*)(),A_int_1>(&A_int_1::mem1, A_int_1())() == 3));
153 //    BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem1, A_int_1()) == 3);
154 #endif
155 
156     A_int_1 a;
157     BOOST_TEST((boost::detail::invoker<int (A_int_1::*)(), A_int_1*>(&A_int_1::mem1, &a)() == 3));
158     }
159     // const member function pointer
160     {
161     BOOST_TEST((boost::detail::invoker<int (A_int_1::*)() const, A_int_1>(&A_int_1::mem2, A_int_1())() == 4));
162     A_int_1 a;
163     BOOST_TEST((boost::detail::invoker<int (A_int_1::*)() const, A_int_1*>(&A_int_1::mem2, &a)() == 4));
164     }
165     // member data pointer
166     {
167 #if defined BOOST_THREAD_PROVIDES_INVOKE
168 //    BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, A_int_1())() == 5));
169 ////    BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, A_int_1()) == 5);
170 //    A_int_1 a;
171 //    BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a)() == 5));
172 //    boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a)() = 6;
173 //    BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a)() == 6));
174 //    BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, &a)() == 6));
175 //    boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, &a)() = 7;
176 //    BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, &a)() == 7));
177 #endif
178     }
179 }
180 
181 // 2 arg, return void
182 
f_void_2(int i,int j)183 void f_void_2(int i, int j)
184 {
185     count += i+j;
186 }
187 
188 struct A_void_2
189 {
operator ()A_void_2190     void operator()(int i, int j)
191     {
192         count += i+j;
193     }
194 
mem1A_void_2195     void mem1(int i) {count += i;}
mem2A_void_2196     void mem2(int i) const {count += i;}
197 };
198 
199 void
test_void_2()200 test_void_2()
201 {
202     int save_count = count;
203     // function
204     {
205     boost::detail::invoke(f_void_2, 2, 3);
206     BOOST_TEST(count == save_count+5);
207     save_count = count;
208     }
209     // member function pointer
210     {
211 #if defined BOOST_THREAD_PROVIDES_INVOKE
212     boost::detail::invoke(&A_void_2::mem1, A_void_2(), 3);
213     BOOST_TEST(count == save_count+3);
214     save_count = count;
215 
216     boost::detail::invoke<void>(&A_void_2::mem1, A_void_2(), 3);
217     BOOST_TEST(count == save_count+3);
218     save_count = count;
219 #endif
220 
221     }
222 }
223 
main()224 int main()
225 {
226     test_void_1();
227     test_int_1();
228     test_void_2();
229     return boost::report_errors();
230 }
231