token_iterator.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Boost token_iterator.hpp -------------------------------------------------//
  2. // Copyright John R. Bandela 2001
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/tokenizer for documentation.
  7. // Revision History:
  8. // 16 Jul 2003 John Bandela
  9. // Allowed conversions from convertible base iterators
  10. // 03 Jul 2003 John Bandela
  11. // Converted to new iterator adapter
  12. #ifndef BOOST_TOKENIZER_POLICY_JRB070303_HPP_
  13. #define BOOST_TOKENIZER_POLICY_JRB070303_HPP_
  14. #include <boost/assert.hpp>
  15. #include <boost/iterator/iterator_adaptor.hpp>
  16. #include <boost/iterator/minimum_category.hpp>
  17. #include <boost/token_functions.hpp>
  18. #include <utility>
  19. namespace boost
  20. {
  21. template <class TokenizerFunc, class Iterator, class Type>
  22. class token_iterator
  23. : public iterator_facade<
  24. token_iterator<TokenizerFunc, Iterator, Type>
  25. , Type
  26. , typename iterators::minimum_category<
  27. forward_traversal_tag
  28. , typename iterator_traversal<Iterator>::type
  29. >::type
  30. , const Type&
  31. >
  32. {
  33. #ifdef __DCC__
  34. friend class boost::iterator_core_access;
  35. #else
  36. friend class iterator_core_access;
  37. #endif
  38. TokenizerFunc f_;
  39. Iterator begin_;
  40. Iterator end_;
  41. bool valid_;
  42. Type tok_;
  43. void increment(){
  44. BOOST_ASSERT(valid_);
  45. valid_ = f_(begin_,end_,tok_);
  46. }
  47. const Type& dereference() const {
  48. BOOST_ASSERT(valid_);
  49. return tok_;
  50. }
  51. template<class Other>
  52. bool equal(const Other& a) const{
  53. return (a.valid_ && valid_)
  54. ?( (a.begin_==begin_) && (a.end_ == end_) )
  55. :(a.valid_==valid_);
  56. }
  57. void initialize(){
  58. if(valid_) return;
  59. f_.reset();
  60. valid_ = (begin_ != end_)?
  61. f_(begin_,end_,tok_):false;
  62. }
  63. public:
  64. token_iterator():begin_(),end_(),valid_(false),tok_() { }
  65. token_iterator(TokenizerFunc f, Iterator begin, Iterator e = Iterator())
  66. : f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); }
  67. token_iterator(Iterator begin, Iterator e = Iterator())
  68. : f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();}
  69. template<class OtherIter>
  70. token_iterator(
  71. token_iterator<TokenizerFunc, OtherIter,Type> const& t
  72. , typename enable_if_convertible<OtherIter, Iterator>::type* = 0)
  73. : f_(t.tokenizer_function()),begin_(t.base())
  74. ,end_(t.end()),valid_(!t.at_end()),tok_(t.current_token()) {}
  75. Iterator base()const{return begin_;}
  76. Iterator end()const{return end_;}
  77. TokenizerFunc tokenizer_function()const{return f_;}
  78. Type current_token()const{return tok_;}
  79. bool at_end()const{return !valid_;}
  80. };
  81. template <
  82. class TokenizerFunc = char_delimiters_separator<char>,
  83. class Iterator = std::string::const_iterator,
  84. class Type = std::string
  85. >
  86. class token_iterator_generator {
  87. private:
  88. public:
  89. typedef token_iterator<TokenizerFunc,Iterator,Type> type;
  90. };
  91. // Type has to be first because it needs to be explicitly specified
  92. // because there is no way the function can deduce it.
  93. template<class Type, class Iterator, class TokenizerFunc>
  94. typename token_iterator_generator<TokenizerFunc,Iterator,Type>::type
  95. make_token_iterator(Iterator begin, Iterator end,const TokenizerFunc& fun){
  96. typedef typename
  97. token_iterator_generator<TokenizerFunc,Iterator,Type>::type ret_type;
  98. return ret_type(fun,begin,end);
  99. }
  100. } // namespace boost
  101. #endif