define_tpl_struct.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_TPL_STRUCT(
  18. (X)(Y),
  19. (ns),
  20. point,
  21. (X, x)
  22. (Y, y)
  23. )
  24. BOOST_FUSION_DEFINE_TPL_STRUCT((M), BOOST_PP_EMPTY(), s, (M, m))
  25. BOOST_FUSION_DEFINE_TPL_STRUCT((M), BOOST_PP_EMPTY(), empty_struct, )
  26. int
  27. main()
  28. {
  29. using namespace boost::fusion;
  30. typedef ns::point<int, int> point;
  31. std::cout << tuple_open('[');
  32. std::cout << tuple_close(']');
  33. std::cout << tuple_delimiter(", ");
  34. {
  35. BOOST_MPL_ASSERT_NOT((traits::is_view<point>));
  36. BOOST_STATIC_ASSERT(!traits::is_view<point>::value);
  37. point p(123, 456);
  38. std::cout << at_c<0>(p) << std::endl;
  39. std::cout << at_c<1>(p) << std::endl;
  40. std::cout << p << std::endl;
  41. BOOST_TEST(p == make_vector(123, 456));
  42. at_c<0>(p) = 6;
  43. at_c<1>(p) = 9;
  44. BOOST_TEST(p == make_vector(6, 9));
  45. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<point>::value == 2);
  46. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<point>::value);
  47. BOOST_TEST(front(p) == 6);
  48. BOOST_TEST(back(p) == 9);
  49. }
  50. {
  51. vector<int, float> v1(4, 2.f);
  52. point v2(5, 3);
  53. vector<long, double> v3(5, 4.);
  54. BOOST_TEST(v1 < v2);
  55. BOOST_TEST(v1 <= v2);
  56. BOOST_TEST(v2 > v1);
  57. BOOST_TEST(v2 >= v1);
  58. BOOST_TEST(v2 < v3);
  59. BOOST_TEST(v2 <= v3);
  60. BOOST_TEST(v3 > v2);
  61. BOOST_TEST(v3 >= v2);
  62. }
  63. {
  64. // conversion from point to vector
  65. point p(5, 3);
  66. vector<int, long> v(p);
  67. v = p;
  68. }
  69. {
  70. // conversion from point to list
  71. point p(5, 3);
  72. list<int, long> l(p);
  73. l = p;
  74. }
  75. { // begin/end
  76. using namespace boost::fusion;
  77. typedef boost::fusion::result_of::begin<s<int> >::type b;
  78. typedef boost::fusion::result_of::end<s<int> >::type e;
  79. // this fails
  80. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::next<b>::type, e>));
  81. }
  82. {
  83. point p = make_list(5,3);
  84. BOOST_TEST(p == make_vector(5,3));
  85. p = make_list(3,5);
  86. BOOST_TEST(p == make_vector(3,5));
  87. }
  88. return boost::report_errors();
  89. }