is_forward_iterable.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. //! @file
  8. //! Defines the is_forward_iterable collection type trait
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP
  11. #define BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP
  12. #if defined(BOOST_NO_CXX11_DECLTYPE) || \
  13. defined(BOOST_NO_CXX11_NULLPTR) || \
  14. defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES)
  15. // this feature works with VC2012 upd 5 while BOOST_NO_CXX11_TRAILING_RESULT_TYPES is defined
  16. #if !defined(BOOST_MSVC) || BOOST_MSVC_FULL_VER < 170061232 /* VC2012 upd 5 */
  17. #define BOOST_TEST_FWD_ITERABLE_CXX03
  18. #endif
  19. #endif
  20. #if defined(BOOST_TEST_FWD_ITERABLE_CXX03)
  21. // Boost
  22. #include <boost/mpl/bool.hpp>
  23. // STL
  24. #include <list>
  25. #include <vector>
  26. #include <map>
  27. #include <set>
  28. #else
  29. // Boost
  30. #include <boost/static_assert.hpp>
  31. #include <boost/utility/declval.hpp>
  32. #include <boost/type_traits/is_same.hpp>
  33. #include <boost/type_traits/remove_reference.hpp>
  34. #include <boost/type_traits/remove_cv.hpp>
  35. #include <boost/test/utils/is_cstring.hpp>
  36. // STL
  37. #include <utility>
  38. #include <type_traits>
  39. #endif
  40. //____________________________________________________________________________//
  41. namespace boost {
  42. namespace unit_test {
  43. template<typename T>
  44. struct is_forward_iterable;
  45. // ************************************************************************** //
  46. // ************** is_forward_iterable ************** //
  47. // ************************************************************************** //
  48. #if defined(BOOST_TEST_FWD_ITERABLE_CXX03) && !defined(BOOST_TEST_DOXYGEN_DOC__)
  49. template<typename T>
  50. struct is_forward_iterable : public mpl::false_ {};
  51. template<typename T>
  52. struct is_forward_iterable<T const> : public is_forward_iterable<T> {};
  53. template<typename T>
  54. struct is_forward_iterable<T&> : public is_forward_iterable<T> {};
  55. template<typename T, std::size_t N>
  56. struct is_forward_iterable< T [N] > : public mpl::true_ {};
  57. template<typename T, typename A>
  58. struct is_forward_iterable< std::vector<T, A> > : public mpl::true_ {};
  59. template<typename T, typename A>
  60. struct is_forward_iterable< std::list<T, A> > : public mpl::true_ {};
  61. template<typename K, typename V, typename C, typename A>
  62. struct is_forward_iterable< std::map<K, V, C, A> > : public mpl::true_ {};
  63. template<typename K, typename C, typename A>
  64. struct is_forward_iterable< std::set<K, C, A> > : public mpl::true_ {};
  65. // string is also forward iterable, even if sometimes we want to treat the
  66. // assertions differently.
  67. template<>
  68. struct is_forward_iterable< std::string > : public mpl::true_ {};
  69. #else
  70. namespace ut_detail {
  71. // SFINAE helper
  72. template<typename T>
  73. struct is_present : public mpl::true_ {};
  74. //____________________________________________________________________________//
  75. // some compiler do not implement properly decltype non expression involving members (eg. VS2013)
  76. // a workaround is to use -> decltype syntax.
  77. template <class T>
  78. struct has_member_size {
  79. private:
  80. struct nil_t {};
  81. template<typename U> static auto test( U* ) -> decltype(boost::declval<U>().size());
  82. template<typename> static nil_t test( ... );
  83. public:
  84. static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
  85. };
  86. //____________________________________________________________________________//
  87. template <class T>
  88. struct has_member_begin {
  89. private:
  90. struct nil_t {};
  91. template<typename U> static auto test( U* ) -> decltype(std::begin(boost::declval<U&>())); // does not work with boost::begin
  92. template<typename> static nil_t test( ... );
  93. public:
  94. static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
  95. };
  96. //____________________________________________________________________________//
  97. template <class T>
  98. struct has_member_end {
  99. private:
  100. struct nil_t {};
  101. template<typename U> static auto test( U* ) -> decltype(std::end(boost::declval<U&>())); // does not work with boost::end
  102. template<typename> static nil_t test( ... );
  103. public:
  104. static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
  105. };
  106. //____________________________________________________________________________//
  107. template <class T, class enabled = void>
  108. struct is_forward_iterable_impl : std::false_type {
  109. };
  110. template <class T>
  111. struct is_forward_iterable_impl<
  112. T,
  113. typename std::enable_if<
  114. has_member_begin<T>::value &&
  115. has_member_end<T>::value
  116. >::type
  117. > : std::true_type
  118. {};
  119. //____________________________________________________________________________//
  120. template <class T, class enabled = void>
  121. struct is_container_forward_iterable_impl : std::false_type {
  122. };
  123. template <class T>
  124. struct is_container_forward_iterable_impl<
  125. T,
  126. typename std::enable_if<
  127. is_present<typename T::const_iterator>::value &&
  128. is_present<typename T::value_type>::value &&
  129. has_member_size<T>::value &&
  130. is_forward_iterable_impl<T>::value
  131. >::type
  132. > : is_forward_iterable_impl<T>
  133. {};
  134. //____________________________________________________________________________//
  135. } // namespace ut_detail
  136. /*! Indicates that a specific type implements the forward iterable concept. */
  137. template<typename T>
  138. struct is_forward_iterable {
  139. typedef typename std::remove_reference<T>::type T_ref;
  140. typedef ut_detail::is_forward_iterable_impl<T_ref> is_fwd_it_t;
  141. typedef mpl::bool_<is_fwd_it_t::value> type;
  142. enum { value = is_fwd_it_t::value };
  143. };
  144. /*! Indicates that a specific type implements the forward iterable concept. */
  145. template<typename T>
  146. struct is_container_forward_iterable {
  147. typedef typename std::remove_reference<T>::type T_ref;
  148. typedef ut_detail::is_container_forward_iterable_impl<T_ref> is_fwd_it_t;
  149. typedef mpl::bool_<is_fwd_it_t::value> type;
  150. enum { value = is_fwd_it_t::value };
  151. };
  152. #endif /* defined(BOOST_TEST_FWD_ITERABLE_CXX03) */
  153. //! Helper structure for accessing the content of a container or an array
  154. template <typename T, bool is_forward_iterable = is_forward_iterable<T>::value >
  155. struct bt_iterator_traits;
  156. template <typename T>
  157. struct bt_iterator_traits< T, true >{
  158. BOOST_STATIC_ASSERT((is_forward_iterable<T>::value));
  159. #if defined(BOOST_TEST_FWD_ITERABLE_CXX03) || \
  160. (defined(BOOST_MSVC) && (BOOST_MSVC_FULL_VER <= 170061232))
  161. typedef typename T::const_iterator const_iterator;
  162. typedef typename std::iterator_traits<const_iterator>::value_type value_type;
  163. #else
  164. typedef decltype(boost::declval<
  165. typename boost::add_const<
  166. typename boost::remove_reference<T>::type
  167. >::type>().begin()) const_iterator;
  168. typedef typename std::iterator_traits<const_iterator>::value_type value_type;
  169. #endif /* BOOST_TEST_FWD_ITERABLE_CXX03 */
  170. static const_iterator begin(T const& container) {
  171. return container.begin();
  172. }
  173. static const_iterator end(T const& container) {
  174. return container.end();
  175. }
  176. #if defined(BOOST_TEST_FWD_ITERABLE_CXX03) || \
  177. (defined(BOOST_MSVC) && (BOOST_MSVC_FULL_VER <= 170061232))
  178. static std::size_t
  179. size(T const& container) {
  180. return container.size();
  181. }
  182. #else
  183. static std::size_t
  184. size(T const& container) {
  185. return size(container,
  186. std::integral_constant<bool, ut_detail::has_member_size<T>::value>());
  187. }
  188. private:
  189. static std::size_t
  190. size(T const& container, std::true_type) { return container.size(); }
  191. static std::size_t
  192. size(T const& container, std::false_type) { return std::distance(begin(container), end(container)); }
  193. #endif /* BOOST_TEST_FWD_ITERABLE_CXX03 */
  194. };
  195. template <typename T, std::size_t N>
  196. struct bt_iterator_traits< T [N], true > {
  197. typedef typename boost::add_const<T>::type T_const;
  198. typedef typename boost::add_pointer<T_const>::type const_iterator;
  199. typedef T value_type;
  200. static const_iterator begin(T_const (&array)[N]) {
  201. return &array[0];
  202. }
  203. static const_iterator end(T_const (&array)[N]) {
  204. return &array[N];
  205. }
  206. static std::size_t size(T_const (&)[N]) {
  207. return N;
  208. }
  209. };
  210. } // namespace unit_test
  211. } // namespace boost
  212. #endif // BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP