BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataIterator.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Data/OutputDataIterator.h
6 //! @brief Defines and implements template class OutputDataIterator.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #ifndef BORNAGAIN_CORE_INTENSITY_OUTPUTDATAITERATOR_H
16 #define BORNAGAIN_CORE_INTENSITY_OUTPUTDATAITERATOR_H
17 
18 #include <cstddef>
19 #include <iterator>
20 #include <utility>
21 
22 //! Iterator for underlying OutputData container.
23 //! @ingroup tools_internal
24 
25 template <class TValue, class TContainer> class OutputDataIterator
26 {
27 public:
28  //! Empty constructor to comply with stl forward iterators
30 
31  //! constructor
32  OutputDataIterator(TContainer* p_output_data, size_t start_at_index = 0);
33 
34  //! templated copy construction
35  template <class TValue2, class TContainer2>
37 
38  //! non-templated copy construction
40 
41  //! templated copy assignment
42  template <class TValue2, class TContainer2>
45 
46  //! non-templated copy asssignment
49 
51 
52  //! prefix increment
54 
55  //! postfix increment
57 
58  //! retrieve current element
59  TValue& operator*() const;
60 
61  //! pointer access
62  TValue* operator->() const;
63 
64  //! Returns current index
65  size_t getIndex() const { return m_current_index; }
66 
67  //! Returns container pointer
68  TContainer* getContainer() const { return mp_output_data; }
69 
70  //! Swaps iterators
72 
73  // typedefs for std::iterator_traits
74  typedef std::forward_iterator_tag iterator_category;
75  typedef TValue value_type;
76  typedef ptrdiff_t difference_type;
77  typedef TValue* pointer_type;
78  typedef TValue& reference_type;
79  //#ifdef _MSC_VER
80  typedef TValue* pointer;
81  typedef TValue& reference;
82  //#endif
83 
84 private:
86  TContainer* mp_output_data;
87 };
88 
89 //! make Swappable
90 template <class TValue, class TContainer>
93 {
94  left.swap(right);
95 }
96 
97 //! test for equality
98 template <class TValue1, class TContainer1, class TValue2, class TContainer2>
101 
102 //! test for inequality
103 template <class TValue1, class TContainer1, class TValue2, class TContainer2>
106 
107 template <class TValue, class TContainer>
108 OutputDataIterator<TValue, TContainer>::OutputDataIterator() : m_current_index(0), mp_output_data(0)
109 {
110 }
111 
112 template <class TValue, class TContainer>
114  size_t start_at_index)
115  : m_current_index(start_at_index), mp_output_data(p_output_data)
116 {
117 }
118 
119 template <class TValue, class TContainer>
120 template <class TValue2, class TContainer2>
123  : m_current_index(0), mp_output_data(0)
124 {
125  mp_output_data = static_cast<TContainer*>(other.getContainer());
126  m_current_index = other.getIndex();
127 }
128 
129 template <class TValue, class TContainer>
132  : m_current_index(0), mp_output_data(0)
133 {
134  mp_output_data = other.getContainer();
135  m_current_index = other.getIndex();
136 }
137 
138 template <class TValue, class TContainer>
139 template <class TValue2, class TContainer2>
142 {
144  swap(copy);
145  return *this;
146 }
147 
148 template <class TValue, class TContainer>
151 {
153  swap(copy);
154  return *this;
155 }
156 
157 template <class TValue, class TContainer>
159 {
160 }
161 
162 template <class TValue, class TContainer>
164 {
165  if (m_current_index < mp_output_data->getAllocatedSize()) {
166  ++m_current_index;
167  }
168  return *this;
169 }
170 
171 template <class TValue, class TContainer>
173 {
175  this->operator++();
176  return result;
177 }
178 
179 template <class TValue, class TContainer>
181 {
182  return (*mp_output_data)[m_current_index];
183 }
184 
185 template <class TValue, class TContainer>
187 {
188  return &((*mp_output_data)[m_current_index]);
189 }
190 
191 template <class TValue, class TContainer>
193 {
194  std::swap(this->m_current_index, other.m_current_index);
195  std::swap(this->mp_output_data, other.mp_output_data);
196 }
197 
198 //! test for equality
199 template <class TValue1, class TContainer1, class TValue2, class TContainer2>
202 {
203  return left.getContainer() == right.getContainer() && left.getIndex() == right.getIndex();
204 }
205 
206 //! test for inequality
207 template <class TValue1, class TContainer1, class TValue2, class TContainer2>
210 {
211  return !(left == right);
212 }
213 
214 #endif // BORNAGAIN_CORE_INTENSITY_OUTPUTDATAITERATOR_H
void swap(OutputDataIterator< TValue, TContainer > &left, OutputDataIterator< TValue, TContainer > &right)
make Swappable
bool operator!=(const BasicVector3D< T > &a, const BasicVector3D< T > &b)
Comparison of two vectors for inequality.
bool operator==(const BasicVector3D< T > &a, const BasicVector3D< T > &b)
Comparison of two vectors for equality.
Iterator for underlying OutputData container.
OutputDataIterator< TValue, TContainer > & operator=(const OutputDataIterator< TValue2, TContainer2 > &right)
templated copy assignment
OutputDataIterator< TValue, TContainer > & operator++()
prefix increment
void swap(OutputDataIterator< TValue, TContainer > &other)
Swaps iterators.
size_t getIndex() const
Returns current index.
TContainer * getContainer() const
Returns container pointer.
TValue * operator->() const
pointer access
TContainer * mp_output_data
std::forward_iterator_tag iterator_category
OutputDataIterator()
Empty constructor to comply with stl forward iterators.
TValue & operator*() const
retrieve current element