fused.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*=============================================================================
  2. Copyright (c) 2006-2007 Tobias Schwinger
  3. Use modification and distribution are subject to the Boost Software
  4. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ==============================================================================*/
  7. #include <boost/fusion/functional/adapter/fused.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/noncopyable.hpp>
  10. #include <boost/fusion/container/generation/make_vector.hpp>
  11. #include <boost/fusion/container/vector.hpp>
  12. #include <boost/type_traits/is_same.hpp>
  13. #include <boost/mpl/assert.hpp>
  14. #include <boost/mpl/empty_base.hpp>
  15. namespace fusion = boost::fusion;
  16. using boost::noncopyable;
  17. template <class Base = boost::mpl::empty_base>
  18. struct test_func
  19. : Base
  20. {
  21. typedef int result_type;
  22. template <typename T0, typename T1>
  23. int operator()(T0 const & x, T1 const & y) const
  24. {
  25. return 1+x-y;
  26. }
  27. template <typename T0, typename T1>
  28. int operator()(T0 const & x, T1 const & y)
  29. {
  30. return 2+x-y;
  31. }
  32. template <typename T0, typename T1>
  33. int operator()(T0 & x, T1 & y) const
  34. {
  35. return 3+x-y;
  36. }
  37. template <typename T0, typename T1>
  38. int operator()(T0 & x, T1 & y)
  39. {
  40. return 4+x-y;
  41. }
  42. };
  43. int main()
  44. {
  45. test_func<noncopyable> f;
  46. typedef fusion::fused< test_func<> > ff;
  47. ff fused_func;
  48. typedef fusion::fused< test_func<noncopyable> & > ffr;
  49. ffr fused_func_ref(f);
  50. typedef fusion::fused< test_func<> const > ffc;
  51. ffc fused_func_c;
  52. typedef fusion::fused< test_func<> > const ffc2;
  53. ffc2 fused_func_c2;
  54. typedef fusion::fused< test_func<noncopyable> const & > ffcr;
  55. ffcr fused_func_c_ref(f);
  56. typedef fusion::vector<int,char> vec;
  57. vec lv_vec(1,'\004');
  58. BOOST_MPL_ASSERT((boost::is_same<boost::result_of<ff(vec)>::type, int>));
  59. BOOST_TEST(fused_func(lv_vec) == 1);
  60. BOOST_MPL_ASSERT((boost::is_same<boost::result_of<ffr(vec)>::type, int>));
  61. BOOST_TEST(fused_func_c(lv_vec) == 0);
  62. BOOST_MPL_ASSERT((boost::is_same<boost::result_of<ffc(vec)>::type, int>));
  63. BOOST_TEST(fused_func_c2(lv_vec) == 0);
  64. BOOST_TEST(fused_func_ref(lv_vec) == 1);
  65. BOOST_MPL_ASSERT((boost::is_same<boost::result_of<ffcr(vec)>::type, int>));
  66. BOOST_TEST(fused_func_c_ref(lv_vec) == 0);
  67. BOOST_TEST(fused_func(fusion::make_vector(2,'\003')) == 1);
  68. BOOST_TEST(fused_func_c(fusion::make_vector(2,'\003')) == 0);
  69. BOOST_TEST(fused_func_c2(fusion::make_vector(2,'\003')) == 0);
  70. BOOST_TEST(fused_func_ref(fusion::make_vector(2,'\003')) == 1);
  71. BOOST_TEST(fused_func_c_ref(fusion::make_vector(2,'\003')) == 0);
  72. return boost::report_errors();
  73. }