concepts.hpp 13 KB

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