head_iterator.hpp 2.1 KB

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