single_view.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2011 Eric Niebler
  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/sequence/io/out.hpp>
  9. #include <boost/fusion/view/single_view/single_view.hpp>
  10. #include <boost/fusion/sequence/intrinsic/at.hpp>
  11. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  12. #include <boost/fusion/sequence/intrinsic/end.hpp>
  13. #include <boost/fusion/sequence/intrinsic/size.hpp>
  14. #include <boost/fusion/sequence/intrinsic/front.hpp>
  15. #include <boost/fusion/sequence/intrinsic/back.hpp>
  16. #include <boost/fusion/sequence/intrinsic/empty.hpp>
  17. #include <boost/fusion/sequence/intrinsic/value_at.hpp>
  18. #include <boost/fusion/iterator/next.hpp>
  19. #include <boost/fusion/iterator/prior.hpp>
  20. #include <boost/fusion/iterator/deref.hpp>
  21. #include <boost/fusion/iterator/advance.hpp>
  22. #include <boost/fusion/iterator/distance.hpp>
  23. #include <boost/mpl/int.hpp>
  24. #include <boost/mpl/assert.hpp>
  25. #include <boost/type_traits/is_same.hpp>
  26. struct X {};
  27. template <typename OS>
  28. OS& operator<<(OS& os, X const&)
  29. {
  30. os << "<X-object>";
  31. return os;
  32. }
  33. void foo() {}
  34. int
  35. main()
  36. {
  37. using namespace boost::fusion;
  38. std::cout << tuple_open('[');
  39. std::cout << tuple_close(']');
  40. std::cout << tuple_delimiter(", ");
  41. {
  42. single_view<int> view1(3);
  43. std::cout << view1 << std::endl;
  44. #ifdef FUSION_TEST_FAIL
  45. // single_view is immutable
  46. *begin(view1) += 4;
  47. #endif
  48. std::cout << view1 << std::endl;
  49. BOOST_TEST(*begin(view1) == 3);
  50. BOOST_TEST(at<boost::mpl::int_<0> >(view1) == 3);
  51. BOOST_TEST(view1.val == 3);
  52. BOOST_TEST(3 == front(view1));
  53. BOOST_TEST(3 == back(view1));
  54. BOOST_TEST(!empty(view1));
  55. BOOST_TEST(next(begin(view1)) == end(view1));
  56. BOOST_TEST(prior(end(view1)) == begin(view1));
  57. BOOST_TEST(!(next(begin(view1)) != end(view1)));
  58. BOOST_TEST(!(prior(end(view1)) != begin(view1)));
  59. BOOST_TEST(1 == distance(begin(view1), end(view1)));
  60. BOOST_TEST(0 == distance(end(view1), end(view1)));
  61. BOOST_TEST(0 == distance(begin(view1), begin(view1)));
  62. BOOST_TEST(end(view1) == advance<boost::mpl::int_<1> >(begin(view1)));
  63. BOOST_TEST(begin(view1) == advance<boost::mpl::int_<0> >(begin(view1)));
  64. BOOST_TEST(end(view1) == advance<boost::mpl::int_<0> >(end(view1)));
  65. BOOST_TEST(begin(view1) == advance<boost::mpl::int_<-1> >(end(view1)));
  66. BOOST_TEST(end(view1) == advance_c<1>(begin(view1)));
  67. BOOST_TEST(begin(view1) == advance_c<0>(begin(view1)));
  68. BOOST_TEST(end(view1) == advance_c<0>(end(view1)));
  69. BOOST_TEST(begin(view1) == advance_c<-1>(end(view1)));
  70. BOOST_TEST(1 == size(view1));
  71. BOOST_MPL_ASSERT((boost::is_same<int, boost::fusion::result_of::value_at<single_view<int>, boost::mpl::int_<0> >::type>));
  72. single_view<X> view2;
  73. std::cout << view2 << std::endl;
  74. }
  75. {
  76. std::cout << make_single_view(1) << std::endl;
  77. std::cout << make_single_view("Hello, World") << std::endl;
  78. std::cout << make_single_view(&foo) << std::endl;
  79. }
  80. return boost::report_errors();
  81. }