adapt_adt.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*=============================================================================
  2. Copyright (c) 2001-2009 Joel de Guzman
  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/type_traits/is_same.hpp>
  31. #include <iostream>
  32. #include <string>
  33. namespace ns
  34. {
  35. class point
  36. {
  37. public:
  38. point() : x(0), y(0), z(0) {}
  39. point(int in_x, int in_y, int in_z) : x(in_x), y(in_y), z(in_z) {}
  40. int get_x() const { return x; }
  41. int get_y() const { return y; }
  42. int get_z() const { return z; }
  43. void set_x(int x_) { x = x_; }
  44. void set_y(int y_) { y = y_; }
  45. void set_z(int z_) { z = z_; }
  46. private:
  47. int x;
  48. int y;
  49. int z;
  50. };
  51. #if !BOOST_WORKAROUND(__GNUC__,<4)
  52. class point_with_private_members
  53. {
  54. friend struct boost::fusion::extension::access;
  55. public:
  56. point_with_private_members() : x(0), y(0), z(0) {}
  57. point_with_private_members(int in_x, int in_y, int in_z)
  58. : x(in_x), y(in_y), z(in_z) {}
  59. int get_x() const { return x; }
  60. int get_y() const { return y; }
  61. int get_z() const { return z; }
  62. void set_x(int x_) { x = x_; }
  63. void set_y(int y_) { y = y_; }
  64. void set_z(int z_) { z = z_; }
  65. private:
  66. int x;
  67. int y;
  68. int z;
  69. };
  70. #endif
  71. // A sequence that has data members defined in an unrelated namespace
  72. // (std, in this case). This allows testing ADL issues.
  73. class name
  74. {
  75. public:
  76. name() {}
  77. name(const std::string& last, const std::string& first)
  78. : last(last), first(first) {}
  79. const std::string& get_last() const { return last; }
  80. const std::string& get_first() const { return first; }
  81. void set_last(const std::string& last_) { last = last_; }
  82. void set_first(const std::string& first_) { first = first_; }
  83. private:
  84. std::string last;
  85. std::string first;
  86. };
  87. }
  88. #if BOOST_PP_VARIADICS
  89. BOOST_FUSION_ADAPT_ADT(
  90. ns::point,
  91. (int, int, obj.get_x(), obj.set_x(val))
  92. // Mixing auto & BOOST_FUSION_ADAPT_AUTO to test backward compatibility
  93. (auto, BOOST_FUSION_ADAPT_AUTO, obj.get_y(), obj.set_y(val))
  94. (obj.get_z(), obj.set_z(val))
  95. )
  96. # if !BOOST_WORKAROUND(__GNUC__,<4)
  97. BOOST_FUSION_ADAPT_ADT(
  98. ns::point_with_private_members,
  99. (obj.get_x(), obj.set_x(val))
  100. (obj.get_y(), obj.set_y(val))
  101. (obj.get_z(), obj.set_z(val))
  102. )
  103. # endif
  104. BOOST_FUSION_ADAPT_ADT(
  105. ns::name,
  106. (obj.get_last(), obj.set_last(val))
  107. (obj.get_first(), obj.set_first(val))
  108. )
  109. #else // BOOST_PP_VARIADICS
  110. BOOST_FUSION_ADAPT_ADT(
  111. ns::point,
  112. (int, int, obj.get_x(), obj.set_x(val))
  113. (auto, auto, obj.get_y(), obj.set_y(val))
  114. (auto, auto, obj.get_z(), obj.set_z(val))
  115. )
  116. # if !BOOST_WORKAROUND(__GNUC__,<4)
  117. BOOST_FUSION_ADAPT_ADT(
  118. ns::point_with_private_members,
  119. (auto, auto, obj.get_x(), obj.set_x(val))
  120. (auto, auto, obj.get_y(), obj.set_y(val))
  121. (auto, auto, obj.get_z(), obj.set_z(val))
  122. )
  123. # endif
  124. BOOST_FUSION_ADAPT_ADT(
  125. ns::name,
  126. (const std::string&, const std::string&, obj.get_last(), obj.set_last(val))
  127. (BOOST_FUSION_ADAPT_AUTO, auto, obj.get_first(), obj.set_first(val))
  128. )
  129. #endif
  130. class empty_adt{};
  131. BOOST_FUSION_ADAPT_ADT(empty_adt,)
  132. int
  133. main()
  134. {
  135. using namespace boost::fusion;
  136. using namespace boost;
  137. std::cout << tuple_open('[');
  138. std::cout << tuple_close(']');
  139. std::cout << tuple_delimiter(", ");
  140. {
  141. BOOST_MPL_ASSERT_NOT((traits::is_view<ns::point>));
  142. BOOST_STATIC_ASSERT(!traits::is_view<ns::point>::value);
  143. ns::point p(123, 456, 789);
  144. std::cout << at_c<0>(p) << std::endl;
  145. std::cout << at_c<1>(p) << std::endl;
  146. std::cout << at_c<2>(p) << std::endl;
  147. std::cout << p << std::endl;
  148. BOOST_TEST(p == make_vector(123, 456, 789));
  149. at_c<0>(p) = 6;
  150. at_c<1>(p) = 9;
  151. at_c<2>(p) = 12;
  152. BOOST_TEST(p == make_vector(6, 9, 12));
  153. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<ns::point>::value == 3);
  154. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<ns::point>::value);
  155. BOOST_TEST(front(p) == 6);
  156. BOOST_TEST(back(p) == 12);
  157. }
  158. {
  159. fusion::vector<int, float, int> v1(4, 2.f, 2);
  160. ns::point v2(5, 3, 3);
  161. fusion::vector<long, double, int> v3(5, 4., 4);
  162. BOOST_TEST(v1 < v2);
  163. BOOST_TEST(v1 <= v2);
  164. BOOST_TEST(v2 > v1);
  165. BOOST_TEST(v2 >= v1);
  166. BOOST_TEST(v2 < v3);
  167. BOOST_TEST(v2 <= v3);
  168. BOOST_TEST(v3 > v2);
  169. BOOST_TEST(v3 >= v2);
  170. }
  171. {
  172. fusion::vector<std::string, std::string> v1("Lincoln", "Abraham");
  173. ns::name v2("Roosevelt", "Franklin");
  174. ns::name v3("Roosevelt", "Theodore");
  175. BOOST_TEST(v1 < v2);
  176. BOOST_TEST(v1 <= v2);
  177. BOOST_TEST(v2 > v1);
  178. BOOST_TEST(v2 >= v1);
  179. BOOST_TEST(v2 < v3);
  180. BOOST_TEST(v2 <= v3);
  181. BOOST_TEST(v3 > v2);
  182. BOOST_TEST(v3 >= v2);
  183. }
  184. {
  185. // conversion from ns::point to vector
  186. ns::point p(5, 3, 3);
  187. fusion::vector<int, long, int> v(p);
  188. v = p;
  189. }
  190. {
  191. // conversion from ns::point to list
  192. ns::point p(5, 3, 3);
  193. fusion::list<int, long, int> l(p);
  194. l = p;
  195. }
  196. {
  197. BOOST_MPL_ASSERT((mpl::is_sequence<ns::point>));
  198. BOOST_MPL_ASSERT((boost::is_same<
  199. boost::fusion::result_of::value_at_c<ns::point,0>::type
  200. , mpl::front<ns::point>::type>));
  201. }
  202. #if !BOOST_WORKAROUND(__GNUC__,<4)
  203. {
  204. BOOST_MPL_ASSERT_NOT((traits::is_view<ns::point_with_private_members>));
  205. BOOST_STATIC_ASSERT(!traits::is_view<ns::point_with_private_members>::value);
  206. ns::point_with_private_members p(123, 456, 789);
  207. std::cout << at_c<0>(p) << std::endl;
  208. std::cout << at_c<1>(p) << std::endl;
  209. std::cout << at_c<2>(p) << std::endl;
  210. std::cout << p << std::endl;
  211. BOOST_TEST(p == make_vector(123, 456, 789));
  212. at_c<0>(p) = 6;
  213. at_c<1>(p) = 9;
  214. at_c<2>(p) = 12;
  215. BOOST_TEST(p == make_vector(6, 9, 12));
  216. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<ns::point_with_private_members>::value == 3);
  217. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<ns::point_with_private_members>::value);
  218. BOOST_TEST(front(p) == 6);
  219. BOOST_TEST(back(p) == 12);
  220. }
  221. #endif
  222. {
  223. // Check types provided in case it's provided
  224. BOOST_MPL_ASSERT((
  225. boost::is_same<
  226. boost::fusion::result_of::front<ns::point>::type,
  227. boost::fusion::extension::adt_attribute_proxy<ns::point,0,false>
  228. >));
  229. BOOST_MPL_ASSERT((
  230. boost::is_same<
  231. boost::fusion::result_of::front<ns::point>::type::type,
  232. int
  233. >));
  234. BOOST_MPL_ASSERT((
  235. boost::is_same<
  236. boost::fusion::result_of::front<ns::point const>::type,
  237. boost::fusion::extension::adt_attribute_proxy<ns::point,0,true>
  238. >));
  239. BOOST_MPL_ASSERT((
  240. boost::is_same<
  241. boost::fusion::result_of::front<ns::point const>::type::type,
  242. int
  243. >));
  244. // Check types provided in case it's deduced
  245. BOOST_MPL_ASSERT((
  246. boost::is_same<
  247. boost::fusion::result_of::back<ns::point>::type,
  248. boost::fusion::extension::adt_attribute_proxy<ns::point,2,false>
  249. >));
  250. BOOST_MPL_ASSERT((
  251. boost::is_same<
  252. boost::fusion::result_of::back<ns::point>::type::type,
  253. int
  254. >));
  255. BOOST_MPL_ASSERT((
  256. boost::is_same<
  257. boost::fusion::result_of::back<ns::point const>::type,
  258. boost::fusion::extension::adt_attribute_proxy<ns::point,2,true>
  259. >));
  260. BOOST_MPL_ASSERT((
  261. boost::is_same<
  262. boost::fusion::result_of::back<ns::point const>::type::type,
  263. int
  264. >));
  265. }
  266. return boost::report_errors();
  267. }