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)
6 // See http://www.boost.org/libs/test for the library home page.
9 //! Defines the is_forward_iterable collection type trait
10 // ***************************************************************************
12 #ifndef BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP
13 #define BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP
15 #if defined(BOOST_NO_CXX11_DECLTYPE) || \
16 defined(BOOST_NO_CXX11_NULLPTR) || \
17 defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES)
19 // some issues with boost.config
20 #if !defined(BOOST_MSVC) || BOOST_MSVC_FULL_VER < 170061030 /* VC2012 upd 5 */
21 #define BOOST_TEST_FWD_ITERABLE_CXX03
25 #if defined(BOOST_TEST_FWD_ITERABLE_CXX03)
27 #include <boost/mpl/bool.hpp>
38 #include <boost/utility/declval.hpp>
39 #include <boost/range.hpp>
40 #include <boost/type_traits/is_same.hpp>
41 #include <boost/type_traits/remove_reference.hpp>
42 #include <boost/type_traits/remove_cv.hpp>
43 #include <boost/test/utils/is_cstring.hpp>
47 #include <type_traits>
50 //____________________________________________________________________________//
56 struct is_forward_iterable;
58 // ************************************************************************** //
59 // ************** is_forward_iterable ************** //
60 // ************************************************************************** //
62 #if defined(BOOST_TEST_FWD_ITERABLE_CXX03) && !defined(BOOST_TEST_DOXYGEN_DOC__)
64 struct is_forward_iterable : public mpl::false_ {};
67 struct is_forward_iterable<T const> : public is_forward_iterable<T> {};
70 struct is_forward_iterable<T&> : public is_forward_iterable<T> {};
72 template<typename T, std::size_t N>
73 struct is_forward_iterable< T [N] > : public mpl::true_ {};
75 template<typename T, typename A>
76 struct is_forward_iterable< std::vector<T, A> > : public mpl::true_ {};
78 template<typename T, typename A>
79 struct is_forward_iterable< std::list<T, A> > : public mpl::true_ {};
81 template<typename K, typename V, typename C, typename A>
82 struct is_forward_iterable< std::map<K, V, C, A> > : public mpl::true_ {};
84 template<typename K, typename C, typename A>
85 struct is_forward_iterable< std::set<K, C, A> > : public mpl::true_ {};
87 // string is also forward iterable, even if sometimes we want to treat the
88 // assertions differently.
90 struct is_forward_iterable< std::string > : public mpl::true_ {};
98 struct is_present : public mpl::true_ {};
100 //____________________________________________________________________________//
102 // some compiler do not implement properly decltype non expression involving members (eg. VS2013)
103 // a workaround is to use -> decltype syntax.
105 struct has_member_size {
108 template<typename U> static auto test( U* ) -> decltype(boost::declval<U>().size());
109 template<typename> static nil_t test( ... );
112 static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
115 //____________________________________________________________________________//
118 struct has_member_begin {
121 template<typename U> static auto test( U* ) -> decltype(std::begin(boost::declval<U&>())); // does not work with boost::begin
122 template<typename> static nil_t test( ... );
124 static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
129 //____________________________________________________________________________//
132 struct has_member_end {
135 template<typename U> static auto test( U* ) -> decltype(std::end(boost::declval<U&>())); // does not work with boost::end
136 template<typename> static nil_t test( ... );
138 static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
141 //____________________________________________________________________________//
143 template <class T, class enabled = void>
144 struct is_forward_iterable_impl : std::false_type {
148 struct is_forward_iterable_impl<
150 typename std::enable_if<
151 has_member_begin<T>::value &&
152 has_member_end<T>::value
157 //____________________________________________________________________________//
159 template <class T, class enabled = void>
160 struct is_container_forward_iterable_impl : std::false_type {
164 struct is_container_forward_iterable_impl<
166 typename std::enable_if<
167 is_present<typename T::const_iterator>::value &&
168 is_present<typename T::value_type>::value &&
169 has_member_size<T>::value &&
170 is_forward_iterable_impl<T>::value
172 > : is_forward_iterable_impl<T>
175 //____________________________________________________________________________//
177 } // namespace ut_detail
179 /*! Indicates that a specific type implements the forward iterable concept. */
181 struct is_forward_iterable {
182 typedef typename std::remove_reference<T>::type T_ref;
183 typedef ut_detail::is_forward_iterable_impl<T_ref> is_fwd_it_t;
184 typedef mpl::bool_<is_fwd_it_t::value> type;
185 enum { value = is_fwd_it_t::value };
188 /*! Indicates that a specific type implements the forward iterable concept. */
190 struct is_container_forward_iterable {
191 typedef typename std::remove_reference<T>::type T_ref;
192 typedef ut_detail::is_container_forward_iterable_impl<T_ref> is_fwd_it_t;
193 typedef mpl::bool_<is_fwd_it_t::value> type;
194 enum { value = is_fwd_it_t::value };
197 #endif /* defined(BOOST_TEST_FWD_ITERABLE_CXX03) */
199 template <typename T, bool is_forward_iterable = is_forward_iterable<T>::value >
200 struct bt_iterator_traits;
202 template <typename T>
203 struct bt_iterator_traits< T, true >{
204 BOOST_STATIC_ASSERT((is_forward_iterable<T>::value)); //, "only for forward iterable types");
205 typedef typename T::const_iterator const_iterator;
206 typedef typename T::value_type value_type;
208 static const_iterator begin(T const& container) {
209 return container.begin();
211 static const_iterator end(T const& container) {
212 return container.end();
214 static std::size_t size(T const& container) {
215 return container.size();
219 template <typename T, std::size_t N>
220 struct bt_iterator_traits< T [N], true > {
221 typedef typename boost::add_const<T>::type T_const;
222 typedef typename boost::add_pointer<T_const>::type const_iterator;
223 typedef T value_type;
225 static const_iterator begin(T_const (&array)[N]) {
228 static const_iterator end(T_const (&array)[N]) {
231 static std::size_t size(T_const (&)[N]) {
236 } // namespace unit_test
239 #endif // BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP