BornAgain  1.19.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 reflection and scattering
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 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_DEVICE_DATA_OUTPUTDATAITERATOR_H
21 #define BORNAGAIN_DEVICE_DATA_OUTPUTDATAITERATOR_H
22 
23 #include <cstddef>
24 #include <iterator>
25 #include <utility>
26 
27 //! Iterator for underlying OutputData container.
28 //! @ingroup tools_internal
29 
30 template <class TValue, class TContainer> class OutputDataIterator {
31 public:
32  //! Empty constructor to comply with stl forward iterators
34 
35  //! constructor
36  OutputDataIterator(TContainer* p_output_data, size_t start_at_index = 0);
37 
38  //! templated copy construction
39  template <class TValue2, class TContainer2>
41 
42  //! non-templated copy construction
44 
45  //! templated copy assignment
46  template <class TValue2, class TContainer2>
49 
50  //! non-templated copy asssignment
53 
55 
56  //! prefix increment
58 
59  //! postfix increment
61 
62  //! retrieve current element
63  TValue& operator*() const;
64 
65  //! pointer access
66  TValue* operator->() const;
67 
68  //! Returns current index
69  size_t getIndex() const { return m_current_index; }
70 
71  //! Returns container pointer
72  TContainer* getContainer() const { return m_output_data; }
73 
74  //! Swaps iterators
76 
77  // typedefs for std::iterator_traits
78  typedef std::forward_iterator_tag iterator_category;
79  typedef TValue value_type;
80  typedef ptrdiff_t difference_type;
81  typedef TValue* pointer_type;
82  typedef TValue& reference_type;
83  //#ifdef _MSC_VER
84  typedef TValue* pointer;
85  typedef TValue& reference;
86  //#endif
87 
88 private:
90  TContainer* m_output_data;
91 };
92 
93 //! make Swappable
94 template <class TValue, class TContainer>
97 {
98  left.swap(right);
99 }
100 
101 //! test for equality
102 template <class TValue1, class TContainer1, class TValue2, class TContainer2>
105 
106 //! test for inequality
107 template <class TValue1, class TContainer1, class TValue2, class TContainer2>
110 
111 template <class TValue, class TContainer>
112 OutputDataIterator<TValue, TContainer>::OutputDataIterator() : m_current_index(0), m_output_data(0)
113 {
114 }
115 
116 template <class TValue, class TContainer>
118  size_t start_at_index)
119  : m_current_index(start_at_index), m_output_data(p_output_data)
120 {
121 }
122 
123 template <class TValue, class TContainer>
124 template <class TValue2, class TContainer2>
127  : m_current_index(0), m_output_data(0)
128 {
129  m_output_data = static_cast<TContainer*>(other.getContainer());
130  m_current_index = other.getIndex();
131 }
132 
133 template <class TValue, class TContainer>
136  : m_current_index(0), m_output_data(0)
137 {
138  m_output_data = other.getContainer();
139  m_current_index = other.getIndex();
140 }
141 
142 template <class TValue, class TContainer>
143 template <class TValue2, class TContainer2>
146 {
148  swap(copy);
149  return *this;
150 }
151 
152 template <class TValue, class TContainer>
155 {
157  swap(copy);
158  return *this;
159 }
160 
161 template <class TValue, class TContainer>
163 {
164 }
165 
166 template <class TValue, class TContainer>
168 {
169  if (m_current_index < m_output_data->getAllocatedSize()) {
170  ++m_current_index;
171  }
172  return *this;
173 }
174 
175 template <class TValue, class TContainer>
177 {
179  this->operator++();
180  return result;
181 }
182 
183 template <class TValue, class TContainer>
185 {
186  return (*m_output_data)[m_current_index];
187 }
188 
189 template <class TValue, class TContainer>
191 {
192  return &((*m_output_data)[m_current_index]);
193 }
194 
195 template <class TValue, class TContainer>
197 {
198  std::swap(this->m_current_index, other.m_current_index);
199  std::swap(this->m_output_data, other.m_output_data);
200 }
201 
202 //! test for equality
203 template <class TValue1, class TContainer1, class TValue2, class TContainer2>
206 {
207  return left.getContainer() == right.getContainer() && left.getIndex() == right.getIndex();
208 }
209 
210 //! test for inequality
211 template <class TValue1, class TContainer1, class TValue2, class TContainer2>
214 {
215  return !(left == right);
216 }
217 
218 #endif // BORNAGAIN_DEVICE_DATA_OUTPUTDATAITERATOR_H
219 #endif // USER_API
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
TContainer * m_output_data
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
std::forward_iterator_tag iterator_category
OutputDataIterator()
Empty constructor to comply with stl forward iterators.
TValue & operator*() const
retrieve current element