test_construct_cref.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 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/any_cast.hpp>
  14. #include <boost/type_erasure/relaxed.hpp>
  15. #include <boost/mpl/vector.hpp>
  16. #define BOOST_TEST_MAIN
  17. #include <boost/test/unit_test.hpp>
  18. using namespace boost::type_erasure;
  19. template<class T = _self>
  20. struct common : ::boost::mpl::vector<
  21. copy_constructible<T>,
  22. typeid_<T>
  23. > {};
  24. template<class T>
  25. T as_rvalue(const T& arg) { return arg; }
  26. template<class T>
  27. const T& as_const(const T& arg) { return arg; }
  28. BOOST_AUTO_TEST_CASE(test_implicit) {
  29. int i = 4;
  30. any<common<>, const _self&> x = i;
  31. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  32. }
  33. BOOST_AUTO_TEST_CASE(test_from_int_with_binding)
  34. {
  35. typedef ::boost::mpl::vector<common<> > test_concept;
  36. int i = 4;
  37. any<test_concept, const _self&> x(i, make_binding<boost::mpl::map<boost::mpl::pair<_self, int> > >());
  38. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  39. }
  40. BOOST_AUTO_TEST_CASE(test_copy)
  41. {
  42. typedef ::boost::mpl::vector<typeid_<> > test_concept;
  43. int i = 4;
  44. any<test_concept, const _self&> x(i);
  45. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  46. any<test_concept, const _self&> y(x);
  47. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  48. any<test_concept, const _self&> z = as_rvalue(x);
  49. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  50. any<test_concept, const _self&> w = as_const(x);
  51. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), &i);
  52. }
  53. BOOST_AUTO_TEST_CASE(test_convert)
  54. {
  55. typedef ::boost::mpl::vector<common<>, typeid_<> > src_concept;
  56. typedef ::boost::mpl::vector<typeid_<> > dst_concept;
  57. int i = 4;
  58. any<src_concept, const _self&> x(i);
  59. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  60. any<dst_concept, const _self&> y(x);
  61. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  62. any<dst_concept, const _self&> z = as_rvalue(x);
  63. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  64. any<dst_concept, const _self&> w = as_const(x);
  65. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), &i);
  66. }
  67. BOOST_AUTO_TEST_CASE(test_rebind)
  68. {
  69. typedef ::boost::mpl::vector<typeid_<> > src_concept;
  70. typedef ::boost::mpl::vector<typeid_<_a> > dst_concept;
  71. int i = 4;
  72. any<src_concept, const _self&> x(i);
  73. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  74. any<dst_concept, const _a&> y(x);
  75. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  76. any<dst_concept, const _a&> z = as_rvalue(x);
  77. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  78. any<dst_concept, const _a&> w = as_const(x);
  79. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), &i);
  80. }
  81. BOOST_AUTO_TEST_CASE(test_rebind_and_convert)
  82. {
  83. typedef ::boost::mpl::vector<common<>, typeid_<> > src_concept;
  84. typedef ::boost::mpl::vector<typeid_<_a> > dst_concept;
  85. int i = 4;
  86. any<src_concept, const _self&> x(i);
  87. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  88. any<dst_concept, const _a&> y(x);
  89. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  90. any<dst_concept, const _a&> z = as_rvalue(x);
  91. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  92. any<dst_concept, const _a&> w = as_const(x);
  93. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), &i);
  94. }
  95. BOOST_AUTO_TEST_CASE(test_rebind_and_convert_with_binding)
  96. {
  97. typedef ::boost::mpl::vector<common<>, incrementable<> > src_concept;
  98. typedef ::boost::mpl::vector<common<_a> > dst_concept;
  99. typedef ::boost::mpl::map<boost::mpl::pair<_a, _self> > map;
  100. typedef ::boost::mpl::map<boost::mpl::pair<_a, int> > types;
  101. binding<dst_concept> table(make_binding<types>());
  102. int i = 4;
  103. any<src_concept, const _self&> x(i);
  104. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  105. any<dst_concept, const _a&> y(x, make_binding<map>());
  106. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  107. any<dst_concept, const _a&> z(x, table);
  108. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  109. any<dst_concept, const _a&> cy(as_const(x), make_binding<map>());
  110. BOOST_CHECK_EQUAL(any_cast<const int*>(&cy), &i);
  111. any<dst_concept, const _a&> cz(as_const(x), table);
  112. BOOST_CHECK_EQUAL(any_cast<const int*>(&cz), &i);
  113. }
  114. BOOST_AUTO_TEST_CASE(test_copy_from_ref)
  115. {
  116. typedef ::boost::mpl::vector<typeid_<> > test_concept;
  117. int i = 4;
  118. any<test_concept, _self&> x(i);
  119. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  120. any<test_concept, const _self&> y(x);
  121. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  122. any<test_concept, const _self&> z = as_rvalue(x);
  123. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  124. any<test_concept, const _self&> w = as_const(x);
  125. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), &i);
  126. }
  127. BOOST_AUTO_TEST_CASE(test_convert_from_ref)
  128. {
  129. typedef ::boost::mpl::vector<common<>, typeid_<> > src_concept;
  130. typedef ::boost::mpl::vector<typeid_<> > dst_concept;
  131. int i = 4;
  132. any<src_concept, _self&> x(i);
  133. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  134. any<dst_concept, const _self&> y(x);
  135. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  136. any<dst_concept, const _self&> z = as_rvalue(x);
  137. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  138. any<dst_concept, const _self&> w = as_const(x);
  139. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), &i);
  140. }
  141. BOOST_AUTO_TEST_CASE(test_rebind_from_ref)
  142. {
  143. typedef ::boost::mpl::vector<typeid_<> > src_concept;
  144. typedef ::boost::mpl::vector<typeid_<_a> > dst_concept;
  145. int i = 4;
  146. any<src_concept, _self&> x(i);
  147. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  148. any<dst_concept, const _a&> y(x);
  149. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  150. any<dst_concept, const _a&> z = as_rvalue(x);
  151. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  152. any<dst_concept, const _a&> w = as_const(x);
  153. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), &i);
  154. }
  155. BOOST_AUTO_TEST_CASE(test_rebind_and_convert_from_ref)
  156. {
  157. typedef ::boost::mpl::vector<common<>, typeid_<> > src_concept;
  158. typedef ::boost::mpl::vector<typeid_<_a> > dst_concept;
  159. int i = 4;
  160. any<src_concept, _self&> x(i);
  161. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  162. any<dst_concept, const _a&> y(x);
  163. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  164. any<dst_concept, const _a&> z = as_rvalue(x);
  165. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  166. any<dst_concept, const _a&> w = as_const(x);
  167. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), &i);
  168. }
  169. BOOST_AUTO_TEST_CASE(test_rebind_and_convert_with_binding_from_ref)
  170. {
  171. typedef ::boost::mpl::vector<common<>, incrementable<> > src_concept;
  172. typedef ::boost::mpl::vector<common<_a> > dst_concept;
  173. typedef ::boost::mpl::map<boost::mpl::pair<_a, _self> > map;
  174. typedef ::boost::mpl::map<boost::mpl::pair<_a, int> > types;
  175. binding<dst_concept> table(make_binding<types>());
  176. int i = 4;
  177. any<src_concept, _self&> x(i);
  178. BOOST_CHECK_EQUAL(any_cast<const int*>(&x), &i);
  179. any<dst_concept, const _a&> y(x, make_binding<map>());
  180. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  181. any<dst_concept, const _a&> z(x, table);
  182. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), &i);
  183. any<dst_concept, const _a&> cy(as_const(x), make_binding<map>());
  184. BOOST_CHECK_EQUAL(any_cast<const int*>(&cy), &i);
  185. any<dst_concept, const _a&> cz(as_const(x), table);
  186. BOOST_CHECK_EQUAL(any_cast<const int*>(&cz), &i);
  187. }
  188. BOOST_AUTO_TEST_CASE(test_copy_from_value)
  189. {
  190. typedef ::boost::mpl::vector<copy_constructible<>, typeid_<> > test_concept;
  191. any<test_concept> x(4);
  192. const int* ip = any_cast<const int*>(&x);
  193. any<test_concept, const _self&> y(x);
  194. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), ip);
  195. BOOST_CHECK_EQUAL(any_cast<int>(any<test_concept, const _self&>(as_rvalue(x))), 4);
  196. any<test_concept, const _self&> w = as_const(x);
  197. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), ip);
  198. }
  199. BOOST_AUTO_TEST_CASE(test_convert_from_value)
  200. {
  201. typedef ::boost::mpl::vector<common<>, typeid_<> > src_concept;
  202. typedef ::boost::mpl::vector<typeid_<> > dst_concept;
  203. any<src_concept> x(4);
  204. const int* ip = any_cast<const int*>(&x);
  205. any<dst_concept, const _self&> y(x);
  206. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), ip);
  207. BOOST_CHECK_EQUAL(any_cast<int>(any<dst_concept, const _self&>(as_rvalue(x))), 4);
  208. any<dst_concept, const _self&> w = as_const(x);
  209. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), ip);
  210. }
  211. BOOST_AUTO_TEST_CASE(test_rebind_from_value)
  212. {
  213. typedef ::boost::mpl::vector<copy_constructible<>, typeid_<> > src_concept;
  214. typedef ::boost::mpl::vector<copy_constructible<_a>, typeid_<_a> > dst_concept;
  215. any<src_concept> x(4);
  216. const int* ip = any_cast<const int*>(&x);
  217. any<dst_concept, const _a&> y(x);
  218. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), ip);
  219. BOOST_CHECK_EQUAL(any_cast<int>(any<dst_concept, const _a&>(as_rvalue(x))), 4);
  220. any<dst_concept, const _a&> w = as_const(x);
  221. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), ip);
  222. }
  223. BOOST_AUTO_TEST_CASE(test_rebind_and_convert_from_value)
  224. {
  225. typedef ::boost::mpl::vector<common<>, typeid_<> > src_concept;
  226. typedef ::boost::mpl::vector<typeid_<_a> > dst_concept;
  227. any<src_concept> x(4);
  228. const int* ip = any_cast<const int*>(&x);
  229. any<dst_concept, const _a&> y(x);
  230. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), ip);
  231. BOOST_CHECK_EQUAL(any_cast<int>(any<dst_concept, const _a&>(as_rvalue(x))), 4);
  232. any<dst_concept, const _a&> w = as_const(x);
  233. BOOST_CHECK_EQUAL(any_cast<const int*>(&w), ip);
  234. }
  235. BOOST_AUTO_TEST_CASE(test_rebind_and_convert_with_binding_from_value)
  236. {
  237. typedef ::boost::mpl::vector<common<>, incrementable<> > src_concept;
  238. typedef ::boost::mpl::vector<common<_a> > dst_concept;
  239. typedef ::boost::mpl::map<boost::mpl::pair<_a, _self> > map;
  240. typedef ::boost::mpl::map<boost::mpl::pair<_a, int> > types;
  241. binding<dst_concept> table(make_binding<types>());
  242. any<src_concept> x(4);
  243. const int* ip = any_cast<const int*>(&x);
  244. any<dst_concept, const _a&> y(x, make_binding<map>());
  245. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), ip);
  246. any<dst_concept, const _a&> z(x, table);
  247. BOOST_CHECK_EQUAL(any_cast<const int*>(&z), ip);
  248. any<dst_concept, const _a&> cy(as_const(x), make_binding<map>());
  249. BOOST_CHECK_EQUAL(any_cast<const int*>(&cy), ip);
  250. any<dst_concept, const _a&> cz(as_const(x), table);
  251. BOOST_CHECK_EQUAL(any_cast<const int*>(&cz), ip);
  252. }