Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:31:57

0001 //  Copyright (c) 2001 Daniel C. Nuffer
0002 //  Copyright (c) 2001-2011 Hartmut Kaiser
0003 // 
0004 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #if !defined(BOOST_SPIRIT_ISTREAM_POLICY_JAN_04_2010_0130PM)
0008 #define BOOST_SPIRIT_ISTREAM_POLICY_JAN_04_2010_0130PM
0009 
0010 #include <boost/spirit/home/support/iterators/multi_pass_fwd.hpp>
0011 #include <boost/spirit/home/support/iterators/detail/multi_pass.hpp>
0012 
0013 namespace boost { namespace spirit { namespace iterator_policies
0014 {
0015     ///////////////////////////////////////////////////////////////////////////
0016     //  class istream
0017     //  Implementation of the InputPolicy used by multi_pass
0018     // 
0019     //  The istream encapsulates an std::basic_istream
0020     ///////////////////////////////////////////////////////////////////////////
0021     struct istream
0022     {
0023         ///////////////////////////////////////////////////////////////////////
0024         template <typename T>
0025         class unique // : public detail::default_input_policy
0026         {
0027         private:
0028             typedef typename T::char_type result_type;
0029 
0030         public:
0031             typedef typename T::off_type difference_type;
0032             typedef typename T::off_type distance_type;
0033             typedef result_type const* pointer;
0034             typedef result_type const& reference;
0035             typedef result_type value_type;
0036 
0037         protected:
0038             unique() {}
0039             explicit unique(T&) {}
0040 
0041             void swap(unique&) {}
0042 
0043         public:
0044             template <typename MultiPass>
0045             static void destroy(MultiPass&) {}
0046 
0047             template <typename MultiPass>
0048             static typename MultiPass::reference get_input(MultiPass& mp)
0049             {
0050                 if (!mp.shared()->initialized_)
0051                     mp.shared()->read_one();
0052                 return mp.shared()->curtok_;
0053             }
0054 
0055             template <typename MultiPass>
0056             static void advance_input(MultiPass& mp)
0057             {
0058                 // We invalidate the currently cached input character to avoid
0059                 // reading more input from the underlying iterator than 
0060                 // required. Without this we would always read ahead one 
0061                 // character, even if this character never gets consumed by the 
0062                 // client.
0063                 mp.shared()->peek_one();
0064             }
0065 
0066             // test, whether we reached the end of the underlying stream
0067             template <typename MultiPass>
0068             static bool input_at_eof(MultiPass const& mp) 
0069             {
0070                 return mp.shared()->eof_reached_;
0071             }
0072 
0073             template <typename MultiPass>
0074             static bool input_is_valid(MultiPass const& mp, value_type const&) 
0075             {
0076                 return mp.shared()->initialized_;
0077             }
0078 
0079             // no unique data elements
0080         };
0081 
0082         ///////////////////////////////////////////////////////////////////////
0083         template <typename T>
0084         struct shared
0085         {
0086         private:
0087             typedef typename T::char_type result_type;
0088 
0089         public:
0090             explicit shared(T& input) 
0091               : input_(input), curtok_(-1)
0092               , initialized_(false), eof_reached_(false) 
0093             {
0094                 peek_one();   // istreams may be at eof right in the beginning
0095             }
0096 
0097             void read_one()
0098             {
0099                 if (!(input_ >> curtok_)) {
0100                     initialized_ = false;
0101                     eof_reached_ = true;
0102                 }
0103                 else {
0104                     initialized_ = true;
0105                 }
0106             }
0107 
0108             void peek_one()
0109             {
0110                 input_.peek();    // try for eof
0111                 initialized_ = false;
0112                 eof_reached_ = input_.eof();
0113             }
0114 
0115             T& input_;
0116             result_type curtok_;
0117             bool initialized_;
0118             bool eof_reached_;
0119         };
0120     };
0121 
0122 }}}
0123 
0124 #endif