head_iterator.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/type_traits/is_same.hpp>
  15. #include <boost/iterator/iterator_adaptor.hpp>
  16. #include <boost/iterator/iterator_traits.hpp>
  17. namespace boost {
  18. namespace archive {
  19. namespace iterators {
  20. template<class Predicate, class Base>
  21. class head_iterator
  22. : public boost::iterator_adaptor<
  23. head_iterator<Predicate, Base>,
  24. Base,
  25. use_default,
  26. single_pass_traversal_tag
  27. >
  28. {
  29. private:
  30. friend class iterator_core_access;
  31. typedef boost::iterator_adaptor<
  32. head_iterator<Predicate, Base>,
  33. Base,
  34. use_default,
  35. single_pass_traversal_tag
  36. > super_t;
  37. typedef head_iterator<Predicate, Base> this_t;
  38. typedef BOOST_DEDUCED_TYPENAME super_t::value_type value_type;
  39. typedef BOOST_DEDUCED_TYPENAME super_t::reference reference_type;
  40. reference_type dereference_impl(){
  41. if(! m_end){
  42. while(! m_predicate(* this->base_reference()))
  43. ++ this->base_reference();
  44. m_end = true;
  45. }
  46. return * this->base_reference();
  47. }
  48. reference_type dereference() const {
  49. return const_cast<this_t *>(this)->dereference_impl();
  50. }
  51. void increment(){
  52. ++base_reference();
  53. }
  54. Predicate m_predicate;
  55. bool m_end;
  56. public:
  57. template<class T>
  58. head_iterator(Predicate f, T start) :
  59. super_t(Base(start)),
  60. m_predicate(f),
  61. m_end(false)
  62. {}
  63. };
  64. } // namespace iterators
  65. } // namespace archive
  66. } // namespace boost
  67. #endif // BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP