input_iterator_policy.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2001 Daniel C. Nuffer
  2. // Copyright (c) 2001-2011 Hartmut Kaiser
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(BOOST_SPIRIT_ITERATOR_INPUT_ITERATOR_POLICY_MAR_16_2007_1156AM)
  7. #define BOOST_SPIRIT_ITERATOR_INPUT_ITERATOR_POLICY_MAR_16_2007_1156AM
  8. #include <boost/spirit/home/support/iterators/multi_pass_fwd.hpp>
  9. #include <boost/spirit/home/support/iterators/detail/multi_pass.hpp>
  10. #include <boost/assert.hpp>
  11. #include <iterator> // for std::iterator_traits
  12. namespace boost { namespace spirit { namespace iterator_policies
  13. {
  14. namespace input_iterator_is_valid_test_
  15. {
  16. ///////////////////////////////////////////////////////////////////////
  17. template <typename Token>
  18. inline bool token_is_valid(Token const& c)
  19. {
  20. return c ? true : false;
  21. }
  22. }
  23. ///////////////////////////////////////////////////////////////////////////
  24. // class input_iterator
  25. // Implementation of the InputPolicy used by multi_pass
  26. //
  27. // The input_iterator encapsulates an input iterator of type T
  28. ///////////////////////////////////////////////////////////////////////////
  29. struct input_iterator
  30. {
  31. ///////////////////////////////////////////////////////////////////////
  32. template <typename T>
  33. class unique // : public detail::default_input_policy
  34. {
  35. private:
  36. typedef
  37. typename std::iterator_traits<T>::value_type
  38. result_type;
  39. public:
  40. typedef
  41. typename std::iterator_traits<T>::difference_type
  42. difference_type;
  43. typedef
  44. typename std::iterator_traits<T>::difference_type
  45. distance_type;
  46. typedef
  47. typename std::iterator_traits<T>::pointer
  48. pointer;
  49. typedef
  50. typename std::iterator_traits<T>::reference
  51. reference;
  52. typedef result_type value_type;
  53. protected:
  54. unique() {}
  55. explicit unique(T x) {}
  56. void swap(unique&) {}
  57. public:
  58. template <typename MultiPass>
  59. static void destroy(MultiPass&) {}
  60. template <typename MultiPass>
  61. static typename MultiPass::reference get_input(MultiPass& mp)
  62. {
  63. return *mp.shared()->input_;
  64. }
  65. template <typename MultiPass>
  66. static void advance_input(MultiPass& mp)
  67. {
  68. ++mp.shared()->input_;
  69. }
  70. // test, whether we reached the end of the underlying stream
  71. template <typename MultiPass>
  72. static bool input_at_eof(MultiPass const& mp)
  73. {
  74. static T const end_iter;
  75. return mp.shared()->input_ == end_iter;
  76. }
  77. template <typename MultiPass>
  78. static bool input_is_valid(MultiPass const& mp, value_type const& t)
  79. {
  80. using namespace input_iterator_is_valid_test_;
  81. return token_is_valid(t);
  82. }
  83. // no unique data elements
  84. };
  85. ///////////////////////////////////////////////////////////////////////
  86. template <typename T>
  87. struct shared
  88. {
  89. explicit shared(T const& input) : input_(input) {}
  90. T input_;
  91. };
  92. };
  93. }}}
  94. #endif