adapt_tpl_adt.cpp 5.1 KB

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