generator_iterator.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // (C) Copyright Jens Maurer 2001.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // Revision History:
  7. // 15 Nov 2001 Jens Maurer
  8. // created.
  9. // See http://www.boost.org/libs/utility/iterator_adaptors.htm for documentation.
  10. #ifndef BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP
  11. #define BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP
  12. #include <boost/iterator/iterator_facade.hpp>
  13. #include <boost/ref.hpp>
  14. namespace boost {
  15. namespace iterators {
  16. template<class Generator>
  17. class generator_iterator
  18. : public iterator_facade<
  19. generator_iterator<Generator>
  20. , typename Generator::result_type
  21. , single_pass_traversal_tag
  22. , typename Generator::result_type const&
  23. >
  24. {
  25. typedef iterator_facade<
  26. generator_iterator<Generator>
  27. , typename Generator::result_type
  28. , single_pass_traversal_tag
  29. , typename Generator::result_type const&
  30. > super_t;
  31. public:
  32. generator_iterator() {}
  33. generator_iterator(Generator* g) : m_g(g), m_value((*m_g)()) {}
  34. void increment()
  35. {
  36. m_value = (*m_g)();
  37. }
  38. const typename Generator::result_type&
  39. dereference() const
  40. {
  41. return m_value;
  42. }
  43. bool equal(generator_iterator const& y) const
  44. {
  45. return this->m_g == y.m_g && this->m_value == y.m_value;
  46. }
  47. private:
  48. Generator* m_g;
  49. typename Generator::result_type m_value;
  50. };
  51. template<class Generator>
  52. struct generator_iterator_generator
  53. {
  54. typedef generator_iterator<Generator> type;
  55. };
  56. template <class Generator>
  57. inline generator_iterator<Generator>
  58. make_generator_iterator(Generator & gen)
  59. {
  60. typedef generator_iterator<Generator> result_t;
  61. return result_t(&gen);
  62. }
  63. } // namespace iterators
  64. using iterators::generator_iterator;
  65. using iterators::generator_iterator_generator;
  66. using iterators::make_generator_iterator;
  67. } // namespace boost
  68. #endif // BOOST_ITERATOR_ADAPTOR_GENERATOR_ITERATOR_HPP