test_free.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2012 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/free.hpp>
  13. #include <boost/mpl/vector.hpp>
  14. #define BOOST_TEST_MAIN
  15. #include <boost/test/unit_test.hpp>
  16. using namespace boost::type_erasure;
  17. struct model {
  18. explicit model(int v) : val(v) {}
  19. int val;
  20. };
  21. int f1(model& m) { return m.val; }
  22. int f1(model& m, int i) { return m.val + i; }
  23. BOOST_TYPE_ERASURE_FREE((global_has_f1_1), f1, 1)
  24. BOOST_AUTO_TEST_CASE(test_global_has_f1_1) {
  25. typedef ::boost::mpl::vector<
  26. global_has_f1_1<int(_self&)>,
  27. copy_constructible<> > concept_type;
  28. model m(10);
  29. any<concept_type> x(m);
  30. BOOST_CHECK_EQUAL(f1(x), 10);
  31. }
  32. BOOST_TYPE_ERASURE_FREE((ns1)(ns2)(ns_has_f1_1), f1, 1)
  33. BOOST_AUTO_TEST_CASE(test_ns_has_f1_1) {
  34. typedef ::boost::mpl::vector<
  35. ns1::ns2::ns_has_f1_1<int(_self&)>,
  36. copy_constructible<> > concept_type;
  37. model m(10);
  38. any<concept_type> x(m);
  39. BOOST_CHECK_EQUAL(f1(x), 10);
  40. }
  41. struct model_const {
  42. explicit model_const(int v) : val(v) {}
  43. int val;
  44. };
  45. int f1(const model_const& m) { return m.val; }
  46. int f1(const model_const& m, int i) { return m.val + i; }
  47. BOOST_AUTO_TEST_CASE(test_global_has_f1_1_const) {
  48. typedef ::boost::mpl::vector<
  49. ns1::ns2::ns_has_f1_1<int(const _self&)>,
  50. copy_constructible<> > concept_type;
  51. model_const m(10);
  52. const any<concept_type> x(m);
  53. BOOST_CHECK_EQUAL(f1(x), 10);
  54. }
  55. BOOST_AUTO_TEST_CASE(test_global_has_f1_1_void) {
  56. typedef ::boost::mpl::vector<
  57. global_has_f1_1<void(_self&)>,
  58. copy_constructible<> > concept_type;
  59. model m(10);
  60. any<concept_type> x(m);
  61. f1(x);
  62. }
  63. BOOST_TYPE_ERASURE_FREE((global_has_f1_2), f1, 2)
  64. BOOST_AUTO_TEST_CASE(test_global_has_f1_overload) {
  65. typedef ::boost::mpl::vector<
  66. global_has_f1_1<int(_self&)>,
  67. global_has_f1_2<int(_self&, int)>,
  68. copy_constructible<> > concept_type;
  69. model m(10);
  70. any<concept_type> x(m);
  71. BOOST_CHECK_EQUAL(f1(x), 10);
  72. BOOST_CHECK_EQUAL(f1(x, 5), 15);
  73. }
  74. BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const) {
  75. typedef ::boost::mpl::vector<
  76. global_has_f1_1<int(const _self&)>,
  77. global_has_f1_2<int(const _self&, int)>,
  78. copy_constructible<> > concept_type;
  79. model_const m(10);
  80. const any<concept_type> x(m);
  81. BOOST_CHECK_EQUAL(f1(x), 10);
  82. BOOST_CHECK_EQUAL(f1(x, 5), 15);
  83. }
  84. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  85. BOOST_AUTO_TEST_CASE(test_global_has_f1_rv) {
  86. typedef ::boost::mpl::vector<
  87. global_has_f1_2<int(_self&&, int&&)>,
  88. copy_constructible<> > concept_type;
  89. model_const m(10);
  90. any<concept_type> x(m);
  91. BOOST_CHECK_EQUAL(f1(std::move(x), 5), 15);
  92. }
  93. #endif
  94. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && \
  95. !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
  96. !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  97. !defined(BOOST_NO_CXX11_DECLTYPE) && \
  98. !BOOST_WORKAROUND(BOOST_MSVC, == 1800)
  99. namespace ns3 {
  100. BOOST_TYPE_ERASURE_FREE(f1)
  101. }
  102. BOOST_AUTO_TEST_CASE(test_simple_free_1) {
  103. typedef ::boost::mpl::vector<
  104. ns3::has_f1<int(_self&)>,
  105. copy_constructible<> > concept_type;
  106. model m(10);
  107. any<concept_type> x(m);
  108. BOOST_CHECK_EQUAL(f1(x), 10);
  109. }
  110. namespace ns3 {
  111. BOOST_TYPE_ERASURE_FREE(has_f1_ex, f1)
  112. }
  113. BOOST_AUTO_TEST_CASE(test_named_free_1) {
  114. typedef ::boost::mpl::vector<
  115. ns3::has_f1_ex<int(_self&)>,
  116. copy_constructible<> > concept_type;
  117. model m(10);
  118. any<concept_type> x(m);
  119. BOOST_CHECK_EQUAL(f1(x), 10);
  120. }
  121. #endif