test_dynamic_any_cast.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2015 Steven Watanabe
  4. //
  5. // Distributed under the Boost Software License Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // $Id$
  10. #include <boost/type_erasure/any.hpp>
  11. #include <boost/type_erasure/builtin.hpp>
  12. #include <boost/type_erasure/operators.hpp>
  13. #include <boost/type_erasure/dynamic_any_cast.hpp>
  14. #include <boost/type_erasure/any_cast.hpp>
  15. #include <boost/mpl/vector.hpp>
  16. #include <boost/mpl/map.hpp>
  17. #define BOOST_TEST_MAIN
  18. #include <boost/test/unit_test.hpp>
  19. using namespace boost::type_erasure;
  20. template<class T = _self>
  21. struct common : ::boost::mpl::vector<
  22. copy_constructible<T>,
  23. typeid_<T>
  24. > {};
  25. struct fixture
  26. {
  27. fixture()
  28. {
  29. register_binding<common<>, int>();
  30. register_binding<incrementable<>, int>();
  31. register_binding<addable<_self, _self, _a> >(make_binding<boost::mpl::map<boost::mpl::pair<_self, int>, boost::mpl::pair<_a, int> > >());
  32. }
  33. };
  34. BOOST_GLOBAL_FIXTURE(fixture);
  35. BOOST_AUTO_TEST_CASE(test_identical)
  36. {
  37. any<common<> > x(1);
  38. any<common<> > y = dynamic_any_cast<any<common<> > >(x);
  39. BOOST_CHECK_EQUAL(any_cast<int>(y), 1);
  40. }
  41. BOOST_AUTO_TEST_CASE(test_downcast)
  42. {
  43. any<common<> > x(1);
  44. typedef any< ::boost::mpl::vector<common<>, incrementable<> > > incrementable_any;
  45. incrementable_any y = dynamic_any_cast<incrementable_any>(x);
  46. ++y;
  47. BOOST_CHECK_EQUAL(any_cast<int>(y), 2);
  48. }
  49. BOOST_AUTO_TEST_CASE(test_cross_cast)
  50. {
  51. any< ::boost::mpl::vector<common<>, decrementable<> > > x(1);
  52. typedef any< ::boost::mpl::vector<common<>, incrementable<> > > incrementable_any;
  53. incrementable_any y = dynamic_any_cast<incrementable_any>(x);
  54. ++y;
  55. BOOST_CHECK_EQUAL(any_cast<int>(y), 2);
  56. }
  57. BOOST_AUTO_TEST_CASE(test_cast_placeholder)
  58. {
  59. any<common<> > x(1);
  60. typedef any< ::boost::mpl::vector<common<_a>, incrementable<_a> >, _a> incrementable_any;
  61. incrementable_any y = dynamic_any_cast<incrementable_any>(x);
  62. ++y;
  63. BOOST_CHECK_EQUAL(any_cast<int>(y), 2);
  64. }
  65. BOOST_AUTO_TEST_CASE(test_throw)
  66. {
  67. any<common<> > x("42");
  68. typedef any< ::boost::mpl::vector<common<_a>, incrementable<_a> >, _a> incrementable_any;
  69. BOOST_CHECK_THROW(dynamic_any_cast<incrementable_any>(x), bad_any_cast);
  70. }
  71. // make sure that a function registered with _self can
  72. // be found with _a.
  73. BOOST_AUTO_TEST_CASE(test_other_placeholder)
  74. {
  75. any<common<_a>, _a> x(1);
  76. typedef any< ::boost::mpl::vector<common<_a>, incrementable<_a> >, _a> incrementable_any;
  77. incrementable_any y = dynamic_any_cast<incrementable_any>(x);
  78. ++y;
  79. BOOST_CHECK_EQUAL(any_cast<int>(y), 2);
  80. }
  81. // Casting to a value only requires the target to provide
  82. // a copy constructor.
  83. BOOST_AUTO_TEST_CASE(test_add_copy)
  84. {
  85. any< ::boost::mpl::vector<destructible<>, typeid_<> > > x(1);
  86. any<common<> > y = dynamic_any_cast<any<common<> > >(x);
  87. BOOST_CHECK_EQUAL(any_cast<int>(y), 1);
  88. }
  89. template<class T, class U>
  90. struct choose_second
  91. {
  92. typedef U type;
  93. };
  94. BOOST_AUTO_TEST_CASE(test_deduced)
  95. {
  96. typedef deduced<choose_second<_self, int> > _p2;
  97. any< ::boost::mpl::vector<common<>, common<_p2> > > x(1);
  98. typedef ::boost::mpl::vector<common<>, common<_p2>, incrementable<_p2>, addable<_self, _self, _p2> > dest_concept;
  99. any<dest_concept> y = dynamic_any_cast<any<dest_concept> >(x);
  100. any<dest_concept, _p2> z = y + y;
  101. ++z;
  102. BOOST_CHECK_EQUAL(any_cast<int>(z), 3);
  103. }
  104. BOOST_AUTO_TEST_CASE(test_multiple_placeholders)
  105. {
  106. typedef ::boost::mpl::map< ::boost::mpl::pair<_a, int>, boost::mpl::pair<_b, int> > init_map;
  107. any< ::boost::mpl::vector<common<_a>, common<_b> >, _a> x(1, make_binding<init_map>());
  108. typedef ::boost::mpl::vector<common<_a>, common<_b>, incrementable<_b>, addable<_a, _a, _b> > dest_concept;
  109. typedef ::boost::mpl::map< ::boost::mpl::pair<_a, _a>, ::boost::mpl::pair<_b, _b> > placeholder_map;
  110. any<dest_concept, _a> y = dynamic_any_cast<any<dest_concept, _a> >(x, make_binding<placeholder_map>());
  111. any<dest_concept, _b> z = y + y;
  112. ++z;
  113. BOOST_CHECK_EQUAL(any_cast<int>(z), 3);
  114. }
  115. BOOST_AUTO_TEST_CASE(test_multiple_placeholders_switch)
  116. {
  117. typedef ::boost::mpl::map< ::boost::mpl::pair<_a, int>, boost::mpl::pair<_b, int> > init_map;
  118. any< ::boost::mpl::vector<common<_a>, common<_b> >, _a> x(1, make_binding<init_map>());
  119. typedef ::boost::mpl::vector<common<_c>, common<_d>, incrementable<_d>, addable<_c, _c, _d> > dest_concept;
  120. typedef ::boost::mpl::map< ::boost::mpl::pair<_c, _a>, ::boost::mpl::pair<_d, _b> > placeholder_map;
  121. any<dest_concept, _c> y = dynamic_any_cast<any<dest_concept, _c> >(x, make_binding<placeholder_map>());
  122. any<dest_concept, _d> z = y + y;
  123. ++z;
  124. BOOST_CHECK_EQUAL(any_cast<int>(z), 3);
  125. }
  126. template<class T>
  127. T as_rvalue(const T& arg) { return arg; }
  128. template<class T>
  129. const T& as_const(const T& arg) { return arg; }
  130. BOOST_AUTO_TEST_CASE(test_val)
  131. {
  132. any<common<> > x(1);
  133. // value
  134. any<common<> > y1 = dynamic_any_cast<any<common<> > >(x);
  135. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(y1));
  136. any<common<> > y2 = dynamic_any_cast<any<common<> > >(as_rvalue(x));
  137. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(y2));
  138. any<common<> > y3 = dynamic_any_cast<any<common<> > >(as_const(x));
  139. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(y3));
  140. // lvalue reference
  141. any<common<>, _self&> r(x);
  142. any<common<> > z1 = dynamic_any_cast<any<common<> > >(r);
  143. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(z1));
  144. any<common<> > z2 = dynamic_any_cast<any<common<> > >(as_rvalue(r));
  145. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(z2));
  146. any<common<> > z3 = dynamic_any_cast<any<common<> > >(as_const(r));
  147. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(z3));
  148. // const reference
  149. any<common<>, const _self&> cr(x);
  150. any<common<> > w1 = dynamic_any_cast<any<common<> > >(cr);
  151. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(w1));
  152. any<common<> > w2 = dynamic_any_cast<any<common<> > >(as_rvalue(cr));
  153. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(w2));
  154. any<common<> > w3 = dynamic_any_cast<any<common<> > >(as_const(cr));
  155. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(w3));
  156. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  157. // rvalue reference
  158. any<common<>, _self&&> rr(std::move(x));
  159. any<common<> > v1 = dynamic_any_cast<any<common<> > >(rr);
  160. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(v1));
  161. any<common<> > v2 = dynamic_any_cast<any<common<> > >(as_rvalue(rr));
  162. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(v2));
  163. any<common<> > v3 = dynamic_any_cast<any<common<> > >(as_const(rr));
  164. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(v3));
  165. #endif
  166. }
  167. BOOST_AUTO_TEST_CASE(test_ref)
  168. {
  169. // A non-const reference can only bind to a few cases
  170. any<common<> > x(1);
  171. any<common<>, _self&> y = dynamic_any_cast<any<common<>, _self&> >(x);
  172. BOOST_CHECK_EQUAL(any_cast<int*>(&x), any_cast<int*>(&y));
  173. any<common<>, _self&> z = dynamic_any_cast<any<common<>, _self&> >(y);
  174. BOOST_CHECK_EQUAL(any_cast<int*>(&x), any_cast<int*>(&z));
  175. any<common<>, _self&> w = dynamic_any_cast<any<common<>, _self&> >(as_rvalue(y));
  176. BOOST_CHECK_EQUAL(any_cast<int*>(&x), any_cast<int*>(&w));
  177. any<common<>, _self&> v = dynamic_any_cast<any<common<>, _self&> >(as_const(y));
  178. BOOST_CHECK_EQUAL(any_cast<int*>(&x), any_cast<int*>(&v));
  179. }
  180. BOOST_AUTO_TEST_CASE(test_cref)
  181. {
  182. any<common<> > x(1);
  183. typedef any<common<>, const _self&> dest_type;
  184. // value
  185. dest_type y1 = dynamic_any_cast<dest_type>(x);
  186. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&y1));
  187. // as_rvalue creates a temporary
  188. BOOST_CHECK_EQUAL(any_cast<int>(x), any_cast<int>(dynamic_any_cast<dest_type>(as_rvalue(x))));
  189. dest_type y3 = dynamic_any_cast<dest_type>(as_const(x));
  190. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&y3));
  191. // lvalue reference
  192. any<common<>, _self&> r(x);
  193. dest_type z1 = dynamic_any_cast<dest_type>(r);
  194. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&z1));
  195. dest_type z2 = dynamic_any_cast<dest_type>(as_rvalue(r));
  196. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&z2));
  197. dest_type z3 = dynamic_any_cast<dest_type>(as_const(r));
  198. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&z3));
  199. // const reference
  200. any<common<>, const _self&> cr(x);
  201. dest_type w1 = dynamic_any_cast<dest_type>(cr);
  202. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&w1));
  203. dest_type w2 = dynamic_any_cast<dest_type>(as_rvalue(cr));
  204. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&w2));
  205. dest_type w3 = dynamic_any_cast<dest_type>(as_const(cr));
  206. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&w3));
  207. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  208. // rvalue reference
  209. any<common<>, _self&&> rr(std::move(x));
  210. dest_type v1 = dynamic_any_cast<dest_type>(rr);
  211. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&v1));
  212. dest_type v2 = dynamic_any_cast<dest_type>(as_rvalue(rr));
  213. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&v2));
  214. dest_type v3 = dynamic_any_cast<dest_type>(as_const(rr));
  215. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), any_cast<const int*>(&v3));
  216. #endif
  217. }
  218. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  219. BOOST_AUTO_TEST_CASE(test_rref)
  220. {
  221. any<common<> > x(1);
  222. typedef any<common<>, _self&&> dest_type;
  223. // value
  224. dest_type y2 = dynamic_any_cast<dest_type>(std::move(x));
  225. BOOST_CHECK_EQUAL(any_cast<int*>(&x), any_cast<int*>(&y2));
  226. // rvalue reference
  227. any<common<>, _self&&> rr(std::move(x));
  228. dest_type v2 = dynamic_any_cast<dest_type>(std::move(rr));
  229. BOOST_CHECK_EQUAL(any_cast<int*>(&x), any_cast<int*>(&v2));
  230. }
  231. #endif