]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/test/utils/is_forward_iterable.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / test / utils / is_forward_iterable.hpp
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
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 //! @file
9 //! Defines the is_forward_iterable collection type trait
10 // ***************************************************************************
11
12 #ifndef BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP
13 #define BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP
14
15 #if defined(BOOST_NO_CXX11_DECLTYPE) || \
16 defined(BOOST_NO_CXX11_NULLPTR) || \
17 defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES)
18
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
22 #endif
23 #endif
24
25 #if defined(BOOST_TEST_FWD_ITERABLE_CXX03)
26 // Boost
27 #include <boost/mpl/bool.hpp>
28
29 // STL
30 #include <list>
31 #include <vector>
32 #include <map>
33 #include <set>
34
35 #else
36
37 // Boost
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>
44
45 // STL
46 #include <utility>
47 #include <type_traits>
48
49 #endif
50 //____________________________________________________________________________//
51
52 namespace boost {
53 namespace unit_test {
54
55 template<typename T>
56 struct is_forward_iterable;
57
58 // ************************************************************************** //
59 // ************** is_forward_iterable ************** //
60 // ************************************************************************** //
61
62 #if defined(BOOST_TEST_FWD_ITERABLE_CXX03) && !defined(BOOST_TEST_DOXYGEN_DOC__)
63 template<typename T>
64 struct is_forward_iterable : public mpl::false_ {};
65
66 template<typename T>
67 struct is_forward_iterable<T const> : public is_forward_iterable<T> {};
68
69 template<typename T>
70 struct is_forward_iterable<T&> : public is_forward_iterable<T> {};
71
72 template<typename T, std::size_t N>
73 struct is_forward_iterable< T [N] > : public mpl::true_ {};
74
75 template<typename T, typename A>
76 struct is_forward_iterable< std::vector<T, A> > : public mpl::true_ {};
77
78 template<typename T, typename A>
79 struct is_forward_iterable< std::list<T, A> > : public mpl::true_ {};
80
81 template<typename K, typename V, typename C, typename A>
82 struct is_forward_iterable< std::map<K, V, C, A> > : public mpl::true_ {};
83
84 template<typename K, typename C, typename A>
85 struct is_forward_iterable< std::set<K, C, A> > : public mpl::true_ {};
86
87 // string is also forward iterable, even if sometimes we want to treat the
88 // assertions differently.
89 template<>
90 struct is_forward_iterable< std::string > : public mpl::true_ {};
91
92 #else
93
94 namespace ut_detail {
95
96 // SFINAE helper
97 template<typename T>
98 struct is_present : public mpl::true_ {};
99
100 //____________________________________________________________________________//
101
102 // some compiler do not implement properly decltype non expression involving members (eg. VS2013)
103 // a workaround is to use -> decltype syntax.
104 template <class T>
105 struct has_member_size {
106 private:
107 struct nil_t {};
108 template<typename U> static auto test( U* ) -> decltype(boost::declval<U>().size());
109 template<typename> static nil_t test( ... );
110
111 public:
112 static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
113 };
114
115 //____________________________________________________________________________//
116
117 template <class T>
118 struct has_member_begin {
119 private:
120 struct nil_t {};
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( ... );
123 public:
124 static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
125
126
127 };
128
129 //____________________________________________________________________________//
130
131 template <class T>
132 struct has_member_end {
133 private:
134 struct nil_t {};
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( ... );
137 public:
138 static bool const value = !std::is_same< decltype(test<T>( nullptr )), nil_t>::value;
139 };
140
141 //____________________________________________________________________________//
142
143 template <class T, class enabled = void>
144 struct is_forward_iterable_impl : std::false_type {
145 };
146
147 template <class T>
148 struct is_forward_iterable_impl<
149 T,
150 typename std::enable_if<
151 has_member_begin<T>::value &&
152 has_member_end<T>::value
153 >::type
154 > : std::true_type
155 {};
156
157 //____________________________________________________________________________//
158
159 template <class T, class enabled = void>
160 struct is_container_forward_iterable_impl : std::false_type {
161 };
162
163 template <class T>
164 struct is_container_forward_iterable_impl<
165 T,
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
171 >::type
172 > : is_forward_iterable_impl<T>
173 {};
174
175 //____________________________________________________________________________//
176
177 } // namespace ut_detail
178
179 /*! Indicates that a specific type implements the forward iterable concept. */
180 template<typename T>
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 };
186 };
187
188 /*! Indicates that a specific type implements the forward iterable concept. */
189 template<typename T>
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 };
195 };
196
197 #endif /* defined(BOOST_TEST_FWD_ITERABLE_CXX03) */
198
199 template <typename T, bool is_forward_iterable = is_forward_iterable<T>::value >
200 struct bt_iterator_traits;
201
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;
207
208 static const_iterator begin(T const& container) {
209 return container.begin();
210 }
211 static const_iterator end(T const& container) {
212 return container.end();
213 }
214 static std::size_t size(T const& container) {
215 return container.size();
216 }
217 };
218
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;
224
225 static const_iterator begin(T_const (&array)[N]) {
226 return &array[0];
227 }
228 static const_iterator end(T_const (&array)[N]) {
229 return &array[N];
230 }
231 static std::size_t size(T_const (&)[N]) {
232 return N;
233 }
234 };
235
236 } // namespace unit_test
237 } // namespace boost
238
239 #endif // BOOST_TEST_UTILS_IS_FORWARD_ITERABLE_HPP