adt_attribute_proxy.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/core/lightweight_test.hpp>
  7. #include <boost/fusion/adapted/adt/adapt_assoc_adt.hpp>
  8. #include <boost/fusion/sequence.hpp>
  9. #include <boost/mpl/assert.hpp>
  10. #include <boost/type_traits/is_same.hpp>
  11. #include <iostream>
  12. #include <string>
  13. namespace fusion=boost::fusion;
  14. template<typename Name, typename Age>
  15. struct employee
  16. {
  17. private:
  18. Name name;
  19. Age age;
  20. public:
  21. template<typename OtherName>
  22. void
  23. set_name(OtherName const& n)
  24. {
  25. name=n;
  26. }
  27. template<typename OtherAge>
  28. void
  29. set_age(OtherAge const& a)
  30. {
  31. age=a;
  32. }
  33. Name& get_name()
  34. {
  35. return name;
  36. }
  37. Name const& get_name()const
  38. {
  39. return name;
  40. }
  41. Age& get_age()
  42. {
  43. return age;
  44. }
  45. Age const& get_age()const
  46. {
  47. return age;
  48. }
  49. };
  50. namespace keys
  51. {
  52. struct name;
  53. struct age;
  54. }
  55. BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
  56. (Name)(Age),
  57. (employee) (Name)(Age),
  58. (Name&, Name const&, obj.get_name(), obj.template set_name<Val>(val), keys::name)
  59. (Age&, Age const&, obj.get_age(), obj.template set_age<Val>(val), keys::age))
  60. int main()
  61. {
  62. typedef employee<std::string, int> et;
  63. typedef et const etc;
  64. et e;
  65. etc& ec=e;
  66. fusion::at_key<keys::name>(e)="marshall mathers";
  67. fusion::at_key<keys::age>(e)=37;
  68. BOOST_MPL_ASSERT((
  69. boost::is_same<
  70. boost::fusion::result_of::value_at_key<et, keys::name>::type,
  71. std::string
  72. >));
  73. BOOST_MPL_ASSERT((
  74. boost::is_same<
  75. boost::fusion::result_of::value_at_key<et, keys::name>::type,
  76. boost::fusion::result_of::value_at_c<et, 0>::type
  77. >));
  78. BOOST_MPL_ASSERT((
  79. boost::is_same<
  80. boost::fusion::result_of::value_at_key<et, keys::age>::type,
  81. int
  82. >));
  83. BOOST_MPL_ASSERT((
  84. boost::is_same<
  85. boost::fusion::result_of::value_at_key<et, keys::age>::type,
  86. boost::fusion::result_of::value_at_c<et, 1>::type
  87. >));
  88. BOOST_MPL_ASSERT((
  89. boost::is_same<
  90. boost::fusion::result_of::at_key<et, keys::name>::type,
  91. fusion::extension::adt_attribute_proxy<et, 0, false>
  92. >));
  93. BOOST_MPL_ASSERT((
  94. boost::is_same<
  95. boost::fusion::result_of::at_key<et, keys::age>::type,
  96. fusion::extension::adt_attribute_proxy<et, 1, false>
  97. >));
  98. BOOST_MPL_ASSERT((
  99. boost::is_same<
  100. boost::fusion::result_of::at_key<et, keys::name>::type,
  101. boost::fusion::result_of::front<et>::type
  102. >));
  103. BOOST_MPL_ASSERT((
  104. boost::is_same<
  105. boost::fusion::result_of::at_key<et, keys::age>::type,
  106. boost::fusion::result_of::back<et>::type
  107. >));
  108. BOOST_MPL_ASSERT((
  109. boost::is_same<
  110. boost::fusion::result_of::at_key<etc, keys::name>::type,
  111. fusion::extension::adt_attribute_proxy<et, 0, true>
  112. >));
  113. BOOST_MPL_ASSERT((
  114. boost::is_same<
  115. boost::fusion::result_of::at_key<etc, keys::age>::type,
  116. fusion::extension::adt_attribute_proxy<et, 1, true>
  117. >));
  118. BOOST_MPL_ASSERT((
  119. boost::is_same<
  120. boost::fusion::result_of::at_key<etc, keys::name>::type,
  121. boost::fusion::result_of::front<etc>::type
  122. >));
  123. BOOST_MPL_ASSERT((
  124. boost::is_same<
  125. boost::fusion::result_of::at_key<etc, keys::age>::type,
  126. boost::fusion::result_of::back<etc>::type
  127. >));
  128. BOOST_MPL_ASSERT((
  129. boost::is_same<
  130. fusion::extension::adt_attribute_proxy<et, 0, false>::type,
  131. std::string&
  132. >));
  133. BOOST_MPL_ASSERT((
  134. boost::is_same<
  135. fusion::extension::adt_attribute_proxy<et, 0, true>::type,
  136. std::string const&
  137. >));
  138. BOOST_MPL_ASSERT((
  139. boost::is_same<
  140. fusion::extension::adt_attribute_proxy<et, 1, false>::type,
  141. int&
  142. >));
  143. BOOST_MPL_ASSERT((
  144. boost::is_same<
  145. fusion::extension::adt_attribute_proxy<et, 1, true>::type,
  146. int const&
  147. >));
  148. {
  149. std::string& name=fusion::at_key<keys::name>(e);
  150. int& age=fusion::at_key<keys::age>(e);
  151. BOOST_TEST(name=="marshall mathers");
  152. BOOST_TEST(age==37);
  153. BOOST_TEST(fusion::at_key<keys::name>(e).get()=="marshall mathers");
  154. BOOST_TEST(fusion::at_key<keys::age>(e).get()==37);
  155. BOOST_TEST(fusion::front(e).get()=="marshall mathers");
  156. BOOST_TEST(fusion::back(e).get()==37);
  157. }
  158. {
  159. std::string const& name=fusion::at_key<keys::name>(ec);
  160. int const& age=fusion::at_key<keys::age>(ec);
  161. BOOST_TEST(name=="marshall mathers");
  162. BOOST_TEST(age==37);
  163. BOOST_TEST(fusion::at_key<keys::name>(ec).get()=="marshall mathers");
  164. BOOST_TEST(fusion::at_key<keys::age>(ec).get()==37);
  165. BOOST_TEST(fusion::front(ec).get()=="marshall mathers");
  166. BOOST_TEST(fusion::back(ec).get()==37);
  167. }
  168. return boost::report_errors();
  169. }