head_iterator.hpp 2.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#ifndef BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP
#define BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP

// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// head_iterator.hpp

// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org for updates, documentation, and revision history.

#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/iterator/iterator_traits.hpp>

namespace boost {
namespace archive {
namespace iterators {

template<class Predicate, class Base>
class head_iterator
    : public boost::iterator_adaptor<
        head_iterator<Predicate, Base>,
        Base,
        use_default,
        single_pass_traversal_tag
    >
{
private:
    friend class iterator_core_access;
    typedef boost::iterator_adaptor<
        head_iterator<Predicate, Base>,
        Base,
        use_default,
        single_pass_traversal_tag
    > super_t;

    typedef head_iterator<Predicate, Base> this_t;
    typedef super_t::value_type value_type;
    typedef super_t::reference reference_type;

    reference_type dereference_impl(){
        if(! m_end){
            while(! m_predicate(* this->base_reference()))
                ++ this->base_reference();
            m_end = true;
        }
        return * this->base_reference();
    }

    reference_type dereference() const {
        return const_cast<this_t *>(this)->dereference_impl();
    }

    void increment(){
        ++base_reference();
    }
    Predicate m_predicate;
    bool m_end;
public:
    template<class T>
    head_iterator(Predicate f, T start) : 
        super_t(Base(start)), 
        m_predicate(f),
        m_end(false)
    {}

};

} // namespace iterators
} // namespace archive
} // namespace boost

#endif // BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP