• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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   CHECK_GE(index_, packet->packet_start_index_);
30   CHECK_LE(index_, packet->packet_end_index_);
31 }
32 
Iterator(const Iterator & itr)33 Iterator::Iterator(const Iterator& itr) { *this = itr; }
34 
operator +(size_t offset)35 Iterator Iterator::operator+(size_t offset) {
36   auto itr(*this);
37 
38   return itr += offset;
39 }
40 
operator +=(size_t offset)41 Iterator& Iterator::operator+=(size_t offset) {
42   size_t new_offset = index_ + offset;
43   index_ = new_offset > packet_->packet_end_index_ ? packet_->packet_end_index_
44                                                    : new_offset;
45   return *this;
46 }
47 
operator ++(int)48 Iterator Iterator::operator++(int) {
49   auto itr(*this);
50   index_++;
51 
52   if (index_ > packet_->packet_end_index_) index_ = packet_->packet_end_index_;
53 
54   return itr;
55 }
56 
operator ++()57 Iterator& Iterator::operator++() {
58   index_++;
59 
60   if (index_ > packet_->packet_end_index_) index_ = packet_->packet_end_index_;
61 
62   return *this;
63 }
64 
operator -(size_t offset)65 Iterator Iterator::operator-(size_t offset) {
66   auto itr(*this);
67 
68   return itr -= offset;
69 }
70 
operator -(const Iterator & itr)71 int Iterator::operator-(const Iterator& itr) { return index_ - itr.index_; }
72 
operator -=(size_t offset)73 Iterator& Iterator::operator-=(size_t offset) {
74   index_ = (index_ < offset || index_ - offset < packet_->packet_start_index_)
75                ? packet_->packet_start_index_
76                : index_ - offset;
77 
78   return *this;
79 }
80 
operator --(int)81 Iterator Iterator::operator--(int) {
82   auto itr(*this);
83   if (index_ != packet_->packet_start_index_) index_--;
84 
85   return itr;
86 }
87 
operator --()88 Iterator& Iterator::operator--() {
89   if (index_ != packet_->packet_start_index_) index_--;
90 
91   return *this;
92 }
93 
operator =(const Iterator & itr)94 Iterator& Iterator::operator=(const Iterator& itr) {
95   packet_ = itr.packet_;
96   index_ = itr.index_;
97 
98   return *this;
99 }
100 
operator ==(const Iterator & itr) const101 bool Iterator::operator==(const Iterator& itr) const {
102   return ((packet_ == itr.packet_) && (index_ == itr.index_));
103 }
104 
operator !=(const Iterator & itr) const105 bool Iterator::operator!=(const Iterator& itr) const { return !(*this == itr); }
106 
operator <(const Iterator & itr) const107 bool Iterator::operator<(const Iterator& itr) const {
108   return ((packet_ == itr.packet_) && (index_ < itr.index_));
109 }
110 
operator >(const Iterator & itr) const111 bool Iterator::operator>(const Iterator& itr) const {
112   return ((packet_ == itr.packet_) && (index_ > itr.index_));
113 }
114 
operator <=(const Iterator & itr) const115 bool Iterator::operator<=(const Iterator& itr) const {
116   return ((packet_ == itr.packet_) && (index_ <= itr.index_));
117 }
118 
operator >=(const Iterator & itr) const119 bool Iterator::operator>=(const Iterator& itr) const {
120   return ((packet_ == itr.packet_) && (index_ >= itr.index_));
121 }
122 
operator *() const123 uint8_t Iterator::operator*() const {
124   CHECK_NE(index_, packet_->packet_end_index_);
125 
126   return packet_->get_at_index(index_);
127 }
128 
129 }  // namespace bluetooth