sl@0: // Boost token_iterator.hpp -------------------------------------------------// sl@0: sl@0: // Copyright John R. Bandela 2001 sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: // See http://www.boost.org/libs/tokenizer for documentation. sl@0: sl@0: // Revision History: sl@0: // 16 Jul 2003 John Bandela sl@0: // Allowed conversions from convertible base iterators sl@0: // 03 Jul 2003 John Bandela sl@0: // Converted to new iterator adapter sl@0: sl@0: sl@0: sl@0: #ifndef BOOST_TOKENIZER_POLICY_JRB070303_HPP_ sl@0: #define BOOST_TOKENIZER_POLICY_JRB070303_HPP_ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost sl@0: { sl@0: template sl@0: class token_iterator sl@0: : public iterator_facade< sl@0: token_iterator sl@0: , Type sl@0: , typename detail::minimum_category< sl@0: forward_traversal_tag sl@0: , typename iterator_traversal::type sl@0: >::type sl@0: , const Type& sl@0: > sl@0: { sl@0: sl@0: friend class iterator_core_access; sl@0: sl@0: TokenizerFunc f_; sl@0: Iterator begin_; sl@0: Iterator end_; sl@0: bool valid_; sl@0: Type tok_; sl@0: sl@0: void increment(){ sl@0: BOOST_ASSERT(valid_); sl@0: valid_ = f_(begin_,end_,tok_); sl@0: } sl@0: sl@0: const Type& dereference() const { sl@0: BOOST_ASSERT(valid_); sl@0: return tok_; sl@0: } sl@0: template sl@0: bool equal(const Other& a) const{ sl@0: return (a.valid_ && valid_) sl@0: ?( (a.begin_==begin_) && (a.end_ == end_) ) sl@0: :(a.valid_==valid_); sl@0: sl@0: } sl@0: sl@0: void initialize(){ sl@0: if(valid_) return; sl@0: f_.reset(); sl@0: valid_ = (begin_ != end_)? sl@0: f_(begin_,end_,tok_):false; sl@0: } sl@0: public: sl@0: token_iterator():begin_(),end_(),valid_(false),tok_() { } sl@0: sl@0: token_iterator(TokenizerFunc f, Iterator begin, Iterator e = Iterator()) sl@0: : f_(f),begin_(begin),end_(e),valid_(false),tok_(){ initialize(); } sl@0: sl@0: token_iterator(Iterator begin, Iterator e = Iterator()) sl@0: : f_(),begin_(begin),end_(e),valid_(false),tok_() {initialize();} sl@0: sl@0: template sl@0: token_iterator( sl@0: token_iterator const& t sl@0: , typename enable_if_convertible::type* = 0) sl@0: : f_(t.tokenizer_function()),begin_(t.base()) sl@0: ,end_(t.end()),valid_(t.at_end()),tok_(t.current_token()) {} sl@0: sl@0: Iterator base()const{return begin_;} sl@0: sl@0: Iterator end()const{return end_;}; sl@0: sl@0: TokenizerFunc tokenizer_function()const{return f_;} sl@0: sl@0: Type current_token()const{return tok_;} sl@0: sl@0: bool at_end()const{return valid_;} sl@0: sl@0: sl@0: sl@0: sl@0: }; sl@0: template < sl@0: class TokenizerFunc = char_delimiters_separator, sl@0: class Iterator = std::string::const_iterator, sl@0: class Type = std::string sl@0: > sl@0: class token_iterator_generator { sl@0: sl@0: private: sl@0: public: sl@0: typedef token_iterator type; sl@0: }; sl@0: sl@0: sl@0: // Type has to be first because it needs to be explicitly specified sl@0: // because there is no way the function can deduce it. sl@0: template sl@0: typename token_iterator_generator::type sl@0: make_token_iterator(Iterator begin, Iterator end,const TokenizerFunc& fun){ sl@0: typedef typename sl@0: token_iterator_generator::type ret_type; sl@0: return ret_type(fun,begin,end); sl@0: } sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif