1 | #ifndef _theplu_yat_utility_weight_iterator_ |
---|
2 | #define _theplu_yat_utility_weight_iterator_ |
---|
3 | |
---|
4 | // $Id: WeightIterator.h 1486 2008-09-09 21:17:19Z 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 this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include <boost/iterator/iterator_adaptor.hpp> |
---|
28 | #include <boost/type_traits/remove_reference.hpp> |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace utility { |
---|
33 | |
---|
34 | /** |
---|
35 | @brief WeightIterator |
---|
36 | */ |
---|
37 | template<typename Base> |
---|
38 | class WeightIterator |
---|
39 | : public boost::iterator_adaptor< |
---|
40 | WeightIterator<Base> // Derived |
---|
41 | , Base // Base |
---|
42 | , typename boost::remove_reference<typename iterator_traits<Base>::weight_reference>::type |
---|
43 | , boost::forward_traversal_tag // CategoryOrTraversal |
---|
44 | , typename iterator_traits<Base>::weight_reference // Reference |
---|
45 | > |
---|
46 | |
---|
47 | { |
---|
48 | public: |
---|
49 | /** |
---|
50 | \brief Constructor from \a Base iterator |
---|
51 | */ |
---|
52 | explicit WeightIterator(Base b) |
---|
53 | : WeightIterator::iterator_adaptor_(b) {} |
---|
54 | |
---|
55 | /** |
---|
56 | \brief Conversion constructor. |
---|
57 | |
---|
58 | Create a WeightIterator<Base> from a |
---|
59 | WeightIterator<B2>. Possible if B2 is convertible to a |
---|
60 | Base. Constructor allows implicit conversions such as iterator |
---|
61 | to const_iterator. |
---|
62 | */ |
---|
63 | template<typename B2> |
---|
64 | WeightIterator(WeightIterator<B2> other, |
---|
65 | typename boost::enable_if_convertible<B2, Base>::type* = 0 ) |
---|
66 | : WeightIterator::iterator_adaptor_(other.base()) {} |
---|
67 | |
---|
68 | /** |
---|
69 | using iterator_traits::data on Base iterator |
---|
70 | |
---|
71 | \return data |
---|
72 | */ |
---|
73 | typename iterator_traits<Base>::weight_reference operator*(void) const |
---|
74 | { return iterator_traits<Base>().weight(this->base()); } |
---|
75 | |
---|
76 | private: |
---|
77 | }; |
---|
78 | |
---|
79 | /** |
---|
80 | \brief convenient function to create WeightIterator |
---|
81 | |
---|
82 | Convenient function in same fashion as std::make_pair. |
---|
83 | */ |
---|
84 | template<typename Base> |
---|
85 | WeightIterator<Base> weight_iterator(Base base) |
---|
86 | { |
---|
87 | return WeightIterator<Base>(base); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | }}} // of namespace utility, yat, and theplu |
---|
93 | |
---|
94 | #endif |
---|