source: NonGTP/Boost/boost/archive/iterators/istream_iterator.hpp @ 857

Revision 857, 2.7 KB checked in by igarcia, 19 years ago (diff)
Line 
1#ifndef BOOST_ARCHIVE_ITERATORS_ISTREAM_ITERATOR_HPP
2#define BOOST_ARCHIVE_ITERATORS_ISTREAM_ITERATOR_HPP
3
4// MS compatible compilers support #pragma once
5#if defined(_MSC_VER) && (_MSC_VER >= 1020)
6# pragma once
7#endif
8
9/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10// istream_iterator.hpp
11
12// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
13// Use, modification and distribution is subject to the Boost Software
14// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15// http://www.boost.org/LICENSE_1_0.txt)
16
17//  See http://www.boost.org for updates, documentation, and revision history.
18
19// note: this is a custom version of the standard istream_iterator.
20// This is necessary as the standard version doesn't work as expected
21// for wchar_t based streams on systems for which wchar_t not a true
22// type but rather a synonym for some integer type.
23
24#include <istream>
25#include <boost/iterator/iterator_facade.hpp>
26
27namespace boost {
28namespace archive {
29namespace iterators {
30
31// given a type, make an input iterator based on a pointer to that type
32template<class Elem = char>
33class istream_iterator : 
34    public boost::iterator_facade<
35        istream_iterator<Elem>,
36        Elem,
37        std::input_iterator_tag,
38        Elem
39    >
40{
41    friend class boost::iterator_core_access;
42    typedef istream_iterator this_t ;
43    typedef BOOST_DEDUCED_TYPENAME boost::iterator_facade<
44        istream_iterator<Elem>,
45        Elem,
46        std::input_iterator_tag,
47        Elem
48    > super_t;
49    typedef BOOST_DEDUCED_TYPENAME std::basic_istream<Elem> istream_type;
50 
51    //Access the value referred to
52    Elem dereference() const {
53        return m_current_value;
54    }
55
56    bool equal(const this_t & rhs) const {
57        // note: only  works for comparison against end of stream
58        return m_istream == rhs.m_istream;
59    }
60
61    void increment(){
62        if(NULL != m_istream){
63            m_current_value = m_istream->get();
64            if(! m_istream->good()){
65                const_cast<this_t *>(this)->m_istream = NULL;
66            }
67        }
68    }
69
70    istream_type *m_istream;
71    Elem m_current_value;
72public:
73    istream_iterator(istream_type & is) :
74        m_istream(& is)
75    {
76        increment();
77    }
78
79    istream_iterator() :
80        m_istream(NULL)
81    {}
82
83    istream_iterator(const istream_iterator<Elem> & rhs) :
84        m_istream(rhs.m_istream),
85        m_current_value(rhs.m_current_value)
86    {}
87
88};
89
90} // namespace iterators
91} // namespace archive
92} // namespace boost
93
94#endif // BOOST_ARCHIVE_ITERATORS_ISTREAM_ITERATOR_HPP
Note: See TracBrowser for help on using the repository browser.