Claw  1.7.3
iterator.hpp
Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien.jorge@gamned.org
00024 */
00031 #ifndef __CLAW_ITERATOR_HPP__
00032 #define __CLAW_ITERATOR_HPP__
00033 
00034 #include <iterator>
00035 
00036 namespace claw
00037 {
00038   /*-------------------------------------------------------------------------*/
00043   template< typename Category, typename Value, typename Iterator,
00044       typename Function >
00045   class wrapped_iterator_by_category
00046   {
00047 
00048   }; // class wrapped_iterator_by_category
00049 
00050   /*-------------------------------------------------------------------------*/
00055   template<typename Value, typename Iterator, typename Function>
00056   class wrapped_iterator_by_category
00057   <std::forward_iterator_tag, Value, Iterator, Function>
00058   {
00059   public:
00060     typedef typename std::iterator_traits<Iterator>::difference_type
00061     difference_type;
00062     typedef Value value_type;
00063     typedef value_type* pointer;
00064     typedef value_type& reference;
00065     typedef typename std::iterator_traits<Iterator>::iterator_category
00066     iterator_category;
00067 
00068     typedef
00069     wrapped_iterator_by_category
00070     <std::forward_iterator_tag, Value, Iterator, Function>
00071     self_type;
00072 
00073   public:
00074     wrapped_iterator_by_category() {}
00075     wrapped_iterator_by_category( const Iterator& it )
00076       : m_it(it)
00077     { }
00078     wrapped_iterator_by_category( const Iterator& it, const Function& f )
00079       : m_it(it), m_fun(f)
00080     { }
00081     template<typename C, typename V, typename I, typename F>
00082     wrapped_iterator_by_category
00083     ( const wrapped_iterator_by_category<C, V, I, F>& that )
00084       : m_it(that.get_iterator()), m_fun(that.get_function())
00085     { }
00086 
00087     const Iterator& get_iterator() const { return m_it; }
00088     const Function& get_function() const { return m_fun; }
00089 
00090     self_type& operator++()
00091     {
00092       ++m_it;
00093       return *this;
00094     }
00095 
00096     self_type operator++(int)
00097     {
00098       self_type tmp(*this);
00099       ++m_it;
00100       return tmp;
00101     }
00102 
00103     reference operator*() const { return m_fun(*m_it); }
00104     pointer operator->() const { return &m_fun(*m_it); }
00105 
00106     bool operator==( const self_type& that ) const { return m_it == that.m_it; }
00107     bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
00108     bool operator==( const Iterator& it ) const { return m_it == it; }
00109     bool operator!=( const Iterator& it ) const { return m_it != it; }
00110 
00111   private:
00113     Iterator m_it;
00114 
00116     Function m_fun;
00117 
00118   }; // class wrapped_iterator_by_category [forward_iterator_tag]
00119 
00120   /*-------------------------------------------------------------------------*/
00126   template<typename Value, typename Iterator, typename Function>
00127   class wrapped_iterator_by_category
00128   <std::bidirectional_iterator_tag, Value, Iterator, Function>
00129   {
00130   public:
00131     typedef typename std::iterator_traits<Iterator>::difference_type
00132     difference_type;
00133     typedef Value value_type;
00134     typedef value_type* pointer;
00135     typedef value_type& reference;
00136     typedef typename std::iterator_traits<Iterator>::iterator_category
00137     iterator_category;
00138 
00139     typedef
00140     wrapped_iterator_by_category
00141     <std::bidirectional_iterator_tag, Value, Iterator, Function> self_type;
00142 
00143   public:
00144     wrapped_iterator_by_category() {}
00145     wrapped_iterator_by_category( const Iterator& it )
00146       : m_it(it)
00147     { }
00148     wrapped_iterator_by_category( const Iterator& it, const Function& f )
00149       : m_it(it), m_fun(f)
00150     { }
00151     template<typename C, typename V, typename I, typename F>
00152     wrapped_iterator_by_category
00153     ( const wrapped_iterator_by_category<C, V, I, F>& that )
00154       : m_it(that.get_iterator()), m_fun(that.get_function())
00155     { }
00156 
00157     const Iterator& get_iterator() const { return m_it; }
00158     const Function& get_function() const { return m_fun; }
00159 
00160     self_type& operator++()
00161     {
00162       ++m_it;
00163       return *this;
00164     }
00165 
00166     self_type operator++(int)
00167     {
00168       self_type tmp(*this);
00169       ++m_it;
00170       return tmp;
00171     }
00172 
00173     self_type& operator--()
00174     {
00175       --m_it;
00176       return *this;
00177     }
00178 
00179     self_type operator--(int)
00180     {
00181       self_type tmp(*this);
00182       --m_it;
00183       return tmp;
00184     }
00185 
00186     reference operator*() const { return m_fun(*m_it); }
00187     pointer operator->() const { return &m_fun(*m_it); }
00188     
00189     bool operator==( const self_type& that ) const { return m_it == that.m_it; }
00190     bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
00191     bool operator==( const Iterator& it ) const { return m_it == it; }
00192     bool operator!=( const Iterator& it ) const { return m_it != it; }
00193 
00194  private:
00196     Iterator m_it;
00197 
00199     Function m_fun;
00200 
00201   }; // class wrapped_iterator_by_category [bidirectional_iterator_tag]
00202 
00203   /*-------------------------------------------------------------------------*/
00208   template<typename Value, typename Iterator, typename Function>
00209   class wrapped_iterator_by_category
00210   <std::random_access_iterator_tag, Value, Iterator, Function>
00211   {
00212   public:
00213     typedef typename std::iterator_traits<Iterator>::difference_type
00214     difference_type;
00215     typedef Value value_type;
00216     typedef value_type* pointer;
00217     typedef value_type& reference;
00218     typedef typename std::iterator_traits<Iterator>::iterator_category
00219     iterator_category;
00220 
00221     typedef
00222     wrapped_iterator_by_category
00223     <std::random_access_iterator_tag, Value, Iterator, Function>
00224     self_type;
00225 
00226   public:
00227     wrapped_iterator_by_category() {}
00228     wrapped_iterator_by_category( const Iterator& it )
00229       : m_it(it)
00230     { }
00231     wrapped_iterator_by_category( const Iterator& it, const Function& f )
00232       : m_it(it), m_fun(f)
00233     { }
00234     template<typename V, typename I>
00235     wrapped_iterator_by_category
00236     ( const wrapped_iterator_by_category
00237       <std::random_access_iterator_tag, V, I, Function>& that )
00238       : m_it(that.m_it), m_fun(that.m_fun)
00239     { }
00240     template<typename C, typename V, typename I, typename F>
00241     wrapped_iterator_by_category
00242     ( const wrapped_iterator_by_category<C, V, I, F>& that )
00243       : m_it(that.get_iterator()), m_fun(that.get_function())
00244     { }
00245 
00246     const Iterator& get_iterator() const { return m_it; }
00247     const Function& get_function() const { return m_fun; }
00248 
00249     self_type& operator++()
00250     {
00251       ++m_it;
00252       return *this;
00253     }
00254 
00255     self_type operator++(int)
00256     {
00257       self_type tmp(*this);
00258       ++m_it;
00259       return tmp;
00260     }
00261 
00262     self_type& operator--()
00263     {
00264       --m_it;
00265       return *this;
00266     }
00267 
00268     self_type operator--(int)
00269     {
00270       self_type tmp(*this);
00271       --m_it;
00272       return tmp;
00273     }
00274 
00275     reference operator*() const { return m_fun(*m_it); }
00276     pointer operator->() const { return &m_fun(*m_it); }
00277 
00278     bool operator==( const self_type& that ) const { return m_it == that.m_it; }
00279     bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
00280     bool operator==( const Iterator& it ) const { return m_it == it; }
00281     bool operator!=( const Iterator& it ) const { return m_it != it; }
00282     bool operator<( const self_type& that ) const { return m_it < that.m_it; }
00283     bool operator<=( const self_type& that ) const { return m_it <= that.m_it; }
00284     bool operator>( const self_type& that ) const { return m_it > that.m_it; }
00285     bool operator>=( const self_type& that ) const { return m_it >= that.m_it; }
00286 
00287     difference_type operator-( const self_type& that ) const
00288     {
00289       return m_it - that.m_it;
00290     }
00291 
00292     self_type& operator+=(int n)
00293     {
00294       m_it += n;
00295       return *this;
00296     }
00297 
00298     self_type operator+(int n) const
00299     {
00300       self_type result(*this);
00301       result += n;
00302       return result;
00303     }
00304 
00305     self_type& operator-=(int n) { return *this += -n; }
00306 
00307     self_type operator-(int n) const
00308     {
00309       self_type result(*this);
00310       result -= n;
00311       return result;
00312     }
00313 
00314     reference operator[](int n) { return m_fun(m_it[n]); }
00315 
00316   private:
00318     Iterator m_it;
00319 
00321     Function m_fun;
00322 
00323   }; // class wrapped_iterator_by_category [random_access_iterator_tag]
00324 
00325   template<typename Value, typename Iterator, typename Function>
00326   wrapped_iterator_by_category
00327   <std::random_access_iterator_tag, Value, Iterator, Function>
00328   operator+
00329   ( int n,
00330     const wrapped_iterator_by_category
00331     < std::random_access_iterator_tag, Value, Iterator, Function >& it )
00332   {
00333     return it + n;
00334   }
00335 
00336   template<typename Value, typename Iterator, typename Function>
00337   wrapped_iterator_by_category
00338   <std::random_access_iterator_tag, Value, Iterator, Function>
00339   operator-
00340   ( int n,
00341     const wrapped_iterator_by_category
00342     < std::random_access_iterator_tag, Value, Iterator, Function >& it )
00343   {
00344     return it - n;
00345   }
00346 
00347   /*-------------------------------------------------------------------------*/
00360   template <typename Value, typename Iterator, typename Function>
00361   class wrapped_iterator
00362   {
00363   public:
00365     typedef wrapped_iterator_by_category
00366     < typename std::iterator_traits<Iterator>::iterator_category,
00367       Value, Iterator, Function >
00368     iterator_type;
00369 
00370   }; // class wrapped_iterator
00371 } // namespace claw
00372 
00373 #endif // __CLAW_ITERATOR_HPP__