adapt_struct.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 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/struct/adapt_struct.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 namespaced_type {
  34. typedef int integer;
  35. }
  36. namespace ns
  37. {
  38. struct point
  39. {
  40. int x;
  41. int y;
  42. namespaced_type::integer z;
  43. };
  44. #if !BOOST_WORKAROUND(__GNUC__,<4)
  45. struct point_with_private_attributes
  46. {
  47. friend struct boost::fusion::extension::access;
  48. private:
  49. int x;
  50. int y;
  51. int z;
  52. public:
  53. point_with_private_attributes(int x, int y, int z):x(x),y(y),z(z)
  54. {}
  55. };
  56. #endif
  57. struct foo
  58. {
  59. int x;
  60. };
  61. struct bar
  62. {
  63. foo foo_;
  64. int y;
  65. };
  66. // Testing non-constexpr compatible types
  67. struct employee {
  68. std::string name;
  69. std::string nickname;
  70. employee(std::string name, std::string nickname)
  71. : name(name), nickname(nickname)
  72. {}
  73. };
  74. }
  75. #if BOOST_PP_VARIADICS
  76. BOOST_FUSION_ADAPT_STRUCT(
  77. ns::point,
  78. x,
  79. y,
  80. z
  81. )
  82. # if !BOOST_WORKAROUND(__GNUC__,<4)
  83. BOOST_FUSION_ADAPT_STRUCT(
  84. ns::point_with_private_attributes,
  85. x,
  86. y,
  87. z
  88. )
  89. # endif
  90. struct s { int m; };
  91. BOOST_FUSION_ADAPT_STRUCT(s, m)
  92. BOOST_FUSION_ADAPT_STRUCT(
  93. ns::bar,
  94. foo_.x, // test that adapted members can actually be expressions
  95. (auto , y)
  96. )
  97. BOOST_FUSION_ADAPT_STRUCT(
  98. ns::employee,
  99. name,
  100. nickname
  101. )
  102. #else // BOOST_PP_VARIADICS
  103. BOOST_FUSION_ADAPT_STRUCT(
  104. ns::point,
  105. (int, x)
  106. (auto, y)
  107. (namespaced_type::integer, z)
  108. )
  109. # if !BOOST_WORKAROUND(__GNUC__,<4)
  110. BOOST_FUSION_ADAPT_STRUCT(
  111. ns::point_with_private_attributes,
  112. (int, x)
  113. (int, y)
  114. (auto, z)
  115. )
  116. # endif
  117. struct s { int m; };
  118. BOOST_FUSION_ADAPT_STRUCT(s, (auto, m))
  119. BOOST_FUSION_ADAPT_STRUCT(
  120. ns::bar,
  121. (auto, foo_.x) // test that adapted members can actually be expressions
  122. (BOOST_FUSION_ADAPT_AUTO, y) // Mixing auto & BOOST_FUSION_ADAPT_AUTO
  123. // to test backward compatibility
  124. )
  125. BOOST_FUSION_ADAPT_STRUCT(
  126. ns::employee,
  127. (std::string, name)
  128. (BOOST_FUSION_ADAPT_AUTO, nickname)
  129. )
  130. #endif
  131. struct empty_struct {};
  132. BOOST_FUSION_ADAPT_STRUCT(empty_struct,)
  133. int
  134. main()
  135. {
  136. using namespace boost::fusion;
  137. using namespace boost;
  138. using ns::point;
  139. std::cout << tuple_open('[');
  140. std::cout << tuple_close(']');
  141. std::cout << tuple_delimiter(", ");
  142. {
  143. BOOST_MPL_ASSERT_NOT((traits::is_view<point>));
  144. BOOST_STATIC_ASSERT(!traits::is_view<point>::value);
  145. point p = {123, 456, 789};
  146. std::cout << at_c<0>(p) << std::endl;
  147. std::cout << at_c<1>(p) << std::endl;
  148. std::cout << at_c<2>(p) << std::endl;
  149. std::cout << p << std::endl;
  150. BOOST_TEST(p == make_vector(123, 456, 789));
  151. at_c<0>(p) = 6;
  152. at_c<1>(p) = 9;
  153. at_c<2>(p) = 12;
  154. BOOST_TEST(p == make_vector(6, 9, 12));
  155. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<point>::value == 3);
  156. BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<point>::value);
  157. BOOST_TEST(front(p) == 6);
  158. BOOST_TEST(back(p) == 12);
  159. }
  160. {
  161. vector<int, float, int> v1(4, 2.f, 2);
  162. point v2 = {5, 3, 3};
  163. vector<long, double, int> v3(5, 4., 4);
  164. BOOST_TEST(v1 < v2);
  165. BOOST_TEST(v1 <= v2);
  166. BOOST_TEST(v2 > v1);
  167. BOOST_TEST(v2 >= v1);
  168. BOOST_TEST(v2 < v3);
  169. BOOST_TEST(v2 <= v3);
  170. BOOST_TEST(v3 > v2);
  171. BOOST_TEST(v3 >= v2);
  172. }
  173. {
  174. // conversion from point to vector
  175. point p = {5, 3, 3};
  176. vector<int, long, int> v(p);
  177. v = p;
  178. }
  179. {
  180. // conversion from point to list
  181. point p = {5, 3, 3};
  182. list<int, long, int> l(p);
  183. l = p;
  184. }
  185. { // begin/end
  186. using namespace boost::fusion;
  187. using boost::is_same;
  188. typedef boost::fusion::result_of::begin<s>::type b;
  189. typedef boost::fusion::result_of::end<s>::type e;
  190. // this fails
  191. BOOST_MPL_ASSERT((is_same<boost::fusion::result_of::next<b>::type, e>));
  192. }
  193. {
  194. BOOST_MPL_ASSERT((mpl::is_sequence<ns::point>));
  195. BOOST_MPL_ASSERT((boost::is_same<
  196. boost::fusion::result_of::value_at_c<ns::point,0>::type
  197. , mpl::front<ns::point>::type>));
  198. }
  199. #if !BOOST_WORKAROUND(__GNUC__,<4)
  200. {
  201. ns::point_with_private_attributes p(123, 456, 789);
  202. std::cout << at_c<0>(p) << std::endl;
  203. std::cout << at_c<1>(p) << std::endl;
  204. std::cout << at_c<2>(p) << std::endl;
  205. std::cout << p << std::endl;
  206. BOOST_TEST(p == make_vector(123, 456, 789));
  207. }
  208. #endif
  209. {
  210. fusion::vector<int, float> v1(4, 2.f);
  211. ns::bar v2 = {{5}, 3};
  212. BOOST_TEST(v1 < v2);
  213. BOOST_TEST(v1 <= v2);
  214. BOOST_TEST(v2 > v1);
  215. BOOST_TEST(v2 >= v1);
  216. }
  217. {
  218. ns::employee emp("John Doe", "jdoe");
  219. std::cout << at_c<0>(emp) << std::endl;
  220. std::cout << at_c<1>(emp) << std::endl;
  221. fusion::vector<std::string, std::string> v1("John Doe", "jdoe");
  222. BOOST_TEST(emp == v1);
  223. }
  224. return boost::report_errors();
  225. }