1 // Boost token_iterator.hpp -------------------------------------------------//
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
10 // See http://www.boost.org/libs/tokenizer for documentation.
12 #ifndef BOOST_TOKENIZER_POLICY_JRB051801_HPP_
13 #define BOOST_TOKENIZER_POLICY_JRB051801_HPP_
15 #include<boost/iterator_adaptors.hpp>
16 #include<boost/token_functions.hpp>
22 // The base "iterator" for iterator adapter
24 class token_iterator_base
29 token_iterator_base():p_(It(),It()),valid_(false){}
30 token_iterator_base(const It& b , const It& e )
31 :p_(b,e),valid_(false){}
32 operator It(){return p_.first;}
35 token_iterator_base(const token_iterator_base<T>& other)
36 :p_(other.p_),valid_(other.valid_){}
40 template<class Type, class TokenizerFunc>
41 class tokenizer_policy{
47 tokenizer_policy(const TokenizerFunc& f):func_(f){};
50 void initialize(Base& b){
53 b.valid_ = (b.p_.first != b.p_.second)?
54 func_(b.p_.first,b.p_.second,tok_):false;
57 template<class Iterator1, class Iterator2>
58 bool equal(const Iterator1& a, const Iterator2& b) const{
59 return (a.base().valid_ && b.base().valid_)
60 ?(a.base().p_==b.base().p_)
61 :(a.base().valid_==b.base().valid_);
65 template<class Iterator>
66 typename Iterator::reference
67 dereference(const Iterator& a) const{
69 assert(a.base().valid_);
72 template <class Iterator>
73 void increment(Iterator& b){
75 assert(b.base().valid_);
76 b.base().valid_ = func_(b.base().p_.first,b.base().p_.second,tok_);
84 class TokenizerFunc = char_delimiters_separator<char>,
85 class Iterator = std::string::const_iterator,
86 class Type = std::string
88 class token_iterator_generator {
91 typedef Type value_type;
92 typedef detail::tokenizer_policy<Type, TokenizerFunc> policies;
93 typedef detail::token_iterator_base<Iterator> base;
94 typedef typename boost::detail::non_bidirectional_category<
95 Iterator>::type category;
97 typedef boost::iterator_adaptor<base,policies,value_type,
98 const value_type&,const value_type*,category,std::ptrdiff_t> type;
102 // Type has to be first because it needs to be explicitly specified
103 // because there is no way the function can deduce it.
104 template<class Type, class Iterator, class TokenizerFunc>
105 typename token_iterator_generator<TokenizerFunc,Iterator,Type>::type
106 make_token_iterator(Iterator begin, Iterator end,const TokenizerFunc& fun){
108 token_iterator_generator<TokenizerFunc,Iterator,Type>::type ret_type;
109 detail::token_iterator_base<Iterator> b(begin,end);
110 detail::tokenizer_policy<Type,TokenizerFunc> f(fun);
111 return ret_type(b,f);