stream.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef _MSC_VER
  41. # pragma warning(push)
  42. # pragma warning(disable: 4512) // assignment operator could not be generated.
  43. #endif
  44. template <typename Iterator>
  45. struct psbuf
  46. : std::basic_streambuf<typename std::iterator_traits<Iterator>::value_type>
  47. {
  48. psbuf(Iterator first_, Iterator const& last_)
  49. : first(first_), last(last_) {}
  50. protected:
  51. typename psbuf::int_type underflow() BOOST_OVERRIDE
  52. {
  53. return first == last ? psbuf::traits_type::eof()
  54. : psbuf::traits_type::to_int_type(*first);
  55. }
  56. typename psbuf::int_type uflow() BOOST_OVERRIDE
  57. {
  58. return first == last ? psbuf::traits_type::eof()
  59. : psbuf::traits_type::to_int_type(*first++);
  60. }
  61. public:
  62. Iterator first;
  63. Iterator const& last;
  64. };
  65. #ifdef _MSC_VER
  66. # pragma warning(pop)
  67. #endif
  68. }
  69. template <typename Char = char, typename T = spirit::basic_hold_any<char> >
  70. struct stream_parser
  71. : primitive_parser<stream_parser<Char, T> >
  72. {
  73. template <typename Context, typename Iterator>
  74. struct attribute
  75. {
  76. typedef T type;
  77. };
  78. template <typename Iterator, typename Context
  79. , typename Skipper, typename Attribute>
  80. bool parse(Iterator& first, Iterator const& last
  81. , Context& /*context*/, Skipper const& skipper
  82. , Attribute& attr_) const
  83. {
  84. qi::skip_over(first, last, skipper);
  85. detail::psbuf<Iterator> pseudobuf(first, last);
  86. std::basic_istream<Char> in(&pseudobuf);
  87. in >> attr_; // use existing operator>>()
  88. // advance the iterator if everything is ok
  89. if (in) {
  90. first = pseudobuf.first;
  91. return true;
  92. }
  93. return false;
  94. }
  95. template <typename Context>
  96. info what(Context& /*context*/) const
  97. {
  98. return info("stream");
  99. }
  100. };
  101. template <typename T, typename Char = char>
  102. struct typed_stream
  103. : proto::terminal<stream_parser<Char, T> >::type
  104. {
  105. };
  106. ///////////////////////////////////////////////////////////////////////////
  107. // Parser generators: make_xxx function (objects)
  108. ///////////////////////////////////////////////////////////////////////////
  109. template <typename Char>
  110. struct make_stream
  111. {
  112. typedef stream_parser<Char> result_type;
  113. result_type operator()(unused_type, unused_type) const
  114. {
  115. return result_type();
  116. }
  117. };
  118. template <typename Modifiers>
  119. struct make_primitive<tag::stream, Modifiers> : make_stream<char> {};
  120. template <typename Modifiers>
  121. struct make_primitive<tag::wstream, Modifiers> : make_stream<wchar_t> {};
  122. }}}
  123. #endif