istream_iterator.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef BOOST_ARCHIVE_ITERATORS_ISTREAM_ITERATOR_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_ISTREAM_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. // istream_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. // note: this is a custom version of the standard istream_iterator.
  15. // This is necessary as the standard version doesn't work as expected
  16. // for wchar_t based streams on systems for which wchar_t not a true
  17. // type but rather a synonym for some integer type.
  18. #include <cstddef> // NULL
  19. #include <istream>
  20. #include <boost/iterator/iterator_facade.hpp>
  21. namespace boost {
  22. namespace archive {
  23. namespace iterators {
  24. // given a type, make an input iterator based on a pointer to that type
  25. template<class Elem = char>
  26. class istream_iterator :
  27. public boost::iterator_facade<
  28. istream_iterator<Elem>,
  29. Elem,
  30. std::input_iterator_tag,
  31. Elem
  32. >
  33. {
  34. friend class boost::iterator_core_access;
  35. typedef istream_iterator this_t ;
  36. typedef BOOST_DEDUCED_TYPENAME boost::iterator_facade<
  37. istream_iterator<Elem>,
  38. Elem,
  39. std::input_iterator_tag,
  40. Elem
  41. > super_t;
  42. typedef BOOST_DEDUCED_TYPENAME std::basic_istream<Elem> istream_type;
  43. bool equal(const this_t & rhs) const {
  44. // note: only works for comparison against end of stream
  45. return m_istream == rhs.m_istream;
  46. }
  47. /*
  48. //Access the value referred to
  49. Elem dereference() const {
  50. return m_current_value;
  51. }
  52. void increment(){
  53. if(NULL != m_istream){
  54. m_current_value = static_cast<Elem>(m_istream->get());
  55. if(! m_istream->good()){
  56. const_cast<this_t *>(this)->m_istream = NULL;
  57. }
  58. }
  59. }
  60. */
  61. //Access the value referred to
  62. Elem dereference() const {
  63. return m_istream->peek();
  64. }
  65. void increment(){
  66. if(NULL != m_istream){
  67. m_istream->ignore(1);
  68. }
  69. }
  70. istream_type *m_istream;
  71. Elem m_current_value;
  72. public:
  73. istream_iterator(istream_type & is) :
  74. m_istream(& is)
  75. {
  76. //increment();
  77. }
  78. istream_iterator() :
  79. m_istream(NULL)
  80. {}
  81. istream_iterator(const istream_iterator<Elem> & rhs) :
  82. m_istream(rhs.m_istream),
  83. m_current_value(rhs.m_current_value)
  84. {}
  85. };
  86. } // namespace iterators
  87. } // namespace archive
  88. } // namespace boost
  89. #endif // BOOST_ARCHIVE_ITERATORS_ISTREAM_ITERATOR_HPP