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