invoker_rvalue_pass.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #if ! defined BOOST_NO_CXX11_DECLTYPE
  15. //#define BOOST_RESULT_OF_USE_DECLTYPE
  16. #endif
  17. #include <boost/thread/detail/invoker.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. int count = 0;
  20. // 1 arg, return void
  21. void f_void_1(int i)
  22. {
  23. count += i;
  24. }
  25. struct A_void_1
  26. {
  27. typedef void result_type;
  28. void operator()(int i)
  29. {
  30. count += i;
  31. }
  32. void mem1() {++count;}
  33. void mem2() const {count += 2;}
  34. };
  35. void
  36. test_void_1()
  37. {
  38. int save_count = count;
  39. // function
  40. {
  41. boost::detail::invoker<void(*)(int), int>(f_void_1, 2)();
  42. BOOST_TEST(count == save_count + 2);
  43. save_count = count;
  44. }
  45. // function pointer
  46. {
  47. void (*fp)(int) = f_void_1;
  48. boost::detail::invoker<void(*)(int), int>(fp, 3)();
  49. BOOST_TEST(count == save_count+3);
  50. save_count = count;
  51. }
  52. // functor
  53. {
  54. A_void_1 a0;
  55. #if defined BOOST_THREAD_PROVIDES_INVOKE
  56. boost::detail::invoker<A_void_1, int>(a0, 4)();
  57. BOOST_TEST(count == save_count+4);
  58. save_count = count;
  59. #endif
  60. // boost::detail::invoke<void>(a0, 4);
  61. // BOOST_TEST(count == save_count+4);
  62. // save_count = count;
  63. }
  64. // member function pointer
  65. {
  66. #if defined BOOST_THREAD_PROVIDES_INVOKE
  67. void (A_void_1::*fp)() = &A_void_1::mem1;
  68. boost::detail::invoker<void (A_void_1::*)(), A_void_1>(fp, A_void_1())();
  69. BOOST_TEST(count == save_count+1);
  70. save_count = count;
  71. #endif
  72. // boost::detail::invoke<void>(fp, A_void_1());
  73. // BOOST_TEST(count == save_count+1);
  74. // save_count = count;
  75. #if defined BOOST_THREAD_PROVIDES_INVOKE
  76. A_void_1 a;
  77. boost::detail::invoker<void (A_void_1::*)(), A_void_1*>(fp, &a)();
  78. BOOST_TEST(count == save_count+1);
  79. save_count = count;
  80. #endif
  81. // boost::detail::invoke<int>(fp, &a);
  82. // BOOST_TEST(count == save_count+1);
  83. // save_count = count;
  84. }
  85. // const member function pointer
  86. {
  87. void (A_void_1::*fp)() const = &A_void_1::mem2;
  88. boost::detail::invoker<void (A_void_1::*)() const, A_void_1>(fp, A_void_1())();
  89. BOOST_TEST(count == save_count+2);
  90. save_count = count;
  91. A_void_1 a;
  92. boost::detail::invoker<void (A_void_1::*)() const, A_void_1*>(fp, &a)();
  93. BOOST_TEST(count == save_count+2);
  94. save_count = count;
  95. }
  96. }
  97. // 1 arg, return int
  98. int f_int_1(int i)
  99. {
  100. return i + 1;
  101. }
  102. struct A_int_1
  103. {
  104. A_int_1() : data_(5) {}
  105. int operator()(int i)
  106. {
  107. return i - 1;
  108. }
  109. int mem1() {return 3;}
  110. int mem2() const {return 4;}
  111. int data_;
  112. };
  113. void
  114. test_int_1()
  115. {
  116. // function
  117. {
  118. BOOST_TEST((boost::detail::invoker<int (*)(int), int>(f_int_1, 2)() == 3));
  119. }
  120. // function pointer
  121. {
  122. int (*fp)(int) = f_int_1;
  123. BOOST_TEST((boost::detail::invoker<int (*)(int), int>(fp, 3)() == 4));
  124. }
  125. // functor
  126. {
  127. #if defined BOOST_THREAD_PROVIDES_INVOKE
  128. BOOST_TEST((boost::detail::invoker<A_int_1, int>(A_int_1(), 4)() == 3));
  129. // BOOST_TEST(boost::detail::invoke<int>(A_int_1(), 4) == 3);
  130. #endif
  131. }
  132. // member function pointer
  133. {
  134. #if defined BOOST_THREAD_PROVIDES_INVOKE
  135. BOOST_TEST((boost::detail::invoker<int (A_int_1::*)(),A_int_1>(&A_int_1::mem1, A_int_1())() == 3));
  136. // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem1, A_int_1()) == 3);
  137. #endif
  138. A_int_1 a;
  139. BOOST_TEST((boost::detail::invoker<int (A_int_1::*)(), A_int_1*>(&A_int_1::mem1, &a)() == 3));
  140. }
  141. // const member function pointer
  142. {
  143. BOOST_TEST((boost::detail::invoker<int (A_int_1::*)() const, A_int_1>(&A_int_1::mem2, A_int_1())() == 4));
  144. A_int_1 a;
  145. BOOST_TEST((boost::detail::invoker<int (A_int_1::*)() const, A_int_1*>(&A_int_1::mem2, &a)() == 4));
  146. }
  147. // member data pointer
  148. {
  149. #if defined BOOST_THREAD_PROVIDES_INVOKE
  150. // BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, A_int_1())() == 5));
  151. //// BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, A_int_1()) == 5);
  152. // A_int_1 a;
  153. // BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a)() == 5));
  154. // boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a)() = 6;
  155. // BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a)() == 6));
  156. // BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, &a)() == 6));
  157. // boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, &a)() = 7;
  158. // BOOST_TEST((boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, &a)() == 7));
  159. #endif
  160. }
  161. }
  162. // 2 arg, return void
  163. void f_void_2(int i, int j)
  164. {
  165. count += i+j;
  166. }
  167. struct A_void_2
  168. {
  169. void operator()(int i, int j)
  170. {
  171. count += i+j;
  172. }
  173. void mem1(int i) {count += i;}
  174. void mem2(int i) const {count += i;}
  175. };
  176. void
  177. test_void_2()
  178. {
  179. int save_count = count;
  180. // function
  181. {
  182. boost::detail::invoke(f_void_2, 2, 3);
  183. BOOST_TEST(count == save_count+5);
  184. save_count = count;
  185. }
  186. // member function pointer
  187. {
  188. #if defined BOOST_THREAD_PROVIDES_INVOKE
  189. boost::detail::invoke(&A_void_2::mem1, A_void_2(), 3);
  190. BOOST_TEST(count == save_count+3);
  191. save_count = count;
  192. boost::detail::invoke<void>(&A_void_2::mem1, A_void_2(), 3);
  193. BOOST_TEST(count == save_count+3);
  194. save_count = count;
  195. #endif
  196. }
  197. }
  198. int main()
  199. {
  200. test_void_1();
  201. test_int_1();
  202. test_void_2();
  203. return boost::report_errors();
  204. }