define_struct_inline.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_STRUCT_INLINE(
  20. point,
  21. (int, x)
  22. (int, y)
  23. )
  24. };
  25. template <typename = int>
  26. struct tpl_cls
  27. {
  28. BOOST_FUSION_DEFINE_STRUCT_INLINE(
  29. point,
  30. (int, x)
  31. (int, y)
  32. )
  33. };
  34. namespace ns
  35. {
  36. BOOST_FUSION_DEFINE_STRUCT_INLINE(s, (int, m))
  37. BOOST_FUSION_DEFINE_STRUCT_INLINE(empty_struct, )
  38. // Testing non-constexpr compatible types
  39. BOOST_FUSION_DEFINE_STRUCT_INLINE(
  40. employee,
  41. (std::string, name)
  42. (std::string, nickname)
  43. )
  44. }
  45. template <typename Point>
  46. void run_test()
  47. {
  48. using namespace boost::fusion;
  49. std::cout << tuple_open('[');
  50. std::cout << tuple_close(']');
  51. std::cout << tuple_delimiter(", ");
  52. {
  53. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<ns::empty_struct>::value == 0);
  54. BOOST_STATIC_ASSERT(boost::fusion::result_of::empty<ns::empty_struct>::value);
  55. }
  56. {
  57. BOOST_MPL_ASSERT_NOT((traits::is_view<Point>));
  58. BOOST_STATIC_ASSERT(!traits::is_view<Point>::value);
  59. Point p(123, 456);
  60. std::cout << at_c<0>(p) << std::endl;
  61. std::cout << at_c<1>(p) << std::endl;
  62. std::cout << p << std::endl;
  63. BOOST_TEST(p == make_vector(123, 456));
  64. at_c<0>(p) = 6;
  65. at_c<1>(p) = 9;
  66. BOOST_TEST(p == make_vector(6, 9));
  67. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<Point>::value == 2);
  68. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<Point>::value);
  69. BOOST_TEST(front(p) == 6);
  70. BOOST_TEST(back(p) == 9);
  71. }
  72. {
  73. vector<int, float> v1(4, 2.0f);
  74. Point v2(5, 3);
  75. vector<long, double> v3(5, 4.);
  76. BOOST_TEST(v1 < v2);
  77. BOOST_TEST(v1 <= v2);
  78. BOOST_TEST(v2 > v1);
  79. BOOST_TEST(v2 >= v1);
  80. BOOST_TEST(v2 < v3);
  81. BOOST_TEST(v2 <= v3);
  82. BOOST_TEST(v3 > v2);
  83. BOOST_TEST(v3 >= v2);
  84. }
  85. {
  86. // conversion from Point to vector
  87. Point p(5, 3);
  88. vector<int, long> v(p);
  89. v = p;
  90. }
  91. {
  92. // conversion from Point to list
  93. Point p(5, 3);
  94. list<int, long> l(p);
  95. l = p;
  96. }
  97. { // begin/end
  98. using namespace boost::fusion;
  99. typedef boost::fusion::result_of::begin<ns::s>::type b;
  100. typedef boost::fusion::result_of::end<ns::s>::type e;
  101. // this fails
  102. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::next<b>::type, e>));
  103. }
  104. {
  105. Point p = make_list(5,3);
  106. BOOST_TEST(p == make_vector(5,3));
  107. p = make_list(3,5);
  108. BOOST_TEST(p == make_vector(3,5));
  109. }
  110. }
  111. int
  112. main()
  113. {
  114. run_test<cls::point>(); // test with non-template enclosing class
  115. run_test<tpl_cls<>::point>(); // test with template enclosing class
  116. {
  117. using namespace boost::fusion;
  118. ns::employee emp = make_list("John Doe", "jdoe");
  119. std::cout << at_c<0>(emp) << std::endl;
  120. std::cout << at_c<1>(emp) << std::endl;
  121. BOOST_TEST(emp == make_vector("John Doe", "jdoe"));
  122. }
  123. return boost::report_errors();
  124. }