1 /*
2 * Copyright 2019 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 "packet/view.h"
18
19 #include "os/log.h"
20
21 namespace bluetooth {
22 namespace packet {
23
View(std::shared_ptr<const std::vector<uint8_t>> data,size_t begin,size_t end)24 View::View(std::shared_ptr<const std::vector<uint8_t>> data, size_t begin, size_t end)
25 : data_(data), begin_(begin < data_->size() ? begin : data_->size()),
26 end_(end < data_->size() ? end : data_->size()) {}
27
View(const View & view,size_t begin,size_t end)28 View::View(const View& view, size_t begin, size_t end) : data_(view.data_) {
29 begin_ = (begin < view.size() ? begin : view.size());
30 begin_ += view.begin_;
31 end_ = (end < view.size() ? end : view.size());
32 end_ += view.begin_;
33 }
34
operator [](size_t i) const35 uint8_t View::operator[](size_t i) const {
36 ASSERT_LOG(i + begin_ < end_, "Out of bounds access at %zu", i);
37 return data_->operator[](i + begin_);
38 }
39
size() const40 size_t View::size() const {
41 return end_ - begin_;
42 }
43 } // namespace packet
44 } // namespace bluetooth
45