make_fused_procedure.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_procedure.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. int effect;
  18. #define CHECK_EFFECT(t,e) \
  19. { \
  20. effect = 1234567; t; \
  21. BOOST_TEST(effect == e); \
  22. }
  23. template <class Base = boost::mpl::empty_base>
  24. struct test_func
  25. : Base
  26. {
  27. template <typename T0, typename T1>
  28. int operator()(T0 const & x, T1 const & y) const
  29. {
  30. return effect = 1+x-y;
  31. }
  32. template <typename T0, typename T1>
  33. int operator()(T0 const & x, T1 const & y)
  34. {
  35. return effect = 2+x-y;
  36. }
  37. template <typename T0, typename T1>
  38. int operator()(T0 & x, T1 & y) const
  39. {
  40. return effect = 3+x-y;
  41. }
  42. template <typename T0, typename T1>
  43. int operator()(T0 & x, T1 & y)
  44. {
  45. return effect = 4+x-y;
  46. }
  47. };
  48. template <typename T>
  49. inline T const & const_(T const & t)
  50. {
  51. return t;
  52. }
  53. int main()
  54. {
  55. fusion::vector<int,char> lv_vec(1,'\004');
  56. test_func<> f;
  57. test_func<noncopyable> f_nc;
  58. boost::fusion::result_of::make_fused_procedure< test_func<> >::type fused_func
  59. = fusion::make_fused_procedure(f);
  60. CHECK_EFFECT(fused_func(lv_vec), 1);
  61. CHECK_EFFECT(const_(fused_func)(lv_vec), 0);
  62. CHECK_EFFECT(fusion::make_fused_procedure(const_(f))(lv_vec), 1);
  63. CHECK_EFFECT(fusion::make_fused_procedure(ref(f_nc))(lv_vec), 1);
  64. CHECK_EFFECT(fusion::make_fused_procedure(cref(f_nc))(lv_vec), 0);
  65. CHECK_EFFECT(fused_func(fusion::make_vector(2,'\003')), 1);
  66. CHECK_EFFECT(const_(fused_func)(fusion::make_vector(2,'\003')), 0);
  67. CHECK_EFFECT(fusion::make_fused_procedure(const_(f))(fusion::make_vector(2,'\003')), 1);
  68. CHECK_EFFECT(fusion::make_fused_procedure(ref(f_nc))(fusion::make_vector(2,'\003')), 1);
  69. CHECK_EFFECT(fusion::make_fused_procedure(cref(f_nc))(fusion::make_vector(2,'\003')), 0);
  70. return boost::report_errors();
  71. }