blob: 58d4589d6c0a81266c2ced1370d854b493e80892 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (C) Copyright Jeremy Siek 2002.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
Jeff Thompson3d613fd2013-10-15 15:39:04 -07006#ifndef NDNBOOST_ITERATOR_CONCEPTS_HPP
7#define NDNBOOST_ITERATOR_CONCEPTS_HPP
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07008
9#include <ndnboost/concept_check.hpp>
10#include <ndnboost/iterator/iterator_categories.hpp>
11
12// Use ndnboost::detail::iterator_traits to work around some MSVC/Dinkumware problems.
13#include <ndnboost/detail/iterator.hpp>
14
15#include <ndnboost/type_traits/is_same.hpp>
16#include <ndnboost/type_traits/is_integral.hpp>
17
18#include <ndnboost/mpl/bool.hpp>
19#include <ndnboost/mpl/if.hpp>
20#include <ndnboost/mpl/and.hpp>
21#include <ndnboost/mpl/or.hpp>
22
23#include <ndnboost/static_assert.hpp>
24
Jeff Thompson9939dcd2013-10-15 15:12:24 -070025// Use ndnboost/limits to work around missing limits headers on some compilers
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070026#include <ndnboost/limits.hpp>
27#include <ndnboost/config.hpp>
28
29#include <algorithm>
30
31#include <ndnboost/concept/detail/concept_def.hpp>
32
33namespace ndnboost_concepts
34{
35 // Used a different namespace here (instead of "boost") so that the
36 // concept descriptions do not take for granted the names in
37 // namespace ndnboost.
38
39 //===========================================================================
40 // Iterator Access Concepts
41
Jeff Thompson3d613fd2013-10-15 15:39:04 -070042 NDNBOOST_concept(ReadableIterator,(Iterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070043 : ndnboost::Assignable<Iterator>
44 , ndnboost::CopyConstructible<Iterator>
45
46 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070047 typedef NDNBOOST_DEDUCED_TYPENAME ndnboost::detail::iterator_traits<Iterator>::value_type value_type;
48 typedef NDNBOOST_DEDUCED_TYPENAME ndnboost::detail::iterator_traits<Iterator>::reference reference;
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070049
Jeff Thompson3d613fd2013-10-15 15:39:04 -070050 NDNBOOST_CONCEPT_USAGE(ReadableIterator)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070051 {
52
53 value_type v = *i;
54 ndnboost::ignore_unused_variable_warning(v);
55 }
56 private:
57 Iterator i;
58 };
59
60 template <
61 typename Iterator
Jeff Thompson3d613fd2013-10-15 15:39:04 -070062 , typename ValueType = NDNBOOST_DEDUCED_TYPENAME ndnboost::detail::iterator_traits<Iterator>::value_type
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070063 >
64 struct WritableIterator
65 : ndnboost::CopyConstructible<Iterator>
66 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070067 NDNBOOST_CONCEPT_USAGE(WritableIterator)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070068 {
69 *i = v;
70 }
71 private:
72 ValueType v;
73 Iterator i;
74 };
75
76 template <
77 typename Iterator
Jeff Thompson3d613fd2013-10-15 15:39:04 -070078 , typename ValueType = NDNBOOST_DEDUCED_TYPENAME ndnboost::detail::iterator_traits<Iterator>::value_type
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070079 >
80 struct WritableIteratorConcept : WritableIterator<Iterator,ValueType> {};
81
Jeff Thompson3d613fd2013-10-15 15:39:04 -070082 NDNBOOST_concept(SwappableIterator,(Iterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070083 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070084 NDNBOOST_CONCEPT_USAGE(SwappableIterator)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070085 {
86 std::iter_swap(i1, i2);
87 }
88 private:
89 Iterator i1;
90 Iterator i2;
91 };
92
Jeff Thompson3d613fd2013-10-15 15:39:04 -070093 NDNBOOST_concept(LvalueIterator,(Iterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070094 {
95 typedef typename ndnboost::detail::iterator_traits<Iterator>::value_type value_type;
96
Jeff Thompson3d613fd2013-10-15 15:39:04 -070097 NDNBOOST_CONCEPT_USAGE(LvalueIterator)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070098 {
99 value_type& r = const_cast<value_type&>(*i);
100 ndnboost::ignore_unused_variable_warning(r);
101 }
102 private:
103 Iterator i;
104 };
105
106
107 //===========================================================================
108 // Iterator Traversal Concepts
109
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700110 NDNBOOST_concept(IncrementableIterator,(Iterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700111 : ndnboost::Assignable<Iterator>
112 , ndnboost::CopyConstructible<Iterator>
113 {
114 typedef typename ndnboost::iterator_traversal<Iterator>::type traversal_category;
115
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700116 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700117 ndnboost::Convertible<
118 traversal_category
119 , ndnboost::incrementable_traversal_tag
120 >));
121
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700122 NDNBOOST_CONCEPT_USAGE(IncrementableIterator)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700123 {
124 ++i;
125 (void)i++;
126 }
127 private:
128 Iterator i;
129 };
130
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700131 NDNBOOST_concept(SinglePassIterator,(Iterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700132 : IncrementableIterator<Iterator>
133 , ndnboost::EqualityComparable<Iterator>
134
135 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700136 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700137 ndnboost::Convertible<
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700138 NDNBOOST_DEDUCED_TYPENAME SinglePassIterator::traversal_category
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700139 , ndnboost::single_pass_traversal_tag
140 > ));
141 };
142
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700143 NDNBOOST_concept(ForwardTraversal,(Iterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700144 : SinglePassIterator<Iterator>
145 , ndnboost::DefaultConstructible<Iterator>
146 {
147 typedef typename ndnboost::detail::iterator_traits<Iterator>::difference_type difference_type;
148
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700149 NDNBOOST_MPL_ASSERT((ndnboost::is_integral<difference_type>));
150 NDNBOOST_MPL_ASSERT_RELATION(std::numeric_limits<difference_type>::is_signed, ==, true);
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700151
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700152 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700153 ndnboost::Convertible<
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700154 NDNBOOST_DEDUCED_TYPENAME ForwardTraversal::traversal_category
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700155 , ndnboost::forward_traversal_tag
156 > ));
157 };
158
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700159 NDNBOOST_concept(BidirectionalTraversal,(Iterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700160 : ForwardTraversal<Iterator>
161 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700162 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700163 ndnboost::Convertible<
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700164 NDNBOOST_DEDUCED_TYPENAME BidirectionalTraversal::traversal_category
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700165 , ndnboost::bidirectional_traversal_tag
166 > ));
167
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700168 NDNBOOST_CONCEPT_USAGE(BidirectionalTraversal)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700169 {
170 --i;
171 (void)i--;
172 }
173 private:
174 Iterator i;
175 };
176
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700177 NDNBOOST_concept(RandomAccessTraversal,(Iterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700178 : BidirectionalTraversal<Iterator>
179 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700180 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700181 ndnboost::Convertible<
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700182 NDNBOOST_DEDUCED_TYPENAME RandomAccessTraversal::traversal_category
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700183 , ndnboost::random_access_traversal_tag
184 > ));
185
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700186 NDNBOOST_CONCEPT_USAGE(RandomAccessTraversal)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700187 {
188 i += n;
189 i = i + n;
190 i = n + i;
191 i -= n;
192 i = i - n;
193 n = i - j;
194 }
195
196 private:
197 typename BidirectionalTraversal<Iterator>::difference_type n;
198 Iterator i, j;
199 };
200
201 //===========================================================================
202 // Iterator Interoperability
203
204 namespace detail
205 {
206 template <typename Iterator1, typename Iterator2>
207 void interop_single_pass_constraints(Iterator1 const& i1, Iterator2 const& i2)
208 {
209 bool b;
210 b = i1 == i2;
211 b = i1 != i2;
212
213 b = i2 == i1;
214 b = i2 != i1;
215 ndnboost::ignore_unused_variable_warning(b);
216 }
217
218 template <typename Iterator1, typename Iterator2>
219 void interop_rand_access_constraints(
220 Iterator1 const& i1, Iterator2 const& i2,
221 ndnboost::random_access_traversal_tag, ndnboost::random_access_traversal_tag)
222 {
223 bool b;
224 typename ndnboost::detail::iterator_traits<Iterator2>::difference_type n;
225 b = i1 < i2;
226 b = i1 <= i2;
227 b = i1 > i2;
228 b = i1 >= i2;
229 n = i1 - i2;
230
231 b = i2 < i1;
232 b = i2 <= i1;
233 b = i2 > i1;
234 b = i2 >= i1;
235 n = i2 - i1;
236 ndnboost::ignore_unused_variable_warning(b);
237 ndnboost::ignore_unused_variable_warning(n);
238 }
239
240 template <typename Iterator1, typename Iterator2>
241 void interop_rand_access_constraints(
242 Iterator1 const&, Iterator2 const&,
243 ndnboost::single_pass_traversal_tag, ndnboost::single_pass_traversal_tag)
244 { }
245
246 } // namespace detail
247
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700248 NDNBOOST_concept(InteroperableIterator,(Iterator)(ConstIterator))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700249 {
250 private:
251 typedef typename ndnboost::detail::pure_traversal_tag<
252 typename ndnboost::iterator_traversal<
253 Iterator
254 >::type
255 >::type traversal_category;
256
257 typedef typename ndnboost::detail::pure_traversal_tag<
258 typename ndnboost::iterator_traversal<
259 ConstIterator
260 >::type
261 >::type const_traversal_category;
262
263 public:
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700264 NDNBOOST_CONCEPT_ASSERT((SinglePassIterator<Iterator>));
265 NDNBOOST_CONCEPT_ASSERT((SinglePassIterator<ConstIterator>));
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700266
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700267 NDNBOOST_CONCEPT_USAGE(InteroperableIterator)
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700268 {
269 detail::interop_single_pass_constraints(i, ci);
270 detail::interop_rand_access_constraints(i, ci, traversal_category(), const_traversal_category());
271
272 ci = i;
273 }
274
275 private:
276 Iterator i;
277 ConstIterator ci;
278 };
279
280} // namespace ndnboost_concepts
281
282#include <ndnboost/concept/detail/concept_undef.hpp>
283
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700284#endif // NDNBOOST_ITERATOR_CONCEPTS_HPP