traits.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*=============================================================================
  2. Copyright (C) 2016 Lee Clagett
  3. Copyright (C) 2018 Kohei Takahashi
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/config.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/fusion/container/list.hpp>
  10. #include <boost/fusion/container/vector.hpp>
  11. #include <boost/type_traits/add_const.hpp>
  12. #include <boost/type_traits/add_reference.hpp>
  13. #include <boost/type_traits/integral_constant.hpp>
  14. #include <boost/type_traits/is_constructible.hpp>
  15. #include <boost/type_traits/is_convertible.hpp>
  16. #include <boost/type_traits/remove_const.hpp>
  17. #include <boost/type_traits/remove_reference.hpp>
  18. struct convertible
  19. {
  20. convertible(int) {}
  21. };
  22. template <typename From, typename To>
  23. bool is_convertible(bool has_conversion)
  24. {
  25. typedef typename boost::remove_reference<
  26. typename boost::remove_const<From>::type
  27. >::type from_rvalue;
  28. typedef typename boost::add_reference<from_rvalue>::type from_lvalue;
  29. typedef typename boost::add_const<from_lvalue>::type from_const_lvalue;
  30. return
  31. boost::is_convertible<from_rvalue, To>::value == has_conversion &&
  32. boost::is_convertible<from_lvalue, To>::value == has_conversion &&
  33. boost::is_convertible<from_const_lvalue, To>::value == has_conversion;
  34. }
  35. // is_constructible has a few requirements
  36. #ifdef BOOST_TT_IS_CONSTRUCTIBLE_CONFORMING
  37. #define FUSION_TEST_HAS_CONSTRUCTIBLE
  38. template <typename To, typename... Args>
  39. bool is_lvalue_constructible(bool has_constructor)
  40. {
  41. return has_constructor ==
  42. boost::is_constructible<
  43. To
  44. , typename boost::add_reference<Args>::type...
  45. >::value;
  46. }
  47. template <typename To, typename... Args>
  48. bool is_constructible_impl(bool has_constructor)
  49. {
  50. return
  51. boost::is_constructible<To, Args...>::value == has_constructor &&
  52. is_lvalue_constructible<To, Args...>(has_constructor) &&
  53. is_lvalue_constructible<
  54. To, typename boost::add_const<Args>::type...
  55. >(has_constructor);
  56. }
  57. template <typename To, typename... Args>
  58. bool is_constructible(bool has_constructor)
  59. {
  60. return
  61. is_constructible_impl<
  62. To
  63. , typename boost::remove_reference<
  64. typename boost::remove_const<Args>::type
  65. >::type...
  66. >(has_constructor);
  67. }
  68. void test_constructible()
  69. {
  70. BOOST_TEST((is_constructible< FUSION_SEQUENCE<> >(true)));
  71. BOOST_TEST((is_constructible< FUSION_SEQUENCE<int> >(true)));
  72. BOOST_TEST((is_constructible<FUSION_SEQUENCE<int>, int>(true)));
  73. BOOST_TEST((is_constructible<FUSION_SEQUENCE<convertible>, int>(true)));
  74. BOOST_TEST((
  75. is_constructible<FUSION_SEQUENCE<convertible>, convertible>(true)
  76. ));
  77. // boost::is_constructible always fail to test ctor which takes 2 or more arguments on GCC 4.7.
  78. #if !BOOST_WORKAROUND(BOOST_GCC, < 40700)
  79. BOOST_TEST((
  80. is_constructible<FUSION_SEQUENCE<int, int>, int, int>(true)
  81. ));
  82. BOOST_TEST((
  83. is_constructible<FUSION_SEQUENCE<convertible, int>, int, int>(true)
  84. ));
  85. BOOST_TEST((
  86. is_constructible<
  87. FUSION_SEQUENCE<convertible, int>, convertible, int
  88. >(true)
  89. ));
  90. BOOST_TEST((
  91. is_constructible<FUSION_SEQUENCE<int, convertible>, int, int>(true)
  92. ));
  93. BOOST_TEST((
  94. is_constructible<
  95. FUSION_SEQUENCE<int, convertible>, int, convertible
  96. >(true)
  97. ));
  98. BOOST_TEST((
  99. is_constructible<
  100. FUSION_SEQUENCE<convertible, convertible>, int, int
  101. >(true)
  102. ));
  103. BOOST_TEST((
  104. is_constructible<
  105. FUSION_SEQUENCE<convertible, convertible>, convertible, int
  106. >(true)
  107. ));
  108. BOOST_TEST((
  109. is_constructible<
  110. FUSION_SEQUENCE<convertible, convertible>, int, convertible
  111. >(true)
  112. ));
  113. BOOST_TEST((
  114. is_constructible<
  115. FUSION_SEQUENCE<convertible, convertible>, convertible, convertible
  116. >(true)
  117. ));
  118. #endif // !(gcc < 4.7)
  119. }
  120. #endif // is_constructible is available
  121. void test_convertible(bool has_seq_conversion)
  122. {
  123. BOOST_TEST((is_convertible<int, FUSION_SEQUENCE<> >(false)));
  124. BOOST_TEST((is_convertible<int, FUSION_SEQUENCE<int> >(false)));
  125. BOOST_TEST((is_convertible<int, FUSION_SEQUENCE<const int&> >(false)));
  126. BOOST_TEST((is_convertible<int, FUSION_SEQUENCE<convertible> >(false)));
  127. BOOST_TEST((
  128. is_convertible<int, FUSION_SEQUENCE<const convertible&> >(false)
  129. ));
  130. BOOST_TEST((is_convertible<int, FUSION_SEQUENCE<int, int> >(false)));
  131. BOOST_TEST((
  132. is_convertible<int, FUSION_SEQUENCE<const int&, const int&> >(false)
  133. ));
  134. BOOST_TEST((is_convertible<int, FUSION_SEQUENCE<convertible, int> >(false)));
  135. BOOST_TEST((
  136. is_convertible<int, FUSION_SEQUENCE<const convertible&, const int&> >(false)
  137. ));
  138. BOOST_TEST((is_convertible<int, FUSION_SEQUENCE<int, convertible> >(false)));
  139. BOOST_TEST((
  140. is_convertible<int, FUSION_SEQUENCE<const int&, const convertible&> >(false)
  141. ));
  142. BOOST_TEST((
  143. is_convertible<int, FUSION_SEQUENCE<convertible, convertible> >(false)
  144. ));
  145. BOOST_TEST((
  146. is_convertible<
  147. int, FUSION_SEQUENCE<const convertible&, const convertible&>
  148. >(false)
  149. ));
  150. BOOST_TEST((is_convertible<FUSION_SEQUENCE<>, FUSION_SEQUENCE<> >(true)));
  151. BOOST_TEST((
  152. is_convertible<FUSION_SEQUENCE<int>, FUSION_SEQUENCE<int> >(true)
  153. ));
  154. BOOST_TEST((
  155. is_convertible<FUSION_SEQUENCE<int>, FUSION_SEQUENCE<const int&> >(true)
  156. ));
  157. BOOST_TEST((
  158. is_convertible<FUSION_SEQUENCE<int>, FUSION_SEQUENCE<convertible> >(true)
  159. ));
  160. BOOST_TEST((
  161. is_convertible<FUSION_SEQUENCE<int, int>, FUSION_SEQUENCE<int, int> >(true)
  162. ));
  163. BOOST_TEST((
  164. is_convertible<
  165. FUSION_SEQUENCE<int, int>, FUSION_SEQUENCE<const int&, const int&>
  166. >(true)
  167. ));
  168. BOOST_TEST((
  169. is_convertible<
  170. FUSION_SEQUENCE<int, int>, FUSION_SEQUENCE<convertible, int>
  171. >(true)
  172. ));
  173. BOOST_TEST((
  174. is_convertible<
  175. FUSION_SEQUENCE<int, int>, FUSION_SEQUENCE<int, convertible>
  176. >(true)
  177. ));
  178. BOOST_TEST((
  179. is_convertible<
  180. FUSION_SEQUENCE<int, int>
  181. , FUSION_SEQUENCE<convertible, convertible>
  182. >(true)
  183. ));
  184. BOOST_TEST((
  185. is_convertible<
  186. FUSION_ALT_SEQUENCE<>, FUSION_SEQUENCE<>
  187. >(has_seq_conversion)
  188. ));
  189. BOOST_TEST((
  190. is_convertible<
  191. FUSION_ALT_SEQUENCE<int>, FUSION_SEQUENCE<int>
  192. >(has_seq_conversion)
  193. ));
  194. BOOST_TEST((
  195. is_convertible<
  196. FUSION_ALT_SEQUENCE<int>, FUSION_SEQUENCE<const int&>
  197. >(has_seq_conversion)
  198. ));
  199. BOOST_TEST((
  200. is_convertible<
  201. FUSION_ALT_SEQUENCE<int>, FUSION_SEQUENCE<convertible>
  202. >(has_seq_conversion)
  203. ));
  204. BOOST_TEST((
  205. is_convertible<
  206. FUSION_ALT_SEQUENCE<int, int>, FUSION_SEQUENCE<int, int>
  207. >(has_seq_conversion)
  208. ));
  209. BOOST_TEST((
  210. is_convertible<
  211. FUSION_ALT_SEQUENCE<int, int>
  212. , FUSION_SEQUENCE<const int&, const int&>
  213. >(has_seq_conversion)
  214. ));
  215. BOOST_TEST((
  216. is_convertible<
  217. FUSION_ALT_SEQUENCE<int, int>, FUSION_SEQUENCE<convertible, int>
  218. >(has_seq_conversion)
  219. ));
  220. BOOST_TEST((
  221. is_convertible<
  222. FUSION_ALT_SEQUENCE<int, int>, FUSION_SEQUENCE<int, convertible>
  223. >(has_seq_conversion)
  224. ));
  225. BOOST_TEST((
  226. is_convertible<
  227. FUSION_ALT_SEQUENCE<int, int>
  228. , FUSION_SEQUENCE<convertible, convertible>
  229. >(has_seq_conversion)
  230. ));
  231. }