/src/contrib/boost/token_iterator.hpp

http://pythonocc.googlecode.com/ · C++ Header · 128 lines · 84 code · 31 blank · 13 comment · 5 complexity · ce086262838a2b787f9b7a4fb1b1b9db MD5 · raw file

  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/detail/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 detail::minimum_category<
  27. forward_traversal_tag
  28. , typename iterator_traversal<Iterator>::type
  29. >::type
  30. , const Type&
  31. >
  32. {
  33. friend class iterator_core_access;
  34. TokenizerFunc f_;
  35. Iterator begin_;
  36. Iterator end_;
  37. bool valid_;
  38. Type tok_;
  39. void increment(){
  40. BOOST_ASSERT(valid_);
  41. valid_ = f_(begin_,end_,tok_);
  42. }
  43. const Type& dereference() const {
  44. BOOST_ASSERT(valid_);
  45. return tok_;
  46. }
  47. template<class Other>
  48. bool equal(const Other& a) const{
  49. return (a.valid_ && valid_)
  50. ?( (a.begin_==begin_) && (a.end_ == end_) )
  51. :(a.valid_==valid_);
  52. }
  53. void initialize(){
  54. if(valid_) return;
  55. f_.reset();
  56. valid_ = (begin_ != end_)?
  57. f_(begin_,end_,tok_):false;
  58. }
  59. public:
  60. token_iterator():begin_(),end_(),valid_(false),tok_() { }
  61. token_iterator(TokenizerFunc f, Iterator begin, Iterator e = Iterator())
  62. : f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); }
  63. token_iterator(Iterator begin, Iterator e = Iterator())
  64. : f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();}
  65. template<class OtherIter>
  66. token_iterator(
  67. token_iterator<TokenizerFunc, OtherIter,Type> const& t
  68. , typename enable_if_convertible<OtherIter, Iterator>::type* = 0)
  69. : f_(t.tokenizer_function()),begin_(t.base())
  70. ,end_(t.end()),valid_(!t.at_end()),tok_(t.current_token()) {}
  71. Iterator base()const{return begin_;}
  72. Iterator end()const{return end_;};
  73. TokenizerFunc tokenizer_function()const{return f_;}
  74. Type current_token()const{return tok_;}
  75. bool at_end()const{return !valid_;}
  76. };
  77. template <
  78. class TokenizerFunc = char_delimiters_separator<char>,
  79. class Iterator = std::string::const_iterator,
  80. class Type = std::string
  81. >
  82. class token_iterator_generator {
  83. private:
  84. public:
  85. typedef token_iterator<TokenizerFunc,Iterator,Type> type;
  86. };
  87. // Type has to be first because it needs to be explicitly specified
  88. // because there is no way the function can deduce it.
  89. template<class Type, class Iterator, class TokenizerFunc>
  90. typename token_iterator_generator<TokenizerFunc,Iterator,Type>::type
  91. make_token_iterator(Iterator begin, Iterator end,const TokenizerFunc& fun){
  92. typedef typename
  93. token_iterator_generator<TokenizerFunc,Iterator,Type>::type ret_type;
  94. return ret_type(fun,begin,end);
  95. }
  96. } // namespace boost
  97. #endif