]> git.lyx.org Git - lyx.git/blob - boost/boost/token_iterator.hpp
Boost 1.31.0
[lyx.git] / boost / boost / token_iterator.hpp
1 // Boost token_iterator.hpp  -------------------------------------------------//
2
3 // Copyright John R. Bandela 2001
4 // Permission to copy, use, modify, sell and distribute this software
5 // is granted provided this copyright notice appears in all
6 // copies. This software is provided "as is" without express or
7 // implied warranty, and with no claim as to its suitability for any
8 // purpose.
9
10 // See http://www.boost.org/libs/tokenizer for documentation.
11
12 // Revision History:
13 // 16 Jul 2003   John Bandela
14 //      Allowed conversions from convertible base iterators
15 // 03 Jul 2003   John Bandela
16 //      Converted to new iterator adapter
17
18
19
20 #ifndef BOOST_TOKENIZER_POLICY_JRB070303_HPP_
21 #define BOOST_TOKENIZER_POLICY_JRB070303_HPP_
22
23 #include<boost/iterator/iterator_adaptor.hpp>
24 #include<boost/iterator/detail/minimum_category.hpp>
25 #include<boost/token_functions.hpp>
26 #include<utility>
27 #include<cassert>
28
29
30 namespace boost
31 {
32   template <class TokenizerFunc, class Iterator, class Type>
33   class token_iterator
34       : public iterator_facade<
35             token_iterator<TokenizerFunc, Iterator, Type>
36           , Type
37           , typename detail::minimum_category<
38                 forward_traversal_tag
39               , typename iterator_traversal<Iterator>::type
40             >::type 
41           , const Type&
42         >
43   {
44
45       friend class iterator_core_access;
46
47       TokenizerFunc f_;
48       Iterator begin_;
49       Iterator end_;
50       bool valid_;
51       Type tok_;
52
53       void increment(){
54           assert(valid_);
55           valid_ = f_(begin_,end_,tok_);
56       }
57
58       const Type&  dereference() const {
59           assert(valid_);
60           return tok_;
61       }
62       template<class Other>
63       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
70       void initialize(){
71           if(valid_) return;
72           f_.reset();
73           valid_ = (begin_ != end_)?
74               f_(begin_,end_,tok_):false;
75       }
76   public:
77       token_iterator():begin_(),end_(),valid_(false),tok_() { }
78
79       token_iterator(TokenizerFunc f, Iterator begin, Iterator e = Iterator())
80           : f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); }
81
82       token_iterator(Iterator begin, Iterator e = Iterator())
83             : f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();}
84
85       template<class OtherIter>
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
92       Iterator base()const{return begin_;}
93
94       Iterator end()const{return end_;};
95
96       TokenizerFunc tokenizer_function()const{return f_;}
97
98       Type current_token()const{return tok_;}
99
100       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 
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