adapt_assoc_tpl_adt.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/support.hpp>
  9. #include <boost/fusion/container/list.hpp>
  10. #include <boost/fusion/container/vector.hpp>
  11. #include <boost/fusion/container/generation/make_vector.hpp>
  12. #include <boost/fusion/adapted/adt/adapt_assoc_adt.hpp>
  13. #include <boost/mpl/assert.hpp>
  14. #include <boost/mpl/not.hpp>
  15. #include <boost/mpl/front.hpp>
  16. #include <boost/mpl/is_sequence.hpp>
  17. #include <boost/type_traits/is_same.hpp>
  18. #include <boost/static_assert.hpp>
  19. #include <iostream>
  20. #include <string>
  21. namespace ns
  22. {
  23. struct x_member;
  24. struct y_member;
  25. struct z_member;
  26. struct non_member;
  27. template<typename X, typename Y, typename Z>
  28. class point
  29. {
  30. public:
  31. point() : x(0), y(0), z(0) {}
  32. point(X in_x, Y in_y, Z in_z) : x(in_x), y(in_y), z(in_z) {}
  33. X get_x() const { return x; }
  34. Y get_y() const { return y; }
  35. Z get_z() const { return z; }
  36. void set_x(X x_) { x = x_; }
  37. void set_y(Y y_) { y = y_; }
  38. void set_z(Z z_) { z = z_; }
  39. private:
  40. X x;
  41. Y y;
  42. Z z;
  43. };
  44. }
  45. #if BOOST_PP_VARIADICS
  46. BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
  47. (X)(Y)(Z),
  48. (ns::point)(X)(Y)(Z),
  49. (X, X, obj.get_x(), obj.set_x(val), ns::x_member)
  50. (Y, Y, obj.get_y(), obj.set_y(val), ns::y_member)
  51. (obj.get_z(), obj.set_z(val), ns::z_member)
  52. )
  53. #else // BOOST_PP_VARIADICS
  54. BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
  55. (X)(Y)(Z),
  56. (ns::point)(X)(Y)(Z),
  57. (X, X, obj.get_x(), obj.set_x(val), ns::x_member)
  58. (Y, Y, obj.get_y(), obj.set_y(val), ns::y_member)
  59. (auto, auto, obj.get_z(), obj.set_z(val), ns::z_member)
  60. )
  61. #endif
  62. template <typename TypeToConstruct>
  63. class empty_adt_templated_factory {
  64. TypeToConstruct operator()() {
  65. return TypeToConstruct();
  66. }
  67. };
  68. BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
  69. (TypeToConstruct),
  70. (empty_adt_templated_factory)(TypeToConstruct),
  71. )
  72. int
  73. main()
  74. {
  75. using namespace boost::fusion;
  76. typedef ns::point<int,int,long> point;
  77. std::cout << tuple_open('[');
  78. std::cout << tuple_close(']');
  79. std::cout << tuple_delimiter(", ");
  80. {
  81. BOOST_MPL_ASSERT_NOT((traits::is_view<point>));
  82. BOOST_STATIC_ASSERT(!traits::is_view<point>::value);
  83. point p(123, 456, 789);
  84. std::cout << at_c<0>(p) << std::endl;
  85. std::cout << at_c<1>(p) << std::endl;
  86. std::cout << at_c<2>(p) << std::endl;
  87. std::cout << p << std::endl;
  88. BOOST_TEST(p == make_vector(123, 456, 789));
  89. at_c<0>(p) = 6;
  90. at_c<1>(p) = 9;
  91. at_c<2>(p) = 12;
  92. BOOST_TEST(p == make_vector(6, 9, 12));
  93. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<point>::value == 3);
  94. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<point>::value);
  95. BOOST_TEST(front(p) == 6);
  96. BOOST_TEST(back(p) == 12);
  97. }
  98. {
  99. boost::fusion::vector<int, float, long> v1(4, 2.f, 2);
  100. point v2(5, 3, 3);
  101. boost::fusion::vector<long, double, long> v3(5, 4., 4);
  102. BOOST_TEST(v1 < v2);
  103. BOOST_TEST(v1 <= v2);
  104. BOOST_TEST(v2 > v1);
  105. BOOST_TEST(v2 >= v1);
  106. BOOST_TEST(v2 < v3);
  107. BOOST_TEST(v2 <= v3);
  108. BOOST_TEST(v3 > v2);
  109. BOOST_TEST(v3 >= v2);
  110. }
  111. {
  112. // conversion from point to vector
  113. point p(5, 3, 3);
  114. boost::fusion::vector<int, long, int> v(p);
  115. v = p;
  116. }
  117. {
  118. // conversion from point to list
  119. point p(5, 3, 3);
  120. boost::fusion::list<int, long, int> l(p);
  121. l = p;
  122. }
  123. {
  124. BOOST_MPL_ASSERT((boost::mpl::is_sequence<point>));
  125. BOOST_MPL_ASSERT((boost::is_same<
  126. boost::fusion::result_of::value_at_c<point,0>::type
  127. , boost::mpl::front<point>::type>));
  128. }
  129. {
  130. // assoc stuff
  131. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<point, ns::x_member>));
  132. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<point, ns::y_member>));
  133. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<point, ns::z_member>));
  134. BOOST_MPL_ASSERT((boost::mpl::not_<boost::fusion::result_of::has_key<point, ns::non_member> >));
  135. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<point, ns::x_member>::type, int>));
  136. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<point, ns::y_member>::type, int>));
  137. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<point, ns::z_member>::type, long>));
  138. point p(5, 3, 1);
  139. BOOST_TEST(at_key<ns::x_member>(p) == 5);
  140. BOOST_TEST(at_key<ns::y_member>(p) == 3);
  141. BOOST_TEST(at_key<ns::z_member>(p) == 1);
  142. }
  143. return boost::report_errors();
  144. }