1 /* 2 * Copyright 2018 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 #include "iterator.h" 18 19 #include <base/logging.h> 20 21 #include "check.h" 22 #include "packet.h" 23 24 namespace bluetooth { 25 Iterator(std::shared_ptr<const Packet> packet,size_t i)26Iterator::Iterator(std::shared_ptr<const Packet> packet, size_t i) { 27 packet_ = packet; 28 index_ = i; 29 30 CHECK_GE(index_, packet->packet_start_index_); 31 CHECK_LE(index_, packet->packet_end_index_); 32 } 33 Iterator(const Iterator & itr)34Iterator::Iterator(const Iterator& itr) { *this = itr; } 35 operator +(size_t offset)36Iterator Iterator::operator+(size_t offset) { 37 auto itr(*this); 38 39 return itr += offset; 40 } 41 operator +=(size_t offset)42Iterator& Iterator::operator+=(size_t offset) { 43 size_t new_offset = index_ + offset; 44 index_ = new_offset > packet_->packet_end_index_ ? packet_->packet_end_index_ 45 : new_offset; 46 return *this; 47 } 48 operator ++(int)49Iterator Iterator::operator++(int) { 50 auto itr(*this); 51 index_++; 52 53 if (index_ > packet_->packet_end_index_) index_ = packet_->packet_end_index_; 54 55 return itr; 56 } 57 operator ++()58Iterator& Iterator::operator++() { 59 index_++; 60 61 if (index_ > packet_->packet_end_index_) index_ = packet_->packet_end_index_; 62 63 return *this; 64 } 65 operator -(size_t offset)66Iterator Iterator::operator-(size_t offset) { 67 auto itr(*this); 68 69 return itr -= offset; 70 } 71 operator -(const Iterator & itr)72int Iterator::operator-(const Iterator& itr) { return index_ - itr.index_; } 73 operator -=(size_t offset)74Iterator& Iterator::operator-=(size_t offset) { 75 index_ = (index_ < offset || index_ - offset < packet_->packet_start_index_) 76 ? packet_->packet_start_index_ 77 : index_ - offset; 78 79 return *this; 80 } 81 operator --(int)82Iterator Iterator::operator--(int) { 83 auto itr(*this); 84 if (index_ != packet_->packet_start_index_) index_--; 85 86 return itr; 87 } 88 operator --()89Iterator& Iterator::operator--() { 90 if (index_ != packet_->packet_start_index_) index_--; 91 92 return *this; 93 } 94 operator =(const Iterator & itr)95Iterator& Iterator::operator=(const Iterator& itr) { 96 packet_ = itr.packet_; 97 index_ = itr.index_; 98 99 return *this; 100 } 101 operator ==(const Iterator & itr) const102bool Iterator::operator==(const Iterator& itr) const { 103 return ((packet_ == itr.packet_) && (index_ == itr.index_)); 104 } 105 operator !=(const Iterator & itr) const106bool Iterator::operator!=(const Iterator& itr) const { return !(*this == itr); } 107 operator <(const Iterator & itr) const108bool Iterator::operator<(const Iterator& itr) const { 109 return ((packet_ == itr.packet_) && (index_ < itr.index_)); 110 } 111 operator >(const Iterator & itr) const112bool Iterator::operator>(const Iterator& itr) const { 113 return ((packet_ == itr.packet_) && (index_ > itr.index_)); 114 } 115 operator <=(const Iterator & itr) const116bool Iterator::operator<=(const Iterator& itr) const { 117 return ((packet_ == itr.packet_) && (index_ <= itr.index_)); 118 } 119 operator >=(const Iterator & itr) const120bool Iterator::operator>=(const Iterator& itr) const { 121 return ((packet_ == itr.packet_) && (index_ >= itr.index_)); 122 } 123 operator *() const124uint8_t Iterator::operator*() const { 125 CHECK_NE(index_, packet_->packet_end_index_); 126 127 return packet_->get_at_index(index_); 128 } 129 130 } // namespace bluetooth