define_struct.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*=============================================================================
  2. Copyright (c) 2010 Christopher Schmidt
  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.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. BOOST_FUSION_DEFINE_STRUCT(
  18. (ns),
  19. point,
  20. (int, x)
  21. (int, y)
  22. )
  23. // Tutorial (compile test only)
  24. BOOST_FUSION_DEFINE_STRUCT(
  25. (demo), employee,
  26. (std::string, name)
  27. (int, age)
  28. )
  29. BOOST_FUSION_DEFINE_STRUCT(BOOST_PP_EMPTY(), s, (int, m))
  30. BOOST_FUSION_DEFINE_STRUCT(BOOST_PP_EMPTY(), empty_struct, )
  31. // Testing non-constexpr compatible types
  32. BOOST_FUSION_DEFINE_STRUCT(
  33. (ns),
  34. employee,
  35. (std::string, name)
  36. (std::string, nickname)
  37. )
  38. int
  39. main()
  40. {
  41. using namespace boost::fusion;
  42. std::cout << tuple_open('[');
  43. std::cout << tuple_close(']');
  44. std::cout << tuple_delimiter(", ");
  45. {
  46. BOOST_MPL_ASSERT_NOT((traits::is_view<ns::point>));
  47. BOOST_STATIC_ASSERT(!traits::is_view<ns::point>::value);
  48. ns::point p(123, 456);
  49. std::cout << at_c<0>(p) << std::endl;
  50. std::cout << at_c<1>(p) << std::endl;
  51. std::cout << p << std::endl;
  52. BOOST_TEST(p == make_vector(123, 456));
  53. at_c<0>(p) = 6;
  54. at_c<1>(p) = 9;
  55. BOOST_TEST(p == make_vector(6, 9));
  56. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<ns::point>::value == 2);
  57. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<ns::point>::value);
  58. BOOST_TEST(front(p) == 6);
  59. BOOST_TEST(back(p) == 9);
  60. }
  61. {
  62. vector<int, float> v1(4, 2.f);
  63. ns::point v2(5, 3);
  64. vector<long, double> v3(5, 4.0);
  65. BOOST_TEST(v1 < v2);
  66. BOOST_TEST(v1 <= v2);
  67. BOOST_TEST(v2 > v1);
  68. BOOST_TEST(v2 >= v1);
  69. BOOST_TEST(v2 < v3);
  70. BOOST_TEST(v2 <= v3);
  71. BOOST_TEST(v3 > v2);
  72. BOOST_TEST(v3 >= v2);
  73. }
  74. {
  75. // conversion from ns::point to vector
  76. ns::point p(5, 3);
  77. vector<int, long> v(p);
  78. v = p;
  79. }
  80. {
  81. // conversion from ns::point to list
  82. ns::point p(5, 3);
  83. list<int, long> l(p);
  84. l = p;
  85. }
  86. { // begin/end
  87. using namespace boost::fusion;
  88. typedef boost::fusion::result_of::begin<s>::type b;
  89. typedef boost::fusion::result_of::end<s>::type e;
  90. // this fails
  91. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::next<b>::type, e>));
  92. }
  93. {
  94. ns::point p = make_list(5,3);
  95. BOOST_TEST(p == make_vector(5,3));
  96. p = make_list(3,5);
  97. BOOST_TEST(p == make_vector(3,5));
  98. }
  99. {
  100. ns::employee emp = make_list("John Doe", "jdoe");
  101. std::cout << at_c<0>(emp) << std::endl;
  102. std::cout << at_c<1>(emp) << std::endl;
  103. BOOST_TEST(emp == make_vector("John Doe", "jdoe"));
  104. }
  105. return boost::report_errors();
  106. }