transform_view.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/fusion/container/vector/vector.hpp>
  8. #include <boost/fusion/adapted/mpl.hpp>
  9. #include <boost/fusion/sequence/io/out.hpp>
  10. #include <boost/fusion/container/generation/make_vector.hpp>
  11. #include <boost/fusion/sequence/comparison/equal_to.hpp>
  12. #include <boost/fusion/view/transform_view/transform_view.hpp>
  13. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  14. #include <boost/fusion/sequence/intrinsic/at.hpp>
  15. #include <boost/fusion/sequence/intrinsic/value_at.hpp>
  16. #include <boost/fusion/iterator/next.hpp>
  17. #include <boost/fusion/iterator/prior.hpp>
  18. #include <boost/fusion/iterator/advance.hpp>
  19. #include <boost/fusion/iterator/deref.hpp>
  20. #include <boost/fusion/iterator/distance.hpp>
  21. #include <boost/mpl/range_c.hpp>
  22. #include <boost/mpl/assert.hpp>
  23. #include <boost/type_traits/is_same.hpp>
  24. struct square
  25. {
  26. template<typename T>
  27. struct result;
  28. template <typename T>
  29. struct result<square(T)>
  30. {
  31. typedef int type;
  32. };
  33. template <typename T>
  34. int operator()(T x) const
  35. {
  36. return x * x;
  37. }
  38. };
  39. struct add
  40. {
  41. template<typename T>
  42. struct result;
  43. template <typename A, typename B>
  44. struct result<add(A,B)>
  45. {
  46. typedef int type;
  47. };
  48. template <typename A, typename B>
  49. int operator()(A a, B b) const
  50. {
  51. return a + b;
  52. }
  53. };
  54. int
  55. main()
  56. {
  57. using namespace boost::fusion;
  58. std::cout << tuple_open('[');
  59. std::cout << tuple_close(']');
  60. std::cout << tuple_delimiter(", ");
  61. /// Testing the transform_view
  62. {
  63. typedef boost::mpl::range_c<int, 5, 9> sequence_type;
  64. sequence_type sequence;
  65. square sq;
  66. typedef transform_view<sequence_type, square> xform_type;
  67. xform_type xform(sequence, sq);
  68. std::cout << xform << std::endl;
  69. BOOST_TEST((xform == make_vector(25, 36, 49, 64)));
  70. typedef boost::fusion::result_of::begin<xform_type>::type first_type;
  71. first_type first_it(boost::fusion::begin(xform));
  72. typedef boost::fusion::result_of::next<first_type>::type next_type;
  73. next_type next_it(boost::fusion::next(first_it));
  74. BOOST_TEST((*next_it == 36));
  75. BOOST_TEST((*boost::fusion::prior(next_it) == 25));
  76. BOOST_TEST((boost::fusion::distance(first_it, next_it) == 1));
  77. BOOST_TEST((*boost::fusion::advance_c<3>(boost::fusion::begin(xform)) == 64));
  78. BOOST_TEST((boost::fusion::at_c<2>(xform) == 49));
  79. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_c<xform_type, 0>::type, int>));
  80. }
  81. {
  82. typedef boost::mpl::range_c<int, 5, 9> sequence1_type;
  83. typedef boost::mpl::range_c<int, 10, 14> sequence2_type;
  84. sequence1_type sequence1;
  85. sequence2_type sequence2;
  86. add f;
  87. typedef transform_view<sequence1_type, sequence2_type, add> xform_type;
  88. xform_type xform(sequence1, sequence2, f);
  89. std::cout << xform << std::endl;
  90. BOOST_TEST((xform == make_vector(15, 17, 19, 21)));
  91. BOOST_TEST((boost::fusion::at_c<2>(xform) == 19));
  92. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_c<xform_type, 0>::type, int>));
  93. }
  94. return boost::report_errors();
  95. }