• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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 iterators::minimum_category<
35                 forward_traversal_tag
36               , typename iterator_traversal<Iterator>::type
37             >::type
38           , const Type&
39         >
40   {
41 
42 #ifdef __DCC__
43       friend class boost::iterator_core_access;
44 #else
45       friend class iterator_core_access;
46 #endif
47       TokenizerFunc f_;
48       Iterator begin_;
49       Iterator end_;
50       bool valid_;
51       Type tok_;
52 
increment()53       void increment(){
54           BOOST_ASSERT(valid_);
55           valid_ = f_(begin_,end_,tok_);
56       }
57 
dereference() const58       const Type&  dereference() const {
59           BOOST_ASSERT(valid_);
60           return tok_;
61       }
62       template<class Other>
equal(const Other & a) const63       bool equal(const Other& a) const{
64           return (a.valid_ && valid_)
65               ?( (a.begin_==begin_) && (a.end_ == end_) )
66               :(a.valid_==valid_);
67 
68       }
69 
initialize()70       void initialize(){
71           if(valid_) return;
72           f_.reset();
73           valid_ = (begin_ != end_)?
74               f_(begin_,end_,tok_):false;
75       }
76   public:
token_iterator()77       token_iterator():begin_(),end_(),valid_(false),tok_() { }
78 
token_iterator(TokenizerFunc f,Iterator begin,Iterator e=Iterator ())79       token_iterator(TokenizerFunc f, Iterator begin, Iterator e = Iterator())
80           : f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); }
81 
token_iterator(Iterator begin,Iterator e=Iterator ())82       token_iterator(Iterator begin, Iterator e = Iterator())
83             : f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();}
84 
85       template<class OtherIter>
token_iterator(token_iterator<TokenizerFunc,OtherIter,Type> const & t,typename enable_if_convertible<OtherIter,Iterator>::type * =0)86       token_iterator(
87             token_iterator<TokenizerFunc, OtherIter,Type> const& t
88             , typename enable_if_convertible<OtherIter, Iterator>::type* = 0)
89             : f_(t.tokenizer_function()),begin_(t.base())
90             ,end_(t.end()),valid_(!t.at_end()),tok_(t.current_token()) {}
91 
base() const92       Iterator base()const{return begin_;}
93 
end() const94       Iterator end()const{return end_;}
95 
tokenizer_function() const96       TokenizerFunc tokenizer_function()const{return f_;}
97 
current_token() const98       Type current_token()const{return tok_;}
99 
at_end() const100       bool at_end()const{return !valid_;}
101 
102 
103 
104 
105   };
106     template <
107         class TokenizerFunc = char_delimiters_separator<char>,
108         class Iterator = std::string::const_iterator,
109         class Type = std::string
110     >
111     class token_iterator_generator {
112 
113     private:
114     public:
115         typedef token_iterator<TokenizerFunc,Iterator,Type> type;
116     };
117 
118 
119     // Type has to be first because it needs to be explicitly specified
120     // because there is no way the function can deduce it.
121     template<class Type, class Iterator, class TokenizerFunc>
122         typename token_iterator_generator<TokenizerFunc,Iterator,Type>::type
make_token_iterator(Iterator begin,Iterator end,const TokenizerFunc & fun)123     make_token_iterator(Iterator begin, Iterator end,const TokenizerFunc& fun){
124         typedef typename
125             token_iterator_generator<TokenizerFunc,Iterator,Type>::type ret_type;
126         return ret_type(fun,begin,end);
127     }
128 
129 } // namespace boost
130 
131 #endif
132