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/byte_inserter.h"
18 
19 #undef NDEBUG
20 #include <cassert>
21 
22 namespace bluetooth {
23 namespace packet {
24 
ByteInserter(std::vector<uint8_t> & vector)25 ByteInserter::ByteInserter(std::vector<uint8_t>& vector) : std::back_insert_iterator<std::vector<uint8_t>>(vector) {}
26 
~ByteInserter()27 ByteInserter::~ByteInserter() {
28   assert(registered_observers_.empty());
29 }
30 
RegisterObserver(const ByteObserver & observer)31 void ByteInserter::RegisterObserver(const ByteObserver& observer) {
32   registered_observers_.push_back(observer);
33 }
34 
UnregisterObserver()35 ByteObserver ByteInserter::UnregisterObserver() {
36   ByteObserver observer = registered_observers_.back();
37   registered_observers_.pop_back();
38   return observer;
39 }
40 
on_byte(uint8_t byte)41 void ByteInserter::on_byte(uint8_t byte) {
42   for (auto& observer : registered_observers_) {
43     observer.OnByte(byte);
44   }
45 }
46 
insert_byte(uint8_t byte)47 void ByteInserter::insert_byte(uint8_t byte) {
48   on_byte(byte);
49   std::back_insert_iterator<std::vector<uint8_t>>::operator=(byte);
50 }
51 
52 }  // namespace packet
53 }  // namespace bluetooth
54