1 // Copyright (c) 2001 Daniel C. Nuffer 2 // Copyright (c) 2001-2011 Hartmut Kaiser 3 // 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) 6 7 #if !defined(BOOST_SPIRIT_ISTREAM_POLICY_JAN_04_2010_0130PM) 8 #define BOOST_SPIRIT_ISTREAM_POLICY_JAN_04_2010_0130PM 9 10 #include <boost/spirit/home/support/iterators/multi_pass_fwd.hpp> 11 #include <boost/spirit/home/support/iterators/detail/multi_pass.hpp> 12 13 namespace boost { namespace spirit { namespace iterator_policies 14 { 15 /////////////////////////////////////////////////////////////////////////// 16 // class istream 17 // Implementation of the InputPolicy used by multi_pass 18 // 19 // The istream encapsulates an std::basic_istream 20 /////////////////////////////////////////////////////////////////////////// 21 struct istream 22 { 23 /////////////////////////////////////////////////////////////////////// 24 template <typename T> 25 class unique // : public detail::default_input_policy 26 { 27 private: 28 typedef typename T::char_type result_type; 29 30 public: 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; 36 37 protected: unique()38 unique() {} unique(T &)39 explicit unique(T&) {} 40 swap(unique &)41 void swap(unique&) {} 42 43 public: 44 template <typename MultiPass> destroy(MultiPass &)45 static void destroy(MultiPass&) {} 46 47 template <typename MultiPass> get_input(MultiPass & mp)48 static typename MultiPass::reference get_input(MultiPass& mp) 49 { 50 if (!mp.shared()->initialized_) 51 mp.shared()->read_one(); 52 return mp.shared()->curtok_; 53 } 54 55 template <typename MultiPass> advance_input(MultiPass & mp)56 static void advance_input(MultiPass& mp) 57 { 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 62 // client. 63 mp.shared()->peek_one(); 64 } 65 66 // test, whether we reached the end of the underlying stream 67 template <typename MultiPass> input_at_eof(MultiPass const & mp)68 static bool input_at_eof(MultiPass const& mp) 69 { 70 return mp.shared()->eof_reached_; 71 } 72 73 template <typename MultiPass> input_is_valid(MultiPass const & mp,value_type const &)74 static bool input_is_valid(MultiPass const& mp, value_type const&) 75 { 76 return mp.shared()->initialized_; 77 } 78 79 // no unique data elements 80 }; 81 82 /////////////////////////////////////////////////////////////////////// 83 template <typename T> 84 struct shared 85 { 86 private: 87 typedef typename T::char_type result_type; 88 89 public: sharedboost::spirit::iterator_policies::istream::shared90 explicit shared(T& input) 91 : input_(input), curtok_(-1) 92 , initialized_(false), eof_reached_(false) 93 {} 94 read_oneboost::spirit::iterator_policies::istream::shared95 void read_one() 96 { 97 if (!(input_ >> curtok_)) { 98 initialized_ = false; 99 eof_reached_ = true; 100 } 101 else { 102 initialized_ = true; 103 } 104 } 105 peek_oneboost::spirit::iterator_policies::istream::shared106 void peek_one() 107 { 108 input_.peek(); // try for eof 109 initialized_ = false; 110 eof_reached_ = input_.eof(); 111 } 112 113 T& input_; 114 result_type curtok_; 115 bool initialized_; 116 bool eof_reached_; 117 }; 118 }; 119 120 }}} 121 122 #endif 123