bind_member_variable_tests.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <cmath>
  8. #include <boost/noncopyable.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/bind.hpp>
  12. namespace test
  13. {
  14. struct x //: boost::noncopyable // test non-copyable (hold this by reference)
  15. {
  16. int m;
  17. };
  18. struct xx {
  19. int m;
  20. };
  21. }
  22. template <typename T, typename F>
  23. void
  24. write_test(F f)
  25. {
  26. using boost::phoenix::arg_names::arg1;
  27. using boost::phoenix::bind;
  28. T x_;
  29. bind(&T::m, f(x_))() = 122;
  30. BOOST_TEST(x_.m == 122);
  31. bind(&T::m, arg1)(f(x_)) = 123;
  32. BOOST_TEST(x_.m == 123);
  33. }
  34. template <typename T, typename F>
  35. void
  36. read_test(F f)
  37. {
  38. using boost::phoenix::arg_names::arg1;
  39. using boost::phoenix::bind;
  40. T x_;
  41. x_.m = 123;
  42. BOOST_TEST(bind(&T::m, f(x_))() == 123);
  43. BOOST_TEST(bind(&T::m, arg1)(f(x_)) == 123);
  44. }
  45. struct identity
  46. {
  47. template <typename T>
  48. T&
  49. operator()(T& t) const
  50. {
  51. return t;
  52. }
  53. };
  54. struct constify
  55. {
  56. template <typename T>
  57. T const&
  58. operator()(T const& t) const
  59. {
  60. return t;
  61. }
  62. };
  63. struct add_pointer
  64. {
  65. template <typename T>
  66. T* /*const*/
  67. operator()(T& t) const
  68. {
  69. return &t;
  70. }
  71. };
  72. struct add_const_pointer
  73. {
  74. template <typename T>
  75. const T* /*const*/
  76. operator()(T const& t) const
  77. {
  78. return &t;
  79. }
  80. };
  81. int
  82. main()
  83. {
  84. write_test<test::x>(add_pointer());
  85. write_test<test::xx>(add_pointer());
  86. read_test<test::x>(identity());
  87. read_test<test::x>(constify());
  88. read_test<test::x>(add_pointer());
  89. read_test<test::x>(add_const_pointer());
  90. read_test<test::xx>(identity());
  91. read_test<test::xx>(constify());
  92. read_test<test::xx>(add_pointer());
  93. read_test<test::xx>(add_const_pointer());
  94. return boost::report_errors();
  95. }