• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List
  • File Members

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-2008 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@yahoo.fr
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 
00082     self_type& operator++()
00083     {
00084       ++m_it;
00085       return *this;
00086     }
00087 
00088     self_type operator++(int)
00089     {
00090       self_type tmp(*this);
00091       ++m_it;
00092       return tmp;
00093     }
00094 
00095     reference operator*() { return m_fun(*m_it); }
00096     pointer operator->() { return &m_fun(*m_it); }
00097 
00098     bool operator==( const self_type& that ) const { return m_it == that.m_it; }
00099     bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
00100     bool operator==( const Iterator& it ) const { return m_it == it; }
00101     bool operator!=( const Iterator& it ) const { return m_it != it; }
00102 
00103   private:
00105     Iterator m_it;
00106 
00108     Function m_fun;
00109 
00110   }; // class wrapped_iterator_by_category [forward_iterator_tag]
00111 
00112   /*-------------------------------------------------------------------------*/
00118   template<typename Value, typename Iterator, typename Function>
00119   class wrapped_iterator_by_category
00120   <std::bidirectional_iterator_tag, Value, Iterator, Function>
00121   {
00122   public:
00123     typedef typename std::iterator_traits<Iterator>::difference_type
00124     difference_type;
00125     typedef Value value_type;
00126     typedef value_type* pointer;
00127     typedef value_type& reference;
00128     typedef typename std::iterator_traits<Iterator>::iterator_category
00129     iterator_category;
00130 
00131     typedef
00132     wrapped_iterator_by_category
00133     <std::bidirectional_iterator_tag, Value, Iterator, Function> self_type;
00134 
00135   public:
00136     wrapped_iterator_by_category() {}
00137     wrapped_iterator_by_category( const Iterator& it )
00138       : m_it(it)
00139     { }
00140     wrapped_iterator_by_category( const Iterator& it, const Function& f )
00141       : m_it(it), m_fun(f)
00142     { }
00143 
00144     self_type& operator++()
00145     {
00146       ++m_it;
00147       return *this;
00148     }
00149 
00150     self_type operator++(int)
00151     {
00152       self_type tmp(*this);
00153       ++m_it;
00154       return tmp;
00155     }
00156 
00157     self_type& operator--()
00158     {
00159       --m_it;
00160       return *this;
00161     }
00162 
00163     self_type operator--(int)
00164     {
00165       self_type tmp(*this);
00166       --m_it;
00167       return tmp;
00168     }
00169 
00170     reference operator*() { return m_fun(*m_it); }
00171     pointer operator->() { return &m_fun(*m_it); }
00172     
00173     bool operator==( const self_type& that ) const { return m_it == that.m_it; }
00174     bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
00175     bool operator==( const Iterator& it ) const { return m_it == it; }
00176     bool operator!=( const Iterator& it ) const { return m_it != it; }
00177 
00178  private:
00180     Iterator m_it;
00181 
00183     Function m_fun;
00184 
00185   }; // class wrapped_iterator_by_category [bidirectional_iterator_tag]
00186 
00187   /*-------------------------------------------------------------------------*/
00192   template<typename Value, typename Iterator, typename Function>
00193   class wrapped_iterator_by_category
00194   <std::random_access_iterator_tag, Value, Iterator, Function>
00195   {
00196   public:
00197     typedef typename std::iterator_traits<Iterator>::difference_type
00198     difference_type;
00199     typedef Value value_type;
00200     typedef value_type* pointer;
00201     typedef value_type& reference;
00202     typedef typename std::iterator_traits<Iterator>::iterator_category
00203     iterator_category;
00204 
00205     typedef
00206     wrapped_iterator_by_category
00207     <std::random_access_iterator_tag, Value, Iterator, Function>
00208     self_type;
00209 
00210   public:
00211     wrapped_iterator_by_category() {}
00212     wrapped_iterator_by_category( const Iterator& it )
00213       : m_it(it)
00214     { }
00215     wrapped_iterator_by_category( const Iterator& it, const Function& f )
00216       : m_it(it), m_fun(f)
00217     { }
00218 
00219     self_type& operator++()
00220     {
00221       ++m_it;
00222       return *this;
00223     }
00224 
00225     self_type operator++(int)
00226     {
00227       self_type tmp(*this);
00228       ++m_it;
00229       return tmp;
00230     }
00231 
00232     self_type& operator--()
00233     {
00234       --m_it;
00235       return *this;
00236     }
00237 
00238     self_type operator--(int)
00239     {
00240       self_type tmp(*this);
00241       --m_it;
00242       return tmp;
00243     }
00244 
00245     reference operator*() { return m_fun(*m_it); }
00246     pointer operator->() { return &m_fun(*m_it); }
00247 
00248     bool operator==( const self_type& that ) const { return m_it == that.m_it; }
00249     bool operator!=( const self_type& that ) const { return m_it != that.m_it; }
00250     bool operator==( const Iterator& it ) const { return m_it == it; }
00251     bool operator!=( const Iterator& it ) const { return m_it != it; }
00252     bool operator<( const self_type& that ) const { return m_it < that.m_it; }
00253     bool operator<=( const self_type& that ) const { return m_it <= that.m_it; }
00254     bool operator>( const self_type& that ) const { return m_it > that.m_it; }
00255     bool operator>=( const self_type& that ) const { return m_it >= that.m_it; }
00256 
00257     self_type& operator+=(int n)
00258     {
00259       m_it += n;
00260       return *this;
00261     }
00262 
00263     self_type operator+(int n) const
00264     {
00265       self_type result(*this);
00266       result += n;
00267       return result;
00268     }
00269 
00270     self_type& operator-=(int n) { return *this += -n; }
00271 
00272     self_type operator-(int n) const
00273     {
00274       self_type result(*this);
00275       result -= n;
00276       return result;
00277     }
00278 
00279     reference operator[](int n) { return m_fun(m_it[n]); }
00280 
00281   private:
00283     Iterator m_it;
00284 
00286     Function m_fun;
00287 
00288   }; // class wrapped_iterator_by_category [random_access_iterator_tag]
00289 
00290   template<typename Value, typename Iterator, typename Function>
00291   wrapped_iterator_by_category
00292   <std::random_access_iterator_tag, Value, Iterator, Function>
00293   operator+
00294   ( int n,
00295     const wrapped_iterator_by_category
00296     < std::random_access_iterator_tag, Value, Iterator, Function >& it )
00297   {
00298     return it + n;
00299   }
00300 
00301   template<typename Value, typename Iterator, typename Function>
00302   wrapped_iterator_by_category
00303   <std::random_access_iterator_tag, Value, Iterator, Function>
00304   operator-
00305   ( int n,
00306     const wrapped_iterator_by_category
00307     < std::random_access_iterator_tag, Value, Iterator, Function >& it )
00308   {
00309     return it - n;
00310   }
00311 
00312   /*-------------------------------------------------------------------------*/
00325   template <typename Value, typename Iterator, typename Function>
00326   class wrapped_iterator
00327   {
00328   public:
00330     typedef wrapped_iterator_by_category
00331     < typename std::iterator_traits<Iterator>::iterator_category,
00332       Value, Iterator, Function >
00333     iterator_type;
00334 
00335   }; // class wrapped_iterator
00336 } // namespace claw
00337 
00338 #endif // __CLAW_ITERATOR_HPP__

Generated on Sat Sep 18 2010 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.7.1