define_tpl_struct_inline.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*=============================================================================
  2. Copyright (c) 2010, 2012 Christopher Schmidt, nathan Ridge
  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/sequence.hpp>
  8. #include <boost/fusion/container.hpp>
  9. #include <boost/fusion/support.hpp>
  10. #include <boost/fusion/sequence/io/out.hpp>
  11. #include <boost/fusion/adapted/struct/define_struct_inline.hpp>
  12. #include <boost/preprocessor/empty.hpp>
  13. #include <boost/mpl/assert.hpp>
  14. #include <boost/static_assert.hpp>
  15. #include <iostream>
  16. #include <string>
  17. struct cls
  18. {
  19. BOOST_FUSION_DEFINE_TPL_STRUCT_INLINE(
  20. (X)(Y),
  21. point,
  22. (X, x)
  23. (Y, y)
  24. )
  25. };
  26. template <typename = int>
  27. struct tpl_cls
  28. {
  29. BOOST_FUSION_DEFINE_TPL_STRUCT_INLINE(
  30. (X)(Y),
  31. point,
  32. (X, x)
  33. (Y, y)
  34. )
  35. };
  36. namespace ns
  37. {
  38. BOOST_FUSION_DEFINE_TPL_STRUCT_INLINE((M), s, (M, m))
  39. BOOST_FUSION_DEFINE_TPL_STRUCT_INLINE((M), empty_struct, )
  40. }
  41. template <typename Point>
  42. void run_test()
  43. {
  44. using namespace boost::fusion;
  45. std::cout << tuple_open('[');
  46. std::cout << tuple_close(']');
  47. std::cout << tuple_delimiter(", ");
  48. {
  49. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<ns::empty_struct<int> >::value == 0);
  50. BOOST_STATIC_ASSERT(boost::fusion::result_of::empty<ns::empty_struct<int> >::value);
  51. }
  52. {
  53. BOOST_MPL_ASSERT_NOT((traits::is_view<Point>));
  54. BOOST_STATIC_ASSERT(!traits::is_view<Point>::value);
  55. Point p(123, 456);
  56. std::cout << at_c<0>(p) << std::endl;
  57. std::cout << at_c<1>(p) << std::endl;
  58. std::cout << p << std::endl;
  59. BOOST_TEST(p == make_vector(123, 456));
  60. at_c<0>(p) = 6;
  61. at_c<1>(p) = 9;
  62. BOOST_TEST(p == make_vector(6, 9));
  63. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<Point>::value == 2);
  64. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<Point>::value);
  65. BOOST_TEST(front(p) == 6);
  66. BOOST_TEST(back(p) == 9);
  67. }
  68. {
  69. vector<int, float> v1(4, 2.f);
  70. Point v2(5, 3);
  71. vector<long, double> v3(5, 4.);
  72. BOOST_TEST(v1 < v2);
  73. BOOST_TEST(v1 <= v2);
  74. BOOST_TEST(v2 > v1);
  75. BOOST_TEST(v2 >= v1);
  76. BOOST_TEST(v2 < v3);
  77. BOOST_TEST(v2 <= v3);
  78. BOOST_TEST(v3 > v2);
  79. BOOST_TEST(v3 >= v2);
  80. }
  81. {
  82. // conversion from Point to vector
  83. Point p(5, 3);
  84. vector<int, long> v(p);
  85. v = p;
  86. }
  87. {
  88. // conversion from Point to list
  89. Point p(5, 3);
  90. list<int, long> l(p);
  91. l = p;
  92. }
  93. { // begin/end
  94. using namespace boost::fusion;
  95. typedef boost::fusion::result_of::begin<ns::s<int> >::type b;
  96. typedef boost::fusion::result_of::end<ns::s<int> >::type e;
  97. // this fails
  98. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::next<b>::type, e>));
  99. }
  100. {
  101. Point p = make_list(5,3);
  102. BOOST_TEST(p == make_vector(5,3));
  103. p = make_list(3,5);
  104. BOOST_TEST(p == make_vector(3,5));
  105. }
  106. }
  107. int
  108. main()
  109. {
  110. run_test<cls::point<int, int> >(); // test non-template enclosing class
  111. run_test<tpl_cls<>::point<int, int> >(); // test template enclosing class
  112. return boost::report_errors();
  113. }