1 // Copyright (c) 2001 Daniel C. Nuffer
2 // Copyright (c) 2001-2011 Hartmut Kaiser
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #if !defined(BOOST_SPIRIT_ISTREAM_POLICY_JAN_04_2010_0130PM)
8 #define BOOST_SPIRIT_ISTREAM_POLICY_JAN_04_2010_0130PM
10 #include <boost/spirit/home/support/iterators/multi_pass_fwd.hpp>
11 #include <boost/spirit/home/support/iterators/detail/multi_pass.hpp>
13 namespace boost { namespace spirit { namespace iterator_policies
15 ///////////////////////////////////////////////////////////////////////////
17 // Implementation of the InputPolicy used by multi_pass
19 // The istream encapsulates an std::basic_istream
20 ///////////////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////////
25 class unique // : public detail::default_input_policy
28 typedef typename T::char_type result_type;
31 typedef typename T::off_type difference_type;
32 typedef typename T::off_type distance_type;
33 typedef result_type const* pointer;
34 typedef result_type const& reference;
35 typedef result_type value_type;
39 explicit unique(T&) {}
44 template <typename MultiPass>
45 static void destroy(MultiPass&) {}
47 template <typename MultiPass>
48 static typename MultiPass::reference get_input(MultiPass& mp)
50 if (!mp.shared()->initialized_)
51 mp.shared()->read_one();
52 return mp.shared()->curtok_;
55 template <typename MultiPass>
56 static void advance_input(MultiPass& mp)
58 // We invalidate the currently cached input character to avoid
59 // reading more input from the underlying iterator than
60 // required. Without this we would always read ahead one
61 // character, even if this character never gets consumed by the
63 mp.shared()->peek_one();
66 // test, whether we reached the end of the underlying stream
67 template <typename MultiPass>
68 static bool input_at_eof(MultiPass const& mp)
70 return mp.shared()->eof_reached_;
73 template <typename MultiPass>
74 static bool input_is_valid(MultiPass const& mp, value_type const&)
76 return mp.shared()->initialized_;
79 // no unique data elements
82 ///////////////////////////////////////////////////////////////////////
87 typedef typename T::char_type result_type;
90 explicit shared(T& input)
91 : input_(input), curtok_(-1)
92 , initialized_(false), eof_reached_(false)
94 peek_one(); // istreams may be at eof right in the beginning
99 if (!(input_ >> curtok_)) {
100 initialized_ = false;
110 input_.peek(); // try for eof
111 initialized_ = false;
112 eof_reached_ = input_.eof();