define_assoc_tpl_struct.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_assoc_struct.hpp>
  12. #include <boost/mpl/assert.hpp>
  13. #include <boost/static_assert.hpp>
  14. #include <iostream>
  15. #include <string>
  16. namespace ns
  17. {
  18. struct x_member;
  19. struct y_member;
  20. struct z_member;
  21. }
  22. BOOST_FUSION_DEFINE_ASSOC_TPL_STRUCT(
  23. (X)(Y),
  24. (ns),
  25. point,
  26. (int, x, ns::x_member)
  27. (int, y, ns::y_member)
  28. )
  29. BOOST_FUSION_DEFINE_ASSOC_TPL_STRUCT((M), BOOST_PP_EMPTY(), empty_struct, )
  30. int
  31. main()
  32. {
  33. using namespace boost::fusion;
  34. typedef ns::point<int,int> point;
  35. std::cout << tuple_open('[');
  36. std::cout << tuple_close(']');
  37. std::cout << tuple_delimiter(", ");
  38. {
  39. BOOST_MPL_ASSERT_NOT((traits::is_view<point>));
  40. BOOST_STATIC_ASSERT(!traits::is_view<point>::value);
  41. point p(123, 456);
  42. std::cout << at_c<0>(p) << std::endl;
  43. std::cout << at_c<1>(p) << std::endl;
  44. std::cout << p << std::endl;
  45. BOOST_TEST(p == make_vector(123, 456));
  46. at_c<0>(p) = 6;
  47. at_c<1>(p) = 9;
  48. BOOST_TEST(p == make_vector(6, 9));
  49. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<point>::value == 2);
  50. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<point>::value);
  51. BOOST_TEST(front(p) == 6);
  52. BOOST_TEST(back(p) == 9);
  53. }
  54. {
  55. vector<int, float> v1(4, 2.f);
  56. point v2(5, 3);
  57. vector<long, double> v3(5, 4.0);
  58. BOOST_TEST(v1 < v2);
  59. BOOST_TEST(v1 <= v2);
  60. BOOST_TEST(v2 > v1);
  61. BOOST_TEST(v2 >= v1);
  62. BOOST_TEST(v2 < v3);
  63. BOOST_TEST(v2 <= v3);
  64. BOOST_TEST(v3 > v2);
  65. BOOST_TEST(v3 >= v2);
  66. }
  67. {
  68. // conversion from point to vector
  69. point p(5, 3);
  70. vector<int, long> v(p);
  71. v = p;
  72. }
  73. {
  74. // conversion from point to list
  75. point p(5, 3);
  76. list<int, long> l(p);
  77. l = p;
  78. }
  79. {
  80. // assoc stuff
  81. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<point, ns::x_member>));
  82. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<point, ns::y_member>));
  83. BOOST_MPL_ASSERT((boost::mpl::not_<boost::fusion::result_of::has_key<point, ns::z_member> >));
  84. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<point, ns::x_member>::type, int>));
  85. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<point, ns::y_member>::type, int>));
  86. point p(5, 3);
  87. BOOST_TEST(at_key<ns::x_member>(p) == 5);
  88. BOOST_TEST(at_key<ns::y_member>(p) == 3);
  89. }
  90. {
  91. point p = make_list(5,3);
  92. BOOST_TEST(p == make_vector(5,3));
  93. p = make_list(3,5);
  94. BOOST_TEST(p == make_vector(3,5));
  95. }
  96. return boost::report_errors();
  97. }