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 #undef NDEBUG
20 #include <cassert>
21
22 namespace bluetooth {
23 namespace packet {
24
View(std::shared_ptr<const std::vector<uint8_t>> data,size_t begin,size_t end)25 View::View(std::shared_ptr<const std::vector<uint8_t>> data, size_t begin, size_t end)
26 : data_(data), begin_(begin < data_->size() ? begin : data_->size()),
27 end_(end < data_->size() ? end : data_->size()) {}
28
View(const View & view,size_t begin,size_t end)29 View::View(const View& view, size_t begin, size_t end) : data_(view.data_) {
30 begin_ = (begin < view.size() ? begin : view.size());
31 begin_ += view.begin_;
32 end_ = (end < view.size() ? end : view.size());
33 end_ += view.begin_;
34 }
35
operator [](size_t i) const36 uint8_t View::operator[](size_t i) const {
37 assert(i + begin_ < end_);
38 return data_->operator[](i + begin_);
39 }
40
size() const41 size_t View::size() const {
42 return end_ - begin_;
43 }
44 } // namespace packet
45 } // namespace bluetooth
46