fix doc example typo
[boost.git] / boost / token_iterator.hpp
blob19b1db26c8625f6d386080f1e56c93d98e283ac5
1 // Boost token_iterator.hpp -------------------------------------------------//
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)
8 // See http://www.boost.org/libs/tokenizer for documentation.
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
18 #ifndef BOOST_TOKENIZER_POLICY_JRB070303_HPP_
19 #define BOOST_TOKENIZER_POLICY_JRB070303_HPP_
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>
27 namespace boost
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&
42 friend class iterator_core_access;
44 TokenizerFunc f_;
45 Iterator begin_;
46 Iterator end_;
47 bool valid_;
48 Type tok_;
50 void increment(){
51 BOOST_ASSERT(valid_);
52 valid_ = f_(begin_,end_,tok_);
55 const Type& dereference() const {
56 BOOST_ASSERT(valid_);
57 return tok_;
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_);
67 void initialize(){
68 if(valid_) return;
69 f_.reset();
70 valid_ = (begin_ != end_)?
71 f_(begin_,end_,tok_):false;
73 public:
74 token_iterator():begin_(),end_(),valid_(false),tok_() { }
76 token_iterator(TokenizerFunc f, Iterator begin, Iterator e = Iterator())
77 : f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); }
79 token_iterator(Iterator begin, Iterator e = Iterator())
80 : f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();}
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()) {}
89 Iterator base()const{return begin_;}
91 Iterator end()const{return end_;};
93 TokenizerFunc tokenizer_function()const{return f_;}
95 Type current_token()const{return tok_;}
97 bool at_end()const{return !valid_;}
103 template <
104 class TokenizerFunc = char_delimiters_separator<char>,
105 class Iterator = std::string::const_iterator,
106 class Type = std::string
108 class token_iterator_generator {
110 private:
111 public:
112 typedef token_iterator<TokenizerFunc,Iterator,Type> type;
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);
126 } // namespace boost
128 #endif