regression_multi_pass_functor.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2010 Larry Evans
  2. //
  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. //Purpose:
  6. // Demonstrate error in non-classic multi_pass iterator compilation.
  7. //
  8. #include <boost/spirit/home/qi.hpp>
  9. #include <boost/spirit/home/support.hpp>
  10. #include <boost/spirit/home/support/multi_pass.hpp>
  11. #include <boost/spirit/home/support/iterators/detail/functor_input_policy.hpp>
  12. #include <fstream>
  13. //[iterate_a2m:
  14. // copied from:
  15. // http://www.boost.org/doc/libs/1_41_0/libs/spirit/doc/html/spirit/support/multi_pass.html
  16. // define the function object
  17. template<typename CharT=char>
  18. class istreambuf_functor
  19. {
  20. public:
  21. typedef
  22. std::istreambuf_iterator<CharT>
  23. buf_iterator_type;
  24. typedef
  25. typename buf_iterator_type::int_type
  26. result_type;
  27. static
  28. result_type
  29. eof;
  30. istreambuf_functor(void)
  31. : current_chr(eof)
  32. {}
  33. istreambuf_functor(std::ifstream& input)
  34. : my_first(input)
  35. , current_chr(eof)
  36. {}
  37. result_type operator()()
  38. {
  39. buf_iterator_type last;
  40. if (my_first == last)
  41. {
  42. return eof;
  43. }
  44. current_chr=*my_first;
  45. ++my_first;
  46. return current_chr;
  47. }
  48. private:
  49. buf_iterator_type my_first;
  50. result_type current_chr;
  51. };
  52. template<typename CharT>
  53. typename istreambuf_functor<CharT>::result_type
  54. istreambuf_functor<CharT>::
  55. eof
  56. ( istreambuf_functor<CharT>::buf_iterator_type::traits_type::eof()
  57. )
  58. ;
  59. //]iterate_a2m:
  60. typedef istreambuf_functor<char> base_iterator_type;
  61. typedef
  62. boost::spirit::multi_pass
  63. < base_iterator_type
  64. , boost::spirit::iterator_policies::default_policy
  65. < boost::spirit::iterator_policies::first_owner
  66. , boost::spirit::iterator_policies::no_check
  67. , boost::spirit::iterator_policies::functor_input
  68. , boost::spirit::iterator_policies::split_std_deque
  69. >
  70. >
  71. chr_iterator_type;
  72. // ======================================================================
  73. // Main
  74. int main()
  75. {
  76. std::ifstream in("multi_pass.txt");
  77. unsigned num_toks=0;
  78. unsigned const max_toks=10;
  79. base_iterator_type base_first(in);
  80. chr_iterator_type chr_first(base_first);
  81. chr_iterator_type chr_last;
  82. for
  83. (
  84. ; (chr_first != chr_last && ++num_toks < max_toks)
  85. ; ++chr_first
  86. )
  87. {
  88. std::cout<<":num_toks="<<num_toks<<":chr="<<*chr_first<<"\n";
  89. }
  90. return 0;
  91. }