make_fused_function_object.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/generation/make_fused_function_object.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/noncopyable.hpp>
  10. #include <boost/mpl/empty_base.hpp>
  11. #include <boost/fusion/container/generation/make_vector.hpp>
  12. #include <boost/fusion/container/vector.hpp>
  13. namespace fusion = boost::fusion;
  14. using boost::noncopyable;
  15. using boost::cref;
  16. using boost::ref;
  17. template <class Base = boost::mpl::empty_base>
  18. struct test_func
  19. : Base
  20. {
  21. template<typename T>
  22. struct result
  23. {
  24. };
  25. template<class Self, typename T0, typename T1>
  26. struct result< Self(T0, T1) >
  27. {
  28. typedef int type;
  29. };
  30. template <typename T0, typename T1>
  31. int operator()(T0 const & x, T1 const & y) const
  32. {
  33. return 1+x-y;
  34. }
  35. template <typename T0, typename T1>
  36. int operator()(T0 const & x, T1 const & y)
  37. {
  38. return 2+x-y;
  39. }
  40. template <typename T0, typename T1>
  41. int operator()(T0 & x, T1 & y) const
  42. {
  43. return 3+x-y;
  44. }
  45. template <typename T0, typename T1>
  46. int operator()(T0 & x, T1 & y)
  47. {
  48. return 4+x-y;
  49. }
  50. };
  51. template <typename T>
  52. inline T const & const_(T const & t)
  53. {
  54. return t;
  55. }
  56. int main()
  57. {
  58. fusion::vector<int,char> lv_vec(1,'\004');
  59. test_func<> f;
  60. test_func<noncopyable> f_nc;
  61. boost::fusion::result_of::make_fused_function_object< test_func<> >::type fused_func
  62. = fusion::make_fused_function_object(f);
  63. BOOST_TEST(fused_func(lv_vec) == 1);
  64. BOOST_TEST(const_(fused_func)(lv_vec) == 0);
  65. BOOST_TEST(fusion::make_fused_function_object(const_(f))(lv_vec) == 1);
  66. BOOST_TEST(fusion::make_fused_function_object(ref(f_nc))(lv_vec) == 1);
  67. BOOST_TEST(fusion::make_fused_function_object(cref(f_nc))(lv_vec) == 0);
  68. BOOST_TEST(fused_func(fusion::make_vector(2,'\003')) == 1);
  69. BOOST_TEST(const_(fused_func)(fusion::make_vector(2,'\003')) == 0);
  70. BOOST_TEST(fusion::make_fused_function_object(const_(f))(fusion::make_vector(2,'\003')) == 1);
  71. BOOST_TEST(fusion::make_fused_function_object(ref(f_nc))(fusion::make_vector(2,'\003')) == 1);
  72. BOOST_TEST(fusion::make_fused_function_object(cref(f_nc))(fusion::make_vector(2,'\003')) == 0);
  73. return boost::report_errors();
  74. }