invoker_lvalue_pass.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. // Copyright (C) 2014 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/detail/invoker.hpp>
  14. #include <boost/thread/detail/invoker.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. int count = 0;
  17. // 1 arg, return void
  18. void f_void_1(int i)
  19. {
  20. count += i;
  21. }
  22. struct A_void_1
  23. {
  24. typedef void result_type;
  25. void operator()(int i)
  26. {
  27. count += i;
  28. }
  29. void mem1() {
  30. std::cout << "mem1 " << count << std::endl;
  31. ++count;
  32. std::cout << "mem1 " << count << std::endl;
  33. }
  34. void mem2() const {count += 2;}
  35. };
  36. void
  37. test_void_1()
  38. {
  39. int save_count = count;
  40. // function
  41. #if defined BOOST_THREAD_PROVIDES_INVOKE
  42. {
  43. int i = 2;
  44. boost::detail::invoker<void(*)(int), int>(f_void_1, i)();
  45. BOOST_TEST(count == save_count + 2);
  46. save_count = count;
  47. }
  48. #endif
  49. // {
  50. // int i = 2;
  51. // boost::detail::invoke<void>(f_void_1, i);
  52. // BOOST_TEST(count == save_count + 2);
  53. // save_count = count;
  54. // }
  55. // function pointer
  56. #if defined BOOST_THREAD_PROVIDES_INVOKE
  57. {
  58. void (*fp)(int) = f_void_1;
  59. int i = 3;
  60. boost::detail::invoker<void(*)(int), int>(fp, i)();
  61. BOOST_TEST(count == save_count+3);
  62. save_count = count;
  63. }
  64. #endif
  65. // {
  66. // void (*fp)(int) = f_void_1;
  67. // int i = 3;
  68. // boost::detail::invoke<void>(fp, i);
  69. // BOOST_TEST(count == save_count+3);
  70. // save_count = count;
  71. // }
  72. #if defined BOOST_THREAD_PROVIDES_INVOKE
  73. {
  74. void (*fp)(int) = f_void_1;
  75. int i = 3;
  76. boost::detail::invoker<void(*)(int), int>(fp, i)();
  77. BOOST_TEST(count == save_count+3);
  78. save_count = count;
  79. }
  80. #endif
  81. // {
  82. // void (*fp)(int) = f_void_1;
  83. // int i = 3;
  84. // boost::detail::invoke<void>(fp, i);
  85. // BOOST_TEST(count == save_count+3);
  86. // save_count = count;
  87. // }
  88. // functor
  89. #if defined BOOST_THREAD_PROVIDES_INVOKE
  90. {
  91. A_void_1 a0;
  92. int i = 4;
  93. boost::detail::invoker<A_void_1, int>(a0, i)();
  94. BOOST_TEST(count == save_count+4);
  95. save_count = count;
  96. }
  97. #endif
  98. // {
  99. // A_void_1 a0;
  100. // int i = 4;
  101. // boost::detail::invoke<void>(a0, i);
  102. // BOOST_TEST(count == save_count+4);
  103. // save_count = count;
  104. // }
  105. // member function pointer
  106. #if defined BOOST_THREAD_PROVIDES_INVOKE
  107. {
  108. void (A_void_1::*fp)() = &A_void_1::mem1;
  109. A_void_1 a;
  110. //BUG
  111. boost::detail::invoker<void (A_void_1::*)(), A_void_1>(fp, a)();
  112. BOOST_TEST_EQ(count, save_count+1);
  113. save_count = count;
  114. A_void_1* ap = &a;
  115. boost::detail::invoker<void (A_void_1::*)(), A_void_1*>(fp, ap)();
  116. BOOST_TEST_EQ(count, save_count+1);
  117. save_count = count;
  118. }
  119. #endif
  120. // {
  121. // void (A_void_1::*fp)() = &A_void_1::mem1;
  122. // A_void_1 a;
  123. // boost::detail::invoke<void>(fp, a);
  124. // BOOST_TEST(count == save_count+1);
  125. // save_count = count;
  126. // A_void_1* ap = &a;
  127. // boost::detail::invoke<void>(fp, ap);
  128. // BOOST_TEST(count == save_count+1);
  129. // save_count = count;
  130. // }
  131. // const member function pointer
  132. #if defined BOOST_THREAD_PROVIDES_INVOKE
  133. {
  134. void (A_void_1::*fp)() const = &A_void_1::mem2;
  135. A_void_1 a;
  136. boost::detail::invoker<void (A_void_1::*)() const, A_void_1>(fp, a)();
  137. BOOST_TEST(count == save_count+2);
  138. save_count = count;
  139. A_void_1* ap = &a;
  140. boost::detail::invoker<void (A_void_1::*)() const, A_void_1*>(fp, ap)();
  141. BOOST_TEST_EQ(count, save_count+2);
  142. save_count = count;
  143. }
  144. #endif
  145. // {
  146. // void (A_void_1::*fp)() const = &A_void_1::mem2;
  147. // A_void_1 a;
  148. // boost::detail::invoke<void>(fp, a);
  149. // BOOST_TEST(count == save_count+2);
  150. // save_count = count;
  151. // A_void_1* ap = &a;
  152. // boost::detail::invoke<void>(fp, ap);
  153. // BOOST_TEST(count == save_count+2);
  154. // save_count = count;
  155. // }
  156. }
  157. // 1 arg, return int
  158. int f_int_1(int i)
  159. {
  160. return i + 1;
  161. }
  162. struct A_int_1
  163. {
  164. A_int_1() : data_(5) {}
  165. int operator()(int i)
  166. {
  167. return i - 1;
  168. }
  169. int mem1() {return 3;}
  170. int mem2() const {return 4;}
  171. int data_;
  172. };
  173. void
  174. test_int_1()
  175. {
  176. // function
  177. {
  178. int i = 2;
  179. #if defined BOOST_THREAD_PROVIDES_INVOKE
  180. BOOST_TEST((boost::detail::invoker<int(*)(int), int>(f_int_1, i)() == 3));
  181. #endif
  182. // BOOST_TEST(boost::detail::invoke<int>(f_int_1, i) == 3);
  183. }
  184. // function pointer
  185. {
  186. int (*fp)(int) = f_int_1;
  187. int i = 3;
  188. #if defined BOOST_THREAD_PROVIDES_INVOKE
  189. BOOST_TEST((boost::detail::invoker<int (*)(int), int>(fp, i)() == 4));
  190. #endif
  191. // BOOST_TEST(boost::detail::invoke<int>(fp, i) == 4);
  192. }
  193. // functor
  194. {
  195. int i = 4;
  196. #if defined BOOST_THREAD_PROVIDES_INVOKE
  197. BOOST_TEST((boost::detail::invoker<A_int_1, int>(A_int_1(), i)() == 3));
  198. #endif
  199. // const A_int_1 ca;
  200. // A_int_1 a;
  201. // BOOST_TEST(boost::detail::invoke<int>(a, i) == 3);
  202. // //BOOST_TEST(boost::detail::invoke<int>(ca, i) == 3);
  203. //#if defined BOOST_THREAD_PROVIDES_INVOKE
  204. // BOOST_TEST(boost::detail::invoke<int>(A_int_1(), i) == 3);
  205. //#endif
  206. }
  207. // member function pointer
  208. {
  209. A_int_1 a;
  210. #if defined BOOST_THREAD_PROVIDES_INVOKE
  211. BOOST_TEST((boost::detail::invoker<int (A_int_1::*)(), A_int_1>(&A_int_1::mem1, a)() == 3));
  212. #endif
  213. // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem1, a) == 3);
  214. A_int_1* ap = &a;
  215. #if defined BOOST_THREAD_PROVIDES_INVOKE
  216. BOOST_TEST((boost::detail::invoker<int (A_int_1::*)(), A_int_1*>(&A_int_1::mem1, ap)() == 3));
  217. #endif
  218. // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem1, ap) == 3);
  219. }
  220. // const member function pointer
  221. {
  222. A_int_1 a;
  223. #if defined BOOST_THREAD_PROVIDES_INVOKE
  224. BOOST_TEST((boost::detail::invoker<int (A_int_1::*)() const, A_int_1>(&A_int_1::mem2, A_int_1())() == 4));
  225. #endif
  226. // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem2, A_int_1()) == 4);
  227. A_int_1* ap = &a;
  228. #if defined BOOST_THREAD_PROVIDES_INVOKE
  229. BOOST_TEST((boost::detail::invoker<int (A_int_1::*)() const, A_int_1*>(&A_int_1::mem2, ap)() == 4));
  230. #endif
  231. // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem2, ap) == 4);
  232. }
  233. // member data pointer
  234. {
  235. A_int_1 a;
  236. #if defined BOOST_THREAD_PROVIDES_INVOKE
  237. // BUG
  238. //BOOST_TEST(boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a) == 5);
  239. // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, a) == 5);
  240. #endif
  241. //#if defined BOOST_THREAD_PROVIDES_INVOKE
  242. // A_int_1* ap = &a;
  243. //
  244. // boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a) = 6;
  245. // BOOST_TEST(boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a) == 6);
  246. //
  247. //// boost::detail::invoke<int>(&A_int_1::data_, a) = 6;
  248. //// BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, a) == 6);
  249. //
  250. //#endif
  251. //
  252. //#if defined BOOST_THREAD_PROVIDES_INVOKE
  253. // BOOST_TEST(boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, ap) == 6);
  254. // boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, ap) = 7;
  255. // BOOST_TEST(boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, ap) == 7);
  256. //
  257. //// BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, ap) == 7);
  258. //// boost::detail::invoke<int>(&A_int_1::data_, ap) = 8;
  259. //// BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, ap) == 8);
  260. //#endif
  261. }
  262. }
  263. // 2 arg, return void
  264. void f_void_2(int i, int j)
  265. {
  266. count += i+j;
  267. }
  268. struct A_void_2
  269. {
  270. void operator()(int i, int j)
  271. {
  272. count += i+j;
  273. }
  274. void mem1(int i) {count += i;}
  275. void mem2(int i) const {count += i;}
  276. };
  277. void
  278. test_void_2()
  279. {
  280. int save_count = count;
  281. // function
  282. {
  283. int i = 2;
  284. int j = 3;
  285. #if defined BOOST_THREAD_PROVIDES_INVOKE
  286. boost::detail::invoke(f_void_2, i, j);
  287. BOOST_TEST(count == save_count+5);
  288. save_count = count;
  289. #endif
  290. // boost::detail::invoke<void>(f_void_2, i, j);
  291. // BOOST_TEST(count == save_count+5);
  292. // save_count = count;
  293. }
  294. // member function pointer
  295. {
  296. #if defined BOOST_THREAD_PROVIDES_INVOKE
  297. int j = 3;
  298. boost::detail::invoke(&A_void_2::mem1, A_void_2(), j);
  299. BOOST_TEST(count == save_count+3);
  300. save_count = count;
  301. // boost::detail::invoke<void>(&A_void_2::mem1, A_void_2(), j);
  302. // BOOST_TEST(count == save_count+3);
  303. // save_count = count;
  304. #endif
  305. // A_void_2 a2;
  306. // boost::detail::invoke<void>(&A_void_2::mem1, a2, j);
  307. // BOOST_TEST(count == save_count+3);
  308. // save_count = count;
  309. }
  310. }
  311. int main()
  312. {
  313. test_void_1();
  314. test_int_1();
  315. test_void_2();
  316. return boost::report_errors();
  317. }