input_iterator_facade.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. //!@file
  8. //! Input iterator facade
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_UTILS_INPUT_ITERATOR_FACADE_HPP
  11. #define BOOST_TEST_UTILS_INPUT_ITERATOR_FACADE_HPP
  12. // Boost
  13. #include <boost/iterator/iterator_facade.hpp>
  14. #include <boost/test/detail/suppress_warnings.hpp>
  15. //____________________________________________________________________________//
  16. namespace boost {
  17. namespace unit_test {
  18. namespace utils {
  19. // ************************************************************************** //
  20. // ************** input_iterator_core_access ************** //
  21. // ************************************************************************** //
  22. class input_iterator_core_access
  23. {
  24. #if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
  25. public:
  26. #else
  27. template <class I, class V, class R, class TC> friend class input_iterator_facade;
  28. #endif
  29. template <class Facade>
  30. static bool get( Facade& f )
  31. {
  32. return f.get();
  33. }
  34. private:
  35. // objects of this class are useless
  36. input_iterator_core_access(); //undefined
  37. };
  38. // ************************************************************************** //
  39. // ************** input_iterator_facade ************** //
  40. // ************************************************************************** //
  41. template<typename Derived,
  42. typename ValueType,
  43. typename Reference = ValueType const&,
  44. typename Traversal = single_pass_traversal_tag>
  45. class input_iterator_facade : public iterator_facade<Derived,ValueType,Traversal,Reference>
  46. {
  47. public:
  48. // Constructor
  49. input_iterator_facade() : m_valid( false ), m_value() {}
  50. protected: // provide access to the Derived
  51. void init()
  52. {
  53. m_valid = true;
  54. increment();
  55. }
  56. // Data members
  57. mutable bool m_valid;
  58. ValueType m_value;
  59. private:
  60. friend class boost::iterator_core_access;
  61. // iterator facade interface implementation
  62. void increment()
  63. {
  64. // we make post-end incrementation indefinetly safe
  65. if( m_valid )
  66. m_valid = input_iterator_core_access::get( *static_cast<Derived*>(this) );
  67. }
  68. Reference dereference() const
  69. {
  70. return m_value;
  71. }
  72. // iterator facade interface implementation
  73. bool equal( input_iterator_facade const& rhs ) const
  74. {
  75. // two invalid iterator equals, inequal otherwise
  76. return !m_valid && !rhs.m_valid;
  77. }
  78. };
  79. } // namespace utils
  80. } // namespace unit_test
  81. } // namespace boost
  82. //____________________________________________________________________________//
  83. #include <boost/test/detail/enable_warnings.hpp>
  84. #endif // BOOST_TEST_UTILS_INPUT_ITERATOR_FACADE_HPP