istream_iterator.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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)
  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 typename boost::iterator_facade<
  37. istream_iterator<Elem>,
  38. Elem,
  39. std::input_iterator_tag,
  40. Elem
  41. > super_t;
  42. typedef 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. //Access the value referred to
  48. Elem dereference() const {
  49. return static_cast<Elem>(m_istream->peek());
  50. }
  51. void increment(){
  52. if(NULL != m_istream){
  53. m_istream->ignore(1);
  54. }
  55. }
  56. istream_type *m_istream;
  57. Elem m_current_value;
  58. public:
  59. istream_iterator(istream_type & is) :
  60. m_istream(& is)
  61. {
  62. //increment();
  63. }
  64. istream_iterator() :
  65. m_istream(NULL),
  66. m_current_value(NULL)
  67. {}
  68. istream_iterator(const istream_iterator<Elem> & rhs) :
  69. m_istream(rhs.m_istream),
  70. m_current_value(rhs.m_current_value)
  71. {}
  72. };
  73. } // namespace iterators
  74. } // namespace archive
  75. } // namespace boost
  76. #endif // BOOST_ARCHIVE_ITERATORS_ISTREAM_ITERATOR_HPP