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