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