fused_function_object.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_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. template <class Base = boost::mpl::empty_base>
  16. struct test_func
  17. : Base
  18. {
  19. template<typename T>
  20. struct result;
  21. template<class Self, typename T0, typename T1>
  22. struct result< Self(T0, T1) >
  23. {
  24. typedef int type;
  25. };
  26. template <typename T0, typename T1>
  27. int operator()(T0 const & x, T1 const & y) const
  28. {
  29. return 1+x-y;
  30. }
  31. template <typename T0, typename T1>
  32. int operator()(T0 const & x, T1 const & y)
  33. {
  34. return 2+x-y;
  35. }
  36. template <typename T0, typename T1>
  37. int operator()(T0 & x, T1 & y) const
  38. {
  39. return 3+x-y;
  40. }
  41. template <typename T0, typename T1>
  42. int operator()(T0 & x, T1 & y)
  43. {
  44. return 4+x-y;
  45. }
  46. };
  47. int main()
  48. {
  49. test_func<noncopyable> f;
  50. fusion::fused_function_object< test_func<> > fused_func;
  51. fusion::fused_function_object< test_func<noncopyable> & > fused_func_ref(f);
  52. fusion::fused_function_object< test_func<> const > fused_func_c;
  53. fusion::fused_function_object< test_func<> > const fused_func_c2;
  54. fusion::fused_function_object< test_func<noncopyable> const & > fused_func_c_ref(f);
  55. fusion::vector<int,char> lv_vec(1,'\004');
  56. BOOST_TEST(fused_func(lv_vec) == 1);
  57. BOOST_TEST(fused_func_c(lv_vec) == 0);
  58. BOOST_TEST(fused_func_c2(lv_vec) == 0);
  59. BOOST_TEST(fused_func_ref(lv_vec) == 1);
  60. BOOST_TEST(fused_func_c_ref(lv_vec) == 0);
  61. BOOST_TEST(fused_func(fusion::make_vector(2,'\003')) == 1);
  62. BOOST_TEST(fused_func_c(fusion::make_vector(2,'\003')) == 0);
  63. BOOST_TEST(fused_func_c2(fusion::make_vector(2,'\003')) == 0);
  64. BOOST_TEST(fused_func_ref(fusion::make_vector(2,'\003')) == 1);
  65. BOOST_TEST(fused_func_c_ref(fusion::make_vector(2,'\003')) == 0);
  66. return boost::report_errors();
  67. }