1 // (C) Copyright David Abrahams 2002.
2 // (C) Copyright Jeremy Siek 2002.
3 // (C) Copyright Thomas Witt 2002.
4 // Permission to copy, use, modify,
5 // sell and distribute this software is granted provided this
6 // copyright notice appears in all copies. This software is provided
7 // "as is" without express or implied warranty, and with no claim as
8 // to its suitability for any purpose.
9 #ifndef BOOST_FILTER_ITERATOR_23022003THW_HPP
10 #define BOOST_FILTER_ITERATOR_23022003THW_HPP
12 #include <boost/iterator.hpp>
13 #include <boost/iterator/iterator_adaptor.hpp>
14 #include <boost/iterator/iterator_categories.hpp>
16 #include <boost/type_traits/is_class.hpp>
17 #include <boost/static_assert.hpp>
21 template <class Predicate, class Iterator>
22 class filter_iterator;
26 template <class Predicate, class Iterator>
27 struct filter_iterator_base
29 typedef iterator_adaptor<
30 filter_iterator<Predicate, Iterator>
35 typename iterator_traversal<Iterator>::type
36 , bidirectional_traversal_tag
38 , forward_traversal_tag
45 template <class Predicate, class Iterator>
47 : public detail::filter_iterator_base<Predicate, Iterator>::type
49 typedef typename detail::filter_iterator_base<
53 friend class iterator_core_access;
58 filter_iterator(Predicate f, Iterator x, Iterator end = Iterator())
59 : super_t(x), m_predicate(f), m_end(end)
64 filter_iterator(Iterator x, Iterator end = Iterator())
65 : super_t(x), m_predicate(), m_end(end)
67 // Pro8 is a little too aggressive about instantiating the
68 // body of this function.
69 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
70 // Don't allow use of this constructor if Predicate is a
71 // function pointer type, since it will be 0.
72 BOOST_STATIC_ASSERT(is_class<Predicate>::value);
77 template<class OtherIterator>
79 filter_iterator<Predicate, OtherIterator> const& t
80 , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
82 : super_t(t.base()), m_predicate(t.predicate()), m_end(t.end()) {}
84 Predicate predicate() const { return m_predicate; }
86 Iterator end() const { return m_end; }
91 ++(this->base_reference());
97 while(!this->m_predicate(*--(this->base_reference()))){};
100 void satisfy_predicate()
102 while (this->base() != this->m_end && !this->m_predicate(*this->base()))
103 ++(this->base_reference());
106 // Probably should be the initial base class so it can be
107 // optimized away via EBO if it is an empty class.
108 Predicate m_predicate;
112 template <class Predicate, class Iterator>
113 filter_iterator<Predicate,Iterator>
114 make_filter_iterator(Predicate f, Iterator x, Iterator end = Iterator())
116 return filter_iterator<Predicate,Iterator>(f,x,end);
119 template <class Predicate, class Iterator>
120 filter_iterator<Predicate,Iterator>
121 make_filter_iterator(
122 typename iterators::enable_if<
126 , Iterator end = Iterator()
127 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
132 return filter_iterator<Predicate,Iterator>(x,end);
137 #endif // BOOST_FILTER_ITERATOR_23022003THW_HPP