/* * Copyright 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "packet/iterator.h" #include "os/log.h" namespace bluetooth { namespace packet { template Iterator::Iterator(std::forward_list data, size_t offset) { data_ = data; index_ = offset; length_ = 0; for (auto& view : data) { length_ += view.size(); } } template Iterator Iterator::operator+(int offset) { auto itr(*this); return itr += offset; } template Iterator& Iterator::operator+=(int offset) { index_ += offset; return *this; } template Iterator Iterator::operator++(int) { auto itr(*this); index_++; return itr; } template Iterator& Iterator::operator++() { index_++; return *this; } template Iterator Iterator::operator-(int offset) { auto itr(*this); return itr -= offset; } template int Iterator::operator-(Iterator& itr) { return index_ - itr.index_; } template Iterator& Iterator::operator-=(int offset) { index_ -= offset; return *this; } template Iterator Iterator::operator--(int) { auto itr(*this); if (index_ != 0) index_--; return itr; } template Iterator& Iterator::operator--() { if (index_ != 0) index_--; return *this; } template Iterator& Iterator::operator=(const Iterator& itr) { data_ = itr.data_; index_ = itr.index_; return *this; } template bool Iterator::operator==(const Iterator& itr) const { return index_ == itr.index_; } template bool Iterator::operator!=(const Iterator& itr) const { return !(*this == itr); } template bool Iterator::operator<(const Iterator& itr) const { return index_ < itr.index_; } template bool Iterator::operator>(const Iterator& itr) const { return index_ > itr.index_; } template bool Iterator::operator<=(const Iterator& itr) const { return index_ <= itr.index_; } template bool Iterator::operator>=(const Iterator& itr) const { return index_ >= itr.index_; } template uint8_t Iterator::operator*() const { ASSERT_LOG(index_ < length_, "Index %zu out of bounds: %zu", index_, length_); size_t index = index_; for (auto view : data_) { if (index < view.size()) { return view[index]; } index -= view.size(); } ASSERT_LOG(false, "Out of fragments searching for index %zu", index_); return 0; } template size_t Iterator::NumBytesRemaining() const { if (length_ > index_) { return length_ - index_; } else { return 0; } } // Explicit instantiations for both types of Iterators. template class Iterator; template class Iterator; } // namespace packet } // namespace bluetooth