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 <bluetooth/log.h> 20 21 #include "packet.h" 22 23 namespace bluetooth { 24 Iterator(std::shared_ptr<const Packet> packet,size_t i)25Iterator::Iterator(std::shared_ptr<const Packet> packet, size_t i) { 26 packet_ = packet; 27 index_ = i; 28 29 log::assert_that(index_ >= packet->packet_start_index_, 30 "assert failed: index_ >= packet->packet_start_index_"); 31 log::assert_that(index_ <= packet->packet_end_index_, 32 "assert failed: index_ <= packet->packet_end_index_"); 33 } 34 Iterator(const Iterator & itr)35Iterator::Iterator(const Iterator& itr) { *this = itr; } 36 operator +(size_t offset)37Iterator Iterator::operator+(size_t offset) { 38 auto itr(*this); 39 40 return itr += offset; 41 } 42 operator +=(size_t offset)43Iterator& Iterator::operator+=(size_t offset) { 44 size_t new_offset = index_ + offset; 45 index_ = new_offset > packet_->packet_end_index_ ? packet_->packet_end_index_ 46 : new_offset; 47 return *this; 48 } 49 operator ++(int)50Iterator Iterator::operator++(int) { 51 auto itr(*this); 52 index_++; 53 54 if (index_ > packet_->packet_end_index_) index_ = packet_->packet_end_index_; 55 56 return itr; 57 } 58 operator ++()59Iterator& Iterator::operator++() { 60 index_++; 61 62 if (index_ > packet_->packet_end_index_) index_ = packet_->packet_end_index_; 63 64 return *this; 65 } 66 operator -(size_t offset)67Iterator Iterator::operator-(size_t offset) { 68 auto itr(*this); 69 70 return itr -= offset; 71 } 72 operator -(const Iterator & itr)73int Iterator::operator-(const Iterator& itr) { return index_ - itr.index_; } 74 operator -=(size_t offset)75Iterator& Iterator::operator-=(size_t offset) { 76 index_ = (index_ < offset || index_ - offset < packet_->packet_start_index_) 77 ? packet_->packet_start_index_ 78 : index_ - offset; 79 80 return *this; 81 } 82 operator --(int)83Iterator Iterator::operator--(int) { 84 auto itr(*this); 85 if (index_ != packet_->packet_start_index_) index_--; 86 87 return itr; 88 } 89 operator --()90Iterator& Iterator::operator--() { 91 if (index_ != packet_->packet_start_index_) index_--; 92 93 return *this; 94 } 95 operator =(const Iterator & itr)96Iterator& Iterator::operator=(const Iterator& itr) { 97 packet_ = itr.packet_; 98 index_ = itr.index_; 99 100 return *this; 101 } 102 operator ==(const Iterator & itr) const103bool Iterator::operator==(const Iterator& itr) const { 104 return ((packet_ == itr.packet_) && (index_ == itr.index_)); 105 } 106 operator !=(const Iterator & itr) const107bool Iterator::operator!=(const Iterator& itr) const { return !(*this == itr); } 108 operator <(const Iterator & itr) const109bool Iterator::operator<(const Iterator& itr) const { 110 return ((packet_ == itr.packet_) && (index_ < itr.index_)); 111 } 112 operator >(const Iterator & itr) const113bool Iterator::operator>(const Iterator& itr) const { 114 return ((packet_ == itr.packet_) && (index_ > itr.index_)); 115 } 116 operator <=(const Iterator & itr) const117bool Iterator::operator<=(const Iterator& itr) const { 118 return ((packet_ == itr.packet_) && (index_ <= itr.index_)); 119 } 120 operator >=(const Iterator & itr) const121bool Iterator::operator>=(const Iterator& itr) const { 122 return ((packet_ == itr.packet_) && (index_ >= itr.index_)); 123 } 124 operator *() const125uint8_t Iterator::operator*() const { 126 log::assert_that(index_ != packet_->packet_end_index_, 127 "assert failed: index_ != packet_->packet_end_index_"); 128 129 return packet_->get_at_index(index_); 130 } 131 132 } // namespace bluetooth 133