annotate DEPENDENCIES/generic/include/boost/range/concepts.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 // Boost.Range library concept checks
Chris@16 2 //
Chris@16 3 // Copyright Neil Groves 2009. Use, modification and distribution
Chris@16 4 // are subject to the Boost Software License, Version 1.0. (See
Chris@16 5 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7 //
Chris@16 8 // Copyright Daniel Walker 2006. Use, modification and distribution
Chris@16 9 // are subject to the Boost Software License, Version 1.0. (See
Chris@16 10 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 11 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 12 //
Chris@16 13 // For more information, see http://www.boost.org/libs/range/
Chris@16 14 //
Chris@16 15
Chris@16 16 #ifndef BOOST_RANGE_CONCEPTS_HPP
Chris@16 17 #define BOOST_RANGE_CONCEPTS_HPP
Chris@16 18
Chris@16 19 #include <boost/concept_check.hpp>
Chris@16 20 #include <boost/iterator/iterator_concepts.hpp>
Chris@16 21 #include <boost/range/begin.hpp>
Chris@16 22 #include <boost/range/end.hpp>
Chris@16 23 #include <boost/range/iterator.hpp>
Chris@16 24 #include <boost/range/value_type.hpp>
Chris@16 25 #include <boost/range/detail/misc_concept.hpp>
Chris@16 26
Chris@16 27 /*!
Chris@16 28 * \file
Chris@16 29 * \brief Concept checks for the Boost Range library.
Chris@16 30 *
Chris@16 31 * The structures in this file may be used in conjunction with the
Chris@16 32 * Boost Concept Check library to insure that the type of a function
Chris@16 33 * parameter is compatible with a range concept. If not, a meaningful
Chris@16 34 * compile time error is generated. Checks are provided for the range
Chris@16 35 * concepts related to iterator traversal categories. For example, the
Chris@16 36 * following line checks that the type T models the ForwardRange
Chris@16 37 * concept.
Chris@16 38 *
Chris@16 39 * \code
Chris@16 40 * BOOST_CONCEPT_ASSERT((ForwardRangeConcept<T>));
Chris@16 41 * \endcode
Chris@16 42 *
Chris@16 43 * A different concept check is required to ensure writeable value
Chris@16 44 * access. For example to check for a ForwardRange that can be written
Chris@16 45 * to, the following code is required.
Chris@16 46 *
Chris@16 47 * \code
Chris@16 48 * BOOST_CONCEPT_ASSERT((WriteableForwardRangeConcept<T>));
Chris@16 49 * \endcode
Chris@16 50 *
Chris@16 51 * \see http://www.boost.org/libs/range/doc/range.html for details
Chris@16 52 * about range concepts.
Chris@16 53 * \see http://www.boost.org/libs/iterator/doc/iterator_concepts.html
Chris@16 54 * for details about iterator concepts.
Chris@16 55 * \see http://www.boost.org/libs/concept_check/concept_check.htm for
Chris@16 56 * details about concept checks.
Chris@16 57 */
Chris@16 58
Chris@16 59 namespace boost {
Chris@16 60
Chris@16 61 namespace range_detail {
Chris@16 62
Chris@16 63 #ifndef BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 64
Chris@16 65 // List broken compiler versions here:
Chris@16 66 #ifdef __GNUC__
Chris@16 67 // GNUC 4.2 has strange issues correctly detecting compliance with the Concepts
Chris@16 68 // hence the least disruptive approach is to turn-off the concept checking for
Chris@16 69 // this version of the compiler.
Chris@16 70 #if __GNUC__ == 4 && __GNUC_MINOR__ == 2
Chris@16 71 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
Chris@16 72 #endif
Chris@16 73 #endif
Chris@16 74
Chris@16 75 #ifdef __BORLANDC__
Chris@16 76 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
Chris@16 77 #endif
Chris@16 78
Chris@16 79 #ifdef __PATHCC__
Chris@16 80 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
Chris@16 81 #endif
Chris@16 82
Chris@16 83 // Default to using the concept asserts unless we have defined it off
Chris@16 84 // during the search for black listed compilers.
Chris@16 85 #ifndef BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 86 #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 1
Chris@16 87 #endif
Chris@16 88
Chris@16 89 #endif
Chris@16 90
Chris@16 91 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 92 #define BOOST_RANGE_CONCEPT_ASSERT( x ) BOOST_CONCEPT_ASSERT( x )
Chris@16 93 #else
Chris@16 94 #define BOOST_RANGE_CONCEPT_ASSERT( x )
Chris@16 95 #endif
Chris@16 96
Chris@16 97 // Rationale for the inclusion of redefined iterator concept
Chris@16 98 // classes:
Chris@16 99 //
Chris@16 100 // The Range algorithms often do not require that the iterators are
Chris@16 101 // Assignable or default constructable, but the correct standard
Chris@16 102 // conformant iterators do require the iterators to be a model of the
Chris@16 103 // Assignable concept.
Chris@16 104 // Iterators that contains a functor that is not assignable therefore
Chris@16 105 // are not correct models of the standard iterator concepts,
Chris@16 106 // despite being adequate for most algorithms. An example of this
Chris@16 107 // use case is the combination of the boost::adaptors::filtered
Chris@16 108 // class with a boost::lambda::bind generated functor.
Chris@16 109 // Ultimately modeling the range concepts using composition
Chris@16 110 // with the Boost.Iterator concepts would render the library
Chris@16 111 // incompatible with many common Boost.Lambda expressions.
Chris@16 112 template<class Iterator>
Chris@16 113 struct IncrementableIteratorConcept : CopyConstructible<Iterator>
Chris@16 114 {
Chris@16 115 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 116 typedef BOOST_DEDUCED_TYPENAME iterator_traversal<Iterator>::type traversal_category;
Chris@16 117
Chris@16 118 BOOST_RANGE_CONCEPT_ASSERT((
Chris@16 119 Convertible<
Chris@16 120 traversal_category,
Chris@16 121 incrementable_traversal_tag
Chris@16 122 >));
Chris@16 123
Chris@16 124 BOOST_CONCEPT_USAGE(IncrementableIteratorConcept)
Chris@16 125 {
Chris@16 126 ++i;
Chris@16 127 (void)i++;
Chris@16 128 }
Chris@16 129 private:
Chris@16 130 Iterator i;
Chris@16 131 #endif
Chris@16 132 };
Chris@16 133
Chris@16 134 template<class Iterator>
Chris@16 135 struct SinglePassIteratorConcept
Chris@16 136 : IncrementableIteratorConcept<Iterator>
Chris@16 137 , EqualityComparable<Iterator>
Chris@16 138 {
Chris@16 139 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 140 BOOST_RANGE_CONCEPT_ASSERT((
Chris@16 141 Convertible<
Chris@16 142 BOOST_DEDUCED_TYPENAME SinglePassIteratorConcept::traversal_category,
Chris@16 143 single_pass_traversal_tag
Chris@16 144 >));
Chris@16 145
Chris@16 146 BOOST_CONCEPT_USAGE(SinglePassIteratorConcept)
Chris@16 147 {
Chris@16 148 Iterator i2(++i);
Chris@16 149 boost::ignore_unused_variable_warning(i2);
Chris@16 150
Chris@16 151 // deliberately we are loose with the postfix version for the single pass
Chris@16 152 // iterator due to the commonly poor adherence to the specification means that
Chris@16 153 // many algorithms would be unusable, whereas actually without the check they
Chris@16 154 // work
Chris@16 155 (void)(i++);
Chris@16 156
Chris@16 157 BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::reference r1(*i);
Chris@16 158 boost::ignore_unused_variable_warning(r1);
Chris@16 159
Chris@16 160 BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::reference r2(*(++i));
Chris@16 161 boost::ignore_unused_variable_warning(r2);
Chris@16 162 }
Chris@16 163 private:
Chris@16 164 Iterator i;
Chris@16 165 #endif
Chris@16 166 };
Chris@16 167
Chris@16 168 template<class Iterator>
Chris@16 169 struct ForwardIteratorConcept
Chris@16 170 : SinglePassIteratorConcept<Iterator>
Chris@16 171 , DefaultConstructible<Iterator>
Chris@16 172 {
Chris@16 173 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 174 typedef BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::difference_type difference_type;
Chris@16 175
Chris@16 176 BOOST_MPL_ASSERT((is_integral<difference_type>));
Chris@16 177 BOOST_MPL_ASSERT_RELATION(std::numeric_limits<difference_type>::is_signed, ==, true);
Chris@16 178
Chris@16 179 BOOST_RANGE_CONCEPT_ASSERT((
Chris@16 180 Convertible<
Chris@16 181 BOOST_DEDUCED_TYPENAME ForwardIteratorConcept::traversal_category,
Chris@16 182 forward_traversal_tag
Chris@16 183 >));
Chris@16 184
Chris@16 185 BOOST_CONCEPT_USAGE(ForwardIteratorConcept)
Chris@16 186 {
Chris@16 187 // See the above note in the SinglePassIteratorConcept about the handling of the
Chris@16 188 // postfix increment. Since with forward and better iterators there is no need
Chris@16 189 // for a proxy, we can sensibly require that the dereference result
Chris@16 190 // is convertible to reference.
Chris@16 191 Iterator i2(i++);
Chris@16 192 boost::ignore_unused_variable_warning(i2);
Chris@16 193 BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::reference r(*(i++));
Chris@16 194 boost::ignore_unused_variable_warning(r);
Chris@16 195 }
Chris@16 196 private:
Chris@16 197 Iterator i;
Chris@16 198 #endif
Chris@16 199 };
Chris@16 200
Chris@16 201 template<class Iterator>
Chris@16 202 struct BidirectionalIteratorConcept
Chris@16 203 : ForwardIteratorConcept<Iterator>
Chris@16 204 {
Chris@16 205 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 206 BOOST_RANGE_CONCEPT_ASSERT((
Chris@16 207 Convertible<
Chris@16 208 BOOST_DEDUCED_TYPENAME BidirectionalIteratorConcept::traversal_category,
Chris@16 209 bidirectional_traversal_tag
Chris@16 210 >));
Chris@16 211
Chris@16 212 BOOST_CONCEPT_USAGE(BidirectionalIteratorConcept)
Chris@16 213 {
Chris@16 214 --i;
Chris@16 215 (void)i--;
Chris@16 216 }
Chris@16 217 private:
Chris@16 218 Iterator i;
Chris@16 219 #endif
Chris@16 220 };
Chris@16 221
Chris@16 222 template<class Iterator>
Chris@16 223 struct RandomAccessIteratorConcept
Chris@16 224 : BidirectionalIteratorConcept<Iterator>
Chris@16 225 {
Chris@16 226 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 227 BOOST_RANGE_CONCEPT_ASSERT((
Chris@16 228 Convertible<
Chris@16 229 BOOST_DEDUCED_TYPENAME RandomAccessIteratorConcept::traversal_category,
Chris@16 230 random_access_traversal_tag
Chris@16 231 >));
Chris@16 232
Chris@16 233 BOOST_CONCEPT_USAGE(RandomAccessIteratorConcept)
Chris@16 234 {
Chris@16 235 i += n;
Chris@16 236 i = i + n;
Chris@16 237 i = n + i;
Chris@16 238 i -= n;
Chris@16 239 i = i - n;
Chris@16 240 n = i - j;
Chris@16 241 }
Chris@16 242 private:
Chris@16 243 BOOST_DEDUCED_TYPENAME RandomAccessIteratorConcept::difference_type n;
Chris@16 244 Iterator i;
Chris@16 245 Iterator j;
Chris@16 246 #endif
Chris@16 247 };
Chris@16 248
Chris@16 249 } // namespace range_detail
Chris@16 250
Chris@16 251 //! Check if a type T models the SinglePassRange range concept.
Chris@16 252 template<class T>
Chris@16 253 struct SinglePassRangeConcept
Chris@16 254 {
Chris@16 255 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 256 typedef BOOST_DEDUCED_TYPENAME range_iterator<T const>::type const_iterator;
Chris@16 257 typedef BOOST_DEDUCED_TYPENAME range_iterator<T>::type iterator;
Chris@16 258
Chris@16 259 BOOST_RANGE_CONCEPT_ASSERT((range_detail::SinglePassIteratorConcept<iterator>));
Chris@16 260 BOOST_RANGE_CONCEPT_ASSERT((range_detail::SinglePassIteratorConcept<const_iterator>));
Chris@16 261
Chris@16 262 BOOST_CONCEPT_USAGE(SinglePassRangeConcept)
Chris@16 263 {
Chris@16 264 // This has been modified from assigning to this->i
Chris@16 265 // (where i was a member variable) to improve
Chris@16 266 // compatibility with Boost.Lambda
Chris@16 267 iterator i1 = boost::begin(*m_range);
Chris@16 268 iterator i2 = boost::end(*m_range);
Chris@16 269
Chris@16 270 ignore_unused_variable_warning(i1);
Chris@16 271 ignore_unused_variable_warning(i2);
Chris@16 272
Chris@16 273 const_constraints(*m_range);
Chris@16 274 }
Chris@16 275
Chris@16 276 private:
Chris@16 277 void const_constraints(const T& const_range)
Chris@16 278 {
Chris@16 279 const_iterator ci1 = boost::begin(const_range);
Chris@16 280 const_iterator ci2 = boost::end(const_range);
Chris@16 281
Chris@16 282 ignore_unused_variable_warning(ci1);
Chris@16 283 ignore_unused_variable_warning(ci2);
Chris@16 284 }
Chris@16 285
Chris@16 286 // Rationale:
Chris@16 287 // The type of m_range is T* rather than T because it allows
Chris@16 288 // T to be an abstract class. The other obvious alternative of
Chris@16 289 // T& produces a warning on some compilers.
Chris@16 290 T* m_range;
Chris@16 291 #endif
Chris@16 292 };
Chris@16 293
Chris@16 294 //! Check if a type T models the ForwardRange range concept.
Chris@16 295 template<class T>
Chris@16 296 struct ForwardRangeConcept : SinglePassRangeConcept<T>
Chris@16 297 {
Chris@16 298 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 299 BOOST_RANGE_CONCEPT_ASSERT((range_detail::ForwardIteratorConcept<BOOST_DEDUCED_TYPENAME ForwardRangeConcept::iterator>));
Chris@16 300 BOOST_RANGE_CONCEPT_ASSERT((range_detail::ForwardIteratorConcept<BOOST_DEDUCED_TYPENAME ForwardRangeConcept::const_iterator>));
Chris@16 301 #endif
Chris@16 302 };
Chris@16 303
Chris@16 304 template<class Range>
Chris@16 305 struct WriteableRangeConcept
Chris@16 306 {
Chris@16 307 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 308 typedef BOOST_DEDUCED_TYPENAME range_iterator<Range>::type iterator;
Chris@16 309
Chris@16 310 BOOST_CONCEPT_USAGE(WriteableRangeConcept)
Chris@16 311 {
Chris@16 312 *i = v;
Chris@16 313 }
Chris@16 314 private:
Chris@16 315 iterator i;
Chris@16 316 BOOST_DEDUCED_TYPENAME range_value<Range>::type v;
Chris@16 317 #endif
Chris@16 318 };
Chris@16 319
Chris@16 320 //! Check if a type T models the WriteableForwardRange range concept.
Chris@16 321 template<class T>
Chris@16 322 struct WriteableForwardRangeConcept
Chris@16 323 : ForwardRangeConcept<T>
Chris@16 324 , WriteableRangeConcept<T>
Chris@16 325 {
Chris@16 326 };
Chris@16 327
Chris@16 328 //! Check if a type T models the BidirectionalRange range concept.
Chris@16 329 template<class T>
Chris@16 330 struct BidirectionalRangeConcept : ForwardRangeConcept<T>
Chris@16 331 {
Chris@16 332 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 333 BOOST_RANGE_CONCEPT_ASSERT((range_detail::BidirectionalIteratorConcept<BOOST_DEDUCED_TYPENAME BidirectionalRangeConcept::iterator>));
Chris@16 334 BOOST_RANGE_CONCEPT_ASSERT((range_detail::BidirectionalIteratorConcept<BOOST_DEDUCED_TYPENAME BidirectionalRangeConcept::const_iterator>));
Chris@16 335 #endif
Chris@16 336 };
Chris@16 337
Chris@16 338 //! Check if a type T models the WriteableBidirectionalRange range concept.
Chris@16 339 template<class T>
Chris@16 340 struct WriteableBidirectionalRangeConcept
Chris@16 341 : BidirectionalRangeConcept<T>
Chris@16 342 , WriteableRangeConcept<T>
Chris@16 343 {
Chris@16 344 };
Chris@16 345
Chris@16 346 //! Check if a type T models the RandomAccessRange range concept.
Chris@16 347 template<class T>
Chris@16 348 struct RandomAccessRangeConcept : BidirectionalRangeConcept<T>
Chris@16 349 {
Chris@16 350 #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT
Chris@16 351 BOOST_RANGE_CONCEPT_ASSERT((range_detail::RandomAccessIteratorConcept<BOOST_DEDUCED_TYPENAME RandomAccessRangeConcept::iterator>));
Chris@16 352 BOOST_RANGE_CONCEPT_ASSERT((range_detail::RandomAccessIteratorConcept<BOOST_DEDUCED_TYPENAME RandomAccessRangeConcept::const_iterator>));
Chris@16 353 #endif
Chris@16 354 };
Chris@16 355
Chris@16 356 //! Check if a type T models the WriteableRandomAccessRange range concept.
Chris@16 357 template<class T>
Chris@16 358 struct WriteableRandomAccessRangeConcept
Chris@16 359 : RandomAccessRangeConcept<T>
Chris@16 360 , WriteableRangeConcept<T>
Chris@16 361 {
Chris@16 362 };
Chris@16 363
Chris@16 364 } // namespace boost
Chris@16 365
Chris@16 366 #endif // BOOST_RANGE_CONCEPTS_HPP