sf_bug_720917.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*=============================================================================
  2. Copyright (c) 2003 Giovanni Bajo
  3. Copyrigh (c) 2003 Martin Wille
  4. http://spirit.sourceforge.net/
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #include <boost/spirit/include/classic_multi_pass.hpp>
  10. #include <iterator>
  11. #include "impl/sstream.hpp"
  12. #include <boost/detail/lightweight_test.hpp>
  13. using namespace std;
  14. using namespace BOOST_SPIRIT_CLASSIC_NS;
  15. // Test for bug #720917
  16. // http://sf.net/tracker/index.php?func=detail&aid=720917&group_id=28447&atid=393386
  17. //
  18. // Check that it's possible to use multi_pass
  19. // together with standard library as a normal iterator
  20. //
  21. // a functor to test out the functor_multi_pass
  22. class my_functor
  23. {
  24. public:
  25. typedef char result_type;
  26. my_functor()
  27. : c('A')
  28. {}
  29. char operator()()
  30. {
  31. if (c == 'M')
  32. return eof;
  33. else
  34. return c++;
  35. }
  36. static result_type eof;
  37. private:
  38. char c;
  39. };
  40. my_functor::result_type my_functor::eof = '\0';
  41. ////////////////////////////////////////////////
  42. // four types of multi_pass iterators
  43. typedef multi_pass<
  44. my_functor,
  45. multi_pass_policies::functor_input,
  46. multi_pass_policies::first_owner,
  47. multi_pass_policies::no_check,
  48. multi_pass_policies::std_deque
  49. > functor_multi_pass_t;
  50. typedef multi_pass<istream_iterator<char> > default_multi_pass_t;
  51. typedef look_ahead<istream_iterator<char>, 6> fixed_multi_pass_t;
  52. typedef multi_pass<
  53. istream_iterator<char>,
  54. multi_pass_policies::input_iterator,
  55. multi_pass_policies::first_owner,
  56. multi_pass_policies::buf_id_check,
  57. multi_pass_policies::std_deque
  58. > first_owner_multi_pass_t;
  59. ////////////////////////////////////////////////
  60. // the test cases
  61. template <typename IterT>
  62. void construct_string_from(void)
  63. {
  64. sstream_t ss;
  65. ss << "test string";
  66. IterT mpend;
  67. istream_iterator<char> a(ss);
  68. IterT mp1(a);
  69. std::string dummy;
  70. #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
  71. dummy.assign(mp1, mpend);
  72. #else
  73. copy(mp1, mpend, inserter(dummy, dummy.end()));
  74. #endif
  75. }
  76. template <>
  77. void construct_string_from<functor_multi_pass_t>(void)
  78. {
  79. functor_multi_pass_t mpend;
  80. functor_multi_pass_t mp1 = functor_multi_pass_t(my_functor());
  81. std::string dummy;
  82. #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
  83. dummy.assign(mp1, mpend);
  84. #else
  85. copy(mp1, mpend, inserter(dummy, dummy.end()));
  86. #endif
  87. }
  88. ////////////////////////////////////////////////
  89. // Definition of the test suite
  90. int
  91. main()
  92. {
  93. construct_string_from<default_multi_pass_t>();
  94. construct_string_from<fixed_multi_pass_t>();
  95. construct_string_from<first_owner_multi_pass_t>();
  96. construct_string_from<functor_multi_pass_t>();
  97. return boost::report_errors();
  98. }