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>
29 * \brief Concept checks for the Boost Range library.
31 * The structures in this file may be used in conjunction with the
32 * Boost Concept Check library to insure that the type of a function
33 * parameter is compatible with a range concept. If not, a meaningful
34 * compile time error is generated. Checks are provided for the range
35 * concepts related to iterator traversal categories. For example, the
36 * following line checks that the type T models the ForwardRange
40 * BOOST_CONCEPT_ASSERT((ForwardRangeConcept<T>));
43 * A different concept check is required to ensure writeable value
44 * access. For example to check for a ForwardRange that can be written
45 * to, the following code is required.
48 * BOOST_CONCEPT_ASSERT((WriteableForwardRangeConcept<T>));
51 * \see http://www.boost.org/libs/range/doc/range.html for details
52 * about range concepts.
53 * \see http://www.boost.org/libs/iterator/doc/iterator_concepts.html
54 * for details about iterator concepts.
55 * \see http://www.boost.org/libs/concept_check/concept_check.htm for
56 * details about concept checks.
61 namespace range_detail {
63 #ifndef BOOST_RANGE_ENABLE_CONCEPT_ASSERT
65 // List broken compiler versions here:
67 // GNUC 4.2 has strange issues correctly detecting compliance with the Concepts
68 // hence the least disruptive approach is to turn-off the concept checking for
69 // this version of the compiler.
70 #if __GNUC__ == 4 && __GNUC_MINOR__ == 2
71 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
76 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
80 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
83 // Default to using the concept asserts unless we have defined it off
84 // during the search for black listed compilers.
85 #ifndef BOOST_RANGE_ENABLE_CONCEPT_ASSERT
86 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 1
91 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
92 #define BOOST_RANGE_CONCEPT_ASSERT( x ) BOOST_CONCEPT_ASSERT( x )
94 #define BOOST_RANGE_CONCEPT_ASSERT( x )
97 // Rationale for the inclusion of redefined iterator concept
100 // The Range algorithms often do not require that the iterators are
101 // Assignable or default constructable, but the correct standard
102 // conformant iterators do require the iterators to be a model of the
103 // Assignable concept.
104 // Iterators that contains a functor that is not assignable therefore
105 // are not correct models of the standard iterator concepts,
106 // despite being adequate for most algorithms. An example of this
107 // use case is the combination of the boost::adaptors::filtered
108 // class with a boost::lambda::bind generated functor.
109 // Ultimately modeling the range concepts using composition
110 // with the Boost.Iterator concepts would render the library
111 // incompatible with many common Boost.Lambda expressions.
112 template<class Iterator>
113 struct IncrementableIteratorConcept : CopyConstructible<Iterator>
115 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
116 typedef BOOST_DEDUCED_TYPENAME iterator_traversal<Iterator>::type traversal_category;
118 BOOST_RANGE_CONCEPT_ASSERT((
121 incrementable_traversal_tag
124 BOOST_CONCEPT_USAGE(IncrementableIteratorConcept)
134 template<class Iterator>
135 struct SinglePassIteratorConcept
136 : IncrementableIteratorConcept<Iterator>
137 , EqualityComparable<Iterator>
139 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
140 BOOST_RANGE_CONCEPT_ASSERT((
142 BOOST_DEDUCED_TYPENAME SinglePassIteratorConcept::traversal_category,
143 single_pass_traversal_tag
146 BOOST_CONCEPT_USAGE(SinglePassIteratorConcept)
149 boost::ignore_unused_variable_warning(i2);
151 // deliberately we are loose with the postfix version for the single pass
152 // iterator due to the commonly poor adherence to the specification means that
153 // many algorithms would be unusable, whereas actually without the check they
157 BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::reference r1(*i);
158 boost::ignore_unused_variable_warning(r1);
160 BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::reference r2(*(++i));
161 boost::ignore_unused_variable_warning(r2);
168 template<class Iterator>
169 struct ForwardIteratorConcept
170 : SinglePassIteratorConcept<Iterator>
171 , DefaultConstructible<Iterator>
173 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
174 typedef BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::difference_type difference_type;
176 BOOST_MPL_ASSERT((is_integral<difference_type>));
177 BOOST_MPL_ASSERT_RELATION(std::numeric_limits<difference_type>::is_signed, ==, true);
179 BOOST_RANGE_CONCEPT_ASSERT((
181 BOOST_DEDUCED_TYPENAME ForwardIteratorConcept::traversal_category,
182 forward_traversal_tag
185 BOOST_CONCEPT_USAGE(ForwardIteratorConcept)
187 // See the above note in the SinglePassIteratorConcept about the handling of the
188 // postfix increment. Since with forward and better iterators there is no need
189 // for a proxy, we can sensibly require that the dereference result
190 // is convertible to reference.
192 boost::ignore_unused_variable_warning(i2);
193 BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::reference r(*(i++));
194 boost::ignore_unused_variable_warning(r);
201 template<class Iterator>
202 struct BidirectionalIteratorConcept
203 : ForwardIteratorConcept<Iterator>
205 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
206 BOOST_RANGE_CONCEPT_ASSERT((
208 BOOST_DEDUCED_TYPENAME BidirectionalIteratorConcept::traversal_category,
209 bidirectional_traversal_tag
212 BOOST_CONCEPT_USAGE(BidirectionalIteratorConcept)
222 template<class Iterator>
223 struct RandomAccessIteratorConcept
224 : BidirectionalIteratorConcept<Iterator>
226 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
227 BOOST_RANGE_CONCEPT_ASSERT((
229 BOOST_DEDUCED_TYPENAME RandomAccessIteratorConcept::traversal_category,
230 random_access_traversal_tag
233 BOOST_CONCEPT_USAGE(RandomAccessIteratorConcept)
243 BOOST_DEDUCED_TYPENAME RandomAccessIteratorConcept::difference_type n;
249 } // namespace range_detail
251 //! Check if a type T models the SinglePassRange range concept.
253 struct SinglePassRangeConcept
255 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
256 typedef BOOST_DEDUCED_TYPENAME range_iterator<T const>::type const_iterator;
257 typedef BOOST_DEDUCED_TYPENAME range_iterator<T>::type iterator;
259 BOOST_RANGE_CONCEPT_ASSERT((range_detail::SinglePassIteratorConcept<iterator>));
260 BOOST_RANGE_CONCEPT_ASSERT((range_detail::SinglePassIteratorConcept<const_iterator>));
262 BOOST_CONCEPT_USAGE(SinglePassRangeConcept)
264 // This has been modified from assigning to this->i
265 // (where i was a member variable) to improve
266 // compatibility with Boost.Lambda
267 iterator i1 = boost::begin(*m_range);
268 iterator i2 = boost::end(*m_range);
270 ignore_unused_variable_warning(i1);
271 ignore_unused_variable_warning(i2);
273 const_constraints(*m_range);
277 void const_constraints(const T& const_range)
279 const_iterator ci1 = boost::begin(const_range);
280 const_iterator ci2 = boost::end(const_range);
282 ignore_unused_variable_warning(ci1);
283 ignore_unused_variable_warning(ci2);
287 // The type of m_range is T* rather than T because it allows
288 // T to be an abstract class. The other obvious alternative of
289 // T& produces a warning on some compilers.
294 //! Check if a type T models the ForwardRange range concept.
296 struct ForwardRangeConcept : SinglePassRangeConcept<T>
298 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
299 BOOST_RANGE_CONCEPT_ASSERT((range_detail::ForwardIteratorConcept<BOOST_DEDUCED_TYPENAME ForwardRangeConcept::iterator>));
300 BOOST_RANGE_CONCEPT_ASSERT((range_detail::ForwardIteratorConcept<BOOST_DEDUCED_TYPENAME ForwardRangeConcept::const_iterator>));
304 template<class Range>
305 struct WriteableRangeConcept
307 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
308 typedef BOOST_DEDUCED_TYPENAME range_iterator<Range>::type iterator;
310 BOOST_CONCEPT_USAGE(WriteableRangeConcept)
316 BOOST_DEDUCED_TYPENAME range_value<Range>::type v;
320 //! Check if a type T models the WriteableForwardRange range concept.
322 struct WriteableForwardRangeConcept
323 : ForwardRangeConcept<T>
324 , WriteableRangeConcept<T>
328 //! Check if a type T models the BidirectionalRange range concept.
330 struct BidirectionalRangeConcept : ForwardRangeConcept<T>
332 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
333 BOOST_RANGE_CONCEPT_ASSERT((range_detail::BidirectionalIteratorConcept<BOOST_DEDUCED_TYPENAME BidirectionalRangeConcept::iterator>));
334 BOOST_RANGE_CONCEPT_ASSERT((range_detail::BidirectionalIteratorConcept<BOOST_DEDUCED_TYPENAME BidirectionalRangeConcept::const_iterator>));
338 //! Check if a type T models the WriteableBidirectionalRange range concept.
340 struct WriteableBidirectionalRangeConcept
341 : BidirectionalRangeConcept<T>
342 , WriteableRangeConcept<T>
346 //! Check if a type T models the RandomAccessRange range concept.
348 struct RandomAccessRangeConcept : BidirectionalRangeConcept<T>
350 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
351 BOOST_RANGE_CONCEPT_ASSERT((range_detail::RandomAccessIteratorConcept<BOOST_DEDUCED_TYPENAME RandomAccessRangeConcept::iterator>));
352 BOOST_RANGE_CONCEPT_ASSERT((range_detail::RandomAccessIteratorConcept<BOOST_DEDUCED_TYPENAME RandomAccessRangeConcept::const_iterator>));
356 //! Check if a type T models the WriteableRandomAccessRange range concept.
358 struct WriteableRandomAccessRangeConcept
359 : RandomAccessRangeConcept<T>
360 , WriteableRangeConcept<T>
366 #endif // BOOST_RANGE_CONCEPTS_HPP