mem_ptr.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // mem_ptr.hpp
  3. //
  4. // Copyright 2009 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/mpl/print.hpp>
  8. #include <iostream>
  9. #include <boost/shared_ptr.hpp>
  10. #include <boost/proto/proto.hpp>
  11. #include <boost/mpl/assert.hpp>
  12. #include <boost/type_traits/is_same.hpp>
  13. #include <boost/utility/result_of.hpp>
  14. #include <boost/test/unit_test.hpp>
  15. namespace proto = boost::proto;
  16. using proto::_;
  17. struct evaluator
  18. : proto::when<_, proto::_default<evaluator> >
  19. {};
  20. template<typename Ret, typename Expr>
  21. void assert_result_type(Expr &)
  22. {
  23. // check that the return type as calculated by the _default transform
  24. // is correct.
  25. BOOST_MPL_ASSERT((
  26. boost::is_same<
  27. Ret
  28. , typename boost::result_of<evaluator(Expr &)>::type
  29. >
  30. ));
  31. // check that the return type as calculated by the default_context
  32. // is correct.
  33. BOOST_MPL_ASSERT((
  34. boost::is_same<
  35. Ret
  36. , typename boost::result_of<proto::functional::eval(Expr &, proto::default_context &)>::type
  37. >
  38. ));
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. struct S
  42. {
  43. S() : x(-42) {}
  44. int x;
  45. };
  46. // like a normal terminal except with an operator() that can
  47. // accept non-const lvalues (Proto's only accepts const lvalues)
  48. template<typename T, typename Dummy = proto::is_proto_expr>
  49. struct my_terminal
  50. {
  51. typedef typename proto::terminal<T>::type my_terminal_base;
  52. BOOST_PROTO_BASIC_EXTENDS(my_terminal_base, my_terminal, proto::default_domain)
  53. template<typename A0>
  54. typename proto::result_of::make_expr<proto::tag::function, my_terminal const &, A0 &>::type const
  55. operator()(A0 &a0) const
  56. {
  57. return proto::make_expr<proto::tag::function>(boost::ref(*this), boost::ref(a0));
  58. }
  59. template<typename A0>
  60. typename proto::result_of::make_expr<proto::tag::function, my_terminal const &, A0 const &>::type const
  61. operator()(A0 const &a0) const
  62. {
  63. return proto::make_expr<proto::tag::function>(boost::ref(*this), boost::ref(a0));
  64. }
  65. };
  66. my_terminal<int S::*> test1 = {{ &S::x }};
  67. // Some tests with the default transform
  68. void test_refs_transform()
  69. {
  70. S s;
  71. BOOST_REQUIRE_EQUAL(s.x, -42);
  72. // Check that evaluating a memptr invocation with a
  73. // non-const lvalue argument yields the member as a
  74. // non-const lvalue
  75. assert_result_type<int &>(test1(s));
  76. evaluator()(test1(s)) = 0;
  77. BOOST_CHECK_EQUAL(s.x, 0);
  78. // Ditto for reference_wrappers
  79. assert_result_type<int &>(test1(boost::ref(s)));
  80. evaluator()(test1(boost::ref(s))) = 42;
  81. BOOST_CHECK_EQUAL(s.x, 42);
  82. // Check that evaluating a memptr invocation with a
  83. // const lvalue argument yields the member as a
  84. // const lvalue
  85. S const &rcs = s;
  86. assert_result_type<int const &>(test1(rcs));
  87. int const &s_x = evaluator()(test1(rcs));
  88. BOOST_CHECK_EQUAL(&s.x, &s_x);
  89. }
  90. // Some tests with the default context
  91. void test_refs_context()
  92. {
  93. proto::default_context ctx;
  94. S s;
  95. BOOST_REQUIRE_EQUAL(s.x, -42);
  96. // Check that evaluating a memptr invocation with a
  97. // non-const lvalue argument yields the member as a
  98. // non-const lvalue
  99. assert_result_type<int &>(test1(s));
  100. proto::eval(test1(s), ctx) = 0;
  101. BOOST_CHECK_EQUAL(s.x, 0);
  102. // Ditto for reference_wrappers
  103. assert_result_type<int &>(test1(boost::ref(s)));
  104. proto::eval(test1(boost::ref(s)), ctx) = 42;
  105. BOOST_CHECK_EQUAL(s.x, 42);
  106. // Check that evaluating a memptr invocation with a
  107. // const lvalue argument yields the member as a
  108. // const lvalue
  109. S const &rcs = s;
  110. assert_result_type<int const &>(test1(rcs));
  111. int const &s_x = proto::eval(test1(rcs), ctx);
  112. BOOST_CHECK_EQUAL(&s.x, &s_x);
  113. }
  114. void test_ptrs_transform()
  115. {
  116. S s;
  117. BOOST_REQUIRE_EQUAL(s.x, -42);
  118. // Check that evaluating a memptr invocation with a
  119. // pointer to a non-const argument yields the member as a
  120. // non-const lvalue
  121. assert_result_type<int &>(test1(&s));
  122. evaluator()(test1(&s)) = 0;
  123. BOOST_CHECK_EQUAL(s.x, 0);
  124. S* ps = &s;
  125. assert_result_type<int &>(test1(ps));
  126. evaluator()(test1(ps)) = 42;
  127. BOOST_CHECK_EQUAL(s.x, 42);
  128. boost::shared_ptr<S> const sp(new S);
  129. BOOST_REQUIRE_EQUAL(sp->x, -42);
  130. // Ditto for shared_ptr (which hook the get_pointer()
  131. // customization point)
  132. assert_result_type<int &>(test1(sp));
  133. evaluator()(test1(sp)) = 0;
  134. BOOST_CHECK_EQUAL(sp->x, 0);
  135. // Check that evaluating a memptr invocation with a
  136. // const lvalue argument yields the member as a
  137. // const lvalue
  138. S const &rcs = s;
  139. assert_result_type<int const &>(test1(&rcs));
  140. int const &s_x0 = evaluator()(test1(&rcs));
  141. BOOST_CHECK_EQUAL(&s.x, &s_x0);
  142. S const *pcs = &s;
  143. assert_result_type<int const &>(test1(pcs));
  144. int const &s_x1 = evaluator()(test1(pcs));
  145. BOOST_CHECK_EQUAL(&s.x, &s_x1);
  146. boost::shared_ptr<S const> spc(new S);
  147. BOOST_REQUIRE_EQUAL(spc->x, -42);
  148. assert_result_type<int const &>(test1(spc));
  149. int const &s_x2 = evaluator()(test1(spc));
  150. BOOST_CHECK_EQUAL(&spc->x, &s_x2);
  151. }
  152. void test_ptrs_context()
  153. {
  154. proto::default_context ctx;
  155. S s;
  156. BOOST_REQUIRE_EQUAL(s.x, -42);
  157. // Check that evaluating a memptr invocation with a
  158. // pointer to a non-const argument yields the member as a
  159. // non-const lvalue
  160. assert_result_type<int &>(test1(&s));
  161. proto::eval(test1(&s), ctx) = 0;
  162. BOOST_CHECK_EQUAL(s.x, 0);
  163. S* ps = &s;
  164. assert_result_type<int &>(test1(ps));
  165. proto::eval(test1(ps), ctx) = 42;
  166. BOOST_CHECK_EQUAL(s.x, 42);
  167. boost::shared_ptr<S> const sp(new S);
  168. BOOST_REQUIRE_EQUAL(sp->x, -42);
  169. // Ditto for shared_ptr (which hook the get_pointer()
  170. // customization point)
  171. assert_result_type<int &>(test1(sp));
  172. proto::eval(test1(sp), ctx) = 0;
  173. BOOST_CHECK_EQUAL(sp->x, 0);
  174. // Check that evaluating a memptr invocation with a
  175. // const lvalue argument yields the member as a
  176. // const lvalue
  177. S const &rcs = s;
  178. assert_result_type<int const &>(test1(&rcs));
  179. int const &s_x0 = proto::eval(test1(&rcs), ctx);
  180. BOOST_CHECK_EQUAL(&s.x, &s_x0);
  181. S const *pcs = &s;
  182. assert_result_type<int const &>(test1(pcs));
  183. int const &s_x1 = proto::eval(test1(pcs), ctx);
  184. BOOST_CHECK_EQUAL(&s.x, &s_x1);
  185. boost::shared_ptr<S const> spc(new S);
  186. BOOST_REQUIRE_EQUAL(spc->x, -42);
  187. assert_result_type<int const &>(test1(spc));
  188. int const &s_x2 = proto::eval(test1(spc), ctx);
  189. BOOST_CHECK_EQUAL(&spc->x, &s_x2);
  190. }
  191. ///////////////////////////////////////////////////////////////////////////////
  192. struct T
  193. {
  194. int x;
  195. };
  196. proto::terminal<int T::*>::type test2 = { &T::x };
  197. int const *get_pointer(T const &t)
  198. {
  199. return &t.x;
  200. }
  201. void with_get_pointer_transform()
  202. {
  203. T t;
  204. evaluator()(test2(t));
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////
  207. template<typename T>
  208. struct dumb_ptr
  209. {
  210. dumb_ptr(T *p_) : p(p_) {}
  211. friend T const *get_pointer(dumb_ptr const &p)
  212. {
  213. return p.p;
  214. }
  215. T *p;
  216. };
  217. struct U : dumb_ptr<U>
  218. {
  219. U() : dumb_ptr<U>(this), x(42) {}
  220. int x;
  221. };
  222. my_terminal<U *dumb_ptr<U>::*> U_p = {{&U::p}};
  223. my_terminal<int U::*> U_x = {{&U::x}};
  224. void potentially_ambiguous_transform()
  225. {
  226. U u;
  227. // This should yield a non-const reference to a pointer-to-const
  228. U *&up = evaluator()(U_p(u));
  229. BOOST_CHECK_EQUAL(&up, &u.p);
  230. // This should yield a non-const reference to a pointer-to-const
  231. int &ux = evaluator()(U_x(u));
  232. BOOST_CHECK_EQUAL(&ux, &u.x);
  233. }
  234. using namespace boost::unit_test;
  235. ///////////////////////////////////////////////////////////////////////////////
  236. // init_unit_test_suite
  237. //
  238. test_suite* init_unit_test_suite( int argc, char* argv[] )
  239. {
  240. test_suite *test = BOOST_TEST_SUITE("test handling of member pointers by the default transform and default contexts");
  241. test->add(BOOST_TEST_CASE(&test_refs_transform));
  242. test->add(BOOST_TEST_CASE(&test_refs_context));
  243. test->add(BOOST_TEST_CASE(&test_ptrs_transform));
  244. test->add(BOOST_TEST_CASE(&test_ptrs_context));
  245. test->add(BOOST_TEST_CASE(&with_get_pointer_transform));
  246. test->add(BOOST_TEST_CASE(&potentially_ambiguous_transform));
  247. return test;
  248. }