]> git.lyx.org Git - lyx.git/blob - boost/boost/token_iterator.hpp
major boost update
[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 for updates, documentation, and revision history.
11
12 #ifndef BOOST_TOKENIZER_POLICY_JRB051801_HPP_
13 #define BOOST_TOKENIZER_POLICY_JRB051801_HPP_
14
15 #include<boost/iterator_adaptors.hpp>
16 #include<boost/token_functions.hpp>
17 #include<utility>
18 #include<cassert>
19
20 namespace boost {
21     namespace detail{
22         // The base "iterator" for iterator adapter
23         template<class It>
24         class token_iterator_base
25         {
26         public:
27             std::pair<It,It> p_;
28             bool valid_;
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;}
33             
34             template<class T>
35             token_iterator_base(const token_iterator_base<T>& other)
36                 :p_(other.p_),valid_(other.valid_){}
37         };
38         
39         
40         template<class Type, class TokenizerFunc>
41         class tokenizer_policy{
42         private:
43             TokenizerFunc func_;
44             Type tok_;
45         public:
46             tokenizer_policy(){}
47             tokenizer_policy(const TokenizerFunc& f):func_(f){};
48             
49             template<class Base>
50             void initialize(Base& b){
51                 if(b.valid_) return;
52                 func_.reset();
53                 b.valid_ = (b.p_.first != b.p_.second)?
54                     func_(b.p_.first,b.p_.second,tok_):false;
55             }
56             
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_);
62                 
63             }
64             
65             template<class Iterator>
66                 typename Iterator::reference
67                 dereference(const Iterator& a) const{
68                 using namespace std;
69                 assert(a.base().valid_);
70                 return tok_;
71             }   
72             template <class Iterator>
73                 void increment(Iterator& b){
74                 using namespace std;
75                 assert(b.base().valid_);
76                 b.base().valid_ = func_(b.base().p_.first,b.base().p_.second,tok_);
77             }
78             
79         };
80         
81     } // namespace detail
82
83     template <
84         class TokenizerFunc = char_delimiters_separator<char>, 
85         class Iterator = std::string::const_iterator,
86         class Type = std::string
87     >
88     class token_iterator_generator {
89
90     private: 
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;
96     public:
97         typedef boost::iterator_adaptor<base,policies,value_type, 
98             const value_type&,const value_type*,category,std::ptrdiff_t> type;
99     };
100     
101     
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){
107         typedef typename 
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);
112     }
113
114 } // namespace boost
115
116 #endif