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)25 Iterator::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)35 Iterator::Iterator(const Iterator& itr) { *this = itr; }
36 
operator +(size_t offset)37 Iterator Iterator::operator+(size_t offset) {
38   auto itr(*this);
39 
40   return itr += offset;
41 }
42 
operator +=(size_t offset)43 Iterator& Iterator::operator+=(size_t offset) {
44   size_t new_offset = index_ + offset;
45   index_ = new_offset > packet_->packet_end_index_ ? packet_->packet_end_index_ : new_offset;
46   return *this;
47 }
48 
operator ++(int)49 Iterator Iterator::operator++(int) {
50   auto itr(*this);
51   index_++;
52 
53   if (index_ > packet_->packet_end_index_) {
54     index_ = packet_->packet_end_index_;
55   }
56 
57   return itr;
58 }
59 
operator ++()60 Iterator& Iterator::operator++() {
61   index_++;
62 
63   if (index_ > packet_->packet_end_index_) {
64     index_ = packet_->packet_end_index_;
65   }
66 
67   return *this;
68 }
69 
operator -(size_t offset)70 Iterator Iterator::operator-(size_t offset) {
71   auto itr(*this);
72 
73   return itr -= offset;
74 }
75 
operator -(const Iterator & itr)76 int Iterator::operator-(const Iterator& itr) { return index_ - itr.index_; }
77 
operator -=(size_t offset)78 Iterator& Iterator::operator-=(size_t offset) {
79   index_ = (index_ < offset || index_ - offset < packet_->packet_start_index_)
80                    ? packet_->packet_start_index_
81                    : index_ - offset;
82 
83   return *this;
84 }
85 
operator --(int)86 Iterator Iterator::operator--(int) {
87   auto itr(*this);
88   if (index_ != packet_->packet_start_index_) {
89     index_--;
90   }
91 
92   return itr;
93 }
94 
operator --()95 Iterator& Iterator::operator--() {
96   if (index_ != packet_->packet_start_index_) {
97     index_--;
98   }
99 
100   return *this;
101 }
102 
operator =(const Iterator & itr)103 Iterator& Iterator::operator=(const Iterator& itr) {
104   packet_ = itr.packet_;
105   index_ = itr.index_;
106 
107   return *this;
108 }
109 
operator ==(const Iterator & itr) const110 bool Iterator::operator==(const Iterator& itr) const {
111   return (packet_ == itr.packet_) && (index_ == itr.index_);
112 }
113 
operator !=(const Iterator & itr) const114 bool Iterator::operator!=(const Iterator& itr) const { return !(*this == itr); }
115 
operator <(const Iterator & itr) const116 bool Iterator::operator<(const Iterator& itr) const {
117   return (packet_ == itr.packet_) && (index_ < itr.index_);
118 }
119 
operator >(const Iterator & itr) const120 bool Iterator::operator>(const Iterator& itr) const {
121   return (packet_ == itr.packet_) && (index_ > itr.index_);
122 }
123 
operator <=(const Iterator & itr) const124 bool Iterator::operator<=(const Iterator& itr) const {
125   return (packet_ == itr.packet_) && (index_ <= itr.index_);
126 }
127 
operator >=(const Iterator & itr) const128 bool Iterator::operator>=(const Iterator& itr) const {
129   return (packet_ == itr.packet_) && (index_ >= itr.index_);
130 }
131 
operator *() const132 uint8_t Iterator::operator*() const {
133   log::assert_that(index_ != packet_->packet_end_index_,
134                    "assert failed: index_ != packet_->packet_end_index_");
135 
136   return packet_->get_at_index(index_);
137 }
138 
139 }  // namespace bluetooth
140