stream.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. std::streamsize pos = in.tellg();
  62. std::advance(first, pos);
  63. return true;
  64. }
  65. return false;
  66. }
  67. template <typename Context>
  68. info what(Context& /*context*/) const
  69. {
  70. return info("stream");
  71. }
  72. };
  73. template <typename T, typename Char = char>
  74. struct typed_stream
  75. : proto::terminal<stream_parser<Char, T> >::type
  76. {
  77. };
  78. ///////////////////////////////////////////////////////////////////////////
  79. // Parser generators: make_xxx function (objects)
  80. ///////////////////////////////////////////////////////////////////////////
  81. template <typename Char>
  82. struct make_stream
  83. {
  84. typedef stream_parser<Char> result_type;
  85. result_type operator()(unused_type, unused_type) const
  86. {
  87. return result_type();
  88. }
  89. };
  90. template <typename Modifiers>
  91. struct make_primitive<tag::stream, Modifiers> : make_stream<char> {};
  92. template <typename Modifiers>
  93. struct make_primitive<tag::wstream, Modifiers> : make_stream<wchar_t> {};
  94. }}}
  95. #endif