transform.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2007 Dan Marsden
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/fusion/container/vector/vector.hpp>
  9. #include <boost/fusion/adapted/mpl.hpp>
  10. #include <boost/fusion/sequence/io/out.hpp>
  11. #include <boost/fusion/container/generation/make_vector.hpp>
  12. #include <boost/fusion/sequence/comparison/equal_to.hpp>
  13. #include <boost/fusion/algorithm/transformation/transform.hpp>
  14. #include <boost/type_traits/is_class.hpp>
  15. #include <boost/type_traits/is_same.hpp>
  16. #include <boost/mpl/range_c.hpp>
  17. #include <boost/type_traits/is_reference.hpp>
  18. struct square
  19. {
  20. template<typename Sig>
  21. struct result;
  22. template <typename T>
  23. struct result<square(T)>
  24. {
  25. typedef int type;
  26. };
  27. template <typename T>
  28. int operator()(T x) const
  29. {
  30. return x * x;
  31. }
  32. };
  33. struct add
  34. {
  35. template<typename Sig>
  36. struct result;
  37. template <typename A, typename B>
  38. struct result<add(A, B)>
  39. {
  40. typedef int type;
  41. };
  42. template <typename A, typename B>
  43. int operator()(A a, B b) const
  44. {
  45. return a + b;
  46. }
  47. };
  48. struct unary_lvalue_transform
  49. {
  50. template<typename Sig>
  51. struct result;
  52. template<typename T>
  53. struct result<unary_lvalue_transform(T&)>
  54. {
  55. typedef T* type;
  56. };
  57. template<typename T>
  58. T* operator()(T& t) const
  59. {
  60. return &t;
  61. }
  62. };
  63. int twice(int v)
  64. {
  65. return v*2;
  66. }
  67. struct binary_lvalue_transform
  68. {
  69. template<typename Sig>
  70. struct result;
  71. template<typename T0, typename T1>
  72. struct result<binary_lvalue_transform(T0&,T1&)>
  73. {
  74. typedef T0* type;
  75. };
  76. template<typename T0, typename T1>
  77. T0* operator()(T0& t0, T1&) const
  78. {
  79. return &t0;
  80. }
  81. };
  82. int
  83. main()
  84. {
  85. using namespace boost::fusion;
  86. using boost::mpl::range_c;
  87. std::cout << tuple_open('[');
  88. std::cout << tuple_close(']');
  89. std::cout << tuple_delimiter(", ");
  90. /// Testing the transform
  91. {
  92. typedef range_c<int, 5, 9> sequence_type;
  93. sequence_type sequence;
  94. std::cout << boost::fusion::transform(sequence, square()) << std::endl;
  95. BOOST_TEST((boost::fusion::transform(sequence, square()) == make_vector(25, 36, 49, 64)));
  96. }
  97. {
  98. typedef range_c<int, 5, 9> mpl_list1;
  99. std::cout << boost::fusion::transform(mpl_list1(), square()) << std::endl;
  100. BOOST_TEST((boost::fusion::transform(mpl_list1(), square()) == make_vector(25, 36, 49, 64)));
  101. }
  102. {
  103. vector<int, int, int> tup(1, 2, 3);
  104. std::cout << boost::fusion::transform(tup, square()) << std::endl;
  105. BOOST_TEST((boost::fusion::transform(tup, square()) == make_vector(1, 4, 9)));
  106. }
  107. {
  108. vector<int, int, int> tup1(1, 2, 3);
  109. vector<int, int, int> tup2(4, 5, 6);
  110. std::cout << boost::fusion::transform(tup1, tup2, add()) << std::endl;
  111. BOOST_TEST((boost::fusion::transform(tup1, tup2, add()) == make_vector(5, 7, 9)));
  112. }
  113. {
  114. // Unary transform that requires lvalues, just check compilation
  115. vector<int, int, int> tup1(1, 2, 3);
  116. BOOST_TEST(at_c<0>(boost::fusion::transform(tup1, unary_lvalue_transform())) == &at_c<0>(tup1));
  117. BOOST_TEST(*begin(boost::fusion::transform(tup1, unary_lvalue_transform())) == &at_c<0>(tup1));
  118. }
  119. {
  120. vector<int, int, int> tup1(1, 2, 3);
  121. vector<int, int, int> tup2(4, 5, 6);
  122. BOOST_TEST(at_c<0>(boost::fusion::transform(tup1, tup2, binary_lvalue_transform())) == &at_c<0>(tup1));
  123. BOOST_TEST(*begin(boost::fusion::transform(tup1, tup2, binary_lvalue_transform())) == &at_c<0>(tup1));
  124. }
  125. {
  126. vector<int, int, int> tup1(1, 2, 3);
  127. BOOST_TEST(boost::fusion::transform(tup1, twice) == make_vector(2,4,6));
  128. }
  129. return boost::report_errors();
  130. }