6ad7d6b32bd94ba0e3178066e9a9298a9e726009
[platform/upstream/boost.git] / boost / archive / iterators / head_iterator.hpp
1 #ifndef BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP
2 #define BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP
3
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER)
6 # pragma once
7 #endif
8
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // head_iterator.hpp
11
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
16
17 //  See http://www.boost.org for updates, documentation, and revision history.
18
19 #include <boost/iterator/iterator_adaptor.hpp>
20 #include <boost/iterator/iterator_traits.hpp>
21
22 namespace boost {
23 namespace archive {
24 namespace iterators {
25
26 template<class Predicate, class Base>
27 class head_iterator
28     : public boost::iterator_adaptor<
29         head_iterator<Predicate, Base>,
30         Base,
31         use_default,
32         single_pass_traversal_tag
33     >
34 {
35 private:
36     friend class iterator_core_access;
37     typedef boost::iterator_adaptor<
38         head_iterator<Predicate, Base>,
39         Base,
40         use_default,
41         single_pass_traversal_tag
42     > super_t;
43
44     typedef head_iterator<Predicate, Base> this_t;
45     typedef super_t::value_type value_type;
46     typedef super_t::reference reference_type;
47
48     reference_type dereference_impl(){
49         if(! m_end){
50             while(! m_predicate(* this->base_reference()))
51                 ++ this->base_reference();
52             m_end = true;
53         }
54         return * this->base_reference();
55     }
56
57     reference_type dereference() const {
58         return const_cast<this_t *>(this)->dereference_impl();
59     }
60
61     void increment(){
62         ++base_reference();
63     }
64     Predicate m_predicate;
65     bool m_end;
66 public:
67     template<class T>
68     head_iterator(Predicate f, T start) : 
69         super_t(Base(start)), 
70         m_predicate(f),
71         m_end(false)
72     {}
73
74 };
75
76 } // namespace iterators
77 } // namespace archive
78 } // namespace boost
79
80 #endif // BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP