mpl.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright David Abrahams 2006.
  2. // Copyright Cromwell D. Enage 2019.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/parameter/config.hpp>
  7. #include <boost/mpl/list.hpp>
  8. #include <boost/mpl/placeholders.hpp>
  9. #include <boost/mpl/for_each.hpp>
  10. #include <boost/mpl/size.hpp>
  11. #include <boost/mpl/contains.hpp>
  12. #include <boost/mpl/assert.hpp>
  13. #include <boost/type_traits/add_pointer.hpp>
  14. #include "basics.hpp"
  15. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  16. #include <boost/mp11/list.hpp>
  17. #include <boost/mp11/map.hpp>
  18. #include <boost/mp11/algorithm.hpp>
  19. #include <boost/mp11/mpl.hpp>
  20. #endif
  21. namespace test {
  22. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  23. template <typename Map>
  24. struct assert_in_map
  25. {
  26. template <typename T>
  27. void operator()(T&&)
  28. {
  29. static_assert(
  30. boost::mp11::mp_map_contains<Map,T>::value
  31. , "T must be in Map"
  32. );
  33. }
  34. };
  35. template <typename Set>
  36. struct assert_in_set_0
  37. {
  38. template <typename T>
  39. void operator()(T&&)
  40. {
  41. static_assert(
  42. boost::mp11::mp_contains<Set,T>::value
  43. , "T must be in Set"
  44. );
  45. }
  46. };
  47. #endif
  48. template <typename Set>
  49. struct assert_in_set_1
  50. {
  51. template <typename T>
  52. void operator()(T*)
  53. {
  54. BOOST_MPL_ASSERT((boost::mpl::contains<Set,T>));
  55. }
  56. };
  57. template <typename Expected, typename Args>
  58. void f_impl(Args const& p BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Expected))
  59. {
  60. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  61. static_assert(
  62. boost::mp11::mp_size<Expected>::value == boost::mp11::mp_size<
  63. Args
  64. >::value
  65. , "mp_size<Expected>::value == mp_size<Args>::value"
  66. );
  67. boost::mp11::mp_for_each<boost::mp11::mp_map_keys<Args> >(
  68. test::assert_in_set_0<Expected>()
  69. );
  70. boost::mp11::mp_for_each<Expected>(test::assert_in_map<Args>());
  71. #endif
  72. BOOST_MPL_ASSERT_RELATION(
  73. boost::mpl::size<Expected>::value
  74. , ==
  75. , boost::mpl::size<Args>::value
  76. );
  77. boost::mpl::for_each<Args,boost::add_pointer<boost::mpl::_1> >(
  78. test::assert_in_set_1<Expected>()
  79. );
  80. boost::mpl::for_each<Expected,boost::add_pointer<boost::mpl::_1> >(
  81. test::assert_in_set_1<Args>()
  82. );
  83. }
  84. template <
  85. typename Expected
  86. , typename Tester
  87. , typename Name
  88. , typename Value
  89. , typename Index
  90. >
  91. void f(
  92. Tester const& t
  93. , Name const& name_
  94. , Value const& value_
  95. , Index const& index_
  96. BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Expected)
  97. )
  98. {
  99. test::f_impl<Expected>(
  100. test::f_parameters()(t, name_, value_, index_)
  101. );
  102. }
  103. template <
  104. typename Expected
  105. , typename Tester
  106. , typename Name
  107. , typename Value
  108. >
  109. void f(
  110. Tester const& t
  111. , Name const& name_
  112. , Value const& value_
  113. BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Expected)
  114. )
  115. {
  116. test::f_impl<Expected>(test::f_parameters()(t, name_, value_));
  117. }
  118. template <typename Expected, typename Tester, typename Name>
  119. void f(
  120. Tester const& t
  121. , Name const& name_
  122. BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Expected)
  123. )
  124. {
  125. test::f_impl<Expected>(test::f_parameters()(t, name_));
  126. }
  127. void run()
  128. {
  129. typedef test::tag::tester tester_;
  130. typedef test::tag::name name_;
  131. typedef test::tag::value value_;
  132. typedef test::tag::index index_;
  133. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  134. test::f<
  135. boost::mp11::mp_list<tester_,name_,value_,index_>
  136. >(1, 2, 3, 4);
  137. test::f<
  138. boost::mp11::mp_list<tester_,name_,index_>
  139. >(1, 2, test::_index = 3);
  140. test::f<
  141. boost::mp11::mp_list<tester_,name_,index_>
  142. >(1, test::_index = 2, test::_name = 3);
  143. test::f<
  144. boost::mp11::mp_list<name_,value_>
  145. >(test::_name = 3, test::_value = 4);
  146. test::f_impl<boost::mp11::mp_list<value_> >(test::_value = 4);
  147. #endif
  148. test::f<boost::mpl::list4<tester_,name_,value_,index_> >(1, 2, 3, 4);
  149. test::f<
  150. boost::mpl::list3<tester_,name_,index_>
  151. >(1, 2, test::_index = 3);
  152. test::f<
  153. boost::mpl::list3<tester_,name_,index_>
  154. >(1, test::_index = 2, test::_name = 3);
  155. test::f<
  156. boost::mpl::list2<name_,value_>
  157. >(test::_name = 3, test::_value = 4);
  158. test::f_impl<boost::mpl::list1<value_> >(test::_value = 4);
  159. }
  160. }
  161. #include <boost/core/lightweight_test.hpp>
  162. int main()
  163. {
  164. test::run();
  165. return boost::report_errors();
  166. }