1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_BASEBMP_STRIDEDARRAYITERATOR_HXX
29 #define INCLUDED_BASEBMP_STRIDEDARRAYITERATOR_HXX
30 
31 #include <basebmp/metafunctions.hxx>
32 
33 namespace basebmp
34 {
35 
36 /** Like vigra::StridedArrayIterator
37 
38     Changed semantics re. DirectionSelector<StridedArrayTag>: stride
39     now counts in <em>raw</em> bytes
40 
41     Adapts given ptr, in a way that iterator increments move forward
42     in strided steps. Stride can, by the way, also be negative
43  */
44 template< typename T > class StridedArrayIterator
45 {
46 public:
47     typedef typename clone_const<T, unsigned char>::type  internal_type;
48 
49     /** Create iterator
50 
51         @param stride
52 
53         Stride in bytes. Given value should better match memory layout
54         of T, as memory gets reinterpret-casted.
55      */
56     explicit StridedArrayIterator(int stride, T* ptr = 0) :
57         stride_(stride),
58         current_(reinterpret_cast<internal_type*>(ptr))
59     {}
60 
61     /** Copy from other StridedArrayIterator, plus given offset
62 
63         @param offset
64         Offset in bytes
65      */
66     StridedArrayIterator( StridedArrayIterator const& rSrc,
67                           int                         offset ) :
68         stride_(rSrc.stride_),
69         current_(reinterpret_cast<internal_type*>(
70                      reinterpret_cast<T*>(rSrc.current_)+offset))
71     {}
72 
73     void operator++()       {current_ += stride_; }
74     void operator++(int)    {current_ += stride_; }
75     void operator--()       {current_ -= stride_; }
76     void operator--(int)    {current_ -= stride_; }
77     void operator+=(int dy) {current_ += dy*stride_; }
78     void operator-=(int dy) {current_ -= dy*stride_; }
79 
80     int operator-(StridedArrayIterator const & rhs) const
81     { return (current_ - rhs.current_) / stride_; }
82 
83     bool operator==(StridedArrayIterator const & rhs) const
84     { return current_ == rhs.current_; }
85 
86     bool operator!=(StridedArrayIterator const & rhs) const
87     { return current_ != rhs.current_; }
88 
89     bool operator<(StridedArrayIterator const & rhs) const
90     { return *this - rhs < 0; }
91 
92     bool operator<=(StridedArrayIterator const & rhs) const
93     { return *this - rhs <= 0; }
94 
95     bool operator>(StridedArrayIterator const & rhs) const
96     { return *this - rhs > 0; }
97 
98     bool operator>=(StridedArrayIterator const & rhs) const
99     { return *this - rhs >= 0; }
100 
101     T* operator()() const
102     { return reinterpret_cast<T*>(current_); }
103 
104     T* operator()(int d) const
105     { return reinterpret_cast<T*>(current_ + d*stride_); }
106 
107 private:
108     int            stride_;
109     internal_type* current_;
110 };
111 
112 } // namespace basebmp
113 
114 #endif /* INCLUDED_BASEBMP_STRIDEDARRAYITERATOR_HXX */
115