1 // Boost.Range library concept checks
3 // Copyright Neil Groves 2009. Use, modification and distribution
4 // are subject to the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // Copyright Daniel Walker 2006. Use, modification and distribution
9 // are subject to the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 // For more information, see http://www.boost.org/libs/range/
16 #ifndef BOOST_RANGE_CONCEPTS_HPP
17 #define BOOST_RANGE_CONCEPTS_HPP
19 #include <boost/concept_check.hpp>
20 #include <boost/iterator/iterator_concepts.hpp>
21 #include <boost/range/begin.hpp>
22 #include <boost/range/end.hpp>
23 #include <boost/range/iterator.hpp>
24 #include <boost/range/value_type.hpp>
25 #include <boost/range/detail/misc_concept.hpp>
26 #include <boost/type_traits/remove_reference.hpp>
32 * \brief Concept checks for the Boost Range library.
34 * The structures in this file may be used in conjunction with the
35 * Boost Concept Check library to insure that the type of a function
36 * parameter is compatible with a range concept. If not, a meaningful
37 * compile time error is generated. Checks are provided for the range
38 * concepts related to iterator traversal categories. For example, the
39 * following line checks that the type T models the ForwardRange
43 * BOOST_CONCEPT_ASSERT((ForwardRangeConcept<T>));
46 * A different concept check is required to ensure writeable value
47 * access. For example to check for a ForwardRange that can be written
48 * to, the following code is required.
51 * BOOST_CONCEPT_ASSERT((WriteableForwardRangeConcept<T>));
54 * \see http://www.boost.org/libs/range/doc/range.html for details
55 * about range concepts.
56 * \see http://www.boost.org/libs/iterator/doc/iterator_concepts.html
57 * for details about iterator concepts.
58 * \see http://www.boost.org/libs/concept_check/concept_check.htm for
59 * details about concept checks.
64 namespace range_detail {
66 #ifndef BOOST_RANGE_ENABLE_CONCEPT_ASSERT
68 // List broken compiler versions here:
71 // GNUC 4.2 has strange issues correctly detecting compliance with the Concepts
72 // hence the least disruptive approach is to turn-off the concept checking for
73 // this version of the compiler.
74 #if __GNUC__ == 4 && __GNUC_MINOR__ == 2
75 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
80 // GCC XML, unsurprisingly, has the same issues
81 #if __GCCXML_GNUC__ == 4 && __GCCXML_GNUC_MINOR__ == 2
82 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
88 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
92 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
95 // Default to using the concept asserts unless we have defined it off
96 // during the search for black listed compilers.
97 #ifndef BOOST_RANGE_ENABLE_CONCEPT_ASSERT
98 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 1
103 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
104 #define BOOST_RANGE_CONCEPT_ASSERT( x ) BOOST_CONCEPT_ASSERT( x )
106 #define BOOST_RANGE_CONCEPT_ASSERT( x )
109 // Rationale for the inclusion of redefined iterator concept
112 // The Range algorithms often do not require that the iterators are
113 // Assignable or default constructable, but the correct standard
114 // conformant iterators do require the iterators to be a model of the
115 // Assignable concept.
116 // Iterators that contains a functor that is not assignable therefore
117 // are not correct models of the standard iterator concepts,
118 // despite being adequate for most algorithms. An example of this
119 // use case is the combination of the boost::adaptors::filtered
120 // class with a boost::lambda::bind generated functor.
121 // Ultimately modeling the range concepts using composition
122 // with the Boost.Iterator concepts would render the library
123 // incompatible with many common Boost.Lambda expressions.
124 template<class Iterator>
125 struct IncrementableIteratorConcept : CopyConstructible<Iterator>
127 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
128 typedef BOOST_DEDUCED_TYPENAME iterator_traversal<Iterator>::type traversal_category;
130 BOOST_RANGE_CONCEPT_ASSERT((
133 incrementable_traversal_tag
136 BOOST_CONCEPT_USAGE(IncrementableIteratorConcept)
146 template<class Iterator>
147 struct SinglePassIteratorConcept
148 : IncrementableIteratorConcept<Iterator>
149 , EqualityComparable<Iterator>
151 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
152 BOOST_RANGE_CONCEPT_ASSERT((
154 BOOST_DEDUCED_TYPENAME SinglePassIteratorConcept::traversal_category,
155 single_pass_traversal_tag
158 BOOST_CONCEPT_USAGE(SinglePassIteratorConcept)
161 boost::ignore_unused_variable_warning(i2);
163 // deliberately we are loose with the postfix version for the single pass
164 // iterator due to the commonly poor adherence to the specification means that
165 // many algorithms would be unusable, whereas actually without the check they
169 BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::reference r1(*i);
170 boost::ignore_unused_variable_warning(r1);
172 BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::reference r2(*(++i));
173 boost::ignore_unused_variable_warning(r2);
180 template<class Iterator>
181 struct ForwardIteratorConcept
182 : SinglePassIteratorConcept<Iterator>
183 , DefaultConstructible<Iterator>
185 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
186 typedef BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::difference_type difference_type;
188 BOOST_MPL_ASSERT((is_integral<difference_type>));
189 BOOST_MPL_ASSERT_RELATION(std::numeric_limits<difference_type>::is_signed, ==, true);
191 BOOST_RANGE_CONCEPT_ASSERT((
193 BOOST_DEDUCED_TYPENAME ForwardIteratorConcept::traversal_category,
194 forward_traversal_tag
197 BOOST_CONCEPT_USAGE(ForwardIteratorConcept)
199 // See the above note in the SinglePassIteratorConcept about the handling of the
200 // postfix increment. Since with forward and better iterators there is no need
201 // for a proxy, we can sensibly require that the dereference result
202 // is convertible to reference.
204 boost::ignore_unused_variable_warning(i2);
205 BOOST_DEDUCED_TYPENAME std::iterator_traits<Iterator>::reference r(*(i++));
206 boost::ignore_unused_variable_warning(r);
213 template<class Iterator>
214 struct BidirectionalIteratorConcept
215 : ForwardIteratorConcept<Iterator>
217 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
218 BOOST_RANGE_CONCEPT_ASSERT((
220 BOOST_DEDUCED_TYPENAME BidirectionalIteratorConcept::traversal_category,
221 bidirectional_traversal_tag
224 BOOST_CONCEPT_USAGE(BidirectionalIteratorConcept)
234 template<class Iterator>
235 struct RandomAccessIteratorConcept
236 : BidirectionalIteratorConcept<Iterator>
238 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
239 BOOST_RANGE_CONCEPT_ASSERT((
241 BOOST_DEDUCED_TYPENAME RandomAccessIteratorConcept::traversal_category,
242 random_access_traversal_tag
245 BOOST_CONCEPT_USAGE(RandomAccessIteratorConcept)
255 BOOST_DEDUCED_TYPENAME BidirectionalIteratorConcept<Iterator>::difference_type n;
261 } // namespace range_detail
263 //! Check if a type T models the SinglePassRange range concept.
265 struct SinglePassRangeConcept
267 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
268 // A few compilers don't like the rvalue reference T types so just
270 typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type Rng;
272 typedef BOOST_DEDUCED_TYPENAME range_iterator<
274 >::type const_iterator;
276 typedef BOOST_DEDUCED_TYPENAME range_iterator<Rng>::type iterator;
278 BOOST_RANGE_CONCEPT_ASSERT((
279 range_detail::SinglePassIteratorConcept<iterator>));
281 BOOST_RANGE_CONCEPT_ASSERT((
282 range_detail::SinglePassIteratorConcept<const_iterator>));
284 BOOST_CONCEPT_USAGE(SinglePassRangeConcept)
286 // This has been modified from assigning to this->i
287 // (where i was a member variable) to improve
288 // compatibility with Boost.Lambda
289 iterator i1 = boost::begin(*m_range);
290 iterator i2 = boost::end(*m_range);
292 boost::ignore_unused_variable_warning(i1);
293 boost::ignore_unused_variable_warning(i2);
295 const_constraints(*m_range);
299 void const_constraints(const Rng& const_range)
301 const_iterator ci1 = boost::begin(const_range);
302 const_iterator ci2 = boost::end(const_range);
304 boost::ignore_unused_variable_warning(ci1);
305 boost::ignore_unused_variable_warning(ci2);
309 // The type of m_range is T* rather than T because it allows
310 // T to be an abstract class. The other obvious alternative of
311 // T& produces a warning on some compilers.
316 //! Check if a type T models the ForwardRange range concept.
318 struct ForwardRangeConcept : SinglePassRangeConcept<T>
320 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
321 BOOST_RANGE_CONCEPT_ASSERT((range_detail::ForwardIteratorConcept<BOOST_DEDUCED_TYPENAME ForwardRangeConcept::iterator>));
322 BOOST_RANGE_CONCEPT_ASSERT((range_detail::ForwardIteratorConcept<BOOST_DEDUCED_TYPENAME ForwardRangeConcept::const_iterator>));
327 struct WriteableRangeConcept
329 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
330 typedef BOOST_DEDUCED_TYPENAME range_iterator<T>::type iterator;
332 BOOST_CONCEPT_USAGE(WriteableRangeConcept)
338 BOOST_DEDUCED_TYPENAME range_value<T>::type v;
342 //! Check if a type T models the WriteableForwardRange range concept.
344 struct WriteableForwardRangeConcept
345 : ForwardRangeConcept<T>
346 , WriteableRangeConcept<T>
350 //! Check if a type T models the BidirectionalRange range concept.
352 struct BidirectionalRangeConcept : ForwardRangeConcept<T>
354 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
355 BOOST_RANGE_CONCEPT_ASSERT((range_detail::BidirectionalIteratorConcept<BOOST_DEDUCED_TYPENAME BidirectionalRangeConcept::iterator>));
356 BOOST_RANGE_CONCEPT_ASSERT((range_detail::BidirectionalIteratorConcept<BOOST_DEDUCED_TYPENAME BidirectionalRangeConcept::const_iterator>));
360 //! Check if a type T models the WriteableBidirectionalRange range concept.
362 struct WriteableBidirectionalRangeConcept
363 : BidirectionalRangeConcept<T>
364 , WriteableRangeConcept<T>
368 //! Check if a type T models the RandomAccessRange range concept.
370 struct RandomAccessRangeConcept : BidirectionalRangeConcept<T>
372 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
373 BOOST_RANGE_CONCEPT_ASSERT((range_detail::RandomAccessIteratorConcept<BOOST_DEDUCED_TYPENAME RandomAccessRangeConcept::iterator>));
374 BOOST_RANGE_CONCEPT_ASSERT((range_detail::RandomAccessIteratorConcept<BOOST_DEDUCED_TYPENAME RandomAccessRangeConcept::const_iterator>));
378 //! Check if a type T models the WriteableRandomAccessRange range concept.
380 struct WriteableRandomAccessRangeConcept
381 : RandomAccessRangeConcept<T>
382 , WriteableRangeConcept<T>
388 #endif // BOOST_RANGE_CONCEPTS_HPP