source: NonGTP/Boost/boost/token_iterator.hpp @ 857

Revision 857, 3.8 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]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/iterator/iterator_adaptor.hpp>
22#include<boost/iterator/detail/minimum_category.hpp>
23#include<boost/token_functions.hpp>
24#include<utility>
25#include<cassert>
26
27
28namespace boost
29{
30  template <class TokenizerFunc, class Iterator, class Type>
31  class token_iterator
32      : public iterator_facade<
33            token_iterator<TokenizerFunc, Iterator, Type>
34          , Type
35          , typename detail::minimum_category<
36                forward_traversal_tag
37              , typename iterator_traversal<Iterator>::type
38            >::type
39          , const Type&
40        >
41  {
42
43      friend class iterator_core_access;
44
45      TokenizerFunc f_;
46      Iterator begin_;
47      Iterator end_;
48      bool valid_;
49      Type tok_;
50
51      void increment(){
52          assert(valid_);
53          valid_ = f_(begin_,end_,tok_);
54      }
55
56      const Type&  dereference() const {
57          assert(valid_);
58          return tok_;
59      }
60      template<class Other>
61      bool equal(const Other& a) const{
62          return (a.valid_ && valid_)
63              ?( (a.begin_==begin_) && (a.end_ == end_) )
64              :(a.valid_==valid_);
65
66      }
67
68      void initialize(){
69          if(valid_) return;
70          f_.reset();
71          valid_ = (begin_ != end_)?
72              f_(begin_,end_,tok_):false;
73      }
74  public:
75      token_iterator():begin_(),end_(),valid_(false),tok_() { }
76
77      token_iterator(TokenizerFunc f, Iterator begin, Iterator e = Iterator())
78          : f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); }
79
80      token_iterator(Iterator begin, Iterator e = Iterator())
81            : f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();}
82
83      template<class OtherIter>
84      token_iterator(
85            token_iterator<TokenizerFunc, OtherIter,Type> const& t
86            , typename enable_if_convertible<OtherIter, Iterator>::type* = 0)
87            : f_(t.tokenizer_function()),begin_(t.base())
88            ,end_(t.end()),valid_(t.at_end()),tok_(t.current_token()) {}
89
90      Iterator base()const{return begin_;}
91
92      Iterator end()const{return end_;};
93
94      TokenizerFunc tokenizer_function()const{return f_;}
95
96      Type current_token()const{return tok_;}
97
98      bool at_end()const{return valid_;}
99
100
101
102
103  };
104    template <
105        class TokenizerFunc = char_delimiters_separator<char>,
106        class Iterator = std::string::const_iterator,
107        class Type = std::string
108    >
109    class token_iterator_generator {
110
111    private:
112    public:
113        typedef token_iterator<TokenizerFunc,Iterator,Type> type;
114    };
115   
116   
117    // Type has to be first because it needs to be explicitly specified
118    // because there is no way the function can deduce it.
119    template<class Type, class Iterator, class TokenizerFunc>
120        typename token_iterator_generator<TokenizerFunc,Iterator,Type>::type
121    make_token_iterator(Iterator begin, Iterator end,const TokenizerFunc& fun){
122        typedef typename
123            token_iterator_generator<TokenizerFunc,Iterator,Type>::type ret_type;
124        return ret_type(fun,begin,end);
125    }
126
127} // namespace boost
128
129#endif
Note: See TracBrowser for help on using the repository browser.