1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_STRIDE_ITERATOR_H_
18 #define ART_RUNTIME_STRIDE_ITERATOR_H_
19 
20 #include <iterator>
21 
22 namespace art {
23 
24 template<typename T>
25 class StrideIterator : public std::iterator<std::forward_iterator_tag, T> {
26  public:
27   StrideIterator(const StrideIterator&) = default;
28   StrideIterator(StrideIterator&&) = default;
29   StrideIterator& operator=(const StrideIterator&) = default;
30   StrideIterator& operator=(StrideIterator&&) = default;
31 
StrideIterator(uintptr_t ptr,size_t stride)32   StrideIterator(uintptr_t ptr, size_t stride)
33       : ptr_(ptr), stride_(stride) {
34   }
35 
36   bool operator==(const StrideIterator& other) const {
37     return ptr_ == other.ptr_;
38   }
39 
40   bool operator!=(const StrideIterator& other) const {
41     return !(*this == other);
42   }
43 
44   StrideIterator operator++() {  // Value after modification.
45     ptr_ += stride_;
46     return *this;
47   }
48 
49   StrideIterator operator++(int) {
50     auto temp = *this;
51     ptr_ += stride_;
52     return temp;
53   }
54 
55   T& operator*() const {
56     return *reinterpret_cast<T*>(ptr_);
57   }
58 
59   T* operator->() const {
60     return &**this;
61   }
62 
63  private:
64   uintptr_t ptr_;
65   size_t stride_;
66 };
67 
68 }  // namespace art
69 
70 #endif  // ART_RUNTIME_STRIDE_ITERATOR_H_
71