1 // (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)
6 #ifndef BOOST_ITERATOR_ARCHETYPES_HPP
7 #define BOOST_ITERATOR_ARCHETYPES_HPP
9 #include <boost/iterator/iterator_categories.hpp>
10 #include <boost/operators.hpp>
11 #include <boost/static_assert.hpp>
12 #include <boost/iterator.hpp>
14 #include <boost/iterator/detail/facade_iterator_category.hpp>
16 #include <boost/type_traits/is_const.hpp>
17 #include <boost/type_traits/add_const.hpp>
18 #include <boost/type_traits/remove_const.hpp>
19 #include <boost/type_traits/remove_cv.hpp>
21 #include <boost/concept_archetype.hpp>
23 #include <boost/mpl/bitand.hpp>
24 #include <boost/mpl/int.hpp>
25 #include <boost/mpl/equal_to.hpp>
26 #include <boost/mpl/if.hpp>
27 #include <boost/mpl/eval_if.hpp>
28 #include <boost/mpl/and.hpp>
29 #include <boost/mpl/identity.hpp>
36 template <class Value, class AccessCategory>
37 struct access_archetype;
39 template <class Derived, class Value, class AccessCategory, class TraversalCategory>
40 struct traversal_archetype;
45 readable_iterator_bit = 1
46 , writable_iterator_bit = 2
47 , swappable_iterator_bit = 4
48 , lvalue_iterator_bit = 8
51 // Not quite tags, since dispatching wouldn't work.
52 typedef mpl::int_<readable_iterator_bit>::type readable_iterator_t;
53 typedef mpl::int_<writable_iterator_bit>::type writable_iterator_t;
56 (readable_iterator_bit|writable_iterator_bit)
57 >::type readable_writable_iterator_t;
60 (readable_iterator_bit|lvalue_iterator_bit)
61 >::type readable_lvalue_iterator_t;
64 (lvalue_iterator_bit|writable_iterator_bit)
65 >::type writable_lvalue_iterator_t;
67 typedef mpl::int_<swappable_iterator_bit>::type swappable_iterator_t;
68 typedef mpl::int_<lvalue_iterator_bit>::type lvalue_iterator_t;
70 template <class Derived, class Base>
73 mpl::bitand_<Derived,Base>
84 assign_proxy& operator=(T) { return *this; }
90 operator T() { return static_object<T>::get(); }
94 struct read_write_proxy
95 : read_proxy<T> // Use to inherit from assign_proxy, but that doesn't work. -JGS
97 read_write_proxy& operator=(T) { return *this; }
103 T const* operator->() const { return 0; }
106 struct no_operator_brackets {};
108 template <class ValueType>
109 struct readable_operator_brackets
111 read_proxy<ValueType> operator[](std::ptrdiff_t n) const { return read_proxy<ValueType>(); }
114 template <class ValueType>
115 struct writable_operator_brackets
117 read_write_proxy<ValueType> operator[](std::ptrdiff_t n) const { return read_write_proxy<ValueType>(); }
120 template <class Value, class AccessCategory, class TraversalCategory>
121 struct operator_brackets
123 is_convertible<TraversalCategory, random_access_traversal_tag>
125 archetypes::has_access<
127 , archetypes::writable_iterator_t
129 , mpl::identity<writable_operator_brackets<Value> >
131 archetypes::has_access<
133 , archetypes::readable_iterator_t
135 , readable_operator_brackets<Value>
136 , no_operator_brackets
139 , mpl::identity<no_operator_brackets>
143 template <class TraversalCategory>
144 struct traversal_archetype_impl
146 template <class Derived,class Value> struct archetype;
149 // Constructor argument for those iterators that
150 // are not default constructible
153 template <class Derived, class Value, class TraversalCategory>
154 struct traversal_archetype_
155 : traversal_archetype_impl<TraversalCategory>::template archetype<Derived,Value>
158 traversal_archetype_impl<TraversalCategory>::template archetype<Derived,Value>
161 traversal_archetype_() {}
163 traversal_archetype_(ctor_arg arg)
169 struct traversal_archetype_impl<incrementable_traversal_tag>
171 template<class Derived, class Value>
174 explicit archetype(ctor_arg) {}
176 struct bogus { }; // This use to be void, but that causes trouble for iterator_facade. Need more research. -JGS
177 typedef bogus difference_type;
179 Derived& operator++() { return (Derived&)static_object<Derived>::get(); }
180 Derived operator++(int) const { return (Derived&)static_object<Derived>::get(); }
185 struct traversal_archetype_impl<single_pass_traversal_tag>
187 template<class Derived, class Value>
189 : public equality_comparable< traversal_archetype_<Derived, Value, single_pass_traversal_tag> >,
190 public traversal_archetype_<Derived, Value, incrementable_traversal_tag>
192 explicit archetype(ctor_arg arg)
193 : traversal_archetype_<Derived, Value, incrementable_traversal_tag>(arg)
196 typedef std::ptrdiff_t difference_type;
200 template <class Derived, class Value>
201 bool operator==(traversal_archetype_<Derived, Value, single_pass_traversal_tag> const&,
202 traversal_archetype_<Derived, Value, single_pass_traversal_tag> const&) { return true; }
205 struct traversal_archetype_impl<forward_traversal_tag>
207 template<class Derived, class Value>
209 : public traversal_archetype_<Derived, Value, single_pass_traversal_tag>
212 : traversal_archetype_<Derived, Value, single_pass_traversal_tag>(ctor_arg())
218 struct traversal_archetype_impl<bidirectional_traversal_tag>
220 template<class Derived, class Value>
222 : public traversal_archetype_<Derived, Value, forward_traversal_tag>
224 Derived& operator--() { return static_object<Derived>::get(); }
225 Derived operator--(int) const { return static_object<Derived>::get(); }
230 struct traversal_archetype_impl<random_access_traversal_tag>
232 template<class Derived, class Value>
234 : public traversal_archetype_<Derived, Value, bidirectional_traversal_tag>
236 Derived& operator+=(std::ptrdiff_t) { return static_object<Derived>::get(); }
237 Derived& operator-=(std::ptrdiff_t) { return static_object<Derived>::get(); }
241 template <class Derived, class Value>
242 Derived& operator+(traversal_archetype_<Derived, Value, random_access_traversal_tag> const&,
243 std::ptrdiff_t) { return static_object<Derived>::get(); }
245 template <class Derived, class Value>
246 Derived& operator+(std::ptrdiff_t,
247 traversal_archetype_<Derived, Value, random_access_traversal_tag> const&)
248 { return static_object<Derived>::get(); }
250 template <class Derived, class Value>
251 Derived& operator-(traversal_archetype_<Derived, Value, random_access_traversal_tag> const&,
253 { return static_object<Derived>::get(); }
255 template <class Derived, class Value>
256 std::ptrdiff_t operator-(traversal_archetype_<Derived, Value, random_access_traversal_tag> const&,
257 traversal_archetype_<Derived, Value, random_access_traversal_tag> const&)
260 template <class Derived, class Value>
261 bool operator<(traversal_archetype_<Derived, Value, random_access_traversal_tag> const&,
262 traversal_archetype_<Derived, Value, random_access_traversal_tag> const&)
265 template <class Derived, class Value>
266 bool operator>(traversal_archetype_<Derived, Value, random_access_traversal_tag> const&,
267 traversal_archetype_<Derived, Value, random_access_traversal_tag> const&)
270 template <class Derived, class Value>
271 bool operator<=(traversal_archetype_<Derived, Value, random_access_traversal_tag> const&,
272 traversal_archetype_<Derived, Value, random_access_traversal_tag> const&)
275 template <class Derived, class Value>
276 bool operator>=(traversal_archetype_<Derived, Value, random_access_traversal_tag> const&,
277 traversal_archetype_<Derived, Value, random_access_traversal_tag> const&)
282 template <class Value>
283 struct convertible_type
284 : mpl::if_< is_const<Value>,
285 typename remove_const<Value>::type,
289 } // namespace detail
292 template <class> struct undefined;
294 template <class AccessCategory>
295 struct iterator_access_archetype_impl
297 template <class Value> struct archetype;
300 template <class Value, class AccessCategory>
301 struct iterator_access_archetype
302 : iterator_access_archetype_impl<
304 >::template archetype<Value>
309 struct iterator_access_archetype_impl<
310 archetypes::readable_iterator_t
313 template <class Value>
316 typedef typename remove_cv<Value>::type value_type;
317 typedef Value reference;
318 typedef Value* pointer;
320 value_type operator*() const { return static_object<value_type>::get(); }
322 detail::arrow_proxy<Value> operator->() const { return detail::arrow_proxy<Value>(); }
327 struct iterator_access_archetype_impl<
328 archetypes::writable_iterator_t
331 template <class Value>
334 BOOST_STATIC_ASSERT(!is_const<Value>::value);
335 typedef void value_type;
336 typedef void reference;
337 typedef void pointer;
339 detail::assign_proxy<Value> operator*() const { return detail::assign_proxy<Value>(); }
344 struct iterator_access_archetype_impl<
345 archetypes::readable_writable_iterator_t
348 template <class Value>
350 : public virtual iterator_access_archetype<
351 Value, archetypes::readable_iterator_t
354 typedef detail::read_write_proxy<Value> reference;
356 detail::read_write_proxy<Value> operator*() const { return detail::read_write_proxy<Value>(); }
361 struct iterator_access_archetype_impl<archetypes::readable_lvalue_iterator_t>
363 template <class Value>
365 : public virtual iterator_access_archetype<
366 Value, archetypes::readable_iterator_t
369 typedef Value& reference;
371 Value& operator*() const { return static_object<Value>::get(); }
372 Value* operator->() const { return 0; }
377 struct iterator_access_archetype_impl<archetypes::writable_lvalue_iterator_t>
379 template <class Value>
381 : public virtual iterator_access_archetype<
382 Value, archetypes::readable_lvalue_iterator_t
385 BOOST_STATIC_ASSERT((!is_const<Value>::value));
390 template <class Value, class AccessCategory, class TraversalCategory>
391 struct iterator_archetype;
393 template <class Value, class AccessCategory, class TraversalCategory>
394 struct traversal_archetype_base
395 : detail::operator_brackets<
396 typename remove_cv<Value>::type
400 , detail::traversal_archetype_<
401 iterator_archetype<Value, AccessCategory, TraversalCategory>
410 template <class Value, class AccessCategory, class TraversalCategory>
411 struct iterator_archetype_base
412 : iterator_access_archetype<Value, AccessCategory>
413 , traversal_archetype_base<Value, AccessCategory, TraversalCategory>
415 typedef iterator_access_archetype<Value, AccessCategory> access;
417 typedef typename detail::facade_iterator_category<
419 , typename mpl::eval_if<
420 archetypes::has_access<
421 AccessCategory, archetypes::writable_iterator_t
423 , remove_const<Value>
426 , typename access::reference
427 >::type iterator_category;
429 // Needed for some broken libraries (see below)
430 typedef boost::iterator<
433 , typename traversal_archetype_base<
434 Value, AccessCategory, TraversalCategory
436 , typename access::pointer
437 , typename access::reference
438 > workaround_iterator_base;
442 template <class Value, class AccessCategory, class TraversalCategory>
443 struct iterator_archetype
444 : public detail::iterator_archetype_base<Value, AccessCategory, TraversalCategory>
446 // These broken libraries require derivation from std::iterator
447 // (or related magic) in order to handle iter_swap and other
448 // iterator operations
449 # if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, < 310) \
450 || BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(0x20101))
451 , public detail::iterator_archetype_base<
452 Value, AccessCategory, TraversalCategory
453 >::workaround_iterator_base
456 // Derivation from std::iterator above caused references to nested
457 // types to be ambiguous, so now we have to redeclare them all
459 # if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, < 310) \
460 || BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(0x20101))
462 typedef detail::iterator_archetype_base<
463 Value,AccessCategory,TraversalCategory
466 typedef typename base::value_type value_type;
467 typedef typename base::reference reference;
468 typedef typename base::pointer pointer;
469 typedef typename base::difference_type difference_type;
470 typedef typename base::iterator_category iterator_category;
473 iterator_archetype() { }
474 iterator_archetype(iterator_archetype const& x)
475 : detail::iterator_archetype_base<
482 iterator_archetype& operator=(iterator_archetype const&)
486 // Optional conversion from mutable
489 typename detail::convertible_type<Value>::type
491 , TraversalCategory> const&
496 } // namespace iterators
498 // Backward compatibility names
499 namespace iterator_archetypes = iterators::archetypes;
500 using iterators::access_archetype;
501 using iterators::traversal_archetype;
502 using iterators::iterator_archetype;
503 using iterators::undefined;
504 using iterators::iterator_access_archetype_impl;
505 using iterators::traversal_archetype_base;
509 #endif // BOOST_ITERATOR_ARCHETYPES_HPP