stream.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_STREAM_MAY_05_2007_1228PM)
  7. #define BOOST_SPIRIT_STREAM_MAY_05_2007_1228PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/detail/string_parse.hpp>
  12. #include <boost/spirit/home/qi/stream/detail/match_manip.hpp>
  13. #include <boost/spirit/home/support/detail/hold_any.hpp>
  14. #include <boost/proto/traits.hpp>
  15. #include <istream>
  16. ///////////////////////////////////////////////////////////////////////////////
  17. namespace boost { namespace spirit
  18. {
  19. ///////////////////////////////////////////////////////////////////////////
  20. // Enablers
  21. ///////////////////////////////////////////////////////////////////////////
  22. template <>
  23. struct use_terminal<qi::domain, tag::stream> // enables stream
  24. : mpl::true_ {};
  25. template <>
  26. struct use_terminal<qi::domain, tag::wstream> // enables wstream
  27. : mpl::true_ {};
  28. }}
  29. ///////////////////////////////////////////////////////////////////////////////
  30. namespace boost { namespace spirit { namespace qi
  31. {
  32. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  33. using spirit::stream;
  34. using spirit::wstream;
  35. #endif
  36. using spirit::stream_type;
  37. using spirit::wstream_type;
  38. namespace detail
  39. {
  40. template <typename Iterator>
  41. struct psbuf
  42. : std::basic_streambuf<typename std::iterator_traits<Iterator>::value_type>
  43. {
  44. psbuf(Iterator first_, Iterator const& last_)
  45. : first(first_), last(last_) {}
  46. // silence MSVC warning C4512: assignment operator could not be generated
  47. BOOST_DELETED_FUNCTION(psbuf& operator=(psbuf const&))
  48. protected:
  49. typename psbuf::int_type underflow() BOOST_OVERRIDE
  50. {
  51. return first == last ? psbuf::traits_type::eof()
  52. : psbuf::traits_type::to_int_type(*first);
  53. }
  54. typename psbuf::int_type uflow() BOOST_OVERRIDE
  55. {
  56. return first == last ? psbuf::traits_type::eof()
  57. : psbuf::traits_type::to_int_type(*first++);
  58. }
  59. public:
  60. Iterator first;
  61. Iterator const& last;
  62. };
  63. }
  64. template <typename Char = char, typename T = spirit::basic_hold_any<char> >
  65. struct stream_parser
  66. : primitive_parser<stream_parser<Char, T> >
  67. {
  68. template <typename Context, typename Iterator>
  69. struct attribute
  70. {
  71. typedef T type;
  72. };
  73. template <typename Iterator, typename Context
  74. , typename Skipper, typename Attribute>
  75. bool parse(Iterator& first, Iterator const& last
  76. , Context& /*context*/, Skipper const& skipper
  77. , Attribute& attr_) const
  78. {
  79. qi::skip_over(first, last, skipper);
  80. detail::psbuf<Iterator> pseudobuf(first, last);
  81. std::basic_istream<Char> in(&pseudobuf);
  82. in >> attr_; // use existing operator>>()
  83. // advance the iterator if everything is ok
  84. if (in) {
  85. first = pseudobuf.first;
  86. return true;
  87. }
  88. return false;
  89. }
  90. template <typename Context>
  91. info what(Context& /*context*/) const
  92. {
  93. return info("stream");
  94. }
  95. };
  96. template <typename T, typename Char = char>
  97. struct typed_stream
  98. : proto::terminal<stream_parser<Char, T> >::type
  99. {
  100. };
  101. ///////////////////////////////////////////////////////////////////////////
  102. // Parser generators: make_xxx function (objects)
  103. ///////////////////////////////////////////////////////////////////////////
  104. template <typename Char>
  105. struct make_stream
  106. {
  107. typedef stream_parser<Char> result_type;
  108. result_type operator()(unused_type, unused_type) const
  109. {
  110. return result_type();
  111. }
  112. };
  113. template <typename Modifiers>
  114. struct make_primitive<tag::stream, Modifiers> : make_stream<char> {};
  115. template <typename Modifiers>
  116. struct make_primitive<tag::wstream, Modifiers> : make_stream<wchar_t> {};
  117. }}}
  118. #endif