NiHu  2.0
casted_iterator.hpp
Go to the documentation of this file.
1 // This file is a part of NiHu, a C++ BEM template library.
2 //
3 // Copyright (C) 2012-2014 Peter Fiala <fiala@hit.bme.hu>
4 // Copyright (C) 2012-2014 Peter Rucz <rucz@hit.bme.hu>
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
23 #ifndef CASTED_ITERATOR_HPP_INCLUDED
24 #define CASTED_ITERATOR_HPP_INCLUDED
25 
26 #include <cstddef> // size_t
27 
28 namespace NiHu
29 {
30 
36 template <class FromIt, class To, class Through = To>
38  private FromIt
39 {
40 public:
43 
44  typedef typename FromIt::difference_type difference_type;
45 
47  typedef To value_type;
48 
49  typedef To value_t;
50 
51  FromIt const &base() const
52  {
53  return *static_cast<FromIt const *>(this);
54  }
55 
56  FromIt &base()
57  {
58  return static_cast<FromIt &>(*this);
59  }
60 
64  casted_iterator(FromIt const &base = FromIt()) :
65  FromIt(base)
66  {
67  }
68 
69  bool operator != (casted_iterator const &other) const
70  {
71  return base() != other.base();
72 
73  }
74 
75  bool operator == (casted_iterator const &other) const
76  {
77  return base() == other.base();
78  }
79 
80  casted_iterator operator+(difference_type offset) const
81  {
82  return base() + offset;
83  }
84 
85  casted_iterator operator-(difference_type offset) const
86  {
87  return base() - offset;
88  }
89 
90  difference_type operator-(casted_iterator const &other) const
91  {
92  return base() - other.base();
93  }
94 
95  casted_iterator &operator++()
96  {
97  return static_cast<casted_iterator &>(++base());
98  }
99 
100  casted_iterator operator++(int)
101  {
102  return base()++;
103  }
104 
108  value_t const &operator *(void) const
109  {
110  return static_cast<value_t const &>(
111  static_cast<Through const &>(
112  FromIt::operator*()));
113  }
114 
118  value_t const &operator[] (size_t idx) const
119  {
120  return static_cast<value_t const &>(
121  static_cast<Through const &>(
122  base()[idx]));
123  }
124 
128  value_t const *operator->(void) const
129  {
130  return static_cast<value_t const*>(
131  static_cast<Through const*>(
132  &(*base())));
133  }
134 };
135 
136 }
137 
138 #endif // CASTED_ITERATOR_HPP_INCLUDED
139 
NiHu::casted_iterator::operator[]
const value_t & operator[](size_t idx) const
index operator converts indexed element to casted type
Definition: casted_iterator.hpp:118
NiHu::casted_iterator::casted_iterator
casted_iterator(FromIt const &base=FromIt())
(copy) constructor from base iterator
Definition: casted_iterator.hpp:64
NiHu::casted_iterator::type
casted_iterator type
self returning metafunction
Definition: casted_iterator.hpp:42
NiHu::casted_iterator
iterator class provides access to its value_t after static cast
Definition: casted_iterator.hpp:37
NiHu::casted_iterator::value_type
To value_type
the new value type
Definition: casted_iterator.hpp:47
NiHu::casted_iterator::operator->
const value_t * operator->(void) const
dereference operator converts dereferenced element to casted type
Definition: casted_iterator.hpp:128
NiHu::field_view
Field automatically generated from an element using a field generation option.
Definition: field.hpp:222
NiHu::casted_iterator::operator*
const value_t & operator*(void) const
dereference operator converts dereferenced element to casted type
Definition: casted_iterator.hpp:108