stream.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/qi/stream/detail/iterator_source.hpp>
  14. #include <boost/spirit/home/support/detail/hold_any.hpp>
  15. #include <boost/proto/traits.hpp>
  16. #include <iosfwd>
  17. #include <sstream>
  18. ///////////////////////////////////////////////////////////////////////////////
  19. namespace boost { namespace spirit
  20. {
  21. ///////////////////////////////////////////////////////////////////////////
  22. // Enablers
  23. ///////////////////////////////////////////////////////////////////////////
  24. template <>
  25. struct use_terminal<qi::domain, tag::stream> // enables stream
  26. : mpl::true_ {};
  27. template <>
  28. struct use_terminal<qi::domain, tag::wstream> // enables wstream
  29. : mpl::true_ {};
  30. }}
  31. ///////////////////////////////////////////////////////////////////////////////
  32. namespace boost { namespace spirit { namespace qi
  33. {
  34. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  35. using spirit::stream;
  36. using spirit::wstream;
  37. #endif
  38. using spirit::stream_type;
  39. using spirit::wstream_type;
  40. template <typename Char = char, typename T = spirit::basic_hold_any<char> >
  41. struct stream_parser
  42. : primitive_parser<stream_parser<Char, T> >
  43. {
  44. template <typename Context, typename Iterator>
  45. struct attribute
  46. {
  47. typedef T type;
  48. };
  49. template <typename Iterator, typename Context
  50. , typename Skipper, typename Attribute>
  51. bool parse(Iterator& first, Iterator const& last
  52. , Context& /*context*/, Skipper const& skipper
  53. , Attribute& attr_) const
  54. {
  55. typedef qi::detail::iterator_source<Iterator> source_device;
  56. typedef boost::iostreams::stream<source_device> instream;
  57. qi::skip_over(first, last, skipper);
  58. instream in(first, last); // copies 'first'
  59. in >> attr_; // use existing operator>>()
  60. // advance the iterator if everything is ok
  61. if (in) {
  62. if (!in.eof()) {
  63. typedef typename
  64. boost::iterator_difference<Iterator>::type diff_type;
  65. diff_type pos = static_cast<diff_type>(in.tellg());
  66. std::advance(first, pos);
  67. } else {
  68. first = last;
  69. }
  70. return true;
  71. }
  72. return false;
  73. }
  74. template <typename Context>
  75. info what(Context& /*context*/) const
  76. {
  77. return info("stream");
  78. }
  79. };
  80. template <typename T, typename Char = char>
  81. struct typed_stream
  82. : proto::terminal<stream_parser<Char, T> >::type
  83. {
  84. };
  85. ///////////////////////////////////////////////////////////////////////////
  86. // Parser generators: make_xxx function (objects)
  87. ///////////////////////////////////////////////////////////////////////////
  88. template <typename Char>
  89. struct make_stream
  90. {
  91. typedef stream_parser<Char> result_type;
  92. result_type operator()(unused_type, unused_type) const
  93. {
  94. return result_type();
  95. }
  96. };
  97. template <typename Modifiers>
  98. struct make_primitive<tag::stream, Modifiers> : make_stream<char> {};
  99. template <typename Modifiers>
  100. struct make_primitive<tag::wstream, Modifiers> : make_stream<wchar_t> {};
  101. }}}
  102. #endif