libpnicore
array_iterator.hpp
1 //
2 // (c) Copyright 2013 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpnicore.
5 //
6 // libpnicore 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 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpnicore 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 libpnicore. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Jan 16, 2013
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include "../error/exceptions.hpp"
27 #include "value.hpp"
28 #include "value_ref.hpp"
29 
30 namespace pni{
31 namespace core{
32 
33  class array;
34 
35  //=========================================================================
49  template<int const_flag> struct array_iterator_types
50  {};
51 
52  //=========================================================================
64  template<> struct array_iterator_types<0>
65  {
67  typedef array *cont_ptr;
73  typedef value_ref* ptr_type;
76  };
77 
78 
79 
80  //=========================================================================
91  template<> struct array_iterator_types<1>
92  {
94  typedef const array *cont_ptr;
96  typedef value value_type;
98  typedef value return_type;
100  typedef const value *ptr_type;
102  typedef const value &ref_type;
103  };
104 
105 
106  //=========================================================================
107 
121  template<int const_flag> class array_iterator
122  {
123  private:
126 
129 
131  ssize_t _state;
132  public:
133  //====================public types==================================
135  typedef typename iterator_types::value_type value_type;
137  typedef typename iterator_types::ptr_type pointer;
139  typedef typename iterator_types::ref_type reference;
141  typedef typename iterator_types::cont_ptr cptr_type;
143  typedef ssize_t difference_type;
145  typedef std::random_access_iterator_tag iterator_category;
148  //================constructor and destructor========================
150  array_iterator():_container(nullptr),_state(0) {}
151 
152  //------------------------------------------------------------------
162  explicit array_iterator(cptr_type container,size_t state=0):
163  _container(container),
164  _state(state)
165  { }
166 
167  //------------------------------------------------------------------
169  array_iterator(const iterator_type &i):
170  _container(i._container),
171  _state(i._state)
172  { }
173 
174  //------------------------------------------------------------------
176  array_iterator(iterator_type &&i):
177  _container(i._container),
178  _state(i._state)
179  {
180  i._container = nullptr;
181  i._state = 0;
182  }
183 
184  //====================public methods and operators==================
198  explicit operator bool() const
199  {
200  //if(!this->_container) return false;
201  //ssize_t size = (ssize_t)(this->_container->size());
202  return !((!_container)||
203  (_state >= ssize_t(_container->size()))||
204  (_state<0));
205  }
206 
207  //------------------------------------------------------------------
219  {
220  if(!(*this))
221  throw iterator_error(EXCEPTION_RECORD,"Iterator invalid!");
222 
223  return (*_container)[_state];
224  }
225 
226  //------------------------------------------------------------------
238  const
239  {
240  if(!(*this))
241  throw iterator_error(EXCEPTION_RECORD,"Iterator invalid!");
242 
243  return (*_container)[_state];
244  }
245  //------------------------------------------------------------------
247  iterator_type &operator++()
248  {
249  _state++;
250  return *this;
251  }
252 
253  //------------------------------------------------------------------
255  iterator_type operator++(int)
256  {
257  iterator_type temp = *this;
258  ++(*this);
259  return temp;
260  }
261 
262  //------------------------------------------------------------------
264  iterator_type &operator--()
265  {
266  _state--;
267  return *this;
268  }
269 
270  //------------------------------------------------------------------
272  iterator_type operator--(int)
273  {
274  iterator_type tmp = *this;
275  --(*this);
276  return tmp;
277  }
278 
279  //------------------------------------------------------------------
281  iterator_type &operator+=(ssize_t i)
282  {
283  _state += i;
284  return *this;
285  }
286 
287  //------------------------------------------------------------------
289  iterator_type &operator-=(ssize_t i)
290  {
291  _state -= i;
292  return *this;
293  }
294  //------------------------------------------------------------------
296  bool operator==(const iterator_type &a)
297  {
298  //check if the iterators point to the same container
299  if(_container != a._container) return false;
300  //check if the iterators point to the same element
301  //within the container
302  if(_state != a._state) return false;
303 
304  return true;
305  }
306 
307  //------------------------------------------------------------------
309  bool operator!=(const iterator_type &a)
310  {
311  if((*this)==a) return false;
312  return true;
313  }
314 
315  //===============comparison operators==============================
317  bool operator<(const iterator_type &b)
318  {
319  return _state < b._state;
320  }
321 
322  //-----------------------------------------------------------------
324  bool operator<=(const iterator_type &b)
325  {
326  return _state <= b._state;
327  }
328 
329  //-----------------------------------------------------------------
331  bool operator>(const iterator_type &b)
332  {
333  return _state > b._state;
334  }
335 
336  //-----------------------------------------------------------------
338  bool operator>=(const iterator_type &b)
339  {
340  return _state >= b._state;
341  }
342 
344  ssize_t state() const { return this->_state; }
345 
346  };
347 
348  //================binary arithmetic operators===============================
356 
362  template<int const_flag>
369  ssize_t b)
370  {
372  iter += b;
373  return iter;
374  }
375 
376  //--------------------------------------------------------------------------
388  template<int const_flag>
391  {
392  return b+a;
393  }
394 
395  //--------------------------------------------------------------------------
407  template<int const_flag>
409  ssize_t b)
410  {
412  iter -= b;
413  return iter;
414  }
415 
416  //--------------------------------------------------------------------------
428  template<int const_flag>
431  {
432  return a.state() - b.state();
433  }
434 
435 //end of namespace
436 }
437 }
bool operator==(const iterator_type &a)
comparsion operator - equality
Definition: array_iterator.hpp:296
value_ref return_type
return type for the dereferencing operator
Definition: array_iterator.hpp:71
iterator_type & operator-=(ssize_t i)
compound assignment with -=
Definition: array_iterator.hpp:289
array_iterator()
default constructor
Definition: array_iterator.hpp:150
type map for the array_iterator template
Definition: array_iterator.hpp:49
value_ref * ptr_type
pointer type for -> operator
Definition: array_iterator.hpp:73
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
value_ref value_type
reference type of the element
Definition: array_iterator.hpp:69
type erasure array types
Definition: array.hpp:74
const value * ptr_type
pointer type for -> operator
Definition: array_iterator.hpp:100
iterator_type operator++(int)
increment iterator position
Definition: array_iterator.hpp:255
iterator_type & operator++()
increment iterator position
Definition: array_iterator.hpp:247
array_iterator_types< const_flag > iterator_types
iterator type
Definition: array_iterator.hpp:125
array_iterator< const_flag > operator+(ssize_t a, const array_iterator< const_flag > &b)
add offset to iterator
Definition: array_iterator.hpp:389
array_iterator_types< const_flag >::cont_ptr _container
pointer to the container object
Definition: array_iterator.hpp:128
bool operator>=(const iterator_type &b)
greater equal than operator
Definition: array_iterator.hpp:338
array * cont_ptr
container pointer
Definition: array_iterator.hpp:67
iterator_types::ptr_type pointer
pointer type the iterator provides
Definition: array_iterator.hpp:137
Definition: add_op.hpp:29
ssize_t _state
actual position state of the iterator
Definition: array_iterator.hpp:131
array iterator
Definition: array_iterator.hpp:121
array_iterator(iterator_type &&i)
move constructor
Definition: array_iterator.hpp:176
array_iterator(cptr_type container, size_t state=0)
constructor
Definition: array_iterator.hpp:162
bool operator!=(const iterator_type &a)
comparison operator - inequality
Definition: array_iterator.hpp:309
ssize_t difference_type
difference type of the iterator
Definition: array_iterator.hpp:143
array_iterator< const_flag > iterator_type
iterator type
Definition: array_iterator.hpp:147
array_iterator_types< const_flag >::return_type operator*()
dereferencing operator
Definition: array_iterator.hpp:218
array_iterator_types< const_flag >::return_type operator*() const
dereferencing operator
Definition: array_iterator.hpp:237
iterator_types::value_type value_type
value type of the container
Definition: array_iterator.hpp:135
ssize_t operator-(const array_iterator< const_flag > &a, const array_iterator< const_flag > &b)
subtract two iterators
Definition: array_iterator.hpp:429
iterator_type & operator--()
decrement operators
Definition: array_iterator.hpp:264
array_iterator(const iterator_type &i)
copy constructor
Definition: array_iterator.hpp:169
iterator_types::ref_type reference
reference type the iterator provides
Definition: array_iterator.hpp:139
iterator error
Definition: exceptions.hpp:692
value_ref ref_type
reference type
Definition: array_iterator.hpp:75
value value_type
value type of the iterator
Definition: array_iterator.hpp:96
bool operator>(const iterator_type &b)
greater than operator
Definition: array_iterator.hpp:331
iterator_types::cont_ptr cptr_type
pointer type of the container
Definition: array_iterator.hpp:141
const value & ref_type
reference type
Definition: array_iterator.hpp:102
iterator_type operator--(int)
decrement operators
Definition: array_iterator.hpp:272
value return_type
return type for dereferencing operator
Definition: array_iterator.hpp:98
type erasure for references to POD data
Definition: value_ref.hpp:43
bool operator<=(const iterator_type &b)
lesser than equal operator
Definition: array_iterator.hpp:324
type erasure for POD data
Definition: value.hpp:46
iterator_type & operator+=(ssize_t i)
compound assignment with +=
Definition: array_iterator.hpp:281
const array * cont_ptr
container pointer
Definition: array_iterator.hpp:94
bool operator<(const iterator_type &b)
lesser than operator
Definition: array_iterator.hpp:317
std::random_access_iterator_tag iterator_category
type of iterator
Definition: array_iterator.hpp:145
ssize_t state() const
get state of the iterator
Definition: array_iterator.hpp:344