source: trunk/yat/utility/DataIterator.h @ 1707

Last change on this file since 1707 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1#ifndef _theplu_yat_utility_data_iterator_
2#define _theplu_yat_utility_data_iterator_
3
4// $Id: DataIterator.h 1487 2008-09-10 08:41:36Z jari $
5
6/*
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "iterator_traits.h"
26
27#include <boost/iterator/iterator_adaptor.hpp>
28#include <boost/type_traits/remove_reference.hpp>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  /**
35     @brief DataIterator
36  */
37  template<typename Base>
38  class DataIterator
39    : public boost::iterator_adaptor<
40    DataIterator<Base>               // Derived
41    , Base                          // Base
42    , typename boost::remove_reference<typename iterator_traits<Base>::data_reference>::type
43    , boost::forward_traversal_tag    // CategoryOrTraversal
44    , typename iterator_traits<Base>::data_reference // Reference
45    >
46
47  {
48  public:
49    /**
50       \brief Constructor from \a Base iterator
51     */
52    explicit DataIterator(Base b)
53      : DataIterator::iterator_adaptor_(b) {}
54
55    /**
56       \brief Conversion constructor.
57
58       Create a DataIterator<Base> from a DataIterator<B2>. Possible
59       if B2 is convertible to a Base. Constructor allows implicit
60       conversions such as iterator to const_iterator.
61     */
62    template<typename B2>
63    DataIterator(DataIterator<B2> other,
64                 typename boost::enable_if_convertible<B2, Base>::type* = 0 )
65      : DataIterator::iterator_adaptor_(other.base()) {}
66
67    /**
68       using iterator_traits::data on Base iterator
69
70       \return data
71     */
72    typename iterator_traits<Base>::data_reference operator*(void) const
73    { return iterator_traits<Base>().data(this->base()); }
74
75  private:
76  };
77
78  /**
79     \brief convenient function to create DataIterator
80
81     Convenient function in same fashion as std::make_pair.
82   */
83  template<typename Base>
84  DataIterator<Base> data_iterator(Base base)
85  {
86    return DataIterator<Base>(base);
87  }
88 
89
90
91}}} // of namespace utility, yat, and theplu
92
93#endif
Note: See TracBrowser for help on using the repository browser.