concepts.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Boost.Range library concept checks
  2. //
  3. // Copyright Daniel Walker 2006. 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)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_CONCEPTS_HPP
  11. #define BOOST_RANGE_CONCEPTS_HPP
  12. #include <boost/concept_check.hpp>
  13. #include <boost/iterator/iterator_concepts.hpp>
  14. #include <boost/range/begin.hpp>
  15. #include <boost/range/end.hpp>
  16. /*!
  17. * \file
  18. * \brief Concept checks for the Boost Range library.
  19. *
  20. * The structures in this file may be used in conjunction with the
  21. * Boost Concept Check library to insure that the type of a function
  22. * parameter is compatible with a range concept. If not, a meaningful
  23. * compile time error is generated. Checks are provided for the range
  24. * concepts related to iterator traversal categories. For example, the
  25. * following line checks that the type T models the ForwardRange
  26. * concept.
  27. *
  28. * \code
  29. * function_requires<ForwardRangeConcept<T> >();
  30. * \endcode
  31. *
  32. * An additional concept check is required for the value access
  33. * property of the range. For example to check for a
  34. * ForwardReadableRange, the following code is required.
  35. *
  36. * \code
  37. * function_requires<ForwardRangeConcept<T> >();
  38. * function_requires<
  39. * ReadableIteratorConcept<
  40. * typename range_iterator<T>::type
  41. * >
  42. * >();
  43. * \endcode
  44. *
  45. * \see http://www.boost.org/libs/range/doc/range.html for details
  46. * about range concepts.
  47. * \see http://www.boost.org/libs/iterator/doc/iterator_concepts.html
  48. * for details about iterator concepts.
  49. * \see http://www.boost.org/libs/concept_check/concept_check.htm for
  50. * details about concept checks.
  51. */
  52. namespace boost {
  53. //! Check if a type T models the SinglePassRange range concept.
  54. template<typename T>
  55. struct SinglePassRangeConcept
  56. {
  57. typedef typename range_iterator<T const>::type range_const_iterator;
  58. typedef typename range_iterator<T>::type range_iterator;
  59. void constraints()
  60. {
  61. function_requires<
  62. boost_concepts::SinglePassIteratorConcept<
  63. range_iterator
  64. >
  65. >();
  66. i = boost::begin(a);
  67. i = boost::end(a);
  68. const_constraints(a);
  69. }
  70. void const_constraints(const T& a)
  71. {
  72. ci = boost::begin(a);
  73. ci = boost::end(a);
  74. }
  75. T a;
  76. range_iterator i;
  77. range_const_iterator ci;
  78. };
  79. //! Check if a type T models the ForwardRange range concept.
  80. template<typename T>
  81. struct ForwardRangeConcept
  82. {
  83. void constraints()
  84. {
  85. function_requires<
  86. SinglePassRangeConcept<T>
  87. >();
  88. function_requires<
  89. boost_concepts::ForwardTraversalConcept<
  90. typename range_iterator<T>::type
  91. >
  92. >();
  93. }
  94. };
  95. //! Check if a type T models the BidirectionalRange range concept.
  96. template<typename T>
  97. struct BidirectionalRangeConcept
  98. {
  99. void constraints()
  100. {
  101. function_requires<
  102. ForwardRangeConcept<T>
  103. >();
  104. function_requires<
  105. boost_concepts::BidirectionalTraversalConcept<
  106. typename range_iterator<T>::type
  107. >
  108. >();
  109. }
  110. };
  111. //! Check if a type T models the RandomAccessRange range concept.
  112. template<typename T>
  113. struct RandomAccessRangeConcept
  114. {
  115. void constraints()
  116. {
  117. function_requires<
  118. BidirectionalRangeConcept<T>
  119. >();
  120. function_requires<
  121. boost_concepts::RandomAccessTraversalConcept<
  122. typename range_iterator<T>::type
  123. >
  124. >();
  125. }
  126. };
  127. } // namespace boost
  128. #endif // BOOST_RANGE_CONCEPTS_HPP