Home | History | Annotate | Download | only in runtime
      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 #include "base/logging.h"
     23 
     24 namespace art {
     25 
     26 template<typename T>
     27 class StrideIterator : public std::iterator<std::forward_iterator_tag, T> {
     28  public:
     29   StrideIterator(const StrideIterator&) = default;
     30   StrideIterator(StrideIterator&&) = default;
     31   StrideIterator& operator=(const StrideIterator&) = default;
     32   StrideIterator& operator=(StrideIterator&&) = default;
     33 
     34   StrideIterator(T* ptr, size_t stride)
     35       : ptr_(reinterpret_cast<uintptr_t>(ptr)),
     36         stride_(stride) {}
     37 
     38   bool operator==(const StrideIterator& other) const {
     39     DCHECK_EQ(stride_, other.stride_);
     40     return ptr_ == other.ptr_;
     41   }
     42 
     43   bool operator!=(const StrideIterator& other) const {
     44     return !(*this == other);
     45   }
     46 
     47   StrideIterator operator++() {  // Value after modification.
     48     ptr_ += stride_;
     49     return *this;
     50   }
     51 
     52   StrideIterator operator++(int) {
     53     StrideIterator<T> temp = *this;
     54     ptr_ += stride_;
     55     return temp;
     56   }
     57 
     58   StrideIterator operator+(ssize_t delta) const {
     59     StrideIterator<T> temp = *this;
     60     temp += delta;
     61     return temp;
     62   }
     63 
     64   StrideIterator& operator+=(ssize_t delta) {
     65     ptr_ += static_cast<ssize_t>(stride_) * delta;
     66     return *this;
     67   }
     68 
     69   T& operator*() const {
     70     return *reinterpret_cast<T*>(ptr_);
     71   }
     72 
     73   T* operator->() const {
     74     return &**this;
     75   }
     76 
     77  private:
     78   uintptr_t ptr_;
     79   // Not const for operator=.
     80   size_t stride_;
     81 };
     82 
     83 }  // namespace art
     84 
     85 #endif  // ART_RUNTIME_STRIDE_ITERATOR_H_
     86